IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
reptt_hiesplit.m
Go to the documentation of this file.
1 %> @brief Hierarchical split
2 %>
3 %> Feeds blocks with successive sub-datasets obtaining by splitting @c data using the @c hie_split class levels
4 %>
5 %> @attention
6 %> The classifiers in the @c clssrs property must be multi-trainable (recommended: @ref aggr_free).
7 %> The classifiers must <b>not</b> be of class @c aggr_hiesplit.
8 %>
9 %> @todo Temporaritly deactivated. This may be obsolete. There is new dataset property instead of this hie_split/hie_classify pair
10 %>
11 %> @sa uip_reptt_hiesplit.m, demo_reptt_hiesplit.m
13  properties
14  %> Number of repetitions. Each repetition corresponds to a random permutation of the order with which the
15  %> sub-datasets are given to the classifier.
16  no_reps = 10;
17  %> =1 . Class levels to use at splitting the dataset
18  hie_split = 1;
19  %> =2 . Class levels to use at selecting the relevant classes for classification
20  hie_classify = 2;
21  %> if > 0, MATLAB's rand('twister', o.randseed) will be called before. This can be used to repeat sequences.
22  randomseed = 0;
23  %> Test dataset
24  data_test;
25  end;
26 
27  properties(Access=protected)
28  pieces;
29  pvt_data_test;
30  end;
31  methods
32  function o = reptt_hiesplit(o)
33  o.classtitle = 'Class-Hierarchical Split';
34 % o.moreactions = {'go', 'extract_logs', 'extract_curves'};
35  o.flag_ui = 0;
36  end;
37  end;
38 %{
39  methods (Access=protected)
40  %> Allocates cell of logs (no_logs)X(no_blocks)X(no_reps) allocated with no_pieces slots
41  function o = allocate_logs(o)
42  if ~iscell(o.log_mold)
43  mold = {o.log_mold};
44  else
45  mold = o.log_mold;
46  end;
47  if ~iscell(o.block_mold)
48  bmold = {o.block_mold};
49  else
50  bmold = o.block_mold;
51  end;
52 
53  no_logs = numel(mold);
54  no_blocks = numel(o.blocks);
55  o.logs = cell(no_logs, no_blocks, o.no_reps);
56  for i = 1:no_logs
57  for j = 1:no_blocks
58  for k = 1:o.no_reps
59  o.logs{i, j, k} = mold{i}.allocate(numel(o.pieces));
60  o.logs{i, j, k}.title = ['From classifier ', bmold{j}.get_description()];
61  end;
62  end;
63  end;
64  end;
65 
66  %> Allocates cell of blocks (1)X(no_blocks)
67  function o = allocate_blocks(o)
68  if ~iscell(o.block_mold)
69  mold = {o.block_mold};
70  else
71  mold = o.block_mold;
72  end;
73  no_blocks = numel(mold);
74  o.blocks = cell(1, no_blocks);
75  for i = 1:no_blocks
76  o.blocks{i} = mold{i}.boot();
77  end;
78  end;
79  end;
80 
81 
82  methods
83  function o = go(o)
84  o = o.boot_postpr(); % from reptt
85 
86  if o.randomseed > 0
87  s = RandStream.getDefaultStream; % Random stream used in random functions (default one).
88  save_State = s.State; % Saves state to restore later.
89  reset(s, o.randomseed); % Resets state to one made from o.randomseed
90  end;
91 
92  o.pieces = data_split_classes(o.data, o.hie_split);
93  np = numel(o.pieces);
94  for i = 1:np % Not sure about the future of aggr_hiesplit
95  o.pieces(i) = data_select_hierarchy(o.pieces(i), o.hie_classify);
96  end;
97 
98  o = o.allocate_blocks();
99 
100  o = o.allocate_logs();
101 
102  % Only needs to do it once
103  if ~isempty(o.postpr_test)
104  o.pvt_data_test = o.postpr_test.use(data_select_hierarchy(o.data_test, o.hie_classify));
105  else
106  o.pvt_data_test = data_select_hierarchy(o.data_test, o.hie_classify);
107  end;
108 
109  [nl, nb, nr] = size(o.logs);
110 
111  ipro = progress2_open('REPTT_HIESPLIT', [], 0, o.no_reps);
112  for i_rep = 1:o.no_reps
113  seq = randperm(np);
114  for i = 1:nb
115  bl = o.blocks{i}.boot();
116 
117  for k = 1:np
118  bl = bl.train(o.pieces(seq(k)));
119 
120  est = bl.use(o.pvt_data_test);
121 
122  if ~isempty(o.postpr_est)
123  est = o.postpr_est.use(est);
124  end;
125 
126  if isempty(est.classes)
127  irerror('Estimation post-processing did not assign classes!');
128  end;
129 
130  pars = struct('est', {est}, 'ds_test', {o.pvt_data_test}, 'clssr', {bl});
131  for j = 1:nl
132  o.logs{j, i, i_rep} = o.logs{j, i, i_rep}.record(pars);
133  end;
134  end;
135  end;
136  ipro = progress2_change(ipro, [], [], i_rep);
137  end;
138  progress2_close(ipro);
139 
140  if o.randomseed > 0
141  set(s, 'State', save_State); % Restores default stream state so that random numbers can be generated as if nothing really happened here.
142  end;
143  end;
144 
145 
146  % Extract evolution curves from logs. Returns a cell matrix of @c irdata objects
147  function out = extract_curves(o)
148  [nl, nb, nr] = size(o.logs);
149  np = numel(o.pieces);
150  out = cell(nl, nb);
151 
152  for i = 1:nl
153  for j = 1:nb
154  d = irdata();
155  d.fea_x = 1:np;
156  d.title = sprintf('LOG %s; BLOCK %s', o.logs{i, j, 1}.get_description(), o.blocks{j}.get_description());
157  d.X = zeros(nr, np);
158  d.xname = 'Number of datasets added';
159  d.xunit = '';
160 
161  for k = 1:nr
162  % Yeah, get_rates() returns one curve
163  d.X(k, :) = o.logs{i, j, k}.get_rates();
164  end;
165 
166  out{i, j} = d;
167  end;
168  end;
169  end;
170  end;
171 %}
172 end
Class-Hierarchical Training Data Split.
Definition: aggr_hiesplit.m:11
Hierarchical split.
Classifiers base class.
Definition: clssr.m:6
REPeated Train-Test.
Definition: reptt.m:8