IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
sgs_randsub_base.m
Go to the documentation of this file.
1 %> @brief Random Sub-sampling base class
2 %>
3 %> http://en.wikipedia.org/wiki/Cross-validation_%28statistics%29#Repeated_random_sub-sampling_validation
4 %>
5 %> @sa uip_sgs_randsub_base.m
6 classdef sgs_randsub_base < sgs
7  properties(Access=protected)
8  pvt_flag_fixed; %> when flag_balanced is true, will be true and pvt_bitess_fixed will be calculated
9  pvt_no_units_sel; %> matrix: one row per piece, one column per bite
10  end;
11 
12  properties
13  %> =[.9, .1]: vectors of percentages.
14  bites = [.9, .1];
15  %> ='simple'. String containing one of the following possibilities:
16  %> @arg @c 'simple' uses percentages in bites
17  %> @arg @c 'balanced' same number of units are picked within each class. Needs flag_perclass to be TRUE, otherwise will give error. The "bites" property will be used to calculate fixed number of units
18  %> @arg @c 'fixed' fixed number of units are picked. Uses bites_fixed
19  type = 'simple';
20  %> Used only if @c type is @c 'fixed', otherwise bites will be used.
21  bites_fixed;
22  %> =10. The number of repetitions.
23  no_reps = 10;
24  end;
25 
26  methods(Access=protected)
27  %> Returns a 2D cell: rows are pieces, columns are bites, elements are indexes
28  function idxs = get_idxs_new(o)
29 
30  idxs = cell(o.no_pieces, size(o.pvt_no_units_sel, 2));
31  for i = 1:o.no_pieces;
32  no_units_sel = o.pvt_no_units_sel(i, :); %> row vector containing number of units for each bite of the piece
33  p = randperm(o.no_unitss(i));
34  ptr = 1;
35  for j = 1:length(no_units_sel)
36  idxs{i, j} = p(ptr:ptr+no_units_sel(j)-1);
37  ptr = ptr+no_units_sel(j);
38  end;
39  end;
40  end;
41 
42  %> Overwrittern
43  function idxs = get_repidxs(o)
44  idxs = cell(1, o.no_reps);
45  for i = 1:o.no_reps
46  idxs{i} = o.get_idxs_new();
47  end;
48  end;
49 
50  %> Parameter validation
51  function o = do_assert(o)
52  if strcmp(o.type, 'balanced') && ~o.flag_perclass
53  irerror('For balanced Random Subsampler, flag_perclass must be TRUE!')
54  end;
55 
56  if (strcmp(o.type, 'simple') || strcmp(o.type, 'balanced')) && sum(o.bites) > 1
57  irerror('Sum of all elements in the "bites" vector needs be <= 1!');
58  end;
59  end;
60 
61  function o = do_setup(o)
62  o.pvt_flag_fixed = strcmp(o.type, 'fixed') || strcmp(o.type, 'balanced');
63  if strcmp(o.type, 'balanced')
64  minunits = min(o.no_unitss);
65  o.pvt_no_units_sel = repmat(percs2no_unitss(o.bites, minunits), o.no_pieces, 1);
66  elseif strcmp(o.type, 'fixed')
67  minunits = min(o.no_unitss);
68  if sum(o.bites_fixed) > minunits
69  irerror(sprintf('Sum of number of fixed units is too big for dataset (should be <= %d)!', minunits));
70  end;
71  o.pvt_no_units_sel = repmat(o.bites_fixed, o.no_pieces, 1);
72  else
73  o.pvt_no_units_sel = zeros(o.no_pieces, length(o.bites));
74  for i = 1:o.no_pieces
75  o.pvt_no_units_sel(i, :) = percs2no_unitss(o.bites, o.no_unitss(i));
76  end;
77  end;
78  end;
79  end;
80 
81  methods
82  function o = sgs_randsub_base(o)
83  o.classtitle = 'Random Sub-sampling base class';
84  end;
85 
86  function idxs = get_obsidxs_new(o)
87  if ~o.flag_setup
88  irerror('Must call setup() first!');
89  end;
90 
91  tmp_save = o.no_reps;
92  o.no_reps = 1;
93  idxs = o.get_obsidxs();
94  o.no_reps = tmp_save;
95  end;
96  end;
97 end
Base Sub-dataset Generation Specification (SGS) class.
Definition: sgs.m:6
Random Sub-sampling base class.
function irerror(in s)
function percs2no_unitss(in percs, in total)