IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
plotter12.m
Go to the documentation of this file.
1 %> @ingroup graphicsapi
2 %> @file
3 %> @brief Generates a colormap from an integer vector of classes.
4 %>
5 %> @brief Functions to draw plots and images based on values and axis data from a @ref sovalues object
6 classdef plotter12
7  properties
8  %> 2D Structure array with fields varying
9  values;
10 
11  %> 2-element Array of axisdata objects
12  ax = raxisdata.empty();
13 
14  flag_hachure = 0;
15  end;
16 
17  methods
18  %> Draws single plot with many lines
19  %> @param name ='rates'. 'rates'/'times1'/'times2'/'times3' etc
20  %> @param flag_legend =1. (Legends will be ax(2).legends)
21  %> @param ylimits =(automatically calculated by MATLAB)
22  %> @param xticks =(taken from the @ref ax property)
23  %> @param xticklabels=(taken from the @ref ax property)
24  function draw_plot(o, name, flag_legend, ylimits, xticks, xticklabels, star_ii)
25  if nargin < 2 || isempty(name)
26  name = 'rates';
27  end;
28 
29  if nargin < 3 || isempty(flag_legend)
30  flag_legend = 1;
31  end;
32 
33  flag_autoylim = 1;
34  if nargin >= 4 && ~isempty(ylimits)
35  flag_autoylim = 0;
36  end;
37 
38  if nargin < 5 || isempty(xticks)
39  xticks = o.ax(1).values;
40  end;
41 
42  if nargin < 6 || isempty(xticklabels)
43  xticklabels = o.ax(1).ticks;
44  end;
45 
46  flag_star = 1;
47  if nargin < 7 || isempty(star_ii)
48  flag_star = 0;
49  end;
50 
51  D = o.get_Y(name);
52  [nfe, nar, fold] = size(D);
53 
54  nfs = o.ax(1).values;
55 
56  if length(nfs) > nfe % This is a hack because I was saving results wrongly!
57  nfs = nfs(1:nfe);
58  end;
59 
60 % y = o.get_y(name);
61 
62 % nar = size(y, 2);
63 
64  for j = 1:nar
65  X = reshape(D(:, j, :), [nfe, fold]); % matrix [nfe]x[number of folds in cross-validation]
66 
67  for m = 1:size(X, 1)
68  ci(1:2, m) = confint(X(m, :))'; % Calculates confidence intervals
69  me(m) = mean(X(m, :));
70  end;
71 
72  if o.flag_hachure
73  draw_hachure2(nfs, ci, find_color(j));
74  hold on;
75  end;
76 
77 
78  try
79  hl(j) = plot(nfs(:), me, 'Color', find_color(j), 'LineStyle', find_linestyle(j), 'Linewidth', scaled(3), 'Marker', find_marker(j), 'MarkerSize', find_marker_size(j));
80  catch ME
81  disp('tentando descobrir a merda no FRANK-PC');
82  rethrow(ME);
83  end;
84  hold on;
85 
86  if flag_star
87  plot(nfs(star_ii(j)), me(star_ii(j)), 'p', 'LineWidth', scaled(2.5), 'MarkerSize', scaled(25), 'Color', find_color(j));
88  end;
89 
90  end;
91  if length(nfs) > 1
92  xl = nfs([1, end]);
93  xlim(xl);
94  end;
95  xlabel(o.ax(1).label);
96  ylabel(labeldict(name));
97  if flag_legend
98  legend(hl, o.ax(2).legends);
99  end;
100  if ~flag_autoylim
101  ylim(ylimits);
102  end;
103  set(gca, 'XTick', xticks);
104  set(gca, 'XTickLabel', xticklabels);
105  decimate_ticks([1, 0]);
106  box off;
107  format_frank();
108 % maximize_window();
109 % set(gca, 'Position', [0.0661 0.0927 0.8953 0.8382]); % Empirical
110  make_box();
111  end;
112 
113 
114  %> Draws a single figure with several subplots.
115  %>
116  %> Each subplot corresponds to a different architecture.
117  %>
118  %> @param name ='rates'. 'rates'/'times1'/'times2'/'times3' etc
119  %> @param ylimits =(automatically calculated by MATLAB for each individual subplot)
120  %> @param xticks =(taken from the @ref ax property)
121  %> @param xticklabels=(taken from the @ref ax property)
122  function draw_subplots(o, name, ylimits, xticks, xticklabels, star_ii)
123 
124  if nargin < 2 || isempty(name)
125  name = 'rates';
126  end;
127 
128  D = o.get_Y(name);
129  [nfe, nar, fold] = size(D);
130 
131  nfs = o.ax(1).values;
132  if length(nfs) > nfe % This is a hack because I was saving results wrongly!
133  nfs = nfs(1:nfe);
134  end;
135 
136 
137  flag_autoylim = 1;
138  if nargin >= 3 && ~isempty(ylimits)
139  flag_autoylim = 0;
140  end;
141 
142  if nargin < 4 || isempty(xticks)
143  xticks = nfs;
144  end;
145 
146  if nargin < 5 || isempty(xticklabels)
147  xticklabels = o.ax(1).ticks;
148  end;
149 
150  flag_star = 1;
151  if nargin < 6 || isempty(star_ii)
152  flag_star = 0;
153  end;
154 
155 
156 
157  % Determines number of plot rows and columns
158  w = ceil(sqrt(nar)); % number of columns (Width)
159  h = ceil(nar/w); % number of rows (Height)
160 
161  % Spacings and margins are sensitive to the number of plot rows
162  % and columns
163 
164  hwref = max(w/4, h/3);
165 
166  kw = (7-hwref*4)/3;
167  kh = (5-hwref*3)/2;
168 
169  MLEF = .06*kw;
170  MRIG = 0.02*kw;
171  MTOP = .05*kh;
172  MBOT = .08*kh;
173  XSPACING = .05*kw;
174  YSPACING = .1*kh;
175 
176  subw = (1-XSPACING*(w-1)-MLEF-MRIG)/w;
177  subh = (1-YSPACING*(h-1)-MTOP-MBOT)/h;
178 
179 
180 
181  y0 = MBOT+(subh+YSPACING)*(h-1);
182 
183  k = 0;
184  for i = 1:h
185  x0 = MLEF;
186  for j = 1:w
187  k = k+1;
188  if k > nar
189  break;
190  end;
191 
192  subplot('Position', [x0, y0, subw, subh]); %h, w, k);
193 
194  X = reshape(D(:, k, :), [nfe, fold]); % matrix [nfe]x[number of folds in cross-validation]
195 
196  for m = 1:size(X, 1)
197  ci(1:2, m) = confint(X(m, :))'; % Calculates confidence intervals
198  me(m) = mean(X(m, :));
199  end;
200 
201  if o.flag_hachure
202  draw_hachure2(nfs, ci, find_color(k));
203  hold on;
204  end;
205 
206  plot(nfs, me, 'Color', find_color(k), 'LineStyle', find_linestyle(k), 'Linewidth', scaled(3), 'Marker', find_marker(k), 'MarkerSize', find_marker_size(k));
207  hold on;
208 
209 
210  if flag_star
211  plot(nfs(star_ii(k)), me(star_ii(k)), 'p', 'LineWidth', scaled(2.5), 'MarkerSize', scaled(25), 'Color', find_color(k));
212  end;
213 
214 % % % % % % if i == h
215 % % % % % % xlabel('Number of features');
216 % % % % % set(gca, 'xtick', xticks);
217 % % % % % % end;
218 % % % % % % ylabel('');
219 
220  if ~flag_autoylim
221  ylim(ylimits);
222  end;
223  set(gca, 'XTick', xticks);
224  set(gca, 'XTickLabel', xticklabels);
225  if length(nfs) > 1
226  xl = nfs([1, end]);
227  xlim(xl);
228  end;
229  decimate_ticks([1, 0]);
230 
231 
232  title(o.ax(2).legends{k}, 'FontWeight', 'bold');
233  format_frank();
234  make_box();
235 
236  x0 = x0+(subw+XSPACING);
237  end;
238  y0 = y0-(subh+YSPACING);
239  end;
240 
241  global FONT FONTSIZE;
242 
243  if h == 1 && w == 1
244  % If there is one plot only, uses MATLAB's default x- and
245  % y-labels
246  xlabel(o.ax(1).label);
247  ylabel(labeldict(name));
248  set(gca, 'Position', [0.0661 0.0927 0.8953 0.8382]); % Empirical
249  else
250  % Vertical label (e.g. "Classification rate (%)")
251  subplot('Position', [0, 0, MLEF*.8, 1]);
252  axis off;
253  text('Position', [0.5, 0.5, 0], ...
254  'Rotation', 90, ...
255  'HorizontalAlignment', 'center', ...
256  'VerticalAlignment', 'middle', ...
257  'FontName', FONT, ...
258  'FontSize', scaled(FONTSIZE), ...
259  'String', labeldict(name));
260 
261  % Vertical label (e.g. "Classification rate (%)")
262  subplot('Position', [MLEF, 0, 1-MLEF, MBOT*0.8]);
263  axis off;
264  text('Position', [0.5, 0.5, 0], ...
265  'Rotation', 0, ...
266  'HorizontalAlignment', 'center', ...
267  'VerticalAlignment', 'middle', ...
268  'FontName', FONT, ...
269  'FontSize', scaled(FONTSIZE), ...
270  'String', o.ax(1).label);
271  end;
272 
273  maximize_window();
274  end;
275 
276 
277  %> Draws each value as an image pixel
278  %>
279  %> For the colors:
280  %> @arg it may use the whole colormap range (don't specify the @c clim parameter), or
281  %> @arg make the colormap span the range specificed by @c clim (and the values will probably be contained within a subrange
282  %> thereof). This case is useful for when you want to draw several images and compare them using the same "color scale"
283  %>
284  %> @param name ='rates'. 'rates'/'times1'/'times2'/'times3' etc
285  %> @param star_ij (optional) Row and Column of the "best", to put a STAR on. If not specified, no star is drawn
286  %> @param clim =[] see description above
287  %> @param flag_logtake =0 Whether to take logarithm of values (useful
288  %> for SVM time images
289  function o = draw_image(o, name, star_ij, clim, flag_logtake, flag_transpose)
290 
291  if nargin < 2 || isempty(name)
292  name = 'rates';
293  end;
294 
295  flag_star = 1;
296  if nargin < 3 || isempty(star_ij)
297  flag_star = 0;
298  end;
299 
300  flag_clim = 1;
301  if nargin < 4 || isempty(clim)
302  flag_clim = 0;
303  end;
304 
305  if nargin < 5 || isempty(flag_logtake)
306  flag_logtake = 0;
307  end;
308 
309  if nargin < 6 || isempty(flag_transpose)
310  flag_transpose = 0;
311  end;
312 
313 
314 
315  Z = o.get_y(name);
316 
317  a1 = 1;
318  a2 = 2;
319  if flag_transpose
320  a1 = 2;
321  a2 = 1;
322  [ny, nx] = size(o.values);
323  Z = Z';
324  else
325  [nx, ny] = size(o.values);
326  end;
327 
328  xx = 1:nx; % x ticks
329  yy = 1:ny; % y ticks
330 
331 
332  if flag_logtake
333  if flag_clim
334  clim = log10(clim);
335  end;
336  Z = log10(Z);
337  end;
338 
339  if flag_clim
340  set(gca, 'CLim', clim);
341  image(xx, yy, Z', 'CDataMapping', 'scaled');
342  set(gca, 'CLim', clim);
343  else
344  imagesc(xx, yy, Z');
345  end;
346  axis image;
347  hold on;
348 
349 
350  xlabel(o.ax(a1).label);
351  ylabel(o.ax(a2).label);
352 
353  set(gca, 'XTick', xx, 'YTick', yy, 'XTickLabel', o.ax(a1).ticks, 'YTickLabel', o.ax(a2).ticks);
354  decimate_ticks([1, 1], [13, 13]);
355 
356 
357  % THis is disabled because one these "ticks" are put, they cannot be removed by xtick([])
358  % rotateticklabel(gca, 90);
359 
360  if flag_star
361  plot3(star_ij(a1), star_ij(a2), 0.1, 'pk', 'LineWidth', scaled(2), 'MarkerSize', scaled(15));
362  end;
363  h = colorbar;
364  format_frank([], [], h);
365  decimate_ticks([], [7, 13]);
366  make_box(); % This is image, makebox maybe not
367  end;
368  end;
369 
370 
371  % Lower-level stuff
372  methods
373  %> Mounts a matrix containing the averages of a field within the "values" property specified by the "name" parameter
374  function y = get_y(o, name)
375  [ni, nj] = size(o.values);
376 
377  for i = 1:ni
378  for j = 1:nj
379  y(i, j) = mean(o.values(i, j).(name));
380  end;
381  end;
382  end;
383 
384 
385  %> Mounts a matrix containing the values of a field within the "values" property specified by the "name" parameter
386  function Y = get_Y(o, name)
387  [ni, nj] = size(o.values);
388 
389  for i = 1:ni
390  for j = 1:nj
391  Y(i, j, :) = o.values(i, j).(name); %#ok<*AGROW>
392  end;
393  end;
394  end;
395  end;
396 end
function compare(in o1, in o2)
function make_box()
function find_marker(in i)
function find_color(in i)
function maximize_window(in h, in aspectratio, in normalizedsize)
function decimate_ticks(in flags, in maxs)
function draw_image(in Y, in height, in direction)
function find_marker_size(in i)
function scaled(in i)
function find_linestyle(in i)
function draw_hachure2(in xaxis, in intervals, in color)
function format_frank(in F, in scale, in handles)
function confint(in x, in perc)