Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on21/01/2009 16:34:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,215 @@
1
+function sMap = som_read_cod(filename)
2
+
3
+%SOM_READ_COD Reads a SOM_PAK format codebook file.
4
+%
5
+% sMap = som_read_cod(filename);
6
+%
7
+%  sMap = som_read_cod('map1.cod');
8
+%
9
+%  Input and output arguments: 
10
+%   filename    (string) name of input file
11
+%   sMap        (struct) self-organizing map structure
12
+%
13
+% The file must be in SOM_PAK format. Empty lines and lines starting 
14
+% with a '#' are ignored, except the ones starting with '#n'. The strings 
15
+% after '#n' are read to field 'comp_names' of the map structure.
16
+%
17
+% For more help, try 'type som_read_cod' or check out online documentation.
18
+% See also SOM_WRITE_COD, SOM_READ_DATA, SOM_WRITE_DATA, SOM_MAP_STRUCT.
19
+
20
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
+%
22
+% som_read_cod
23
+%
24
+% PURPOSE
25
+%
26
+% Reads a Self-Organizing Map from an ascii file in SOM_PAK format.
27
+%
28
+% SYNTAX
29
+%
30
+%  sMap = som_read_cod(filename); 
31
+%
32
+% DESCRIPTION
33
+%
34
+% This function is offered for compatibility with SOM_PAK, a SOM 
35
+% software package in C. It reads map files written in SOM_PAK format.
36
+%
37
+% The SOM_PAK map file format is as follows. The first line must contain
38
+% the input space dimension, lattice type ('rect' or 'hexa'), map grid
39
+% size in x-direction, map grid size in y-direction, and neighborhood
40
+% function ('bubble' or 'gaussian'), in that order. The following lines
41
+% are comment lines, empty lines or data lines. 
42
+%
43
+% Each data line contains the weight vector of one map unit and its
44
+% labels. From the beginning of the line, first are values of the vector
45
+% components separated by whitespaces, then labels, again separated by
46
+% whitespaces. The order of map units in the file are one row at a time
47
+% from right to left, from the top to the bottom of the map (x-direction
48
+% first, then y-direction). 
49
+% 
50
+% Comment lines start with '#'. Comment lines as well as empty lines are
51
+% ignored, except if the comment line starts with '#n'. In that case the
52
+% line should contain names of the vector components separated by
53
+% whitespaces.
54
+%
55
+% In the returned map struct, several fields has to be set to default
56
+% values, since the SOM_PAK file does not contain information on
57
+% them. These include map shape ('sheet'), mask ([1 ... 1]),
58
+% normalizations (none), trainhist (two entries, first with algorithm
59
+% 'init' and the second with 'seq', both with data name 'unknown'),
60
+% possibly also component names ('Var1',...). 
61
+%
62
+% REQUIRED INPUT PARAMETERS
63
+%
64
+%  filename   (string) the name of the input file
65
+%
66
+% OUTPUT ARGUMENTS
67
+%
68
+%  sMap       (struct) the resulting SOM struct
69
+% 
70
+% EXAMPLES
71
+%
72
+%  sMap = som_read_cod('map1.cod');
73
+%
74
+% SEE ALSO
75
+% 
76
+%  som_write_cod    Writes a map struct into a file in SOM_PAK format.
77
+%  som_read_data    Reads data from an ascii file.
78
+%  som_write_data   Writes data struct into a file in SOM_PAK format.
79
+%  som_map_struct   Creates map structs.
80
+
81
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
82
+% http://www.cis.hut.fi/projects/somtoolbox/
83
+
84
+% Version 1.0beta ecco 221097
85
+% Version 2.0beta juuso 151199 250400
86
+
87
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88
+%% check arguments
89
+
90
+error(nargchk(1, 1, nargin))  % check no. of input args is correct
91
+
92
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93
+%% initialize variables
94
+
95
+lnum           = 0;    % codebook vector counter
96
+comment_start  = '#';  % the char a SOM_PAK command line starts with
97
+comp_name_line = '#n'; % string used to start a special command line,
98
+                       % which contains names of each component
99
+
100
+% open input file
101
+
102
+fid = fopen(filename);
103
+if fid < 0, error(['Cannot open ' filename]); end
104
+ 
105
+% read header line
106
+
107
+ok_cnt = 0;
108
+lin = fgetl(fid); li = lin;
109
+[dim c err n]  = sscanf(li, '%d%[^ \t]'); ok_cnt=ok_cnt+c; li = li(n:end);
110
+[lattice c err n] = sscanf(li,'%s%[^ \t]'); ok_cnt=ok_cnt+c; li = li(n:end);
111
+[msize(2) c err n] = sscanf(li, '%d%[^ \t]'); ok_cnt=ok_cnt+c; li = li(n:end);
112
+[msize(1) c err n] = sscanf(li, '%d%[^ \t]'); ok_cnt=ok_cnt+c; li = li(n:end);
113
+[neigh c err n] = sscanf(li, '%s%[^ \t\n]'); ok_cnt=ok_cnt+c;
114
+
115
+if ok_cnt ~= 5
116
+  error([ 'Invalid header line: ' lin ]); 
117
+end                                 
118
+
119
+% create map struct and set its fields according to header line
120
+
121
+munits = prod(msize);
122
+sMap   = som_map_struct(dim, 'msize', msize, ...
123
+			lattice, 'sheet', 'neigh', neigh);
124
+[sT0, ok] = som_set('som_train','algorithm','init','data_name','unknown');
125
+sT1       = som_set('som_train','algorithm','seq','data_name','unknown',...
126
+                    'neigh',neigh,'mask',ones(dim,1));
127
+[sMap, ok, msgs] = som_set(sMap,'name',filename,'trainhist',{sT0,sT1});
128
+
129
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130
+%% read codebook from the file
131
+
132
+codebook   = zeros(munits,dim);
133
+labels     = cell(munits,1);
134
+comp_names = sMap.comp_names;
135
+form       = [repmat('%f',[1 dim-1]) '%f%[^ \t]'];
136
+
137
+while 1, 
138
+  li = fgetl(fid);                         % read next line
139
+  if ~isstr(li), break, end;               % is this the end of file?
140
+
141
+  [data, c, err, n] = sscanf(li, form);
142
+  if c < dim % if there were less numbers than dim on the input file line
143
+    if c == 0
144
+      if strncmp(li, comp_name_line, 2) % component name line?
145
+        li = li(3:end); i = 0; c = 1;
146
+        while c
147
+          [s, c, e, n] = sscanf(li, '%s%[^ \t]');
148
+          if ~isempty(s), i = i + 1; comp_names{i} = s; li = li(n:end); end
149
+        end
150
+
151
+        if i ~= dim
152
+          error(['Illegal number of component names: ' num2str(i) ...
153
+                 ' (dimension is ' num2str(dim) ')']);
154
+        end
155
+      elseif ~strncmp(li, comment_start, 1) % not a comment, is it error?
156
+        [s, c, e, n] = sscanf(li, '%s%[^ \t]');
157
+        if c
158
+          error(['Invalid vector on input file line ' ...
159
+                 num2str(lnum+1) ': [' deblank(li) ']']),
160
+        end
161
+      end
162
+    else
163
+      error(['Only ' num2str(c) ' vector components on input file line ' ...
164
+             num2str(lnum+1) ' (dimension is ' num2str(dim) ')']);
165
+    end
166
+
167
+  else
168
+
169
+    lnum = lnum + 1;                % this was a line containing data vector
170
+    codebook(lnum, 1:dim) = data'; % add data to struct
171
+
172
+    % read labels
173
+
174
+    if n < length(li)
175
+      li = li(n:end);
176
+      i = 0; n = 1; c = 1;
177
+      while c
178
+        [s, c, e, n_new] = sscanf(li(n:end), '%s%[^ \t]');
179
+        if c, i = i + 1; labels{lnum, i} = s; n = n + n_new - 1; end
180
+      end
181
+    end
182
+  end
183
+end
184
+
185
+% close the input file
186
+
187
+if fclose(fid) < 0
188
+  error(['Cannot close file ' filename]); 
189
+else
190
+  fprintf(2, '\rmap read ok         \n');
191
+end
192
+
193
+% check that the number of lines read was correct
194
+
195
+if lnum ~= munits
196
+  error(['Illegal number of map units: ' num2str(lnum) ' (should be ' num2str(munits) ').']);
197
+end
198
+
199
+% set values
200
+
201
+% in SOM_PAK the xy-indexing is used, while in Matlab ij-indexing
202
+% therefore, the codebook vectors have to be reorganized 
203
+
204
+order = reshape([1:munits],msize);
205
+order = reshape(order',[munits 1]);
206
+codebook(order,:) = codebook;
207
+labels(order,:) = labels; 
208
+
209
+sMap.codebook   = codebook;
210
+sMap.labels     = labels;
211
+sMap.comp_names = comp_names;
212
+
213
+return;
214
+
215
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%