IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
gridsearchparam.m
Go to the documentation of this file.
1 %>@file
2 %>@ingroup maths misc
3 %
4 %> Grid Searc Parameter: auxiliary class for Grid Searches.
5 %> I think this is deprecated.
6 %>
7 %> @attention Works in log base 10
8 %>
9 %> @sa gridsearch
11  properties
12  %> name: name of a field in '.obj'
13  name = [];
14  %> Double vector (numeric) / cell of strings
15  %>
16  %> If numeric, it is essential to be increasing.
17  values = [];
18  %> =0. Indicates whether the ticks and axis label should be formatted to show log values
19  %>
20  %> This parameter is ignored if the gridsearchparam is not numeric
21  flag_log = 0;
22  end;
23 
24  properties(Dependent)
25  % Whether the possible values are numeric
26  flag_numeric;
27  end;
28 
29  methods
30  function o = gridsearchparam(name_, values_, flag_log_)
31  if nargin >= 1 && ~isempty(name_)
32  o.name = name_;
33  end;
34 
35  if nargin >= 2 && ~isempty(values_)
36  o.values = values_;
37  end;
38 
39  if nargin >= 3 && ~isempty(flag_log_)
40  o.flag_log = flag_log_;
41  end;
42  end;
43 
44  function z = get.flag_numeric(o)
45  z = isnumeric(o.values);
46  end;
47 
48  function z = get_ticklabels(o)
49  if ~o.flag_numeric
50  z = o.values;
51  elseif o.flag_log
52  z = arrayfun(@(x) sprintf('%.3g', log10(x)), o.values, 'UniformOutput', 0);
53  else
54  z = arrayfun(@(x) sprintf('%.3g', x), o.values, 'UniformOutput', 0);
55  end;
56  end;
57 
58  function z = get_label(o)
59  na = replace_underscores(o.name);
60  f = find(o.name == '.');
61  if ~isempty(f)
62  na = na(f(end)+1:end); % uses only after last dot
63  end;
64 
65  if o.flag_log && o.flag_numeric
66  z = sprintf('log_{10}(%s)', na);
67  else
68  z = na;
69  end;
70  end;
71 
72  function z = get_value(o, idx)
73  if o.flag_numeric
74  z = o.values(idx);
75  else
76  z = o.values{idx};
77  end;
78  end;
79 
80 
81  function z = get_value_string(o, idx)
82  if o.flag_numeric
83  z = o.values(idx);
84  else
85  z = o.values{idx};
86  end;
87  if o.flag_log
88  z = ['10^', num2str(log10(z))];
89  else
90  z = num2str(z);
91  end;
92  end;
93 
94  %> If log, takes log
95  function z = get_values_numeric(o)
96  if ~o.flag_numeric
97  z = 1:numel(o.values);
98  elseif o.flag_log
99  z = log10(o.values);
100  else
101  z = o.values;
102  end;
103  end;
104 
105  function z = get_legends(o)
106  a = o.get_ticklabels();
107  if o.flag_numeric && o.flag_log
108  x = cellfun(@(z) ['10^{', z, '}'], a, 'UniformOutput', 0);
109  else
110  x = a;
111  end;
112  z = cellfun(@(x) sprintf('%s=%s', o.name, x), x, 'UniformOutput', 0);
113  end;
114 
115 
116  %> Keeps the span
117  function o = move_to(o, idx)
118  if ~o.flag_numeric
119  irerror('Cannot move, grid search parameter is not numeric!');
120  end;
121  if o.flag_log
122  v = log10(o.values);
123  else
124  v = o.values;
125  end;
126  newv = v-v(1)-(v(end)-v(1))/2+v(idx);
127  if o.flag_log
128  newv = 10.^newv;
129  end;
130 
131  o.values = newv;
132  end;
133 
134  %>
135  %> [1 3 5 7] around 2 --> [1 2.3333 3.6667 5]
136  %>
137  function o = shrink_around(o, idx)
138  if ~o.flag_numeric
139  irerror('Cannot shrink, grid search parameter is not numeric!');
140  end;
141  ni = numel(o.values);
142  if o.flag_log
143  v = log10(o.values);
144  else
145  v = o.values;
146  end;
147  x2 = v(idx);
148  if idx > 1 && idx < ni
149  x1 = v(idx-1);
150  x3 = v(idx+1);
151  elseif idx == 1
152  x3 = v(idx+1);
153  x1 = x2-(x3-x2); % values need to be increasing
154  else % idx == ni
155  x1 = v(idx-1);
156  x3 = x2+(x2-x1);
157  end;
158 
159  oldspan = v(end)-v(1);
160  newspan = x3-x1;
161  newv = (v-v(1))*newspan/oldspan+x1;
162  if o.flag_log
163  newv = 10.^newv;
164  end;
165 
166  o.values = newv;
167  end;
168  end;
169 end
170 
171 
function replace_underscores(in s)
Grid Search.
Definition: gridsearch.m:20