1 %> @ingroup conversion classlabelsgroup
string
3 %> @brief Class labels hierarchy resolver.
5 %> This
function parses a list of multi-level
class labels, essentially finding the individual labels between the
8 %> It also mounts
new class labels and gives their corresponding new class numbers, if the 'new_hierarchy' input is
11 %> Everything is returned in a 2D cell that contains one row
for each class label. The meanings of the cell columns are
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
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'};
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'
40 %> @param new_hierarchy =[]. New hierarchy. 0 means
"none"; [] means
"all"
44 no = length(classlabels);
47 no_levels = max(no_levels, sum(classlabels{i} ==
'|')+1);
50 flag_new_hierarchy = exist(
'new_hierarchy',
'var') && ~isempty(new_hierarchy);
51 if ~flag_new_hierarchy
52 new_hierarchy = 1:no_levels;
55 if max(new_hierarchy) > no_levels
56 irerror(sprintf(
'Class labels only have %d level(s)!', no_levels));
59 new_hierarchy(new_hierarchy == 0) = [];
63 out = cell(no, no_levels+NO_COLS_FIXED);
67 out{i, 1} = i-1; % old
class index
68 out{i, 2} = classlabels{i};
70 % properties to be filled out later
71 out{i, 3} =
''; % name_modified
72 out{i, 4} = -1; %
new class index
74 c = regexp(classlabels{i},
'\|',
'split');
75 out(i, NO_COLS_FIXED+1:NO_COLS_FIXED+length(c)) = c;
78 % Fills gaps with empty spaces. TODO there may be an error here with
this "length(c)"
80 for j = NO_COLS_FIXED+1:NO_COLS_FIXED+length(c)
83 len_max = max(len_max, length(out{i, j}));
85 spaces = ones(1, len_max)*
' ';
88 out{i, j} = [out{i, j} spaces(1:len_max-length(out{i, j}))];
95 out{i, 3} = cell2classlabel(out(i, new_hierarchy+NO_COLS_FIXED));
98 % Sort in the
new hierarchy order in order to count and assign
new class indexes
99 out = sortrows(out, 3);
101 s_cmp =
'@!&*(#&*(%'; % Something random highly unlikely to be used
as a
class label by the user
104 if ~strcmp(s_cmp, out{i, 3})
111 out = sortrows(out, 1);
114 % Last pass will renumber the
new class numbers to retain the original order
119 %
if out{i, 4} ~= classnow
120 idxclass = find(already == out{i, 4});
121 if ~isempty(idxclass)
122 out{i, 4} = idxclass-1;
124 classnow = out{i, 4};
125 classnew = classnew+1;
126 out{i, 4} = classnew;
127 already = [already, classnow];
138 %---------------------------------------------------
139 function cl = cell2classlabel(c)
function cell2classlabels(in cc)
Analysis Session (AS) base class.
function classlabels2cell(in classlabels, in new_hierarchy)