forked from BUNPC/Homer3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotProbe.m
363 lines (308 loc) · 9.77 KB
/
plotProbe.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
% h = plotProbe( y, t, SD, ml, ystd, axFactor, tStep, tAmp, tVis )
%
% Plot the data in the probe format. If no data is provided,
% this plots the probe geometry given in SD.
%
% y - is the data to plot. This 2D or 3D array is either
%
% If it's 3D then the meaning of the dimensions are
%
% <DATA TIME POINTS> x <HB TYPES x CHANNELS>
%
% or if it's 2D
%
% <DATA TIME POINTS> x <CHANNELS OF ALL WAVELENGTHS>
%
% If empty, then the probe is plotted with lines joining sources
% and detectors in the ml with solid line and
% dotted line distringuishing active measurements as indicated in
% mlActMan
%
% t - is the corresponding time vector
%
% SD - SD structure
%
% ystd - Standard deviation to be plotted as y +/- ystd. OPTIONAL
%
% axFactor - [x y] scale the width and height of the subplots. OPTIONAL
%
% tVis - The value of the visible property of the vertical time markers. if
% 'on' 1, they are shown, if 'off' or 0, they are not.
%
% tStep - Time intervals at which to draw vertical time markers, starting at
% t0 (stim onset). tStep has be within the range of 5 <= tStep <= t.
% OPTIONAL
%
% tAmp - Amplitude of the vertical time markers.
% OPTIONAL
%
% To Do
% cmin and cmax
% toggle nearest neighbors
% assuming y is concentration data... need to check dimensions
function h = plotProbe( y, t, SD, ml, ~, axFactor, tStep, tAmp, tVis)
EXPLODE_THRESH = 0.02;
EXPLODE_VECTOR = [0.02, 0.08];
h = [];
% Get initial arg values
if ~exist('y','var')
y = [];
end
if ~exist('ystd','var')
ystd = [];
end
if ~exist('SD','var')
SD = [];
end
if ~exist('ml','var')
ml = [];
end
if ~exist('tVis','var')
tVis = 'on';
else
if tVis == 1
tVis = 'on';
elseif tVis == 0
tVis = 'off';
end
end
if ~exist('axFactor','var')
axFactor = [1,1];
elseif isempty(axFactor)
axFactor = [1,1];
elseif ndims(axFactor)~=2
axFactor(2) = axFactor(1);
end
if ~isempty(t)
if ~exist('tStep','var') || isempty(tStep) || tStep>t(end)
tStep = t(end);
elseif tStep<2 && tStep~=0
tStep = 2;
end
end
if ~exist('tAmp','var') || isempty(tAmp) || tAmp<0
tAmp = 0;
end
% Conditions causing early exit
if isempty(y)
hold off
return;
end
if isempty(ml)
hold off
return;
end
if isempty(SD)
hold off
return;
end
% This section will give the option to display subsections of the probe
% based on nearest-neighbor etc distances. If the probe only has one
% distance, this option is not given
Distances = ((SD.SrcPos(ml(:,1),1) - SD.DetPos(ml(:,2),1)).^2 +...
(SD.SrcPos(ml(:,1),2) - SD.DetPos(ml(:,2),2)).^2 +...
(SD.SrcPos(ml(:,1),3) - SD.DetPos(ml(:,2),3)).^2).^0.5;
nearneighborLst = ones(length(Distances),1);
lstNN = find(nearneighborLst==1);
%Use the probe SD positions to define the look of this plot
sPos = SD.SrcPos;
dPos = SD.DetPos;
sdMin = min([sPos;dPos]) - mean(Distances(lstNN));
sdMax = max([sPos;dPos]) + mean(Distances(lstNN));
sdWid = sdMax(1) - sdMin(1);
sdHgt = sdMax(2) - sdMin(2);
sd2axScl = max(sdWid,sdHgt);
sPos = sPos / sd2axScl;
dPos = dPos / sd2axScl; % xlim([min(t) max(t)])
nAcross = length(unique([sPos(:,1); dPos(:,1)]))+1;
nUp = length(unique([sPos(:,2); dPos(:,2)]))+1;
axWid = axFactor(1) * 1/nAcross;
axHgt = axFactor(2) * 1/nUp;
axXoff = mean([sPos(:,1);dPos(:,1)])-.5;
axYoff = mean([sPos(:,2);dPos(:,2)])-.5;
mlAct = ones(size(ml,1),1);
%This is the plotting routine
% Initialize channel idx for try/catch. Catch uses it
% to display which channel was being processed when
% error ocurred.
colorActive = [
1.00 0.00 0.00;
0.00 0.00 1.00;
0.00 1.00 0.00;
1.00 0.00 1.00;
0.00 1.00 1.00;
0.50 0.80 0.30
];
colorInactive = [
0.80 0.00 0.20;
0.00 0.20 0.80;
0.20 0.80 0.00;
1.00 0.00 1.00;
0.00 1.00 1.00;
0.50 0.80 0.30
];
% ha = subplot(1,1,1);
minT = min(t);
maxT = max(t);
nTSteps = round(t(end)/tStep);
tStep = tStep/(t(2)-t(1));
if ~isempty(y)
iSrc = ml(1,1);
iDet = ml(1,2);
iMeasSDpair = find( (ml(:,1) == iSrc) & (ml(:,2) == iDet) );
nDataTypes = length(iMeasSDpair);
nSDpairs = size(ml,1)/nDataTypes;
iSDpairs = find( ml(:,4) == 1 );
minAmp = min(min(y));
maxAmp = max(max(y));
if length(tAmp)==2
cmin = tAmp(1);
cmax = tAmp(2);
else
cmin = minAmp;
cmax = maxAmp;
end
h = zeros(nSDpairs, nDataTypes);
ls = repmat({''}, nSDpairs, 1);
lw = zeros(nSDpairs, nDataTypes);
lv = repmat({''}, nSDpairs, nDataTypes);
lc = zeros(nSDpairs, nDataTypes, 3);
try
for iSD = 1:length(iSDpairs)
% Get all measurements with current source and detector pair
iSrc = ml(iSDpairs(iSD),1);
iDet = ml(iSDpairs(iSD),2);
iSDpairAllMeas = find( (ml(:,1) == iSrc) & (ml(:,2) == iDet) );
% To eliminate displayed data drifting when scaling y up
% or down offset data to align min/max midpoint with zero.
[y, yOffset] = offsetData(y, iSDpairAllMeas);
if ml(iSDpairAllMeas(1),5) == 1
color = colorActive;
linewidth = 2;
linestyle = '-';
else
color = colorInactive;
linewidth = 1;
linestyle = '--';
end
for iMeasType = 1:length(iSDpairAllMeas)
% Record line graphics properties based on the object type
[lc,lv,lw,ls] = setLineProperties(lc,lv,lw,ls,iMeasType,mlAct,color,nDataTypes,nTSteps);
xa = ( sPos(ml(iSDpairAllMeas(iMeasType),1), 1) + dPos(ml(iSDpairAllMeas(iMeasType),2), 1) ) / 2 - axXoff;
ya = ( sPos(ml(iSDpairAllMeas(iMeasType),1), 2) + dPos(ml(iSDpairAllMeas(iMeasType),2), 2) ) / 2 - axYoff;
xT = xa-axWid/4 + axWid*((t-minT)/(maxT-minT))/2;
AvgT = ya-axHgt/4 + axHgt*((y(:,iSDpairAllMeas(iMeasType))-cmin)/(cmax-cmin))/2;
hold on
h(iSD,iMeasType) = plot( xT, AvgT,'color',color(iMeasType,:), 'visible','on', 'linewidth',linewidth, 'linestyle',linestyle );
end
% Plot time markers starting with stim onset
if length(tAmp)==1
AvgTmax0 = max(max(AvgT));
AvgTmin0 = min(min(AvgT));
if tAmp==0
% tAmp is a relative amplitude
AvgTmax = AvgTmax0;
AvgTmin = AvgTmin0;
else
% tAmp is a fixed amplitude
AvgTmax = ya-axHgt/4 + axHgt*((tAmp-cmin)/(cmax-cmin))/2;
AvgTmin = ya-axHgt/4 + axHgt*((0-cmin)/(cmax-cmin))/2;
AvgTmax = AvgTmax+AvgTmin0-AvgTmin;
AvgTmin = AvgTmin0;
end
if abs(AvgTmin-AvgTmax)<1.0e-10
AvgTmin = AvgTmin-(AvgTmin*.01);
AvgTmax = AvgTmax+(AvgTmax*.01);
end
elseif length(tAmp)==2
% tAmp is a fixed range
AvgTmax = ya-axHgt/4 + axHgt*(((cmax-offset(idx))-cmin)/(cmax-cmin))/2;
AvgTmin = ya-axHgt/4 + axHgt*(((cmin-offset(idx))-cmin)/(cmax-cmin))/2;
end
kk = nDataTypes+1;
yMark = [AvgTmin,AvgTmax]-yOffset;
xT0 = xT(find(t==0));
xTStep = tStep*(xT(2)-xT(1));
if strcmp(lv{iSD(1),kk}, 'off')
tvis_ch = 'off';
else
tvis_ch = tVis;
end
for xTi = xT0:xTStep:xT(end)
xTi = [xTi, xTi];
hTmarks = plot(xTi, yMark, 'color','k', 'visible',tvis_ch);
h(iSD,kk) = hTmarks;
lw(iSD,kk) = 1.0;
kk = kk+1;
end
end
catch
menu(sprintf('plotProbe exited with ERROR while plotting data for channel # %d',iSD),'OK');
h=[];
end
end
% Plot the optodes on the axes
if ismac() || islinux()
fs = 14;
else
fs = 11;
end
for idx2 = 1:size(sPos,1)
xa = sPos(idx2,1) - axXoff;
ya = sPos(idx2,2) - axYoff;
ht = text(xa,ya,sprintf('S%d',idx2));
set(ht,'fontweight','bold','fontsize',fs)
set(ht,'color',[1 0 0])
end
for idx2 = 1:size(dPos,1)
xa = dPos(idx2,1) - axXoff;
ya = dPos(idx2,2) - axYoff;
ht = text(xa,ya,sprintf('D%d',idx2));
set(ht,'fontweight','bold','fontsize',fs)
set(ht,'color',[0 0 1])
end
hold off
% ----------------------------------------------------------------------------------------
function [lc,lv,lw,ls] = setLineProperties(lc,lv,lw,ls,idx,mlAct,color,nDataTypes,nTSteps)
if mlAct(idx)==0 % Make manually and automaticaly pruned or disabled channels dotted
ls{idx} = ':';
for ii=1:nDataTypes
lw(idx,ii) = 1;
lc(idx,ii,:) = color(ii,:);
lv{idx,ii} = 'on';
end
for ii=nDataTypes+1:nDataTypes+nTSteps
lc(idx,ii,:) = [0 0 0];
lv{idx,ii} = 'on';
end
elseif mlAct(idx)==1
ls{idx} = '-';
for ii=1:nDataTypes
lw(idx,ii) = 1;
lc(idx,ii,:) = color(ii,:);
lv{idx,ii} = 'on';
end
for ii=nDataTypes+1:nDataTypes+nTSteps
lc(idx,ii,:) = [0 0 0];
lv{idx,ii} = 'on';
end
elseif mlAct(idx)==-1 % Hide hidden channels
ls{idx} = '-';
for ii=1:nDataTypes
lw(idx,ii) = 1.0;
lc(idx,ii,:) = color(ii+nDataTypes,:);
lv{idx,ii} = 'off';
end
for ii=nDataTypes+1:nDataTypes+nTSteps
lc(idx,ii,:) = [0 0 0];
lv{idx,ii} = 'off';
end
end
% ------------------------------------------------------
function [y,ampMp] = offsetData(y, k)
minAmp = min(min(y(:,k)));
maxAmp = max(max(y(:,k)));
% Find amplitude mid-point and shift data midpoint to zero
ampDiff = maxAmp - minAmp;
ampMp = minAmp + ampDiff/2;
y(:,k) = y(:,k) - ampMp;