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