IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
draw_loadings.m
Go to the documentation of this file.
1 %>@ingroup graphicsapi
2 %> @file
3 %> @brief Draws loadings curves with many options
4 %
5 %> @param x [nf] x-axis values.
6 %> @param L [nf][number_of_loadings] loadings matrix.
7 %> @param x_hint =[]. [nf] x-axis values. If not passed or empty, uses @a x if @a hint is passed.
8 %> @param hint [nf] "hint" curve that helps the reading of the loadings. If not passed or empty, no hint curve is drawn.
9 %> @param legends =[] One legend per loadings curve. If not passed, no legend is drawn.
10 %> @param flag_abs =0 Whether to take the absolute value of the loadings matrix.
11 %> @param peakd =[] Peak Detector object.
12 %> @param flag_trace_minalt =0 Whether to draw the threshold line. Only works if @a threshold is passed.
13 %> @param flag_draw_peaks =0 Whether to mark the detected peaks in the figure.
14 %> @param flag_print_peaks =0 Whether to print detected peaks on the command line window.
15 %> @param flag_histogram =0 Whether to plot line or histogram.
16 %> @param flag_envelope =0 If true, will detect all peaks, do spline interpolation and plot this instead
17 %> @param colors =[1, 2, 3...]. See the possibilities:
18 %> @arg [] : Defaults to sequential color indexes [1, 2, 3...]
19 %> @arg 1-D vector : interpreted as color indexes (arguments to @ref find_color.m)
20 %> @arg cell : interpreted as a cell of colours
21 function draw_loadings(x, L, x_hint, hint, legends, flag_abs, peakd, flag_trace_minalt, flag_draw_peaks, flag_print_peaks, flag_histogram, flag_envelope, colors)
22 global SCALE;
23 if ~exist('x_hint', 'var') || isempty(x_hint)
24  x_hint = x;
25 end;
26 if ~exist('hint', 'var') || isempty(hint)
27  hint = [];
28 end;
29 if ~exist('legends', 'var')
30  legends = [];
31 end;
32 if ~exist('peakd', 'var')
33  peakd = [];
34 end;
35 if ~exist('flag_trace_minalt', 'var') || isempty(flag_trace_minalt)
36  flag_trace_minalt = 0;
37 end;
38 if ~exist('flag_abs', 'var') || isempty(flag_abs)
39  flag_abs = 0;
40 end;
41 if ~exist('flag_draw_peaks', 'var') || isempty(flag_draw_peaks)
42  flag_draw_peaks = 0;
43 end;
44 if ~exist('flag_print_peaks', 'var') || isempty(flag_print_peaks)
45  flag_print_peaks = 0;
46 end;
47 if ~exist('flag_histogram', 'var') || isempty(flag_histogram)
48  flag_histogram = 0;
49 end;
50 if ~exist('flag_envelope', 'var') || isempty(flag_envelope)
51  flag_envelope = 0;
52 end;
53 if ~exist('colors', 'var') || isempty(colors)
54  colors = 1:size(L, 2);
55 end;
56 
57 % Processes colors
58 if iscell(colors)
59  colors_eff = colors;
60 else
61  colors_eff = arrayfun(@find_color, colors, 'UniformOutput', 0);
62 end;
63 
64 nl = size(L, 2);
65 % Bonus: checks whether L is a row vector
66 if size(L, 1) == 1
67  if nl == length(x)
68  L = L'; % Daring guess;
69  else
70  irerror('Loadings vector has incorrect size!');
71  end;
72 end;
73 nl = size(L, 2);
74 
75 % Used for the calculations
76 Lcalc = L;
77 Ltemp = Lcalc(:);
78 Ltemp = Ltemp(Ltemp ~= Inf);
79 Lcalc(Lcalc == Inf) = max(Ltemp(:)); % Inf protection
80 
81 if flag_abs
82  ymin = 0;
83  offmin = 0;
84  L = abs(L);
85  Lcalc = abs(Lcalc);
86 else
87  ymin = min(min(Lcalc));
88  offmin = 0.1;
89 end;
90 ymax = max(max(Lcalc));
91 ymaxabs = max(max(abs(Lcalc)));
92 flag_bother_peaks = ~isempty(peakd) && (flag_print_peaks || flag_draw_peaks);
94 hold on;
95 
96 if flag_envelope
97  opd = peakdetector();
98  opd.mindist = 2;
99  Lenv = L;
100  for i = 1:nl
101  ipe = opd.use(x, L(:, i));
102 % Lenv(:, i) = spline(x(ipe), L(ipe, i), x);
103  Lenv(:, i) = interp1(x(ipe), L(ipe, i), x);
104  end;
105 end;
106 
107 
108 if ~isempty(hint)
109  draw_hint_curve(x_hint, hint/max(hint)*ymaxabs);
110 end;
111 
112 
113 
114 
115 
116 
117 
118 for i = 1:nl
119  y = L(:, i)';
120 
121  if flag_envelope
122  % Makes a fader color
123  color1 = rgb(colors_eff{i});
124  color1 = color1*0.3+max(color1)*0.7;
125  width1 = SCALE;
126  color2 = colors_eff{i};
127  width2 = scaled(3);
128  else
129  color1 = colors_eff{i};
130  width1 = scaled(2);
131  end;
132 
133 
134 
135  if ~flag_histogram
136  hh = plot_curve_pieces(x, y, 'Color', color1, 'LineWidth', width1);
137  handles(i) = hh{1};
138  else
139  for j = 1:1 %numel(y)
140  hh = stem(x, y, 'Color', color1, 'LineWidth', 2*width1, 'Marker', 'none');
141  if ~flag_envelope && j == 1
142  handles(i) = hh;
143  end;
144  end;
145  end;
146  hold on;
147 
148  if flag_envelope
149  handles(i) = plot(x, Lenv(:, i)', 'Color', color2, 'LineWidth', width2);
150  end;
151 
152 
153 
154  % Peak detectors know how to deal with Inf, need not use Lcalc
155  if flag_bother_peaks
156  peakd = peakd.boot(x, y);
157  idxs_peaks = peakd.use([], y);
158 
159  if flag_draw_peaks
160  draw_peaks(x, y, idxs_peaks);
161  end;
162 
163  if flag_print_peaks
164  if ~isempty(legends)
165  s = sprintf('for ''%s''', legends{i});
166  else
167  s = sprintf('%d', i);
168  end;
169  fprintf('---------------- Peaks %s -----------------\n', s);
170  print_peaks(x, idxs_peaks);
171  end;
172 
173  % Plots "minimum altitude" line
174  if flag_trace_minalt
175  draw_threshold_line(x, peakd.minalt, [], colors_eff{i});
176  if ~flag_abs
177  draw_threshold_line(x, -peakd.minalt, [], colors_eff{i});
178  end;
179  end;
180  end;
181 end;
182 
183 hl = [];
184 if ~isempty(legends)
185  hl = legend(handles, legends);
186 end;
187 
188 % Formatting
189 format_frank([], [], hl);
190 format_xaxis(x);
191 ylabel('Coefficient (a.u.)');
192 
193 
194 
195 y1 = min(ymin, 0);
196 if flag_abs
197  yspan = ymaxabs-y1;
198  y2 = ymaxabs;
199 else
200  yspan = ymax-y1;
201  y2 = ymax;
202 end;
203 if y1 == 0
204  y0 = 0;
205 else
206  y0 = y1-yspan*.025;
207 end;
208 if yspan > 0
209  ylim([y0, y2+yspan*0.025]);
210 end;
211 
function draw_peaks(in x, in y, in indexes, in flag_text, in color, in marker, in markersize)
function find_color(in i)
function irerror(in s)
Peak Detector.
Definition: peakdetector.m:6
function draw_hint_curve(in x, in y, in color)
function draw_threshold_line(in x, in y, in width, in color)
function plot_curve_pieces(in x, in y, in varargin)
function scaled(in i)
function draw_loadings(in x, in L, in x_hint, in hint, in legends, in flag_abs, in peakd, in flag_trace_minalt, in flag_draw_peaks, in flag_print_peaks, in flag_histogram, in flag_envelope, in colors)
function format_frank(in F, in scale, in handles)
Analysis Session (AS) base class.
Definition: as.m:6
function draw_zero_line(in x, in linewidth)
function format_xaxis(in par)