IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
rowaggr_nmean.m
Go to the documentation of this file.
1 %> @file
2 %> @ingroup groupgroup
3 %> @brief Average every ... spectra/rows
5  % Modes for determining the number of bins
6  properties(SetAccess=private)
7  % Number of bins is floor(numberOfSpectra/n)
8  M_FLOOR = 1;
9  % Number of bins is ceil(numberOfSpectra/n).
10  % Obviously this mode will generate output dataset with more spectra
11  % each corresponding to averages of less spectra if compared to M_FLOOR
12  M_CEIL = 2;
13  end;
14 
15  properties
16  %> Average every ... n
17  n = 10;
18  %> =1. Whether to perform averaging within each group. This certainly makes sense
19  %> because you don't want to average together spectra from different samples.
20  flag_pergroup = 1;
21  %> = rowaggr_mean.M_FLOOR. Whether to determine the number of bins by "floor" or "ceiling" modes.
22  mode = 2;
23 
24  end;
25 
26  properties(Access=protected)
27  %> Temporary storage, released after @c use()' d
28  indata;
29  %> Temporary storage, released after @c use()' d
30  outdata;
31  %> Row pointer
32  no_out;
33  end;
34 
35  methods
36  function o = rowaggr_nmean()
37  o.classtitle = 'Average every ...';
38  o.flag_trainable = 0;
39  o.flag_params = 1;
40  end;
41 
42  function z = get_no_bins(o, no_spectra)
43  if o.mode == o.M_FLOOR
44  z = floor(no_spectra/o.n);
45  else
46  z = ceil(no_spectra/o.n);
47  end;
48  end;
49  end;
50 
51  methods(Access=protected)
52  function out = do_use(o, data)
53  flag_groups = o.flag_pergroup && ~isempty(data.groupcodes);
54 
55  % Really didn't make much of a difference, actually it seems that comparing strings is even faster
56  o.indata = data;
57 
58  % Prepares output dataset
59  o.outdata = data.copy_emptyrows();
60  o.no_out = 0; % Increments on this are left to process_indexes()
61  o.outdata.groupcodes = cell(o.indata.no, 1); % Same number of rows to start with
62  o.outdata.obsnames = cell(o.indata.no, 1);
63  o.outdata.X(o.indata.no, o.indata.nf) = 0;
64  o.outdata.classes(o.indata.no, 1) = 0;
65 
66  if ~flag_groups
67  o = o.process_indexes(1:data.no);
68  else
69  % Determines the groups
70  codes = unique(data.groupcodes);
71  ng = numel(codes);
72 
73  for i = 1:ng
74  % Determines the groups
75  codes = unique(data.groupcodes);
76  idxs = find(strcmp(codes{i}, data.groupcodes)); % observation indexes
77  o = o.process_indexes(idxs);
78  end;
79  end;
80 
81  out = o.outdata;
82  % Trimming
83  ii = 1:o.no_out;
84  out.X = out.X(ii, :);
85  out.classes = out.classes(ii, :);
86  out.groupcodes = out.groupcodes(ii, :);
87  out.obsnames = out.obsnames(ii, :);
88  o.indata = []; % Cleanup
89  o.outdata = [];
90  end;
91 
92 
93  %> Bins the spectra indicated by idxs and averages every bin
94  function o = process_indexes(o, idxs)
95  no = numel(idxs);
96  no_bins = o.get_no_bins(no);
97  if no_bins > 0
98  h = [1, hist(1:no, no_bins)];
99  a = cumsum(h); % start of each bin
100 
101  for i = 1:no_bins
102  o.no_out = o.no_out+1;
103  ii = idxs(a(i):a(i+1)-1);
104  o.outdata.X(o.no_out, :) = mean(o.indata.X(ii, :), 1);
105  o.outdata.obsnames{o.no_out} = sprintf('average of %d', h(i+1));
106  o.outdata.groupcodes{o.no_out} = o.indata.groupcodes{ii(1)};
107  o.outdata.classes(o.no_out) = o.indata.classes(ii(1));
108  end;
109  end;
110  end;
111  end;
112 
113 end
function process_indexes(in o, in idxs)
Bins the spectra indicated by idxs and averages every bin.
Definition: rowaggr.m:6