annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_write_cod.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function som_write_cod(sMap, filename)
wolffd@0 2
wolffd@0 3 %SOM_WRITE_COD Writes a map struct to ascii file in SOM_PAK format.
wolffd@0 4 %
wolffd@0 5 % som_write_cod(sMap,filename)
wolffd@0 6 %
wolffd@0 7 % som_write_cod(sMap,'map1.cod');
wolffd@0 8 %
wolffd@0 9 % Input and output arguments:
wolffd@0 10 % sMap (struct) self-organizing map structure
wolffd@0 11 % filename (string) name of input file
wolffd@0 12 %
wolffd@0 13 % Note that much of the information in the map struct is lost.
wolffd@0 14 % Typically, when saving map structs into files use the 'save' command.
wolffd@0 15 %
wolffd@0 16 % For more help, try 'type som_write_cod' or check out online documentation.
wolffd@0 17 % See also SOM_READ_COD, SOM_READ_DATA, SOM_WRITE_DATA.
wolffd@0 18
wolffd@0 19 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 20 %
wolffd@0 21 % som_write_cod
wolffd@0 22 %
wolffd@0 23 % PURPOSE
wolffd@0 24 %
wolffd@0 25 % Writes a self-organizing map struct to a file in SOM_PAK format.
wolffd@0 26 %
wolffd@0 27 % SYNTAX
wolffd@0 28 %
wolffd@0 29 % som_write_cod(sMap,filename);
wolffd@0 30 %
wolffd@0 31 % DESCRIPTION
wolffd@0 32 %
wolffd@0 33 % This function is offered for compatibility with SOM_PAK, a SOM
wolffd@0 34 % software package in C. It writes map struct to files in SOM_PAK format.
wolffd@0 35 %
wolffd@0 36 % See SOM_READ_COD for description of the SOM_PAK map file format.
wolffd@0 37 % Because the SOM_PAK package does not support many of the features and
wolffd@0 38 % options of the SOM Toolbox, some of the information is changed, or even
wolffd@0 39 % lost.
wolffd@0 40 %
wolffd@0 41 % SOM_PAK does not support 3- or higher dimensional map grids. These cannot
wolffd@0 42 % be exported using this function.
wolffd@0 43 % SOM_PAK always supposes that the map has 'sheet' shape.
wolffd@0 44 % SOM_PAK only supports 'bubble' and 'gaussian' neighborhood functions.
wolffd@0 45 % Any other neighborhood function is changed to 'gaussian'.
wolffd@0 46 % SOM_PAK doesn't support component names. However, the component names are
wolffd@0 47 % written on a comment line which begins with '#n '. Any spaces (' ')
wolffd@0 48 % in the component names are replaced with underscores ('_').
wolffd@0 49 % Information on map name, mask, training history and normalizations is lost.
wolffd@0 50 %
wolffd@0 51 % This function is only offered for compatibility with SOM_PAK. In general,
wolffd@0 52 % when saving map structs in files, use 'save filename.mat sMap'. This is
wolffd@0 53 % faster and retains all information of the map.
wolffd@0 54 %
wolffd@0 55 % REQUIRED INPUT ARGUMENTS
wolffd@0 56 %
wolffd@0 57 % sMap (struct) the SOM struct to be written
wolffd@0 58 % filename (string) the name of the input file
wolffd@0 59 %
wolffd@0 60 % EXAMPLES
wolffd@0 61 %
wolffd@0 62 % som_write_cod(sMap,'map1.cod');
wolffd@0 63 %
wolffd@0 64 % SEE ALSO
wolffd@0 65 %
wolffd@0 66 % som_read_cod Read a map from a file in SOM_PAK format.
wolffd@0 67 % som_read_data Reads data from an ascii file.
wolffd@0 68 % som_write_data Writes data struct into a file in SOM_PAK format.
wolffd@0 69
wolffd@0 70 % Copyright (c) 1997-2000 by the SOM toolbox programming team.
wolffd@0 71 % http://www.cis.hut.fi/projects/somtoolbox/
wolffd@0 72
wolffd@0 73 % Version 1.0beta ecco 221097
wolffd@0 74 % Version 2.0beta ecco 030899, juuso 151199
wolffd@0 75
wolffd@0 76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 77 %% check arguments and initialize
wolffd@0 78
wolffd@0 79 error(nargchk(2, 2, nargin)) % check no. of input args is correct
wolffd@0 80
wolffd@0 81 % sMap
wolffd@0 82 msize = sMap.topol.msize; % map grid size
wolffd@0 83 mdim = length(msize); % map grid dimension
wolffd@0 84 [munits dim] = size(sMap.codebook); % input space dimension
wolffd@0 85
wolffd@0 86 % map dimension check:
wolffd@0 87 % map dimensions higher than 2 are not supported by SOM_PAK
wolffd@0 88 if mdim > 2,
wolffd@0 89 error('Cannot write maps with higher dimension than two');
wolffd@0 90 end
wolffd@0 91
wolffd@0 92 % in SOM_PAK the xy-indexing is used, while in Matlab ij-indexing
wolffd@0 93 % therefore, the codebook vectors have to be reorganized
wolffd@0 94 order = reshape([1:munits],msize);
wolffd@0 95 order = reshape(order',[munits 1]);
wolffd@0 96 msize = fliplr(msize);
wolffd@0 97
wolffd@0 98 % open output file
wolffd@0 99 fid = fopen(filename, 'w');
wolffd@0 100 if fid < 0,
wolffd@0 101 error(['Cannot open file ' filename]);
wolffd@0 102 end
wolffd@0 103
wolffd@0 104 % check version
wolffd@0 105 v = version;
wolffd@0 106 ver_53_or_newer = (str2num(v(1:3)) >= 5.3);
wolffd@0 107
wolffd@0 108
wolffd@0 109 [lines numlabs] = size(sMap.labels);
wolffd@0 110 has_labels = zeros(lines, 1);
wolffd@0 111 if ver_53_or_newer
wolffd@0 112 has_labels = sum((~(cellfun('isempty', sMap.labels))), 2);
wolffd@0 113 else
wolffd@0 114 for i = 1:lines
wolffd@0 115 for j = 1:numlabs
wolffd@0 116 if ~isempty(sMap.labels{i,j})
wolffd@0 117 has_labels(i) = 1; break;
wolffd@0 118 end
wolffd@0 119 end
wolffd@0 120 end
wolffd@0 121 end
wolffd@0 122
wolffd@0 123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 124 %% write map into a file
wolffd@0 125
wolffd@0 126 % write header
wolffd@0 127
wolffd@0 128 fprintf(fid, '%d %s ', dim, sMap.topol.lattice); % dimension and lattice
wolffd@0 129 fprintf(fid, '%d ', msize); % map size
wolffd@0 130 % neighborhood type ('ep' and 'cutgauss' are not supported by SOM_PAK;
wolffd@0 131 % they are converted to 'gaussian')
wolffd@0 132 if strcmp(sMap.neigh,'bubble'), fprintf(fid, 'bubble\n');
wolffd@0 133 else
wolffd@0 134 if ~strcmp(sMap.neigh,'gaussian'),
wolffd@0 135 warning(['Neighborhood type ''' sMap.neigh ''' converted to ''gaussian''']);
wolffd@0 136 end
wolffd@0 137 fprintf(fid,'gaussian\n');
wolffd@0 138 end
wolffd@0 139
wolffd@0 140 % write the component names as a SOM_PAK comment line
wolffd@0 141
wolffd@0 142 fprintf(fid,'#n ');
wolffd@0 143 for i=1:dim, fprintf(fid, '%s ', strrep(sMap.comp_names{i},' ','_')); end
wolffd@0 144 fprintf(fid,'\n');
wolffd@0 145
wolffd@0 146 % write codebook
wolffd@0 147
wolffd@0 148 form = [repmat('%g ',[1 dim]) '\n'];
wolffd@0 149 if ~has_labels % no labels; fast
wolffd@0 150 fprintf(fid, form, sMap.codebook(order,:)');
wolffd@0 151 else % has labels; slow
wolffd@0 152 for i=1:munits,
wolffd@0 153 fprintf(fid, '%g ', sMap.codebook(order(i),:));
wolffd@0 154
wolffd@0 155 if has_labels(order(i))
wolffd@0 156 temp = '';
wolffd@0 157 if ver_53_or_newer
wolffd@0 158 nonempty = ~(cellfun('isempty', sMap.labels(i,:)));
wolffd@0 159 else
wolffd@0 160 for j = 1:numlabs, nonempty(j) = ~isempty(sMap.labels{i, j}); end
wolffd@0 161 end
wolffd@0 162 labs = char(sMap.labels{order(i), nonempty});
wolffd@0 163 labs(:,end + 1) = ' ';
wolffd@0 164 temp = reshape(labs',[1 prod(size(labs))]);
wolffd@0 165 temp(findstr(' ', temp))='';
wolffd@0 166 fprintf(fid, '%s\n', temp(1:end-1));
wolffd@0 167 else
wolffd@0 168 fprintf(fid, '\n');
wolffd@0 169 end
wolffd@0 170 end
wolffd@0 171 end
wolffd@0 172
wolffd@0 173 % close file
wolffd@0 174
wolffd@0 175 if fclose(fid)
wolffd@0 176 error(['Cannot close file ' filename]);
wolffd@0 177 else
wolffd@0 178 fprintf(2, 'map write ok\n');
wolffd@0 179 end
wolffd@0 180
wolffd@0 181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wolffd@0 182
wolffd@0 183
wolffd@0 184
wolffd@0 185
wolffd@0 186
wolffd@0 187