annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_trajectory.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 som_trajectory(bmus,varargin)
Daniel@0 2
Daniel@0 3 %SOM_TRAJECTORY Launch a "comet" trajectory visualization GUI.
Daniel@0 4 %
Daniel@0 5 % som_show(sM,'umat','all')
Daniel@0 6 % bmus = som_bmus(sM,sD);
Daniel@0 7 % som_trajectory(bmus)
Daniel@0 8 % som_trajectory(bmus, 'data1', sD, 'trajsize', [12 6 3 1]')
Daniel@0 9 % som_trajectory(bmus, 'data1', sD.data(:,[1 2 3]), 'name1', {'fii' 'faa' 'foo'})
Daniel@0 10 %
Daniel@0 11 % Input arguments ([]'s are optional):
Daniel@0 12 % bmus (matrix) size Nx1, vector of BMUS
Daniel@0 13 % ['argID', (string) Other arguments can be given as 'argID', value
Daniel@0 14 % value] (varies) pairs. See list below for valid values.
Daniel@0 15 %
Daniel@0 16 % NOTE: the GUI only works on a figure which has been made with SOM_SHOW.
Daniel@0 17 %
Daniel@0 18 % Here are the valid argument IDs (case insensitive) and associated values:
Daniel@0 19 % 'color' string 'xor' or ColorSpec, default: 'xor'.
Daniel@0 20 % (default: according to lattice as in som_cplane)
Daniel@0 21 % 'TrajSize' vector of size Nx1 to define the length of comet
Daniel@0 22 % (N) and size of the comet dots in points.
Daniel@0 23 % default: [16 12 10 8 6 4]'
Daniel@0 24 % 'Data1' SOM Toolbox data struct or matrix. The size of
Daniel@0 25 % data matrix (in data struct the field .data) is
Daniel@0 26 % Nxd, where N must be the same as the amount of
Daniel@0 27 % BMUS given in the first input argument 'bmus'
Daniel@0 28 % This data is shown in a new window in d subplots.
Daniel@0 29 % Default: BMU indices (first input argument)
Daniel@0 30 % 'Name1' cell array of d strings which contains names
Daniel@0 31 % for the components in 'Data1'. If 'Data1' is a SOM
Daniel@0 32 % Toolbox data struct, the existing component names
Daniel@0 33 % are overdone.
Daniel@0 34 % 'Figure' scalar that must be a handle to an existing figure
Daniel@0 35 % which has been made using SOM_SHOW function.
Daniel@0 36 % Default: current active figure (gcf).
Daniel@0 37 %
Daniel@0 38 % The following tools can be found in the 'Tools' -menu.
Daniel@0 39 %
Daniel@0 40 % Remove Trajectory: removes trajectory from the map.
Daniel@0 41 % Dye Nodes : opens GUI for selecting color for the nodes
Daniel@0 42 % and points selected.
Daniel@0 43 % Clear Markers : removes markers from map and data figure.
Daniel@0 44 % Save : saves the current situation as a struct.
Daniel@0 45 % Load : loads the struct from workspace and draws markers.
Daniel@0 46 %
Daniel@0 47 % Mouse operation
Daniel@0 48 %
Daniel@0 49 % In data window: Left button is used to drag the operation point ruler
Daniel@0 50 % if left button is on blank area, it starts
Daniel@0 51 % In map window : Left button starts a polygon; right button
Daniel@0 52 % finishes; middle button toggles a unit.
Daniel@0 53 %
Daniel@0 54 % SOM_TRAJECTORY is an application for observing trajectory behavior.
Daniel@0 55 %
Daniel@0 56 % Using mouse the line in data figure can be dragged and the
Daniel@0 57 % trajectory moves in the SOM SHOW figure. It is also possible to move
Daniel@0 58 % trajectory by pressing keys '>' and '<' when mouse pointer is above
Daniel@0 59 % data figure.
Daniel@0 60 %
Daniel@0 61 % Regions can be chosen from the data and the points in that region
Daniel@0 62 % are mapped to the component planes. Regions can be chosen also in
Daniel@0 63 % the map. In this situation data points and map nodes are also
Daniel@0 64 % marked (Left mouse button adds point to the polygon indicating the
Daniel@0 65 % region and right button finals the polygon). By clicking a node (the
Daniel@0 66 % middle button) that node is either added or removed from selection.
Daniel@0 67 %
Daniel@0 68 % It should be noticed that choosing intervals from data may cause
Daniel@0 69 % situations that seem to be bugs. If there exisist marks of different
Daniel@0 70 % color, removing them by clicking the map may left some marks in the
Daniel@0 71 % data, because more than one point in the data is mapped to the same
Daniel@0 72 % node in the map and the removing operation depends on the color of
Daniel@0 73 % the marks. However, all the marks can be removed by using the 'Clear
Daniel@0 74 % Markers' -operation.
Daniel@0 75 %
Daniel@0 76 % FEATURES
Daniel@0 77 %
Daniel@0 78 % The first input argument 'bmus' may also be a munits x N matrix
Daniel@0 79 % In this case each column defines a "fuzzy response". That is,
Daniel@0 80 % each column defines a hit histogram function). The element
Daniel@0 81 % bmus(i,t) sets the size of marker on unit i at time t.
Daniel@0 82 % NOTE: - in this case no regions can be selcted on the map!
Daniel@0 83 % - only > and < keys can be used to move the operation point
Daniel@0 84 % line: it can't be dragged
Daniel@0 85 % - "fuzzy response is always black (hope this will be fixed)
Daniel@0 86 %
Daniel@0 87 % It is possible to open a second data window showing different data:
Daniel@0 88 % use indetifiers 'Data2' (and 'Name2'). The argument syntax is
Daniel@0 89 % identical to 'Data1' (and 'Name1').
Daniel@0 90 %
Daniel@0 91 % See also SOM_SHOW, SOM_SHOW_ADD, SOM_BMUS.
Daniel@0 92
Daniel@0 93 % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Johan
Daniel@0 94 % Himberg and Juha Parhankangas
Daniel@0 95 % Copyright (c) 2000 by the Johan Himberg and Juha Parhankangas
Daniel@0 96 % http://www.cis.hut.fi/projects/somtoolbox/
Daniel@0 97
Daniel@0 98 % Check arguments
Daniel@0 99
Daniel@0 100 error(nargchk(1,Inf,nargin)); % Check no. of input arguments
Daniel@0 101
Daniel@0 102 %% Init input argument struct (see subfunction)
Daniel@0 103 Traj=iniTraj(bmus);
Daniel@0 104
Daniel@0 105 % Check tentative BMU input validity
Daniel@0 106
Daniel@0 107 if ~vis_valuetype(bmus,{'nxm'}),
Daniel@0 108 error(['First input should be a vector of BMU indices or' ...
Daniel@0 109 ' a "response matrix"']);
Daniel@0 110 end
Daniel@0 111
Daniel@0 112 %% Check optional arguments
Daniel@0 113 for i=1:2:length(varargin)
Daniel@0 114 identifier=lower(varargin{i});
Daniel@0 115 value=varargin{i+1};
Daniel@0 116
Daniel@0 117 % Trajectory color
Daniel@0 118 switch identifier
Daniel@0 119 case 'color'
Daniel@0 120 if isempty(value)
Daniel@0 121 value='xor';
Daniel@0 122 end
Daniel@0 123 if vis_valuetype(value,{'colorstyle','xor'})
Daniel@0 124 Traj.color=value;
Daniel@0 125 else
Daniel@0 126 error('''Color'' has to be ColorSpec or string ''xor''.');
Daniel@0 127 end
Daniel@0 128
Daniel@0 129 % 1st data
Daniel@0 130 case 'data1'
Daniel@0 131 if isempty(value),
Daniel@0 132 value=[];
Daniel@0 133 elseif vis_valuetype(value,{'nxm'})
Daniel@0 134 Traj.primary_data=value;
Daniel@0 135 elseif isstruct(value) & isfield(value,'type') & ...
Daniel@0 136 ischar(value.type) & strcmp(value.type,'som_data'),
Daniel@0 137 Traj.primary_data=value.data;
Daniel@0 138 if isempty(Traj.primary_names),
Daniel@0 139 Traj.primary_names=value.comp_names;
Daniel@0 140 end
Daniel@0 141 end
Daniel@0 142
Daniel@0 143 % 2nd data
Daniel@0 144 case 'data2'
Daniel@0 145 if isempty(value),
Daniel@0 146 value=[];
Daniel@0 147 elseif vis_valuetype(value,{'nxm'})
Daniel@0 148 Traj.secondary_data=value;
Daniel@0 149 elseif isstruct(value) & isfield(value,'type') & ...
Daniel@0 150 ischar(value.type) & strcmp(value.type,'som_data'),
Daniel@0 151 Traj.secondary_data=value.data;
Daniel@0 152 if isempty(Traj.secondary_names),
Daniel@0 153 Traj.secondary_names=value.comp_names;
Daniel@0 154 end
Daniel@0 155 end
Daniel@0 156
Daniel@0 157 % Trajectory length & size
Daniel@0 158 case 'trajsize'
Daniel@0 159 if isempty(value),
Daniel@0 160 Traj.size=[16 12 10 8 6 4]';
Daniel@0 161 end
Daniel@0 162 if vis_valuetype(value,{'nx1'})
Daniel@0 163 Traj.size=value
Daniel@0 164 else
Daniel@0 165 error('''TrajSize'' has to be a nx1 vector.');
Daniel@0 166 end
Daniel@0 167
Daniel@0 168 % Names for first data variables
Daniel@0 169 case 'name1'
Daniel@0 170 if isempty(value),
Daniel@0 171 Traj.primary_names=[];
Daniel@0 172 elseif ~vis_valuetype(value,{'cellcolumn_of_char'}),
Daniel@0 173 error('''Name1'': variable names must be in a cell column array.')
Daniel@0 174 else
Daniel@0 175 Traj.primary_names = value;
Daniel@0 176 end
Daniel@0 177 % Names for 2nd data variables
Daniel@0 178 case 'name2'
Daniel@0 179 if isempty(value),
Daniel@0 180 Traj.secondary_names=[];
Daniel@0 181 elseif ~vis_valuetype(value,{'cellcolumn_of_char'}),
Daniel@0 182 error('''Name2'': variable names must be in a cell column array.')
Daniel@0 183 else
Daniel@0 184 Traj.secondary_names = value;
Daniel@0 185 end
Daniel@0 186
Daniel@0 187 % Figure number
Daniel@0 188 case 'figure'
Daniel@0 189 if isempty(value)
Daniel@0 190 Traj.figure='gcf';
Daniel@0 191 end
Daniel@0 192 if vis_valuetype(value,{'1x1'})
Daniel@0 193 Traj.figure=value;
Daniel@0 194 else
Daniel@0 195 error('''Figure'' should be number of an existing figure.')
Daniel@0 196 end
Daniel@0 197 end
Daniel@0 198 end
Daniel@0 199
Daniel@0 200 %% Get SOM data from figure
Daniel@0 201 [h,msg,lattice,msize,dim]=vis_som_show_data('all',Traj.figure);
Daniel@0 202
Daniel@0 203 %% Not a SOM_SHOW figure?
Daniel@0 204 if ~isempty(msg);
Daniel@0 205 error('Figure is invalid: use SOM_SHOW to draw the figure.');
Daniel@0 206 end
Daniel@0 207
Daniel@0 208 % Get map size from figure data
Daniel@0 209 Traj.lattice=lattice;
Daniel@0 210 Traj.msize=msize;
Daniel@0 211 if length(msize)>2,
Daniel@0 212 error(['This function works only with 2D maps: figure contains' ...
Daniel@0 213 ' something else.']);
Daniel@0 214 end
Daniel@0 215 munits=prod(msize);
Daniel@0 216
Daniel@0 217 % Check BMU (or response) and map match
Daniel@0 218
Daniel@0 219 if vis_valuetype(bmus,{'nx1'});
Daniel@0 220 if max(bmus)>prod(msize) | min(bmus) <1
Daniel@0 221 error('BMU indexes out of range.')
Daniel@0 222 elseif any(round(bmus)~=bmus)
Daniel@0 223 error('BMU indexes must be integer.');
Daniel@0 224 elseif isempty(Traj.primary_data),
Daniel@0 225 Traj.primary_data=bmus;
Daniel@0 226 end
Daniel@0 227 elseif size(bmus,1) ~= munits
Daniel@0 228 error(['Response matrix column number must match with the number of' ...
Daniel@0 229 ' map units.']);
Daniel@0 230 else
Daniel@0 231 bmus=bmus';
Daniel@0 232 if isempty(Traj.primary_data),
Daniel@0 233 Traj.primary_data=[1:size(bmus,1)]';
Daniel@0 234 Traj.primary_names={'BMU Index'};
Daniel@0 235 end
Daniel@0 236 end
Daniel@0 237
Daniel@0 238 size1=size(Traj.primary_data);
Daniel@0 239 size2=size(Traj.secondary_data);
Daniel@0 240
Daniel@0 241 % Data2 must not be defined alone
Daniel@0 242
Daniel@0 243 if isempty(Traj.primary_data)&~isempty(Traj.secondary_data),
Daniel@0 244 error('If ''Data2'' is specified ''Data1'' must be specified, too.');
Daniel@0 245 elseif ~isempty(Traj.secondary_data) ...
Daniel@0 246 & size1~= size2
Daniel@0 247 % If data1 and data2 exist both, check data1 and data2 match
Daniel@0 248 error('''Data1'' and ''Data2'' have different amount of data vectors.')
Daniel@0 249 end
Daniel@0 250
Daniel@0 251 % Check BMU and data1 match (data2 matches with 1 anyway)
Daniel@0 252
Daniel@0 253 if ~isempty(Traj.primary_data) & size(bmus,1) ~= size1,
Daniel@0 254 error(['The number of data vectors in ''data1'' must match with' ...
Daniel@0 255 ' the number of rows in the first input argument (bmus).']);
Daniel@0 256 end
Daniel@0 257
Daniel@0 258 % Check that number of names and data dimension is consistent
Daniel@0 259
Daniel@0 260 if ~isempty(Traj.primary_names) & (size1(2)~=length(Traj.primary_names)),
Daniel@0 261 error('Number of component names and ''Data1'' dimension mismatch.');
Daniel@0 262 end
Daniel@0 263 if ~isempty(Traj.secondary_names) & ...
Daniel@0 264 (size2(2)~=length(Traj.secondary_names)),
Daniel@0 265 error('Number of component names and ''Data2'' dimension mismatch.');
Daniel@0 266 end
Daniel@0 267
Daniel@0 268 %% Call the function that does the job
Daniel@0 269 vis_trajgui(Traj);
Daniel@0 270
Daniel@0 271 %%% Subfunctions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 272
Daniel@0 273 function Traj=iniTraj(bmus)
Daniel@0 274
Daniel@0 275 Traj.figure=gcf;
Daniel@0 276 Traj.primary_data=[];
Daniel@0 277 Traj.secondary_data=[];
Daniel@0 278 Traj.primary_names = [];
Daniel@0 279 Traj.secondary_names = [];
Daniel@0 280 Traj.size=[16 12 10 8 6 4]';
Daniel@0 281 Traj.bmus=bmus;
Daniel@0 282 Traj.color='xor';
Daniel@0 283 Traj.msize=[];
Daniel@0 284 Traj.lattice=[];
Daniel@0 285