IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
estlog_classxclass.m
Go to the documentation of this file.
1 %> @brief Records (test class)x([rejected, estimation class]) hits
3  properties
4  %> All possible class labels in reference datasets
5  testlabels = {};
6  %> All possible class labels in estimation datasets
7  estlabels = {};
8  %> =0. What to give as a "rate". 0-mean sensitivity; 1-diagonal element defined by idx_rate
9  ratemode = 0;
10  %> =1. Diagonal element if @c ratemode is 1.
11  idx_rate = 1;
12  end;
13 
14  methods
15  function o = estlog_classxclass()
16  o.classtitle = 'Class X Class';
17  o.flag_sensspec = 1;
18  o.flag_params = 1;
19  end;
20  end;
21 
22  methods(Access=protected)
23  %> Returns the contents of the @c estlabels property.
24  function z = get_collabels(o)
25  z = o.estlabels;
26  end;
27 
28  %> Returns the contents of the @c testlabels property.
29  function z = get_rowlabels(o)
30  z = o.testlabels;
31  end;
32 
33  function o = do_record(o, pars)
34  est = pars.est;
35  ds_test = pars.ds_test;
36  if isempty(est.classes)
37  irerror('Classes of estimation dataset are empty! Are you sure it has been put through a decider?');
38  end;
39  if numel(est.classes) ~= numel(ds_test.classes)
40  irerror('Number of items in estimation is different from number of items in reference dataset!');
41  end;
42  estclasses = renumber_classes(est.classes, est.classlabels, o.estlabels);
43  for i = 1:numel(o.testlabels)
44  classidx = find(strcmp(o.testlabels{i}, ds_test.classlabels)); % rowlabel of turn class index in reference dataset
45  if isempty(classidx)
46  % Class was not tested <--> not present in reference (test) dataset
47  else
48  rowidxs = ds_test.classes == classidx-1; % Indexes of rows belonging to i-th class
49  sel = estclasses(rowidxs);
50  if o.flag_support
51  supp = est.X(rowidxs, 1)';
52  end;
53 
54 
55 % % Method 1: better with more classes; better with less rows
56 % x = sort(sel);
57 % difference = diff([x;max(x)+1]);
58 % count = diff(find([1;difference]));
59 % y = x(find(difference));
60 
61  % Method 2: better with fewer classes; better with more rows
62  % Both methods are quite quick anyway
63  for j = 1:numel(o.estlabels)
64  idxidxbool = sel == j-1;
65  o.hits(i, j+1, o.t) = o.hits(i, j+1, o.t)+sum(idxidxbool);
66  if o.flag_support
67  o.supports{i, j+1, o.t} = supp(idxidxbool); % Assumeed that est is the output of a decider block which produces a X with one feature only, which is the support.
68  end;
69  end;
70 
71  idxidxbool = sel == -1;
72  o.hits(i, 1, o.t) = sum(idxidxbool); % Rejection count.
73  if o.flag_support
74  o.supports{i, 1, o.t} = supp(idxidxbool); % Assumeed that est is the output of a decider block which produces a X with one feature only, which is the support.
75  end;
76  end;
77  end;
78  end;
79  end;
80 
81  methods
82  %> Returns average of diagonal of confusion matrix.
83  %>
84  %> If one row wasn't tested, it won't enter the average calculation
85  function z = get_meandiag(o)
86  C = o.get_C([], 1, 3, 1);
87  W = o.get_weights([], 1); % (no_rows)x(no_t) matrix of weights
88  w = sum(W, 2) > 0;
89  z = diag(C(:, 2:end))'*w/sum(w(:)); % Re-normalizes using weights for every element of the diagonal
90  end;
91 
92  %> Either redirects to get_meandiag() or returns diagonal element of average confusion matrix
93  function z = get_rate(o)
94  if o.ratemode == 0
95  z = o.get_meandiag();
96  else
97  C = o.get_C([], 1, 3, 1); % Gets average percentage with discounted rejected items
98  z = C(o.idx_rate, o.idx_rate+1);
99  end;
100  end;
101 
102  %> Returns vector with time-wise-calculated averages
103  %>
104  %> @return If @c ratemode == 0, returns the average of the diagonal calculated for each time instant, weighted by
105  %> whether each row was tested or not; if @c ratemode > 0, returns one diagonal element for each time instant. In either case, rejected
106  %> items are discounted.
107  function z = get_rates(o)
108  CC = o.get_C([], 1, 0, 1); % gets per-time matrices of percentages
109  if o.ratemode > 0
110  z(:) = CC(o.idx_rate, o.idx_rate+1, :);
111  else
112  W = o.get_weights([], 1); % (no_rows)x(no_t) matrix of weights
113  n = size(CC, 3);
114  z = zeros(1, n);
115  for i = 1:n
116  z(i) = diag(CC(:, 2:end, i))'*W(:, i); % dot product
117  end;
118  end;
119  end;
120  end;
121 end
function irerror(in s)
Block that resolves estimato posterior probabilities into classes.
Definition: decider.m:10
Estimation logs base class.
Definition: estlog.m:4
Base Block class.
Definition: block.m:2
Records (test class)x([rejected, estimation class]) hits.
function renumber_classes(in classes_orig, in classlabels_orig, in classlabels_ref)
Analysis Session (AS) base class.
Definition: as.m:6