IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
fsg.m
Go to the documentation of this file.
1 %> @brief FSG - Feature Subset Grader
2 %>
3 %> FSG is always equipped with cross-validation capability. Descendant classes must always implement the
4 %> cross-validation and no cross-validation cases. Whether there is cross-validation is determining by the @ref sgs
5 %> property being set or not.
6 classdef fsg < irobj
7  properties
8  %> =@min. Function pointer to aggregate a vector of pairwise grades. As seen, the default behaviour is taking the least grade obtained among all pairwise gradings.
9  f_aggr = @(z) min(z, [], 1);
10  %> SGS object. If set,
11  sgs;
12  %> Some trainable block for convenience. This can be used to pass a pre_std so that it doesn't have to be part of the classifier itself.
13  %> This block typically is trainable and treats the features independently. Having it as part of the classifier would result in unnecessary
14  %> repetition of the same operation.
15  fext;
16  end;
17 
18  properties(SetAccess=protected)
19  %> =0;
20  flag_pairwise = 0;
21  %> =0;
22  flag_univariate = 0;
23  grade;
24  %> Assigned by @ref prepare(). This is a cell vector of Cell of matrices of @c irdata datasets
25  subddd;
26  %> Prepared.
27  flag_sgs;
28  end;
29 
30  properties(Access=protected)
31  flag_booted = 0;
32  %> The algorithm is clever enough to go non-pairwise mode if flag_pairwise is TRUE but the dataset has less than three classes
33  pvt_flag_pairwise = 0;
34  %> Assigned by @c set_data()
35  %>
36  %> If not pairwise (see @ref flag_pairwise), @c data will be assigned to @ref datasets.
37  %>
38  %> If pairwise, @ref datasets will be a matrix of dimensions <code>(number of pairs)x(numel(data))</code>
39  %>
40  %> Why one would pass a more-than-one-element dataset through @c data depends on the behaviour of a particular
41  %> @ref fsg descendant. For instance a two-element vector could be a train-test pair.
42  %>
43  %> Please note that
44  datasets;
45  end;
46 
47  methods
48  %> @return A string for an y-axis label
49  function s = get_yname(o)
50  s = 'FSG';
51  end;
52 
53  %> @return A string for a y-axis unit
54  function s = get_yunit(o)
55  s = '(?)';
56  end;
57  end;
58 
59  methods(Access=protected)
60  %> Abstract. Must return a column vector with as elements as number of datasets
61  function z = do_calculate_pairgrades(o, idxs) %#ok<INUSD>
62  z = [];
63  end;
64 
65  function o = reset(o)
66  o.datasets = [];
67  o.flag_booted = 0;
68  end;
69  end
70 
71  properties(Dependent)
72  %> @sa datasets
73  data;
74  end;
75 
76  methods
77  %> Assigns the @ref datasets property.
78  %>
79  %> @sa @ref datasets
80  %>
81  %> @param data Vector of datasets.
82  function o = set.data(o, data)
83 % o.data = data;
84  if isempty(data)
85  o.datasets = [];
86  else
87  o.pvt_flag_pairwise = o.flag_pairwise && data.nc > 2;
88  if ~o.pvt_flag_pairwise
89  o.datasets = data;
90  else
91  % pairwise is slow, who cares
92  if numel(data) > 1
93  irwarning('One-versus-one FSG needs all datasets with exactly same classlabels');
94  end;
95  bl = blmisc_split_ovo();
96  % pairwise split is applied dataset-wise. Each dataset originates a column in o.datasets
97  o.datasets = irdata.empty;
98  for i = 1:numel(data);
99  pieces = bl.use(data(i));
100  o.datasets(:, i) = pieces';
101  end;
102 % for i = 1:length(pieces)
103 % o.datasets{i} = pieces(i);
104 % end;
105  end;
106  end;
107  end;
108 
109  function o = fsg(o)
110  o.classtitle = 'Feature Subset Grader';
111  o.color = [170, 193, 181]/255;
112  end;
113 
114  %> Assigns the @ref subddd property
115  function o = boot(o)
116  o.flag_sgs = ~isempty(o.sgs);
117  if o.flag_sgs
118  for i = 1:size(o.datasets, 1)
119  data = o.datasets(i, 1); % See that for cross-validaton only the first column of o.datasets is used.
120  obsidxs = o.sgs.get_obsidxs(data);
121  o.subddd{i} = data.split_map(obsidxs, [], o.fext);
122  end;
123  end;
124 
125  o.flag_booted = 1;
126  end;
127 
128  %> May return one grade or a vector thereof
129  %>
130  %> @param idxs May be either: a cell of feature subsets; or a vector of feature indexes. Developers please note
131  %> that it is the end class responsibility to give proper treatment to this. The option is left open for speed,
132  %> as there is no point in using a cell in univariate cases.
133  function grades = calculate_grades(o, idxs)
134  if ~o.flag_booted
135  irerror('Boot FSG first!');
136  end;
137  if o.flag_univariate && length(idxs{1}) > 1
138  irerror('Univariate Feature-Subset Grader was requested to grade multivariate data!');
139  end;
140 
141  grades_ = o.do_calculate_pairgrades(idxs);
142  if o.flag_pairwise
143  for k = 1:size(grades_, 3)
144  grades(:, :, k) = o.f_aggr(grades_(:, :, k));
145  end;
146  else
147  grades = grades_;
148  end;
149  end;
150  end;
151 end
Standardization (trained)
Definition: pre_std.m:4
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
function irerror(in s)
Base Block class.
Definition: block.m:2
FSG - Feature Subset Grader.
Definition: fsg.m:6
Property flag_booted
Definition: fsg.m:42
Analysis Session (AS) base class.
Definition: as.m:6
Base class.
Definition: irobj.m:33