IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
sgs_one_class_out.m
Go to the documentation of this file.
1 %> @brief One-class-out SGS
2 %>
3 %> Not published in GUI, currently.
4 %>
5 %> @sa sgs
7  properties
8  %> =0. Whether to perform the one-class-out split within each class of @ref hie_split1 separately. Similar to the meaning of the
9  %> original @ref sgs::flag_perclass
10  flag_split1;
11  %> Hierarchical level(s) that will be used to separate the dataset if @ref flag_split1 is 1. Otherwise it is ignored
12  hie_split1;
13  %> Split level(s).
14  hie_split2;
15  %> Class labels (because the dataset passed to @ref get_obsidxs() will have different class labels)
16  classlabels;
17  %> Classes. Must physically/semantically match the rows within the dataset that is passed to @ref get_obs_idxs(). Don't worry, @ref
18  %> data_select_hierarchy.m does not reorganize the row order in a dataset.
19  classes;
20  %> Number of sub-sets per repetition. 2 is the classical train-test. 3 is the train-validate-test. From the second on, there is
21  %> going to be only one reserved class, then the rest goes into the first (training) (that's why it is called leave-one-class-out).
22  no_bites;
23  %> =10. Stands for the "K".
24  no_reps = 1;
25 
26  end;
27 
28  methods(Access=protected)
29  %> Overwrittern
30  function idxs = get_repidxs(o)
31  if ~o.flag_split1
32  map = classlabels2cell(o.classlabels, o.hie_split2);
33  map = cell2mat(map(:, 4));
34 % uniquemap = unique(map);
35  nc = max(map)+1;
36 
37  nreff = min(o.no_reps, nc);
38  if nreff < o.no_reps
39  irwarning(sprintf('Number of repetitions will be %d instead %d', nreff, o.no_reps));
40  end;
41 
42  if o.no_bites > nreff
43  irerror(sprintf('Number of subsets ("bites") too big. Maximum possible is %d', nreff));
44  end;
45 
46  idxsc = cell(nreff, o.no_bites);
47  cperm = randperm(nc);
48  for i = 1:nreff
49  gone = zeros(1, nc);
50  for j = 1:o.no_bites-1
51  idx = cperm(mod(i+j-2, nc)+1);
52  gone(idx) = 1;
53  idxsc{i, j+1} = idx;
54  end;
55  idxsc{i, 1} = find(gone == 0);
56  end;
57 
58  classeseff = map(o.classes+1);
59  idxs_ = classmap2obsmap(idxsc, classeseff);
60  else
61  % First sees how many sub-problems we have
62  map1 = classlabels2cell(o.classlabels, o.hie_split1);
63  map1 = cell2mat(map1(:, 4));
64  nc1 = max(map1)+1;
65 
66  map2 = classlabels2cell(o.classlabels, o.hie_split2);
67  map2 = cell2mat(map2(:, 4));
68  classeseff = map2(o.classes+1);
69 
70  % Now sees the minimum effective number of repetitions
71  nreff = o.no_reps;
72  minnc = Inf;
73  for h = 1:nc1
74  idxs1 = map1 == h-1;
75  temp = numel(unique(map2(idxs1)));
76  nreff = min(nreff, temp);
77  minnc = min(minnc, temp);
78  end;
79 
80  if nreff < o.no_reps
81  irwarning(sprintf('Number of repetitions will be %d instead %d', nreff, o.no_reps));
82  end;
83 
84  if o.no_bites > minnc
85  irerror(sprintf('Number of subsets ("bites") too big. Maximum possible is %d', nreff));
86  end;
87 
88  idxsc_ = cell(nreff, o.no_bites, nc1);
89 
90  for h = 1:nc1
91  map2now = map2(map1 == h-1);
92  unique2now = unique(map2now); % Unique class numbers
93  nc2now = numel(unique2now);
94  cperm = randperm(nc2now);
95  for i = 1:nreff
96  gone = zeros(1, nc2now);
97  for j = 1:o.no_bites-1
98  idx0 = cperm(mod(i+j-2, nc2now)+1);
99  gone(idx0) = 1;
100  idxsc_{i, j+1, h} = unique2now(idx0);
101  end;
102  idxsc_{i, 1, h} = unique2now(find(gone == 0));
103  end;
104  end;
105 
106  % contatenates third dimension
107  idxsc = cell(nreff, o.no_bites);
108  for i = 1:nreff
109  for j = 1:o.no_bites
110  temp = idxsc_(i, j, :);
111  idxsc{i, j} = cell2mat(temp(:));
112  end;
113  end;
114 
115  classeseff = map2(o.classes+1);
116  idxs_ = classmap2obsmap(idxsc, classeseff);
117  end;
118 
119  ni = size(idxs_, 1);
120  idxs = cell(1, ni);
121  for i = 1:ni
122  idxs{i} = idxs_(i, :);
123  end;
124 
125  end;
126 
127 
128  %> Parameter validation
129  function o = do_assert(o)
130  if o.flag_group
131  irerror('Grouping is not applicable!');
132  end;
133  if o.flag_perclass
134  irerror('flag_perclass is not applicable!');
135  end;
136  do_assert@sgs(o);
137  end;
138  end;
139 
140  methods
141  function o = sgs_one_class_out(o)
142  o.classtitle = 'One-class-out SGS';
143  o.flag_ui = 0;
144  end;
145  end;
146 end
Property flag_perclass
=0. Whether to perform on each class separately.
Definition: sgs.m:15
function data_select_hierarchy(in data, in hierarchy)
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
One-class-out SGS.
function irerror(in s)
function classmap2obsmap(in classmaps, in classes)
function classlabels2cell(in classlabels, in new_hierarchy)
function irwarning(in s)