IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr_incr.m
Go to the documentation of this file.
1 %> @brief Base class for Incremental Classifiers
2 %>
3 %> These classifiers have a learning process that uses one data row at a time.
4 %>
5 %> This class introduced facilities to record the "learning curve" (evolution of the classifier performance as it learns) of the classifier.
6 %>
7 %> @note If the classifier is set to record, (i.e., flag_rtrecord is true), it can only be trained once
8 %>
9 %>
10 classdef clssr_incr < clssr
11  properties
12  %> =0. Whether to attempt to record the learning curve in RealTime (RT)
13  flag_rtrecord = 0;
14  %> =1. Recording periodicity
15  record_every = 1;
16 
17  %> (Optional) Block to post-process the test data. For example, a @ref grag_classes_first.
18  postpr_test;
19  %> Block to post-process the estimation issued by the classifier. Examples:
20  %> @arg a @ref decider
21  %> @arg a @block_cascade_base consisting of a @ref decider followed by a @ref grag_classes_vote
22  postpr_est;
23 
24  %> Test dataset, to be used only if @ref flag_rtrecord is true
25  data_test;
26 
27  %> Cell of @ref ttlog objects
28  log_mold;
29  end;
30 
31  properties (SetAccess=protected)
32  %> (number of elements in log_mold)x(number of elements in training set (see also @ref clssr_incr::allocate()).
33  %> Learning curves recorded here
34  rates;
35  %> Recording index
36  i_r;
37  %> Restarting counter to know when to record (see @ref every)
38  i_e;
39  %> Number of allocated recordings
40  nar;
41  %> Number of rows in the training set
42  no_rows;
43  %> Row index, incremented when record() is called
44  i_row;
45  %> Whether recording has been allocated
46  flag_allocated = 0;
47  end;
48 
49  methods
50  function o = clssr_incr()
51  o.classtitle = 'Incremental';
52  end;
53  end;
54 
55  methods(Access=protected)
56  function o = do_boot(o)
57  if o.flag_rtrecord
58  % Checks if postpr_est is ok; boots the post-processors
59  if ~isempty(o.postpr_est)
60  o.postpr_est = o.postpr_est.boot();
61  end;
62  if ~isempty(o.postpr_test)
63  o.postpr_test = o.postpr_test.boot();
64  end;
65 
66  assert_decider(o.postpr_est);
67 
68  o.i_r = 0;
69  o.i_e = 1;
70 
71  o.flag_allocated = 0;
72  end;
73 
74  o = do_boot@clssr(o);
75  end;
76 
77  %> Records one column of @ref clssr_incr::rates
78  function o = record(o)
79  if ~o.flag_allocated
80  o = o.allocate(o.no);
81  end;
82 
83  o.i_row = o.i_row+1;
84  if o.i_row == o.no_rows || o.i_e >= o.record_every
85  o.i_e = 1;
86 
87  o.i_r = o.i_r+1;
88 
89  if o.i_r > o.nar
90  irerror(sprintf('Number of allocated recordings is only %d, but wanted to record element # %d!', o.nar, o.i_r));
91  end;
92 
93  est = o.use(o.data_test);
94 
95  if ~isempty(o.postpr_est)
96  est = o.postpr_est.use(est);
97  end;
98  if isempty(est.classes)
99  irerror('Estimation post-processing did not assign classes!');
100  end;
101 
102  if ~isempty(o.postpr_test)
103  ds_test = o.postpr_test.use(o.data_test);
104  else
105  ds_test = o.data_test;
106  end;
107 
108  pars = struct('est', {est}, 'ds_test', {ds_test}, 'clssr', {o});
109 
110  for i = 1:numel(o.log_mold)
111  log = o.log_mold{i}.allocate(1);
112  log = log.record(pars);
113  o.rates(i, o.i_r) = log.get_rate();
114  end;
115  else
116  o.i_e = o.i_e+1;
117  end;
118  end;
119 
120  %> Allocates the @ref clssr_incr::rates property
121  %>
122  %> Called every time train() is called
123  function o = allocate(o, n)
124  o.no_rows = n;
125  o.i_row = 0;
126  o.nar = o.get_no_recordings(n);
127  o.rates = zeros(numel(o.log_mold), o.nar);
128  o.flag_allocated = 1;
129  end;
130 
131  end;
132 
133  methods
134 
135  %> Returns number of recordings based on internal setup
136  %>
137  %> returns <code>ceil(n/o.record_every)</code>
138  function nr = get_no_recordings(o, n)
139  nr = ceil(n/o.record_every);
140  end;
141  end;
142 end
Property rates
Definition: clssr_incr.m:43
Group Aggregator - Classes - Vote.
Base class for Incremental Classifiers.
Definition: clssr_incr.m:10
function irerror(in s)
Block that resolves estimato posterior probabilities into classes.
Definition: decider.m:10
function allocate(in o, in n)
Classifiers base class.
Definition: clssr.m:6
function assert_decider(in obj)
Group Aggregator - Classes - First row.
Analysis Session (AS) base class.
Definition: as.m:6
Cascade block: sequence of blocks represented by a block.
Train-Test Log.
Definition: ttlog.m:4