IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
classlabels2cell.m
Go to the documentation of this file.
1 %> @ingroup conversion classlabelsgroup string
2 %> @file
3 %> @brief Class labels hierarchy resolver.
4 %>
5 %> This function parses a list of multi-level class labels, essentially finding the individual labels between the
6 %> '|' separators.
7 %>
8 %> It also mounts new class labels and gives their corresponding new class numbers, if the 'new_hierarchy' input is
9 %> passed.
10 %>
11 %> Everything is returned in a 2D cell that contains one row for each class label. The meanings of the cell columns are
12 %> as follows:
13 %>
14 %> @arg Column 1: original class numbers, actually just a zero-based increasing counter
15 %> @arg Column 2: the original class labels, in the same order as in 'classlabels'
16 %> @arg Column 3: new class labels according to the class levels to be retained
17 %> @arg Column 4: new class numbers according to new hierarchy
18 %> @arg Columns 5, 6, 7 ...: parsed individual labels per level. The number of columns in the cell will be thus 4+no_levels
19 %>
20 %> <h3>Example:</h3>
21 %> @verbatim
22 %> >> c = {'A|1|T', 'A|1|F', 'A|2|T', 'A|2|F', 'A|3|T', 'A|3|F', 'B|1|T', 'B|1|F'};
23 %> >> cc = classlabels2cell(c, [1, 3])
24 %>
25 %> cc =
26 %>
27 %> [0] 'A|1|T' 'A|T' [1] 'A' '1' 'T'
28 %> [1] 'A|1|F' 'A|F' [0] 'A' '1' 'F'
29 %> [2] 'A|2|T' 'A|T' [1] 'A' '2' 'T'
30 %> [3] 'A|2|F' 'A|F' [0] 'A' '2' 'F'
31 %> [4] 'A|3|T' 'A|T' [1] 'A' '3' 'T'
32 %> [5] 'A|3|F' 'A|F' [0] 'A' '3' 'F'
33 %> [6] 'B|1|T' 'B|T' [3] 'B' '1' 'T'
34 %> [7] 'B|1|F' 'B|F' [2] 'B' '1' 'F'
35 %> @endverbatim
36 %>
37 %> @sa cell2classlabels.m
38 %
39 %> @param classlabels
40 %> @param new_hierarchy =[]. New hierarchy. 0 means "none"; [] means "all"
41 %> @return \em cc
42 function out = classlabels2cell(classlabels, new_hierarchy)
43 
44 no = length(classlabels);
45 no_levels = 0;
46 for i = 1:no
47  no_levels = max(no_levels, sum(classlabels{i} == '|')+1);
48 end;
49 
50 flag_new_hierarchy = exist('new_hierarchy', 'var') && ~isempty(new_hierarchy);
51 if ~flag_new_hierarchy
52  new_hierarchy = 1:no_levels;
53 end;
54 
55 if max(new_hierarchy) > no_levels
56  irerror(sprintf('Class labels only have %d level(s)!', no_levels));
57 end;
58 
59 new_hierarchy(new_hierarchy == 0) = [];
60 
61 
62 NO_COLS_FIXED = 4;
63 out = cell(no, no_levels+NO_COLS_FIXED);
64 
65 c = [];
66 for i = 1:no
67  out{i, 1} = i-1; % old class index
68  out{i, 2} = classlabels{i};
69 
70  % properties to be filled out later
71  out{i, 3} = ''; % name_modified
72  out{i, 4} = -1; % new class index
73 
74  c = regexp(classlabels{i}, '\|', 'split');
75  out(i, NO_COLS_FIXED+1:NO_COLS_FIXED+length(c)) = c;
76 end;
77 
78 % Fills gaps with empty spaces. TODO there may be an error here with this "length(c)"
79 %{
80 for j = NO_COLS_FIXED+1:NO_COLS_FIXED+length(c)
81  len_max = 0;
82  for i = 1:no
83  len_max = max(len_max, length(out{i, j}));
84  end;
85  spaces = ones(1, len_max)*' ';
86 
87  for i = 1:no
88  out{i, j} = [out{i, j} spaces(1:len_max-length(out{i, j}))];
89  end;
90 end;
91 %}
92 
93 
94 for i = 1:no
95  out{i, 3} = cell2classlabel(out(i, new_hierarchy+NO_COLS_FIXED));
96 end;
97 
98 % Sort in the new hierarchy order in order to count and assign new class indexes
99 out = sortrows(out, 3);
100 
101 s_cmp = '@!&*(#&*(%'; % Something random highly unlikely to be used as a class label by the user
102 idx = -1;
103 for i = 1:no
104  if ~strcmp(s_cmp, out{i, 3})
105  idx = idx+1;
106  s_cmp = out{i, 3};
107  end;
108  out{i, 4} = idx;
109 end;
110 
111 out = sortrows(out, 1);
112 
113 
114 % Last pass will renumber the new class numbers to retain the original order
115 classnew = -1;
116 classnow = -1;
117 already = [];
118 for i = 1:no
119 % if out{i, 4} ~= classnow
120  idxclass = find(already == out{i, 4});
121  if ~isempty(idxclass)
122  out{i, 4} = idxclass-1;
123  else
124  classnow = out{i, 4};
125  classnew = classnew+1;
126  out{i, 4} = classnew;
127  already = [already, classnow];
128  end;
129 % end;
130 % out{i, 4} = idx;
131 end;
132 
133 
134 
135 
136 
137 %>@cond
138 %---------------------------------------------------
139 function cl = cell2classlabel(c)
140 cl = '';
141 for i = 1:length(c)
142  if i > 1
143  cl = [cl '|'];
144  end;
145  cl = [cl c{i}];
146 end;
147 %>@endcond
function irerror(in s)
function cell2classlabels(in cc)
Analysis Session (AS) base class.
Definition: as.m:6
function classlabels2cell(in classlabels, in new_hierarchy)