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,231 @@
1
+function sMap = som_map_struct(dim, varargin)
2
+
3
+%SOM_MAP_STRUCT Create map struct. 
4
+% 
5
+% sMap = som_map_struct(dim, [[argID,] value, ...])
6
+%
7
+%  sMap = som_map_struct(4);
8
+%  sMap = som_map_struct(4,'msize',[3 4],'hexa','sheet');
9
+%  sMap = som_map_struct(4,'msize',[3 4 5],'rect','name','a 3D-SOM');
10
+%  sMap = som_map_struct(4,'msize',[3 4],'bubble','mask',[1 1 1 0]);
11
+%
12
+%  Input and output arguments ([]'s are optional): 
13
+%   dim      (scalar) input space dimension
14
+%   [argID,  (string) See below. The values which are unambiguous can 
15
+%    value]  (varies) be given without the preceeding argID.
16
+%
17
+%   sMap     (struct) self-organizing map struct
18
+%
19
+% Here are the valid argument IDs and corresponding values. The values
20
+% which are unambiguous (marked with '*') can be given without the
21
+% preceeding argID.
22
+%   'mask'       (vector) BMU search mask, size dim x 1
23
+%   'msize'      (vector) map grid size, default is [0]
24
+%   'labels'     (string array / cellstr) labels for each map unit, 
25
+%                 length=prod(msize)
26
+%   'name'       (string) map name
27
+%   'comp_names' (string array / cellstr) component names, size dim x 1
28
+%   'comp_norm'  (cell array) normalization operations for each
29
+%                 component, size dim x 1. Each cell is either empty, 
30
+%                 or a cell array of normalization structs.
31
+%   'topol'     *(struct) topology struct
32
+%   'som_topol','sTopol' = 'topol'
33
+%   'lattice'   *(string) map lattice, 'hexa' or 'rect'
34
+%   'shape'     *(string) map shape, 'sheet', 'cyl' or 'toroid'
35
+%   'neigh'     *(string) neighborhood function, 'gaussian', 'cutgauss',
36
+%                 'ep' or 'bubble'
37
+%
38
+% For more help, try 'type som_map_struct' or check out online documentation.
39
+% See also SOM_SET, SOM_INFO, SOM_DATA_STRUCT, SOM_TOPOL_STRUCT, SOM_MAKE.
40
+
41
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
+%
43
+% som_map_struct
44
+%
45
+% PURPOSE
46
+%
47
+% Creates a self-organizing map structure. 
48
+%
49
+% SYNTAX
50
+%
51
+%  sM = som_map_struct(dim)
52
+%  sM = som_map_struct(...,'argID',value,...);
53
+%  sM = som_map_struct(...,value,...);
54
+%
55
+% DESCRIPTION
56
+%
57
+% Creates a self-organizing map struct. The struct contains the map
58
+% codebook, labels, topology, information on normalization and training, 
59
+% as well as component names and a name for the map. The obligatory
60
+% parameter is the map dimension. Most of the other fields can be
61
+% given values using optional arguments. If they are left unspecified,
62
+% default values are used.
63
+%
64
+%  Field         Type         Size / default value (munits = prod(msize))
65
+%  ------------------------------------------------------------------------
66
+%   .type        (string)     'som_map'               
67
+%   .name        (string)     'SOM date'
68
+%   .codebook    (matrix)     rand(munits, dim)
69
+%   .topol       (struct)     topology struct, with the following fields
70
+%     .type         (string)  'som_topol'
71
+%     .msize        (vector)  size k x 1, [0] 
72
+%     .lattice      (string)  'hexa' 
73
+%     .shape        (string)  'sheet'
74
+%   .labels      (cellstr)    size munits x m, {''; ''; ... ''}
75
+%   .neigh       (string)     'gaussian'
76
+%   .mask        (vector)     size dim x 1, [1; 1; ...; 1]
77
+%   .trainhist   (cell array) size tl x 1, []
78
+%   .comp_names  (cellstr)    size dim x 1, {'Variable1', 'Variable2', ...}
79
+%   .comp_norm   (cell array) size dim x 1, {[], [], ... []}
80
+%
81
+% '.type' field is the struct identifier. Do not change it.
82
+% '.name' field is the identifier for the whole map struct
83
+% '.codebook' field is the codebook matrix, each row corresponds to one unit
84
+% '.topol' field is the topology of the map. This struct has three fields:
85
+%   '.msize' field is the dimensions of the map grid. Note that the
86
+%         matrix notation of indeces is used.
87
+%   '.lattice' field is the map grid lattice
88
+%   '.shape' field is the map grid shape
89
+% '.labels' field contains the labels for each of the vectors. The ith row
90
+%         of '.labels' contains the labels for ith map unit. Note that 
91
+%         if some vectors have more labels than others, the others are
92
+%         are given empty labels ('') to pad the '.labels' array up.
93
+% '.neigh' field is the neighborhood function. 
94
+% '.mask' field is the BMU search mask.
95
+% '.trainhist' field contains information on the training. It is a cell
96
+%         array of training structs. The first training struct contains
97
+%         information on initialization, the others on actual trainings. 
98
+%         If the map has not been initialized, '.trainhist' is empty ([]).
99
+% '.comp_names' field contains the names of the vector components
100
+% '.comp_norm' field contains normalization information for each
101
+%         component. Each cell of '.comp_norm' is itself a cell array of
102
+%         normalization structs. If no normalizations are performed for 
103
+%         the particular component, the cell is empty ([]).
104
+%
105
+% REQUIRED INPUT ARGUMENTS
106
+%
107
+%  dim    (scalar) Input space dimension. 
108
+%  
109
+% OPTIONAL INPUT ARGUMENTS 
110
+%
111
+%  argID (string) Argument identifier string (see below).
112
+%  value (varies) Value for the argument (see below).
113
+%
114
+%  The optional arguments are given as 'argID',value -pairs. If the
115
+%  value is unambiguous (marked below with '*'), it can be given
116
+%  without the preceeding argID. If an argument is given value
117
+%  multiple times, the last one is used.
118
+%
119
+%   'mask'       (vector) BMU search mask, size dim x 1
120
+%   'msize'      (vector) map grid size, default is [0]
121
+%   'labels'     (string array / cellstr) labels for each map unit, 
122
+%                 length=prod(msize)
123
+%   'name'       (string) map name
124
+%   'comp_names' (string array / cellstr) component names, size dim x 1
125
+%   'comp_norm'  (cell array) normalization operations for each
126
+%                 component, size dim x 1. Each cell is either empty, 
127
+%                 or a cell array of normalization structs.
128
+%   'lattice'   *(string) map lattice, 'hexa' or 'rect'
129
+%   'shape'     *(string) map shape, 'sheet', 'cyl' or 'toroid'
130
+%   'topol'     *(struct) topology struct, sets msize, lattice and shape
131
+%   'som_topol','sTopol' = 'topol'
132
+%   'neigh'     *(string) neighborhood function, 'gaussian', 'cutgauss',
133
+%                 'ep' or 'bubble'
134
+%
135
+% OUTPUT ARGUMENTS
136
+% 
137
+%  sMap (struct) the map struct
138
+%
139
+% EXAMPLES
140
+%
141
+% Simplest case:
142
+%  sMap = som_map_struct(3);
143
+%  
144
+% With optional arguments, the other fields can be given values:
145
+%  sTo    = som_set('som_topol','msize',[10 5]);
146
+%  labs   = cell(50, 1); labs{1, 1} = 'first_unit';
147
+%  cnames = {'first'; 'second'; 'third'};
148
+%  sN     = som_set('som_norm');
149
+%  csN    = {sN; sN; sN};
150
+%  
151
+%  sMap = som_map_struct(3,'msize',[10 5],'rect');
152
+%  sMap = som_map_struct(3,'msize',[10 5],'lattice','rect');
153
+%  sMap = som_map_struct(3,sTo,'bubble','labels',labs);
154
+%  sMap = som_map_struct(3,sTo,'comp_names',cnames);
155
+%  sMap = som_map_struct(3,sTo,'name','a data struct');
156
+%  sMap = som_map_struct(3,sTo,'comp_norm',csN,'mask',[1 0 0.5]);
157
+%
158
+% SEE ALSO
159
+% 
160
+%  som_set          Set values and create SOM Toolbox structs.
161
+%  som_data_struct  Create a data struct.
162
+%  som_make         Initialize and train self-organizing map.
163
+%  som_topol_struct Default values for map topology.
164
+
165
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
166
+% http://www.cis.hut.fi/projects/somtoolbox/
167
+
168
+% Version 1.0beta ecco 100997
169
+% Version 2.0beta juuso 101199 130300
170
+
171
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172
+
173
+% default values
174
+sTopol     = som_set('som_topol','lattice','hexa','shape','sheet');
175
+neigh      = 'gaussian';
176
+mask       = ones(dim,1);
177
+name       = sprintf('SOM %s', datestr(now, 1));
178
+labels     = cell(prod(sTopol.msize),1);
179
+for i=1:length(labels), labels{i} = ''; end
180
+comp_names = cell(dim,1); 
181
+for i = 1:dim, comp_names{i} = sprintf('Variable%d', i); end
182
+comp_norm  = cell(dim,1); 
183
+
184
+% varargin
185
+i=1; 
186
+while i<=length(varargin), 
187
+  argok = 1; 
188
+  if ischar(varargin{i}), 
189
+    switch varargin{i}, 
190
+      % argument IDs
191
+     case 'mask',       i=i+1; mask = varargin{i}; 
192
+     case 'msize',      i=i+1; sTopol.msize = varargin{i}; 
193
+     case 'labels',     i=i+1; labels = varargin{i};
194
+     case 'name',       i=i+1; name = varargin{i};
195
+     case 'comp_names', i=i+1; comp_names = varargin{i}; 
196
+     case 'comp_norm',  i=i+1; comp_norm = varargin{i};
197
+     case 'lattice',    i=i+1; sTopol.lattice = varargin{i};
198
+     case 'shape',      i=i+1; sTopol.shape = varargin{i}; 
199
+     case {'topol','som_topol','sTopol'}, i=i+1; sTopol = varargin{i};
200
+     case 'neigh',      i=i+1; neigh = varargin{i};
201
+      % unambiguous values
202
+     case {'hexa','rect'}, sTopol.lattice = varargin{i};
203
+     case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; 
204
+     case {'gaussian','cutgauss','ep','bubble'}, neigh = varargin{i};
205
+     otherwise argok=0; 
206
+    end
207
+  elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), 
208
+    switch varargin{i}(1).type, 
209
+     case 'som_topol', sTopol = varargin{i};
210
+     otherwise argok=0; 
211
+    end
212
+  else
213
+    argok = 0; 
214
+  end
215
+  if ~argok, 
216
+    disp(['(som_map_struct) Ignoring invalid argument #' num2str(i+1)]); 
217
+  end
218
+  i = i+1; 
219
+end
220
+
221
+% create the SOM
222
+codebook = rand(prod(sTopol.msize),dim); 
223
+sTrain = som_set('som_train','time',datestr(now,0),'mask',mask);
224
+sMap = som_set('som_map','codebook',codebook,'topol',sTopol,...
225
+                         'neigh',neigh,'labels',labels,'mask',mask,...
226
+                         'comp_names',comp_names,'name',name,...
227
+                         'comp_norm',comp_norm,'trainhist',sTrain);
228
+
229
+
230
+
231
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%