annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_normalize.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 sD = som_normalize(sD,method,comps)
Daniel@0 2
Daniel@0 3 %SOM_NORMALIZE (Re)normalize data or add new normalizations.
Daniel@0 4 %
Daniel@0 5 % sS = som_normalize(sS,[method],[comps])
Daniel@0 6 %
Daniel@0 7 % sS = som_normalize(sD)
Daniel@0 8 % sS = som_normalize(sS,sNorm)
Daniel@0 9 % D = som_normalize(D,'var')
Daniel@0 10 % sS = som_normalize(sS,'histC',[1:3 10])
Daniel@0 11 %
Daniel@0 12 % Input and output arguments ([]'s are optional):
Daniel@0 13 % sS The data to which the normalization is applied.
Daniel@0 14 % The modified and updated data is returned.
Daniel@0 15 % (struct) data or map struct
Daniel@0 16 % (matrix) data matrix (a matrix is also returned)
Daniel@0 17 % [method] The normalization method(s) to add/use. If missing,
Daniel@0 18 % or an empty variable ('') is given, the
Daniel@0 19 % normalizations in sS are used.
Daniel@0 20 % (string) identifier for a normalization method to be added:
Daniel@0 21 % 'var', 'range', 'log', 'logistic', 'histD' or 'histC'.
Daniel@0 22 % (struct) Normalization struct, or an array of such.
Daniel@0 23 % Alternatively, a map/data struct can be given
Daniel@0 24 % in which case its '.comp_norm' field is used
Daniel@0 25 % (see below).
Daniel@0 26 % (cell array) Of normalization structs. Typically, the
Daniel@0 27 % '.comp_norm' field of a map/data struct. The
Daniel@0 28 % length of the array must be equal to data dimension.
Daniel@0 29 % (cellstr array) norm and denorm operations in a cellstr array
Daniel@0 30 % which are evaluated with EVAL command with variable
Daniel@0 31 % name 'x' reserved for the variable.
Daniel@0 32 % [comps] (vector) the components to which the normalization is
Daniel@0 33 % applied, default is [1:dim] ie. all components
Daniel@0 34 %
Daniel@0 35 % For more help, try 'type som_normalize' or check out online documentation.
Daniel@0 36 % See also SOM_DENORMALIZE, SOM_NORM_VARIABLE, SOM_INFO.
Daniel@0 37
Daniel@0 38 %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 39 %
Daniel@0 40 % som_normalize
Daniel@0 41 %
Daniel@0 42 % PURPOSE
Daniel@0 43 %
Daniel@0 44 % Add/apply/redo normalization on data structs/sets.
Daniel@0 45 %
Daniel@0 46 % SYNTAX
Daniel@0 47 %
Daniel@0 48 % sS = som_normalize(sS)
Daniel@0 49 % sS = som_normalize(sS,method)
Daniel@0 50 % D = som_normalize(D,sNorm)
Daniel@0 51 % sS = som_normalize(sS,csNorm)
Daniel@0 52 % sS = som_normalize(...,comps)
Daniel@0 53 %
Daniel@0 54 % DESCRIPTION
Daniel@0 55 %
Daniel@0 56 % This function is used to (initialize and) add, redo and apply
Daniel@0 57 % normalizations on data/map structs/sets. If a data/map struct is given,
Daniel@0 58 % the specified normalizations are added to the '.comp_norm' field of the
Daniel@0 59 % struct after ensuring that all normalizations specified therein have
Daniel@0 60 % status 'done'. SOM_NORMALIZE actually uses function SOM_NORM_VARIABLE
Daniel@0 61 % to handle the normalization operations, and only handles the data
Daniel@0 62 % struct/set specific stuff itself.
Daniel@0 63 %
Daniel@0 64 % The different normalization methods are listed below. For more
Daniel@0 65 % detailed descriptions, see SOM_NORM_VARIABLE.
Daniel@0 66 %
Daniel@0 67 % method description
Daniel@0 68 % 'var' Variance is normalized to one (linear operation).
Daniel@0 69 % 'range' Values are normalized between [0,1] (linear operation).
Daniel@0 70 % 'log' Natural logarithm is applied to the values:
Daniel@0 71 % xnew = log(x-m+1)
Daniel@0 72 % where m = min(x).
Daniel@0 73 % 'logistic' Logistic or softmax trasformation which scales all
Daniel@0 74 % possible values between [0,1].
Daniel@0 75 % 'histD' Histogram equalization, values scaled between [0,1].
Daniel@0 76 % 'histC' Approximate histogram equalization with partially
Daniel@0 77 % linear operations. Values scaled between [0,1].
Daniel@0 78 % 'eval' freeform operations
Daniel@0 79 %
Daniel@0 80 % To enable undoing and applying the exactly same normalization to
Daniel@0 81 % other data sets, normalization information is saved into a
Daniel@0 82 % normalization struct, which has the fields:
Daniel@0 83 %
Daniel@0 84 % .type ; struct type, ='som_norm'
Daniel@0 85 % .method ; normalization method, a string
Daniel@0 86 % .params ; normalization parameters
Daniel@0 87 % .status ; string: 'uninit', 'undone' or 'done'
Daniel@0 88 %
Daniel@0 89 % Normalizations are always one-variable operations. In the data and map
Daniel@0 90 % structs the normalization information for each component is saved in the
Daniel@0 91 % '.comp_norm' field, which is a cell array of length dim. Each cell
Daniel@0 92 % contains normalizations for one vector component in a struct array of
Daniel@0 93 % normalization structs. Each component may have different amounts of
Daniel@0 94 % different kinds of normalizations. Typically, all normalizations are
Daniel@0 95 % either 'undone' or 'done', but in special situations this may not be the
Daniel@0 96 % case. The easiest way to check out the status of the normalizations is to
Daniel@0 97 % use function SOM_INFO, e.g. som_info(sS,3)
Daniel@0 98 %
Daniel@0 99 % REQUIRED INPUT ARGUMENTS
Daniel@0 100 %
Daniel@0 101 % sS The data to which the normalization is applied.
Daniel@0 102 % (struct) Data or map struct. Before adding any new
Daniel@0 103 % normalizations, it is ensured that the
Daniel@0 104 % normalizations for the specified components in the
Daniel@0 105 % '.comp_norm' field have status 'done'.
Daniel@0 106 % (matrix) data matrix
Daniel@0 107 %
Daniel@0 108 % OPTIONAL INPUT ARGUMENTS
Daniel@0 109 %
Daniel@0 110 % method The normalization(s) to add/use. If missing,
Daniel@0 111 % or an empty variable ('' or []) is given, the
Daniel@0 112 % normalizations in the data struct are used.
Daniel@0 113 % (string) Identifier for a normalization method to be added:
Daniel@0 114 % 'var', 'range', 'log', 'logistic', 'histD' or 'histC'. The
Daniel@0 115 % same method is applied to all specified components
Daniel@0 116 % (given in comps). The normalizations are first
Daniel@0 117 % initialized (for each component separately, of
Daniel@0 118 % course) and then applied.
Daniel@0 119 % (struct) Normalization struct, or an array of structs, which
Daniel@0 120 % is applied to all specified components. If the
Daniel@0 121 % '.status' field of the struct(s) is 'uninit',
Daniel@0 122 % the normalization(s) is initialized first.
Daniel@0 123 % Alternatively, the struct may be map or data struct
Daniel@0 124 % in which case its '.comp_norm' field is used
Daniel@0 125 % (see the cell array option below).
Daniel@0 126 % (cell array) In practice, the '.comp_norm' field of
Daniel@0 127 % a data/map struct. The length of the array
Daniel@0 128 % must be equal to the dimension of the given
Daniel@0 129 % data set (sS). Each cell contains the
Daniel@0 130 % normalization(s) for one component. Only the
Daniel@0 131 % normalizations listed in comps argument are
Daniel@0 132 % applied though.
Daniel@0 133 % (cellstr array) norm and denorm operations in a cellstr array
Daniel@0 134 % which are evaluated with EVAL command with variable
Daniel@0 135 % name 'x' reserved for the variable.
Daniel@0 136 %
Daniel@0 137 % comps (vector) The components to which the normalization(s) is
Daniel@0 138 % applied. Default is to apply to all components.
Daniel@0 139 %
Daniel@0 140 % OUTPUT ARGUMENTS
Daniel@0 141 %
Daniel@0 142 % sS Modified and/or updated data.
Daniel@0 143 % (struct) If a struct was given as input argument, the
Daniel@0 144 % same struct is returned with normalized data and
Daniel@0 145 % updated '.comp_norm' fields.
Daniel@0 146 % (matrix) If a matrix was given as input argument, the
Daniel@0 147 % normalized data matrix is returned.
Daniel@0 148 %
Daniel@0 149 % EXAMPLES
Daniel@0 150 %
Daniel@0 151 % To add (initialize and apply) a normalization to a data struct:
Daniel@0 152 %
Daniel@0 153 % sS = som_normalize(sS,'var');
Daniel@0 154 %
Daniel@0 155 % This uses 'var'-method to all components. To add a method only to
Daniel@0 156 % a few selected components, use the comps argument:
Daniel@0 157 %
Daniel@0 158 % sS = som_normalize(sS,'log',[1 3:5]);
Daniel@0 159 %
Daniel@0 160 % To ensure that all normalization operations have indeed been done:
Daniel@0 161 %
Daniel@0 162 % sS = som_normalize(sS);
Daniel@0 163 %
Daniel@0 164 % The same for only a few components:
Daniel@0 165 %
Daniel@0 166 % sS = som_normalize(sS,'',[1 3:5]);
Daniel@0 167 %
Daniel@0 168 % To apply the normalizations of a data struct sS to a new data set D:
Daniel@0 169 %
Daniel@0 170 % D = som_normalize(D,sS);
Daniel@0 171 % or
Daniel@0 172 % D = som_normalize(D,sS.comp_norm);
Daniel@0 173 %
Daniel@0 174 % To normalize a data set:
Daniel@0 175 %
Daniel@0 176 % D = som_normalize(D,'histD');
Daniel@0 177 %
Daniel@0 178 % Note that in this case the normalization information is lost.
Daniel@0 179 %
Daniel@0 180 % To check out the status of normalization in a struct use SOM_INFO:
Daniel@0 181 %
Daniel@0 182 % som_info(sS,3)
Daniel@0 183 %
Daniel@0 184 %
Daniel@0 185 % SEE ALSO
Daniel@0 186 %
Daniel@0 187 % som_denormalize Undo normalizations of a data struct/set.
Daniel@0 188 % som_norm_variable Normalization operations for a set of scalar values.
Daniel@0 189 % som_info User-friendly information of SOM Toolbox structs.
Daniel@0 190
Daniel@0 191 % Copyright (c) 1998-2000 by the SOM toolbox programming team.
Daniel@0 192 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 193
Daniel@0 194 % Version 2.0beta juuso 151199 150500
Daniel@0 195
Daniel@0 196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 197 %% check arguments
Daniel@0 198
Daniel@0 199 error(nargchk(1, 3, nargin)); % check no. of input arguments is correct
Daniel@0 200
Daniel@0 201 % sD
Daniel@0 202 struct_mode = isstruct(sD);
Daniel@0 203 if struct_mode,
Daniel@0 204 switch sD.type
Daniel@0 205 case 'som_map', D = sD.codebook;
Daniel@0 206 case 'som_data', D = sD.data;
Daniel@0 207 otherwise, error('Illegal struct.')
Daniel@0 208 end
Daniel@0 209 else
Daniel@0 210 D = sD;
Daniel@0 211 end
Daniel@0 212 [dlen dim] = size(D);
Daniel@0 213
Daniel@0 214 % comps
Daniel@0 215 if nargin<3 | (ischar(comps) & strcmp(comps,'all')),
Daniel@0 216 comps = [1:dim];
Daniel@0 217 end
Daniel@0 218 if isempty(comps), return; end
Daniel@0 219 if size(comps,1)>1, comps = comps'; end % make it a row vector
Daniel@0 220
Daniel@0 221 % method
Daniel@0 222 csNorm = cell(dim,1);
Daniel@0 223 if nargin<2 | isempty(method),
Daniel@0 224 if ~struct_mode,
Daniel@0 225 warning('No normalization method given. Data left unchanged.');
Daniel@0 226 return;
Daniel@0 227 end
Daniel@0 228 method = '';
Daniel@0 229 else
Daniel@0 230 % check out the given method
Daniel@0 231 % (and if necessary, copy it for each specified component)
Daniel@0 232 if ischar(method),
Daniel@0 233 switch method,
Daniel@0 234 case {'var','range','log','histD','histC','logistic'},
Daniel@0 235 sN = som_set('som_norm','method',method);
Daniel@0 236 otherwise,
Daniel@0 237 error(['Unrecognized method: ' method]);
Daniel@0 238 end
Daniel@0 239 for i=comps, csNorm{i} = sN; end
Daniel@0 240 elseif isstruct(method),
Daniel@0 241 switch method(1).type,
Daniel@0 242 case {'som_map','som_data'}, csNorm = method(1).comp_norm;
Daniel@0 243 case {'som_norm'}, for i=comps, csNorm{i} = method; end
Daniel@0 244 otherwise,
Daniel@0 245 error('Invalid struct given as normalization method.')
Daniel@0 246 end
Daniel@0 247 elseif iscellstr(method),
Daniel@0 248 [dummy,sN] = som_norm_variable(1,method,'init');
Daniel@0 249 for i=comps, csNorm{i} = sN; end
Daniel@0 250 elseif iscell(method),
Daniel@0 251 csNorm = method;
Daniel@0 252 else
Daniel@0 253 error('Illegal method argument.')
Daniel@0 254 end
Daniel@0 255 % check the size of csNorm is the same as data dimension
Daniel@0 256 if length(csNorm) ~= dim,
Daniel@0 257 error('Given number of normalizations does not match data dimension.')
Daniel@0 258 end
Daniel@0 259 end
Daniel@0 260
Daniel@0 261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 262 %% initialize
Daniel@0 263
Daniel@0 264 % make sure all the current normalizations for current
Daniel@0 265 % components have been done
Daniel@0 266 if struct_mode,
Daniel@0 267 alldone = 1;
Daniel@0 268 for i = comps,
Daniel@0 269 for j=1:length(sD.comp_norm{i}),
Daniel@0 270 sN = sD.comp_norm{i}(j);
Daniel@0 271 if ~strcmp(sN.status,'done'),
Daniel@0 272 alldone = 0;
Daniel@0 273 [x,sN] = som_norm_variable(D(:,i), sN, 'do');
Daniel@0 274 D(:,i) = x;
Daniel@0 275 sD.comp_norm{i}(j) = sN;
Daniel@0 276 end
Daniel@0 277 end
Daniel@0 278 end
Daniel@0 279 if isempty(method),
Daniel@0 280 if alldone,
Daniel@0 281 warning('No ''undone'' normalizations found. Data left unchanged.');
Daniel@0 282 else
Daniel@0 283 fprintf(1,'Normalizations have been redone.\n');
Daniel@0 284 end
Daniel@0 285 end
Daniel@0 286 end
Daniel@0 287
Daniel@0 288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 289 %% action
Daniel@0 290
Daniel@0 291 % add the new normalizations to the old ones
Daniel@0 292 for i = comps,
Daniel@0 293 if ~isempty(csNorm{i}),
Daniel@0 294 [x,sN] = som_norm_variable(D(:,i), csNorm{i}, 'do');
Daniel@0 295 D(:,i) = x;
Daniel@0 296 if struct_mode,
Daniel@0 297 if isempty(sD.comp_norm{i}), sD.comp_norm{i} = sN;
Daniel@0 298 else sD.comp_norm{i} = [sD.comp_norm{i}, sN]; end
Daniel@0 299 end
Daniel@0 300 end
Daniel@0 301 end
Daniel@0 302
Daniel@0 303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 304 %% output
Daniel@0 305
Daniel@0 306 if struct_mode,
Daniel@0 307 switch sD.type
Daniel@0 308 case 'som_map', sD.codebook = D;
Daniel@0 309 case 'som_data', sD.data = D;
Daniel@0 310 otherwise, error('Illegal struct.')
Daniel@0 311 end
Daniel@0 312 else
Daniel@0 313 sD = D;
Daniel@0 314 end
Daniel@0 315
Daniel@0 316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 317
Daniel@0 318
Daniel@0 319