0001 function r = ui_plot(o, plot_spec, plot_params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 [n_rows n_cols] = summary_size(o);
0063 if ~prod([n_rows n_cols]), warning('No data to plot'), return, end
0064 rows = block_rows(o);
0065 Y = summary_data(o);
0066 N = region_name(o);
0067 S = sumfunc(o);
0068 info = summary_info(o);
0069 if strcmp(S, 'unknown'), S = ''; end
0070 if ~isempty(S), S = [' - ' S]; end
0071
0072 def_spec = struct('types', {{'raw'}}, ...
0073 'r_nos', 1:n_cols, ...
0074 'b_nos', 1:length(rows),...
0075 'graphics_obj', []);
0076
0077
0078 if nargin < 2
0079 plot_spec = [];
0080 end
0081 if ischar(plot_spec), plot_spec = struct('types', plot_spec); end
0082 plot_spec = mars_struct('fillafromb', plot_spec, def_spec);
0083
0084 plot_types = plot_spec.types;
0085 if ~iscell(plot_types)
0086 plot_types = {plot_types};
0087 end
0088 if strcmp('all', plot_types{1})
0089 plot_types = {'raw','acf','fft'};
0090 end
0091 n_p_t = length(plot_types);
0092 n_plots = n_p_t * length(plot_spec.r_nos);
0093
0094
0095 gr_ob = plot_spec.graphics_obj;
0096 if isempty(gr_ob)
0097 gr_ob = spm_figure('GetWin','Graphics');
0098 spm_results_ui('Clear', gr_ob, 0);
0099 end
0100 if ~all(ishandle(gr_ob))
0101 error('One or more graphics objects are not valid');
0102 end
0103 o_t = get(gr_ob, 'type');
0104 if iscell(o_t)
0105 if any(diff(strvcat(o_t{:}),[],1))
0106 error('All graphics objects must be of the same type');
0107 end
0108 o_t = o_t{1};
0109 end
0110 switch o_t
0111 case 'figure'
0112 figure(gr_ob);
0113 case 'axes'
0114 if n_plots > length(gr_ob)
0115 error('Not enough axes for planned number of plots');
0116 end
0117 otherwise
0118 error(['Completely confused by graphics object type: ' o_t]);
0119 end
0120
0121 rows = rows(plot_spec.b_nos);
0122 n_blocks = length(rows);
0123
0124
0125 if nargin < 3
0126 plot_params = cell(1,n_p_t);
0127 end
0128 if ~iscell(plot_params)
0129 plot_params = {plot_params};
0130 end
0131 if length(plot_params) == 1
0132 plot_params = repmat(plot_params, 1, n_p_t);
0133 elseif length(plot_params) < n_p_t
0134 error(sprintf('You need %d plot_param entries', n_p_t));
0135 end
0136
0137
0138 if mars_struct('isthere', info, 'TR')
0139 bin_length = info.TR;
0140 bin_str = 'Hz';
0141 else
0142 bin_length = 1;
0143 bin_str = 'cycles per time point';
0144 end
0145
0146 p_ctr = 1;
0147 for c = plot_spec.r_nos
0148 for p = 1:n_p_t
0149 switch o_t
0150 case 'figure'
0151 subplot(n_plots, 1, p_ctr);
0152 case 'axes'
0153 axes(gr_ob(p_ctr));
0154 end
0155
0156 y = Y(:,c);
0157
0158 switch lower(plot_types{p})
0159 case 'raw'
0160 a_r = [];
0161 for s = 1:n_blocks
0162 a_r = [a_r; rows{s}(:)];
0163 end
0164 plot(y(a_r));
0165 axis tight
0166 hold on
0167
0168 yrg = [min(y) max(y)];
0169 for s = 1:n_blocks
0170 rw = rows{s};
0171 if s > 1
0172 plot([rw(1) rw(1)]-0.5, yrg, 'k:');
0173 end
0174 end
0175 ylabel(['Signal intensity' S])
0176 xlabel('Time point');
0177 r{p_ctr} = y;
0178
0179 case 'acf'
0180 if isfield(plot_params{p}, 'lags')
0181 lags = plot_params{p}.lags;
0182 else lags = 10;
0183 end
0184 mn_len = Inf;
0185 for s = 1:n_blocks
0186 if length(rows{s}) < mn_len, mn_len = length(rows{s}); end
0187 end
0188 lags = max([1 min([mn_len-4 lags])]);
0189
0190 C = [];
0191 for s = 1:n_blocks
0192 by = y(rows{s});
0193 nb_rows = length(by);
0194 ty = toeplitz(by, [by(1) zeros(1, lags)]);
0195 ty([1:lags n_rows+1:end], :) = [];
0196 Cs = corrcoef(ty);
0197 C = [C Cs(1,:)];
0198 n = nb_rows - 2;
0199 t_th = spm_invTcdf(1-0.025, n);
0200 r_th(s) = sqrt(1/(n/t_th.^2+1));
0201 end
0202 stem(C);
0203 axis tight
0204 hold on
0205
0206 Crg = [min(C) max(C)];
0207 for s = 1:n_blocks
0208 s_vals = [0 lags] + (s-1)*(lags+1) + 1;
0209 plot(s_vals, [r_th(s) r_th(s)], 'r:');
0210 plot(s_vals, -[r_th(s) r_th(s)], 'r:');
0211 if s > 1
0212 plot([s_vals(1) s_vals(1)]-0.5, Crg, 'k:');
0213 end
0214 end
0215 xtl = get(gca,'xticklabel');
0216 xtl = num2str(mod(str2num(xtl)-1, lags+1));
0217 set(gca, 'xticklabel', xtl);
0218
0219 ylabel('Correlation coefficient')
0220 xlabel('Lag');
0221 r{p_ctr} = C;
0222 case 'fft'
0223 if isfield(plot_params{p}, 'bin_length')
0224 b_len = plot_params{p}.bin_length;
0225 b_str = 'Hz';
0226 else
0227 b_len = bin_length;
0228 b_str = bin_str;
0229 end
0230 P = []; H = []; St = [];
0231 for s = 1:n_blocks
0232 by = y(rows{s});
0233 gX = abs(fft(by)).^2;
0234 gX = gX*diag(1./sum(gX));
0235 q = size(gX,1);
0236 Hz = [0:(q - 1)]/(q * b_len);
0237 q = 2:fix(q/2);
0238 P = [P gX(q)'];
0239 St(s) = length(H)+1;
0240 H = [H; Hz(q)'];
0241 end
0242 plot(P)
0243 hold on
0244 Prg = [min(P) max(P)];
0245 for s = 2:n_blocks
0246 plot([St(s) St(s)]-0.5, Prg, 'k-');
0247 end
0248 axis tight
0249
0250 xt = get(gca, 'xtick');
0251 xt = xt(xt==fix(xt));
0252 set(gca, 'xtick', xt);
0253 for t = 1:length(xt)
0254 xtl_fft{t} = sprintf('%5.3f', H(xt(t)));
0255 end
0256 set(gca, 'xticklabel', xtl_fft);
0257 xlabel(sprintf('Frequency (%s)', b_str))
0258 ylabel('Relative spectral density')
0259 axis tight
0260 r{p_ctr} = P;
0261 otherwise
0262 error(['What is this plot type: ' plot_types{p} '?']);
0263 end
0264 title(N{c}, 'interpreter', 'none');
0265
0266 p_ctr = p_ctr + 1;
0267 end
0268 end
0269
0270 return
0271