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