comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/sompak_train.m @ 0:e9a9cd732c1e tip

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