annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/sompak_train.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function sMap=sompak_train(sMap,ft,cout,ct,din,dt,rlen,alpha,radius)
Daniel@0 2
Daniel@0 3 %SOMPAK_TRAIN Call SOM_PAK training program from Matlab.
Daniel@0 4 %
Daniel@0 5 % sMap=sompak_train(sMap,ft,cout,ct,din,dt,rlen,alpha,radius)
Daniel@0 6 %
Daniel@0 7 % ARGUMENTS ([]'s are optional and can be given as empty: [] or '')
Daniel@0 8 % sMap (struct) map struct
Daniel@0 9 % (string) filename
Daniel@0 10 % [ft] (string) 'pak' or 'box'. Argument must be defined, if input file
Daniel@0 11 % is used.
Daniel@0 12 % [cout] (string) filename for output SOM, if argument is not defined
Daniel@0 13 % (i.e. argument is '[]') temporary file '__abcdef' is
Daniel@0 14 % used in operations and *it_is_removed* after
Daniel@0 15 % operations!!!
Daniel@0 16 % [ct] (string) 'pak' or 'box'. Argument must be defined, if output
Daniel@0 17 % file is used.
Daniel@0 18 % din (struct) data struct to be used in teaching
Daniel@0 19 % (matrix) data matrix
Daniel@0 20 % (string) filename
Daniel@0 21 % If argument is not a filename or file is .mat -file,
Daniel@0 22 % temporary file '__din' is used in operations
Daniel@0 23 % and *it_is_removed* after operations!!!
Daniel@0 24 % [dt] (string) 'pak' or 'box'. Argument must be defined, if input file
Daniel@0 25 % is used.
Daniel@0 26 % rlen (scalar) running length of teaching
Daniel@0 27 % alpha (float) initial alpha value
Daniel@0 28 % radius (float) initial radius of neighborhood
Daniel@0 29 %
Daniel@0 30 % RETURNS
Daniel@0 31 % sMap (struct) map struct
Daniel@0 32 %
Daniel@0 33 % Calls SOM_PAK training program (vsom) from Matlab. Notice that to
Daniel@0 34 % use this function, the SOM_PAK programs must be in your search path,
Daniel@0 35 % or the variable 'SOM_PAKDIR' which is a string containing the
Daniel@0 36 % program path, must be defined in the workspace. SOM_PAK programs can
Daniel@0 37 % be found from: http://www.cis.hut.fi/research/som_lvq_pak.shtml
Daniel@0 38 %
Daniel@0 39 % See also SOMPAK_TRAIN, SOMPAK_SAMMON, SOMPAK_TRAIN_GUI,
Daniel@0 40 % SOMPAK_GUI, SOM_SEQTRAIN.
Daniel@0 41
Daniel@0 42 % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas
Daniel@0 43 % Copyright (c) by Juha Parhankangas
Daniel@0 44 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 45
Daniel@0 46 % Juha Parhankangas 050100
Daniel@0 47
Daniel@0 48
Daniel@0 49 nargchk(9,9,nargin);
Daniel@0 50
Daniel@0 51 NO_FILE=0;
Daniel@0 52 DIN_FILE = 0;
Daniel@0 53
Daniel@0 54 if ~isstruct(sMap) & ~isstr(sMap)
Daniel@0 55 error('Argument ''sMap'' must be a struct or string.');
Daniel@0 56 end
Daniel@0 57
Daniel@0 58 if isstr(sMap)
Daniel@0 59 if isempty(ft)
Daniel@0 60 error('Argument ''ft'' must be defined.');
Daniel@0 61 end
Daniel@0 62 if strcmp(ft,'pak')
Daniel@0 63 sMap=som_read_cod(sMap);
Daniel@0 64 elseif strcmp(ft,'box')
Daniel@0 65 new_var=diff_varname;
Daniel@0 66 varnames=evalin('base','who');
Daniel@0 67 loadname=eval(cat(2,'who(''-file'',''',sMap,''')'));
Daniel@0 68 if any(strcmp(loadname{1},evalin('base','who')))
Daniel@0 69 assignin('base',new_var,evalin('base',loadname{1}));
Daniel@0 70 evalin('base',cat(2,'load(''',sMap,''');'));
Daniel@0 71 new_var2=diff_varname;
Daniel@0 72
Daniel@0 73 assignin('base',new_var2,evalin('base',loadname{1}));
Daniel@0 74 assignin('base',loadname{1},evalin('base',new_var));
Daniel@0 75 evalin('base',cat(2,'clear ',new_var));
Daniel@0 76 sMap=evalin('base',new_var2);
Daniel@0 77 evalin('base',cat(2,'clear ',new_var2));
Daniel@0 78 else
Daniel@0 79 evalin('base',cat(2,'load(''',sMap,''');'));
Daniel@0 80 sMap=evalin('base',loadname{1});
Daniel@0 81 evalin('base',cat(2,'clear ',loadname{1}));
Daniel@0 82 end
Daniel@0 83
Daniel@0 84 end
Daniel@0 85 end
Daniel@0 86 if ~isstr(cout) & isempty(cout)
Daniel@0 87 cout = '__abcdef';
Daniel@0 88 NO_FILE = 1;
Daniel@0 89 elseif ~isstr(cout) | (isstr(cout) & isempty(cout))
Daniel@0 90 error('Argument ''cout'' must be a string or ''[]''.');
Daniel@0 91 end
Daniel@0 92
Daniel@0 93 if ~NO_FILE & (isempty(ct) | ~(~isempty(ct) & ...
Daniel@0 94 (strcmp(ct,'pak') | strcmp(ct,'box'))))
Daniel@0 95 error('Argument ''ct'' must be string ''pak'' or ''box''.');
Daniel@0 96 end
Daniel@0 97
Daniel@0 98 map_name=sMap.name;
Daniel@0 99 som_write_cod(sMap,cout);
Daniel@0 100
Daniel@0 101 if ~isempty(din)
Daniel@0 102 som_write_data(din, '__din');
Daniel@0 103 DIN_FILE = 1;
Daniel@0 104 din = '__din';
Daniel@0 105 else
Daniel@0 106 DIN_FILE=0;
Daniel@0 107 end
Daniel@0 108
Daniel@0 109 if ~DIN_FILE
Daniel@0 110 if isempty(dt) | ~isstr(dt) | ~(strcmp(dt,'box') | strcmp(dt,'pak'))
Daniel@0 111 error('Argument ''dt'' must be string ''pak'' or ''box''.');
Daniel@0 112 end
Daniel@0 113 if strcmp(dt,'box');
Daniel@0 114 DIN_FILE = 1;
Daniel@0 115 din_var=diff_varname;
Daniel@0 116 varnames=evalin('base','who');
Daniel@0 117 loadname=eval(cat(2,'who(''-file'',''',din,''')'));
Daniel@0 118 if any(strcmp(loadname{1},evalin('base','who')))
Daniel@0 119 assignin('base',din_var,evalin('base',loadname{1}));
Daniel@0 120 evalin('base',cat(2,'load(''',din,''');'));
Daniel@0 121 din_var2=diff_varname;
Daniel@0 122
Daniel@0 123 assignin('base',new_var2,evalin('base',loadname{1}));
Daniel@0 124 assignin('base',loadname{1},evalin('base',din_var));
Daniel@0 125 evalin('base',cat(2,'clear ',din_var));
Daniel@0 126 din=evalin('base',din_var2);
Daniel@0 127 else
Daniel@0 128 evalin('base',cat(2,'load(''',din,''')'));
Daniel@0 129 din=evalin('base',loadname{1});
Daniel@0 130 evalin('base',cat(2,'clear ',loadname{1}));
Daniel@0 131 end
Daniel@0 132 som_write_data(din,'__din');
Daniel@0 133 din = '__din';
Daniel@0 134 end
Daniel@0 135 end
Daniel@0 136 if ~is_positive_integer(rlen)
Daniel@0 137 error('Argument ''rlen'' must be positive integer.');
Daniel@0 138 end
Daniel@0 139
Daniel@0 140 if ~(isreal(alpha) & all(size(alpha)==1))
Daniel@0 141 error('Argument ''alpha'' must be a floating point number.');
Daniel@0 142 end
Daniel@0 143
Daniel@0 144 if ~(isreal(radius) & all(size(radius)==1) & radius > 0)
Daniel@0 145 error('Argument ''radius'' must be a positive floating point number.');
Daniel@0 146 end
Daniel@0 147
Daniel@0 148 if any(strcmp('SOM_PAKDIR',evalin('base','who')))
Daniel@0 149 traincommand=cat(2,evalin('base','SOM_PAKDIR'),'vsom ');
Daniel@0 150 else
Daniel@0 151 traincommand='vsom ';
Daniel@0 152 end
Daniel@0 153
Daniel@0 154 str=cat(2,traincommand,sprintf('-cin %s -din %s -cout %s ',cout,din,cout),...
Daniel@0 155 sprintf(' -rlen %d -alpha %f -radius %f',rlen,alpha,radius));
Daniel@0 156 if isunix
Daniel@0 157 unix(str);
Daniel@0 158 else
Daniel@0 159 dos(str);
Daniel@0 160 end
Daniel@0 161
Daniel@0 162 sMap=som_read_cod(cout);
Daniel@0 163 sMap.name=map_name;
Daniel@0 164
Daniel@0 165 if ~NO_FILE
Daniel@0 166 if isunix
Daniel@0 167 unix(cat(2,'/bin/rm ',cout));
Daniel@0 168 else
Daniel@0 169 dos(cat(2,'del ',cout));
Daniel@0 170 end
Daniel@0 171 if isempty(ct) | ~isstr(ct) | ~(strcmp(ct,'pak') | strcmp(ct,'box'))
Daniel@0 172 error('Argument ''ct'' must be string ''pak'' or ''box''.');
Daniel@0 173 elseif strcmp(ct,'box');
Daniel@0 174 eval(cat(2,'save ',cout,' sMap'));
Daniel@0 175 disp(cat(2,'Output written to the file ',sprintf('''%s.mat''.',cout)));
Daniel@0 176 else
Daniel@0 177 som_write_cod(sMap,cout);
Daniel@0 178 end
Daniel@0 179 else
Daniel@0 180 if isunix
Daniel@0 181 unix('/bin/rm __abcdef');
Daniel@0 182 else
Daniel@0 183 dos('del __abcdef');
Daniel@0 184 end
Daniel@0 185 end
Daniel@0 186
Daniel@0 187 if DIN_FILE
Daniel@0 188 if isunix
Daniel@0 189 unix('/bin/rm __din');
Daniel@0 190 else
Daniel@0 191 dos('del __abcdef');
Daniel@0 192 end
Daniel@0 193 end
Daniel@0 194
Daniel@0 195
Daniel@0 196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 197
Daniel@0 198 function bool = is_positive_integer(x)
Daniel@0 199
Daniel@0 200 bool = ~isempty(x) & isreal(x) & all(size(x) == 1) & x > 0;
Daniel@0 201 if ~isempty(bool)
Daniel@0 202 if bool & x~=round(x)
Daniel@0 203 bool = 0;
Daniel@0 204 end
Daniel@0 205 else
Daniel@0 206 bool = 0;
Daniel@0 207 end
Daniel@0 208
Daniel@0 209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 210
Daniel@0 211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 212
Daniel@0 213 function str = diff_varname();
Daniel@0 214
Daniel@0 215 array=evalin('base','who');
Daniel@0 216
Daniel@0 217 if isempty(array)
Daniel@0 218 str='a';
Daniel@0 219 return;
Daniel@0 220 end
Daniel@0 221
Daniel@0 222 for i=1:length(array)
Daniel@0 223 lens(i)=length(array{i});
Daniel@0 224 end
Daniel@0 225
Daniel@0 226
Daniel@0 227 ind=max(lens);
Daniel@0 228
Daniel@0 229 str(1:ind+1)='a';
Daniel@0 230
Daniel@0 231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 232
Daniel@0 233
Daniel@0 234
Daniel@0 235
Daniel@0 236
Daniel@0 237
Daniel@0 238
Daniel@0 239
Daniel@0 240
Daniel@0 241
Daniel@0 242
Daniel@0 243