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