IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
clssr.m
Go to the documentation of this file.
1 %> @brief Classifiers base class
2 %>
3 %> A classifier is a block that outputs class probabilities for each observation. Output is given as an @c
4 %> estimato dataset.
5 
6 classdef clssr < block
7  properties(SetAccess=protected)
8  classlabels = {};
9  end;
10 
11  properties(SetAccess=protected)
12  no_outputs;
13  end;
14 
15 
16 
17  properties
18  flag_datay = 0; % means data.classes are to be ignored and data.Y to be used as target. Basically defineas a regression problem.
19  end;
20 
21 
22 
23  methods
24  function o = clssr(o)
25  o.classtitle = 'Classifier';
26  o.flag_trainable = 1;
27  o.flag_bootable = 1;
28  o.color = [157, 0, 0]/255;
29  end;
30 
31  %> @brief Resets @ref classlabels and calls @ref clssr::boot()
32  function o = boot(o)
33  o.classlabels = {};
34 
35  o = boot@block(o);
36  end;
37 
38 
39  function z = get.no_outputs(o)
40  z = o.get_no_outputs();
41  end;
42 
43  % Renumbers data classes to match o.classlabels order
44  function classes = get_classes(o, data)
45  classes = renumber_classes(data.classes, data.classlabels, o.classlabels);
46  end;
47 
48 
49 
50 
51  function o = draw_domain(o, parameters)
52  if o.nf ~= 2
53  irerror('Domain needs to be 2-dimensional!');
54  end;
55 
56  o.do_draw_domain(parameters);
57  make_box();
58  end;
59 
60  end;
61 
62 
63  methods(Access=protected)
64  function z = get_no_outputs(o)
65  z = length(o.classlabels);
66  end;
67 
68  % Fields expected in params:
69  % .x_range
70  % .y_range
71  % .x_no
72  % .y_no
73  % .ds_train
74 
75  % TODO needs see what to do with estimation
76  function o = do_draw_domain(o, params)
77 
78  de = decider();
79 
80 
81  % classification regions
82  flag_regions = ~o.flag_datay && isfield(params, 'flag_regions') && params.flag_regions;
83  if flag_regions
84  [Q1, Q2] = meshgrid(linspace(params.x_range(1), params.x_range(2), params.x_no), ...
85  linspace(params.y_range(1), params.y_range(2), params.y_no));
86  X_map = [Q1(:) Q2(:)];
87 
88  ds = irdata();
89  ds.X = X_map;
90 
91 
92  est = o.use(ds);
93  est = de.use(est);
94 
95  classes_map_block = zeros(params.y_no, params.x_no);
96  for i = 1:params.x_no
97  classes_map_block(:, i) = est.classes((i-1)*params.y_no+1:i*params.y_no);
98  end;
99 
100  h = pcolor(Q1, Q2, classes_map_block);
101 
102  vtemp = unique(est.classes);
103  for i = 1:numel(vtemp)
104  color_matrix(i, :) = rgb(find_color(vtemp(i)+1));
105  end;
106 
107  colormap(color_matrix);
108  shading flat;
109  alpha(h, 0.4);
110  hold on;
111  end;
112 
113 
114 
115  % training points
116  flag_ds_train = isfield(params, 'ds_train') && ~isempty(params.ds_train);
117  if flag_ds_train
118  if ~o.flag_datay
119  pieces = data_split_classes(params.ds_train);
120  else
121  pieces = params.ds_train;
122  end;
123 
124  ihandle = 1;
125  for i = 1:length(pieces)
126  if ~o.flag_datay
127  i_class = renumber_classes(pieces(i).classes(1), pieces(i).classlabels, o.classlabels);
128  else
129  i_class = 0;
130  end;
131 
132  no = size(pieces(i).X, 1);
133 
134  hh = plot3(pieces(i).X(:, 1), pieces(i).X(:, 2), ones(no, 1)*1, 'Color', find_color(i_class+1), ...
135  'Marker', find_marker(i_class+1), 'MarkerFaceColor', find_color(i_class+1), 'LineStyle', 'none', ...
136  'MarkerSize', find_marker_size(i_class+1));
137  if ~isempty(hh)
138  handles(ihandle) = hh(1);
139  ihandle = ihandle+1;
140  end;
141  hold on;
142  end
143 
144  end;
145 
146  flag_ds_test = isfield(params, 'ds_test') && ~isempty(params.ds_test);
147  if flag_ds_test
148  if ~o.flag_datay
149  pieces = data_split_classes(params.ds_test);
150  else
151  pieces = params.ds_test;
152  end;
153 
154  ihandle = 1;
155  for i = 1:length(pieces)
156  if ~o.flag_datay
157  i_class = renumber_classes(pieces(i).classes(1), pieces(i).classlabels, o.classlabels);
158  else
159  i_class = 0;
160  end;
161 
162  no = size(pieces(i).X, 1);
163 
164  hh = plot3(pieces(i).X(:, 1), pieces(i).X(:, 2), ones(no, 1)*1, 'LineWidth', 2, 'Color', 'k', ...
165  'Marker', find_marker(i_class+1), 'MarkerFaceColor', find_color(i_class+1), 'LineStyle', 'none', ...
166  'MarkerSize', find_marker_size(i_class+1)*1.5);
167  if ~isempty(hh)
168  handles(ihandle) = hh(1);
169  ihandle = ihandle+1;
170  end;
171  hold on;
172  end
173 
174  end;
175 
176 
177  if flag_ds_train || flag_ds_test
178  % Ok, this is gonna give an error if only the test data exists, but... this is life
179  feanames = params.ds_train.get_fea_names([1, 2]);
180  xlabel(feanames{1});
181  ylabel(feanames{2});
182  end;
183 
184 
185  % plot_piece(pieces_train(1).X, find_color(1));
186  % alpha(gco, 1);
187  % hold on;
188  % plot_piece(pieces_train(2).X, find_color(2));
189  % alpha(gco, 1);
190  %
191  % plot_piece(pieces_test(1).X, find_color(1), 1);
192  % alpha(gco, 0.5);
193  % hold on;
194  % plot_piece(pieces_test(2).X, find_color(2), 1);
195  % alpha(gco, 0.5);
196  %
197 
198  % gscatter(X_map(:, 1), X_map(:, 2), classes_map, COLORS);
199  axis([params.x_range(1), params.x_range(2), params.y_range(1), params.y_range(2)]);
200  % legend off;
201 
202  format_frank(gcf);
203 
204 
205  end;
206  end;
207 end
function make_box()
function find_marker(in i)
function find_color(in i)
function irerror(in s)
Dataset class.
Definition: irdata.m:30
function find_marker_size(in i)
Block that resolves estimato posterior probabilities into classes.
Definition: decider.m:10
Base Block class.
Definition: block.m:2
Classifiers base class.
Definition: clssr.m:6
Dataset representing estimation.
Definition: estimato.m:4
function data_split_classes(in data, in hierarchy)
function renumber_classes(in classes_orig, in classlabels_orig, in classlabels_ref)
function format_frank(in F, in scale, in handles)
Analysis Session (AS) base class.
Definition: as.m:6