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