IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
fsg_clssr.m
Go to the documentation of this file.
1 %>@brief FSG that uses classifier to grade the subsets
2 %>
3 %> The grades are classification rates expressed in percentages (from 0 to 100)
4 %>
5 %> If not using SGS, must assign a 2-dataset vector to @ref data
6 %>
7 %> @sa uip_fsg_clssr.m, reptt
8 classdef fsg_clssr < fsg
9  properties
10  clssr;
11  estlog;
12  %> (Optional) Block to post-process the test data. For example, a @ref grag_classes_first.
13  postpr_test;
14  %> Block to post-process the estimation issued by the classifier. Examples:
15  %> @arg a @ref decider
16  %> @arg a @block_cascade_base consisting of a @ref decider followed by a @ref grag_classes_vote
17  postpr_est;
18  end;
19 
20  methods
21  function s = get_yname(o)
22  s = o.estlog.get_legend();
23  end;
24  function s = get_yunit(o)
25  s = o.estlog.get_unit();
26  end;
27  end;
28 
29  methods(Access=protected)
30  %> Cross-validation is performed here.
31  %> return a (no_pairs)x(no_idxs)x(no_test_sets) grades vector
32  function z = do_calculate_pairgrades(o, idxs)
33  if ~o.flag_sgs
34  [npair, ntt] = size(o.datasets); % number of pairs, number of sub-datasets
35  nidx = numel(idxs);
36  z = zeros(npair, nidx);
37  for ipair = 1:npair % Pairwise LOOP
38  for itt = ntt:-1:1 % Backwards to pre-allocate
39  train_test(:, itt) = o.datasets(ipair, itt).select_features(idxs)';
40  end;
41 
42  for iidx = nidx:-1:1
43 % t = tic();
44  cl = o.clssr.boot();
45  cl = cl.train(train_test(iidx, 1));
46  for k = 2:ntt
47 
48  est = cl.use(train_test(iidx, k));
49  est = o.postpr_est.use(est);
50 
51  if ~isempty(o.postpr_test)
52  ds_test = o.postpr_test.use(train_test(iidx, k));
53  else
54  ds_test = train_test(iidx, k);
55  end;
56 
57  pars = struct('est', {est}, 'ds_test', {ds_test}, 'clssr', {cl});
58 
59  lo = o.estlog.allocate(1);
60  lo = lo.record(pars);
61  z(ipair, iidx, k-1) = lo.get_rate();
62  end;
63  end;
64  end;
65  else
66  npair = numel(o.subddd);
67  [nreps, ntt] = size(o.subddd{1});
68  nidx = numel(idxs);
69  z = zeros(npair, nidx);
70  for ipair = 1:npair % Pairwise LOOP
71  dd = o.subddd{ipair};
72 
73  for k = ntt:-1:1
74  for irep = nreps:-1:1
75  dnows(irep, k, :) = dd(irep, k).select_features(idxs);
76  end;
77  end;
78 
79  for iidx = 1:nidx % Feature Subset LOOP
80  % Allocates one log per test set
81  for k = ntt:-1:2
82  lo(k-1) = o.estlog.allocate(nreps);
83  end;
84 
85  for irep = 1:nreps % Cross-validation loop
86  cl = o.clssr.boot();
87  cl = cl.train(dnows(irep, 1, iidx));
88 
89  for k = 2:ntt
90  est = cl.use(dnows(irep, k, iidx));
91  est = o.postpr_est.use(est);
92 
93  if ~isempty(o.postpr_test)
94  ds_test = o.postpr_test.use(dnow(k));
95  else
96  ds_test = dnows(irep, k, iidx);
97  end;
98 
99  pars = struct('est', {est}, 'ds_test', {ds_test}, 'clssr', {cl});
100  lo(k-1) = lo(k-1).record(pars);
101  end;
102  end;
103 
104  for k = 2:ntt
105  z(ipair, iidx, k-1) = lo(k-1).get_rate();
106  end;
107  end;
108  end;
109  end;
110  end;
111  end;
112 
113  methods
114 
115  %> Couple of checks
116  %>
117  %> @arg Makes sure that @ref postpr_est is able to decide
118  %> Checks datasets
119  function o = boot(o)
120  if isempty(o.postpr_est)
121  irverbose('fsg_clssr is creating default postpr_est decider', 1);
122  o.postpr_est = decider();
123  end;
124 
125  if isempty(o.estlog)
126  irverbose('fsg_clssr is creating default estlog estlog_classxclass', 1);
127  z = estlog_classxclass();
128  z.estlabels = o.datasets(1, 1).classlabels;
129  z.testlabels = z.estlabels;
130  o.estlog = z;
131  end;
132 
133  % Checks if postpr_est is ok; boots the post-processors
134  if ~isempty(o.postpr_est)
135  o.postpr_est = o.postpr_est.boot();
136  end;
137  if ~isempty(o.postpr_test)
138  o.postpr_test = o.postpr_test.boot();
139  end;
140 
141 
142  assert_decider(o.postpr_est);
143 
144 
145  % Checks whether the sgs property is set, or else if the datasets property has two columns
146  if isempty(o.sgs) && size(o.datasets, 2) < 2
147  irerror('If sgs is not set, the data property must receive two datasets (train-test)!');
148  end;
149 
150  o = boot@fsg(o);
151  end;
152 
153  function o = fsg_clssr()
154  o.classtitle = 'using Classifier';
155  end;
156  end;
157 end
function irverbose(in s, in level)
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
Group Aggregator - Classes - Vote.
function irerror(in s)
Block that resolves estimato posterior probabilities into classes.
Definition: decider.m:10
Pre-processing block base class.
Definition: pre.m:2
Estimation logs base class.
Definition: estlog.m:4
Records (test class)x([rejected, estimation class]) hits.
Classifiers base class.
Definition: clssr.m:6
function assert_decider(in obj)
FSG that uses classifier to grade the subsets.
Definition: fsg_clssr.m:8
FSG - Feature Subset Grader.
Definition: fsg.m:6
Group Aggregator - Classes - First row.
REPeated Train-Test.
Definition: reptt.m:8
Cascade block: sequence of blocks represented by a block.