annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/sompak_init.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function sMap=sompak_init(sData,ft,init_type,cout,ct,xdim,ydim,topol,neigh)
wolffd@0 2
wolffd@0 3 %SOMPAK_INIT Call SOM_PAK initialization programs from Matlab.
wolffd@0 4 %
wolffd@0 5 % sMap=sompak_init(sData,ft,init_type,cout,ct,xdim,ydim,topol,neigh)
wolffd@0 6 %
wolffd@0 7 % ARGUMENTS ([]'s are optional and can be given as empty: [] or '')
wolffd@0 8 % sData (struct) data struct
wolffd@0 9 % (matrix) data matrix
wolffd@0 10 % (string) filename
wolffd@0 11 % [ft] (string) 'pak' or 'box'. Argument must be defined, if input
wolffd@0 12 % file is used.
wolffd@0 13 % init_type (string) string 'rand' or 'linear'
wolffd@0 14 % [cout] (string) filename for output SOM, if argument is not defined
wolffd@0 15 % (i.e. argument is '[]') temporary file '__abcdef' is
wolffd@0 16 % used in operations and *it_is_removed* after
wolffd@0 17 % operations!!!
wolffd@0 18 % [ct] (string) 'pak' or 'box'. Argument must be defined, if output
wolffd@0 19 % file is used.
wolffd@0 20 % xdim (scalar) Number of units of the map in x-direction.
wolffd@0 21 % ydim (scalar) Number of units of the map in y-direction.
wolffd@0 22 % topol (string) string 'hexa' or 'rect'
wolffd@0 23 % neigh (string) string 'bubble' or 'gaussian'.
wolffd@0 24 %
wolffd@0 25 % RETURNS
wolffd@0 26 % sMap (struct) map struct
wolffd@0 27 %
wolffd@0 28 % Calls SOM_PAK initialization programs (randinit and lininit) from
wolffd@0 29 % Matlab. Notice that to use this function, the SOM_PAK programs must
wolffd@0 30 % be in your search path, or the variable 'SOM_PAKDIR' which is a
wolffd@0 31 % string containing the program path, must be defined in the
wolffd@0 32 % workspace. SOM_PAK programs can be found from:
wolffd@0 33 % http://www.cis.hut.fi/research/som_lvq_pak.shtml
wolffd@0 34 %
wolffd@0 35 % See also SOMPAK_TRAIN, SOMPAK_SAMMON, SOMPAK_INIT_GUI,
wolffd@0 36 % SOMPAK_GUI, SOM_LININIT, SOM_RANDINIT.
wolffd@0 37
wolffd@0 38 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas
wolffd@0 39 % Copyright (c) by Juha Parhankangas
wolffd@0 40 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 41
wolffd@0 42 % Juha Parhankangas 050100
wolffd@0 43
wolffd@0 44 nargchk(9,9,nargin);
wolffd@0 45
wolffd@0 46 NO_FILE = 0;
wolffd@0 47 if isstruct(sData);
wolffd@0 48 sData=sData.data;
wolffd@0 49 elseif ~(isreal(sData) | isstr(sData))
wolffd@0 50 error('Argument ''sData'' must be a struct or a real matrix.');
wolffd@0 51 else
wolffd@0 52 if isempty(ft)
wolffd@0 53 if isstr(sData)
wolffd@0 54 error('Argument ''file_type'' must be defined when input file is used.');
wolffd@0 55 end
wolffd@0 56 elseif strcmp(ft,'pak');
wolffd@0 57 sData=som_read_data(sData);
wolffd@0 58 elseif strcmp(ft,'box')
wolffd@0 59 new_var=diff_varname;
wolffd@0 60 varnames=evalin('base','who');
wolffd@0 61 loadname=eval(cat(2,'who(''-file'',''',sData,''')'));
wolffd@0 62 if any(strcmp(loadname{1},evalin('base','who')))
wolffd@0 63 assignin('base',new_var,evalin('base',loadname{1}));
wolffd@0 64 evalin('base',cat(2,'load(''',sData,''');'));
wolffd@0 65 new_var2=diff_varname;
wolffd@0 66
wolffd@0 67 assignin('base',new_var2,evalin('base',loadname{1}));
wolffd@0 68 assignin('base',loadname{1},evalin('base',new_var));
wolffd@0 69 evalin('base',cat(2,'clear ',new_var));
wolffd@0 70 sData=evalin('base',new_var2);
wolffd@0 71 evalin('base',cat(2,'clear ',new_var2));
wolffd@0 72 else
wolffd@0 73 evalin('base',cat(2,'load(''',sData,''');'));
wolffd@0 74 sData=evalin('base',loadname{1});
wolffd@0 75 evalin('base',cat(2,'clear ',loadname{1}));
wolffd@0 76 end
wolffd@0 77 else
wolffd@0 78 error('Argument ''ft'' must be a string ''pak'' or ''box''.');
wolffd@0 79 end
wolffd@0 80 end
wolffd@0 81 if isstr(init_type)
wolffd@0 82 if strcmp(init_type,'rand')
wolffd@0 83 if any(strcmp('SOM_PAKDIR',evalin('base','who')))
wolffd@0 84 init_command=cat(2,evalin('base','SOM_PAKDIR'),'randinit');
wolffd@0 85 else
wolffd@0 86 init_command='randinit';
wolffd@0 87 end
wolffd@0 88 elseif strcmp(init_type,'linear')
wolffd@0 89 if any(strcmp('SOM_PAKDIR',evalin('base','who')))
wolffd@0 90 init_command=cat(2,evalin('base','SOM_PAKDIR'),'lininit');
wolffd@0 91 else
wolffd@0 92 init_command='lininit';
wolffd@0 93 end
wolffd@0 94 else
wolffd@0 95 error('Argument ''init_type'' must be string ''rand'' or ''linear''.');
wolffd@0 96 end
wolffd@0 97 else
wolffd@0 98 error('Argument ''init_type'' must be string ''rand'' or ''linear''.');
wolffd@0 99 end
wolffd@0 100
wolffd@0 101 if (isstr(cout) & isempty(cout)) | (~isstr(cout) & isempty(cout))
wolffd@0 102 NO_FILE = 1;
wolffd@0 103 cout = '__abcdef';
wolffd@0 104 elseif ~isstr(cout) & ~isempty(cout)
wolffd@0 105 error('Argument ''cout'' must be a string or ''[]''.');
wolffd@0 106 end
wolffd@0 107
wolffd@0 108 if ~is_positive_integer(xdim)
wolffd@0 109 error('Argument ''xdim'' must be a positive integer.');
wolffd@0 110 end
wolffd@0 111
wolffd@0 112 if ~is_positive_integer(ydim)
wolffd@0 113 error('Argument ''ydim'' must be a positive integer.');
wolffd@0 114 end
wolffd@0 115
wolffd@0 116 if isstr(topol)
wolffd@0 117 if isempty(topol) | (~strcmp(topol,'hexa') & ~strcmp(topol,'rect'))
wolffd@0 118 error ('Argument ''topol'' must be either a string ''hexa'' or ''rect''.');
wolffd@0 119 end
wolffd@0 120 else
wolffd@0 121 error ('Argument ''topol'' must be either a string ''hexa'' or ''rect''.');
wolffd@0 122 end
wolffd@0 123
wolffd@0 124 if isstr(neigh)
wolffd@0 125 if isempty(neigh) | (~strcmp(neigh,'bubble') & ~strcmp(neigh,'gaussian'))
wolffd@0 126 error(sprintf(cat(2,'Argument ''neigh'' must be either a string ',...
wolffd@0 127 '''bubble'' or ''gaussian''.')));
wolffd@0 128 end
wolffd@0 129 else
wolffd@0 130 error(sprintf(cat(2,'Argument ''neigh'' must be either a string ',...
wolffd@0 131 '''bubble'' or ''gaussian''.')));
wolffd@0 132 end
wolffd@0 133
wolffd@0 134 som_write_data(sData, cout);
wolffd@0 135 str=cat(2,init_command,sprintf(' -din %s -cout %s ', cout ,cout),...
wolffd@0 136 sprintf('-topol %s ',topol),...
wolffd@0 137 sprintf('-neigh %s ',neigh),...
wolffd@0 138 sprintf('-xdim %d -ydim %d',xdim,ydim));
wolffd@0 139
wolffd@0 140 if isunix
wolffd@0 141 unix(str);
wolffd@0 142 else
wolffd@0 143 dos(str);
wolffd@0 144 end
wolffd@0 145
wolffd@0 146 sMap=som_read_cod(cout);
wolffd@0 147
wolffd@0 148 if ~NO_FILE
wolffd@0 149 if isunix
wolffd@0 150 unix(cat(2,'/bin/rm ',cout));
wolffd@0 151 else
wolffd@0 152 dos(cat(2,'del ',cout));
wolffd@0 153 end
wolffd@0 154 if strcmp(ct,'pak')
wolffd@0 155 som_write_cod(sMap,cout);
wolffd@0 156 disp(cat(2,'Output written to the file ',cout,'.'));
wolffd@0 157 elseif strcmp(ct,'box')
wolffd@0 158 eval(cat(2,'save ',cout,' sMap'));
wolffd@0 159 disp(cat(2,'Output written to the file ',sprintf('''%s.mat''.',cout)));
wolffd@0 160 end
wolffd@0 161 else
wolffd@0 162 sMap.name=cat(2,'SOM ',date);
wolffd@0 163 if isunix
wolffd@0 164 unix('/bin/rm __abcdef');
wolffd@0 165 else
wolffd@0 166 dos('del __abcdef');
wolffd@0 167 end
wolffd@0 168 end
wolffd@0 169
wolffd@0 170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 171
wolffd@0 172 function bool = is_positive_integer(x)
wolffd@0 173
wolffd@0 174 bool = ~isempty(x) & isreal(x) & all(size(x) == 1) & x > 0;
wolffd@0 175 if ~isempty(bool)
wolffd@0 176 if bool & x~=round(x)
wolffd@0 177 bool = 0;
wolffd@0 178 end
wolffd@0 179 else
wolffd@0 180 bool = 0;
wolffd@0 181 end
wolffd@0 182
wolffd@0 183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 184
wolffd@0 185 function str = diff_varname();
wolffd@0 186
wolffd@0 187 array=evalin('base','who');
wolffd@0 188
wolffd@0 189 if isempty(array)
wolffd@0 190 str='a';
wolffd@0 191 return;
wolffd@0 192 end
wolffd@0 193
wolffd@0 194 for i=1:length(array)
wolffd@0 195 lens(i)=length(array{i});
wolffd@0 196 end
wolffd@0 197
wolffd@0 198
wolffd@0 199 ind=max(lens);
wolffd@0 200
wolffd@0 201 str(1:ind+1)='a';
wolffd@0 202
wolffd@0 203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 204
wolffd@0 205
wolffd@0 206
wolffd@0 207
wolffd@0 208
wolffd@0 209
wolffd@0 210
wolffd@0 211