Daniel@0: function sMap=sompak_train(sMap,ft,cout,ct,din,dt,rlen,alpha,radius) Daniel@0: Daniel@0: %SOMPAK_TRAIN Call SOM_PAK training program from Matlab. Daniel@0: % Daniel@0: % sMap=sompak_train(sMap,ft,cout,ct,din,dt,rlen,alpha,radius) Daniel@0: % Daniel@0: % ARGUMENTS ([]'s are optional and can be given as empty: [] or '') Daniel@0: % sMap (struct) map struct Daniel@0: % (string) filename Daniel@0: % [ft] (string) 'pak' or 'box'. Argument must be defined, if input file Daniel@0: % is used. Daniel@0: % [cout] (string) filename for output SOM, if argument is not defined Daniel@0: % (i.e. argument is '[]') temporary file '__abcdef' is Daniel@0: % used in operations and *it_is_removed* after Daniel@0: % operations!!! Daniel@0: % [ct] (string) 'pak' or 'box'. Argument must be defined, if output Daniel@0: % file is used. Daniel@0: % din (struct) data struct to be used in teaching Daniel@0: % (matrix) data matrix Daniel@0: % (string) filename Daniel@0: % If argument is not a filename or file is .mat -file, Daniel@0: % temporary file '__din' is used in operations Daniel@0: % and *it_is_removed* after operations!!! Daniel@0: % [dt] (string) 'pak' or 'box'. Argument must be defined, if input file Daniel@0: % is used. Daniel@0: % rlen (scalar) running length of teaching Daniel@0: % alpha (float) initial alpha value Daniel@0: % radius (float) initial radius of neighborhood Daniel@0: % Daniel@0: % RETURNS Daniel@0: % sMap (struct) map struct Daniel@0: % Daniel@0: % Calls SOM_PAK training program (vsom) from Matlab. Notice that to Daniel@0: % use this function, the SOM_PAK programs must be in your search path, Daniel@0: % or the variable 'SOM_PAKDIR' which is a string containing the Daniel@0: % program path, must be defined in the workspace. SOM_PAK programs can Daniel@0: % be found from: http://www.cis.hut.fi/research/som_lvq_pak.shtml Daniel@0: % Daniel@0: % See also SOMPAK_TRAIN, SOMPAK_SAMMON, SOMPAK_TRAIN_GUI, Daniel@0: % SOMPAK_GUI, SOM_SEQTRAIN. Daniel@0: Daniel@0: % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas Daniel@0: % Copyright (c) by Juha Parhankangas Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Juha Parhankangas 050100 Daniel@0: Daniel@0: Daniel@0: nargchk(9,9,nargin); Daniel@0: Daniel@0: NO_FILE=0; Daniel@0: DIN_FILE = 0; Daniel@0: Daniel@0: if ~isstruct(sMap) & ~isstr(sMap) Daniel@0: error('Argument ''sMap'' must be a struct or string.'); Daniel@0: end Daniel@0: Daniel@0: if isstr(sMap) Daniel@0: if isempty(ft) Daniel@0: error('Argument ''ft'' must be defined.'); Daniel@0: end Daniel@0: if strcmp(ft,'pak') Daniel@0: sMap=som_read_cod(sMap); Daniel@0: elseif strcmp(ft,'box') Daniel@0: new_var=diff_varname; Daniel@0: varnames=evalin('base','who'); Daniel@0: loadname=eval(cat(2,'who(''-file'',''',sMap,''')')); Daniel@0: if any(strcmp(loadname{1},evalin('base','who'))) Daniel@0: assignin('base',new_var,evalin('base',loadname{1})); Daniel@0: evalin('base',cat(2,'load(''',sMap,''');')); Daniel@0: new_var2=diff_varname; Daniel@0: Daniel@0: assignin('base',new_var2,evalin('base',loadname{1})); Daniel@0: assignin('base',loadname{1},evalin('base',new_var)); Daniel@0: evalin('base',cat(2,'clear ',new_var)); Daniel@0: sMap=evalin('base',new_var2); Daniel@0: evalin('base',cat(2,'clear ',new_var2)); Daniel@0: else Daniel@0: evalin('base',cat(2,'load(''',sMap,''');')); Daniel@0: sMap=evalin('base',loadname{1}); Daniel@0: evalin('base',cat(2,'clear ',loadname{1})); Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: end Daniel@0: if ~isstr(cout) & isempty(cout) Daniel@0: cout = '__abcdef'; Daniel@0: NO_FILE = 1; Daniel@0: elseif ~isstr(cout) | (isstr(cout) & isempty(cout)) Daniel@0: error('Argument ''cout'' must be a string or ''[]''.'); Daniel@0: end Daniel@0: Daniel@0: if ~NO_FILE & (isempty(ct) | ~(~isempty(ct) & ... Daniel@0: (strcmp(ct,'pak') | strcmp(ct,'box')))) Daniel@0: error('Argument ''ct'' must be string ''pak'' or ''box''.'); Daniel@0: end Daniel@0: Daniel@0: map_name=sMap.name; Daniel@0: som_write_cod(sMap,cout); Daniel@0: Daniel@0: if ~isempty(din) Daniel@0: som_write_data(din, '__din'); Daniel@0: DIN_FILE = 1; Daniel@0: din = '__din'; Daniel@0: else Daniel@0: DIN_FILE=0; Daniel@0: end Daniel@0: Daniel@0: if ~DIN_FILE Daniel@0: if isempty(dt) | ~isstr(dt) | ~(strcmp(dt,'box') | strcmp(dt,'pak')) Daniel@0: error('Argument ''dt'' must be string ''pak'' or ''box''.'); Daniel@0: end Daniel@0: if strcmp(dt,'box'); Daniel@0: DIN_FILE = 1; Daniel@0: din_var=diff_varname; Daniel@0: varnames=evalin('base','who'); Daniel@0: loadname=eval(cat(2,'who(''-file'',''',din,''')')); Daniel@0: if any(strcmp(loadname{1},evalin('base','who'))) Daniel@0: assignin('base',din_var,evalin('base',loadname{1})); Daniel@0: evalin('base',cat(2,'load(''',din,''');')); Daniel@0: din_var2=diff_varname; Daniel@0: Daniel@0: assignin('base',new_var2,evalin('base',loadname{1})); Daniel@0: assignin('base',loadname{1},evalin('base',din_var)); Daniel@0: evalin('base',cat(2,'clear ',din_var)); Daniel@0: din=evalin('base',din_var2); Daniel@0: else Daniel@0: evalin('base',cat(2,'load(''',din,''')')); Daniel@0: din=evalin('base',loadname{1}); Daniel@0: evalin('base',cat(2,'clear ',loadname{1})); Daniel@0: end Daniel@0: som_write_data(din,'__din'); Daniel@0: din = '__din'; Daniel@0: end Daniel@0: end Daniel@0: if ~is_positive_integer(rlen) Daniel@0: error('Argument ''rlen'' must be positive integer.'); Daniel@0: end Daniel@0: Daniel@0: if ~(isreal(alpha) & all(size(alpha)==1)) Daniel@0: error('Argument ''alpha'' must be a floating point number.'); Daniel@0: end Daniel@0: Daniel@0: if ~(isreal(radius) & all(size(radius)==1) & radius > 0) Daniel@0: error('Argument ''radius'' must be a positive floating point number.'); Daniel@0: end Daniel@0: Daniel@0: if any(strcmp('SOM_PAKDIR',evalin('base','who'))) Daniel@0: traincommand=cat(2,evalin('base','SOM_PAKDIR'),'vsom '); Daniel@0: else Daniel@0: traincommand='vsom '; Daniel@0: end Daniel@0: Daniel@0: str=cat(2,traincommand,sprintf('-cin %s -din %s -cout %s ',cout,din,cout),... Daniel@0: sprintf(' -rlen %d -alpha %f -radius %f',rlen,alpha,radius)); Daniel@0: if isunix Daniel@0: unix(str); Daniel@0: else Daniel@0: dos(str); Daniel@0: end Daniel@0: Daniel@0: sMap=som_read_cod(cout); Daniel@0: sMap.name=map_name; Daniel@0: Daniel@0: if ~NO_FILE Daniel@0: if isunix Daniel@0: unix(cat(2,'/bin/rm ',cout)); Daniel@0: else Daniel@0: dos(cat(2,'del ',cout)); Daniel@0: end Daniel@0: if isempty(ct) | ~isstr(ct) | ~(strcmp(ct,'pak') | strcmp(ct,'box')) Daniel@0: error('Argument ''ct'' must be string ''pak'' or ''box''.'); Daniel@0: elseif strcmp(ct,'box'); Daniel@0: eval(cat(2,'save ',cout,' sMap')); Daniel@0: disp(cat(2,'Output written to the file ',sprintf('''%s.mat''.',cout))); Daniel@0: else Daniel@0: som_write_cod(sMap,cout); Daniel@0: end Daniel@0: else Daniel@0: if isunix Daniel@0: unix('/bin/rm __abcdef'); Daniel@0: else Daniel@0: dos('del __abcdef'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if DIN_FILE Daniel@0: if isunix Daniel@0: unix('/bin/rm __din'); Daniel@0: else Daniel@0: dos('del __abcdef'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: function bool = is_positive_integer(x) Daniel@0: Daniel@0: bool = ~isempty(x) & isreal(x) & all(size(x) == 1) & x > 0; Daniel@0: if ~isempty(bool) Daniel@0: if bool & x~=round(x) Daniel@0: bool = 0; Daniel@0: end Daniel@0: else Daniel@0: bool = 0; Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: function str = diff_varname(); Daniel@0: Daniel@0: array=evalin('base','who'); Daniel@0: Daniel@0: if isempty(array) Daniel@0: str='a'; Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: for i=1:length(array) Daniel@0: lens(i)=length(array{i}); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: ind=max(lens); Daniel@0: Daniel@0: str(1:ind+1)='a'; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: