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