forked from ilarinieminen/SOM-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsom_denormalize.m
293 lines (272 loc) · 9.77 KB
/
som_denormalize.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
function sD = som_denormalize(sD,varargin)
%SOM_DENORMALIZE Denormalize data.
%
% sS = som_denormalize(sS, [argID, value, ...])
%
% sS = som_denormalize(sS)
% sS = som_denormalize(sS,[1:3 10],'remove')
% D = som_denormalize(D,sM.comp_norm)
% D = som_denormalize(D,sM,[1:3 10])
%
% Input and output arguments ([]'s are optional):
% sS The data to which the denormalization is applied.
% The modified and updated data is returned.
% (struct) data or map struct
% (matrix) data matrix (a matrix is also returned)
% [argID, (string) See below. The values which are unambiguous can
% value] (varies) be given without the preceeding argID.
%
% Here are the valid argument IDs and corresponding values. The values which
% are unambiguous (marked with '*') can be given without the preceeding argID.
% 'norm' *(struct) Normalization struct, or an array of such.
% Alternatively, a map/data struct can be given
% in which case its '.comp_norm' field is used
% (see below).
% *(cell array) Of normalization structs. Typically, the
% '.comp_norm' field of a map/data struct. The
% length of the array must be equal to data dimension.
% 'remove' *(string) If 'remove' tag is specified, the
% normalization operations are not only undone,
% they are also removed from the struct.
% 'comps' *(vector) the components to which the denormalization is
% applied, default is [1:dim] ie. all components
%
% For more help, try 'type som_denormalize' or check out online documentation.
% See also SOM_NORMALIZE, SOM_NORM_VARIABLE, SOM_INFO.
%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% som_denormalize
%
% PURPOSE
%
% Undo normalizations of data structs/sets.
%
% SYNTAX
%
% sS = som_denormalize(sS)
% sS = som_denormalize(...,'argID',value,...);
% sS = som_denormalize(...,value,...);
% D = som_denormalize(D,sNorm)
%
% DESCRIPTION
%
% This function is used to undo normalizations of data structs/sets. If a
% data/map struct is given, all normalizations in the '.comp_norm' field are
% undone and, thus, the values in the original data context are returned. If
% a matrix is given, the normalizations to undo must be given as the second
% argument. SOM_DENORMALIZE actually uses function SOM_NORM_VARIABLE to
% handle the normalization operations, and only handles the data struct/set
% specific stuff itself.
%
% Normalizations are always one-variable operations. In the data and map
% structs the normalization information for each component is saved in the
% '.comp_norm' field, which is a cell array of length dim. Each cell
% contains normalizations for one vector component in a
% struct array of normalization structs. Each component may have different
% amounts of different kinds of normalizations. Typically, all
% normalizations are either 'undone' or 'done', but in special situations
% this may not be the case. The easiest way to check out the status of the
% normalizations is to use function SOM_INFO, e.g. som_info(sS,3)
%
% REQUIRED INPUT ARGUMENTS
%
% sS The data to which the denormalization is applied.
% (struct) Data or map struct. The normalizations in the
% '.comp_norm' field are undone for the specified
% components.
% (matrix) Data matrix. The normalization to undo must be
% given in the second argument.
%
% OPTIONAL INPUT ARGUMENTS
%
% argID (string) Argument identifier string (see below).
% value (varies) Value for the argument (see below).
%
% The optional arguments can be given as 'argID',value -pairs. If an
% argument is given value multiple times, the last one is used. The
% valid IDs and corresponding values are listed below. The values
% which are unambiguous (marked with '*') can be given without the
% preceeding argID.
%
% sNorm *(struct) Normalization struct, or an array of structs, which
% is undone for all specified components. If the
% '.status' field of the struct(s) is 'uninit',
% the undoing operation is interrupted - it cannot be
% done. Alternatively, the struct may be map or
% data struct in which case its '.comp_norm' field
% is used (see the cell array option below).
% *(cell array) In practice, the '.comp_norm' field of
% a data/map struct. The length of the array
% must be equal to the dimension of the given
% data set (sS). Each cell contains the
% normalization(s) for one component. Only the
% normalizations listed in comps argument are
% undone though.
%
% 'remove' *(string) If 'remove' tag is specified, the
% normalization operations are not only undone,
% they are also removed from the struct.
%
% 'comps' *(vector) The components which are denormalized.
% Default is to undo all components.
% *(string) 'all'
%
% OUTPUT ARGUMENTS
%
% sS Modified and/or updated data.
% (struct) If a struct was given as input argument, the
% same struct is returned with denormalized data and
% updated '.comp_norm' fields.
% (matrix) If a matrix was given as input argument, the
% denormalized data matrix is returned.
%
% EXAMPLES
%
% To undo normalization of a data/map struct:
%
% sD = som_denormalize(sD);
% sM = som_denormalize(sM);
%
% To completely remove the normalizations, use the 'remove' tag:
%
% sD = som_denormalize(sD,'remove');
%
% To undo only a few selected components, use the comps argument:
%
% sD = som_denormalize(sD,[1 3:5]);
%
% To denormalize a set of values from a data set D (which must be
% of equal dimension as the data in sD):
%
% D = som_denormalize(D,sD);
% or
% D = som_denormalize(D,sD.comp_norm);
% only denormalize a few components
% D = som_denormalize(D,sD,[1 3:5]);
%
% Assuming you have a few values of a certain vector component (i)
% in a vector (x) which you want to denormalize:
%
% xorig = som_denormalize(x,sD.comp_norm{i});
% or using SOM_NORM_VARIABLE
% xorig = som_norm_variable(x,sD.comp_norm{i},'undo');
%
% To check out the status of normalization in a struct use SOM_INFO:
%
% som_info(sM,3)
% som_info(sD,3)
%
% SEE ALSO
%
% som_normalize Add/apply/redo normalizations of a data struct/set.
% som_norm_variable Normalization operations for a set of scalar values.
% som_info User-friendly information of SOM Toolbox structs.
% Copyright (c) 1998-2000 by the SOM toolbox programming team.
% http://www.cis.hut.fi/projects/somtoolbox/
% Version 2.0beta juuso 151199 150300
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% check arguments
error(nargchk(1, 3, nargin)); % check no. of input arguments is correct
% sD
struct_mode = isstruct(sD);
if struct_mode,
switch sD.type
case 'som_map', D = sD.codebook;
case 'som_data', D = sD.data;
otherwise, error('Illegal struct.')
end
else
D = sD;
end
[dlen dim] = size(D);
% varargin
comps = [1:dim];
remove_tag = 0;
if struct_mode, sNorm = sD.comp_norm; else sNorm = []; end
i=1;
while i<=length(varargin),
argok = 1;
if ischar(varargin{i}),
switch varargin{i},
% argument IDs
case 'comps', i=i+1; comps = varargin{i};
case {'norm','sNorm','som_norm'}, i=i+1; sNorm = varargin{i};
% unambiguous values
case 'remove', remove_tag = 1;
otherwise argok=0;
end
elseif isnumeric(varargin{i}),
comps = varargin{i};
elseif isstruct(varargin{i}),
sNorm = varargin{i};
elseif iscell(varargin{i}),
sNorm = varargin{i};
else
argok = 0;
end
if ~argok,
disp(['(som_denormalize) Ignoring invalid argument #' num2str(i+1)]);
end
i = i+1;
end
% check comps
if ischar(comps), comps = [1:dim]; end
if isempty(comps), return; end
if size(comps,1)>1, comps = comps'; end % make it a row vector
% sNorm
% check out the given normalization
% (and if necessary, copy it for each specified component)
if isstruct(sNorm),
switch sNorm(1).type,
case {'som_map','som_data'}, csNorm = sNorm(1).comp_norm;
case {'som_norm'}, for i=comps, csNorm{i} = sNorm; end
otherwise,
error('Invalid struct for sNorm.')
end
elseif iscell(sNorm),
csNorm = sNorm;
else
error('Illegal value for sNorm.')
end
% check that csNorm and comps possibly agree
if max(comps) > length(csNorm),
error('Given normalizations does not match the components.')
end
if length(csNorm) ~= dim,
error('Given normalizations does not match data dimension.')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% action
% undo the normalizations
for i = comps,
len = length(csNorm{i});
for j=len:-1:1,
sN = csNorm{i}(j);
if struct_mode,
if strcmp(sN.status,'done'),
[x,sN] = som_norm_variable(D(:,i), sN, 'undo');
D(:,i) = x;
csNorm{i}(j) = sN;
end
else
D(:,i) = som_norm_variable(D(:,i), sN, 'undo');
end
end
end
% remove normalizations
if struct_mode && remove_tag,
for i = comps, csNorm{i} = []; end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% output
if struct_mode,
switch sD.type
case 'som_map', sD.codebook = D;
case 'som_data', sD.data = D;
otherwise, error('Illegal struct.')
end
sD.comp_norm = csNorm;
else
sD = D;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%