forked from acsutt0n/Drosophila-larvae_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotGetSpikes.m
160 lines (144 loc) · 4.5 KB
/
PlotGetSpikes.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
function varargout = PlotGetSpikes(dt, v, spike, options, burst)
% PlotGetSpikes(dt, v, spike, options, burst)
% plot spike info (and optionally bursts) on top of voltage trace
% INPUTS:
% -t: time array (ms)
% -v: voltage trace array (mV)
% -spike: structure with spike information, as calculated by GetSpikes.m
% -options: structure with detection/plot options
% OPTIONAL:
% -burst: structure with burst information, as caculated by AnalyzeBurst.m
if length(dt) == 1
% passed dt
dt = dt / 1000; % convert to sec
tFinal = dt * (length(v) - 1);
t = 0:dt:tFinal;
else
% passed t (as dt)
t = 0.001 * dt;
if t(1) ~= 0
t = t - t(1);
end
dt = (t(end) - t(1)) / (length(v) - 1);
end
spikeTimes = spike.times / 1000;
if nargin == 5
% using burst information
burstTimes = burst.Times / 1000;
burstLen = burst.Durations.list / 1000;
baseTitle = 'Spike/Burst Detection';
else
% only using spike information
baseTitle = 'Spike Detection';
end
% define upper and lower voltages of lines indicating spike times
top = max(v);
bottom = min(v);
delta = 0.1 * (top - bottom);
bottom = bottom - delta;
top = top + delta;
% make the title
if ischar(options.plotSubject)
titleStr = [options.plotSubject, ': ', baseTitle];
else
titleStr = baseTitle;
end
if ~isfield(options, 'timesOnly')
options.timesOnly = false;
end
if ~options.timesOnly
% get some shape information
if isfield(spike.maxV.v, 'list')
% spike is "structified"
vMax = spike.maxV.v.list;
vPreMaxK = spike.preMaxCurve.v.list;
vPostMaxK = spike.postMaxCurve.v.list;
vPreMinV = spike.preMinV.v.list;
vPostMinV = spike.postMinV.v.list;
vMaxDeriv = spike.maxDeriv.v.list;
vMinDeriv = spike.minDeriv.v.list;
else
vMax = spike.maxV.v;
vPreMaxK = spike.preMaxCurve.v;
vPostMaxK = spike.postMaxCurve.v;
vPreMinV = spike.preMinV.v;
vPostMinV = spike.postMinV.v;
vMaxDeriv = spike.maxDeriv.v;
vMinDeriv = spike.minDeriv.v;
end
end
% create a new figure, name it, and make it ready for plotting
h = NamedFigure(titleStr);
set(h, 'WindowStyle', 'docked');
clf; % clear the plot, in case something is already there
whitebg(h, 'k'); % make the background black
hold on;
legendEntries = {};
if nargin == 5
% first draw blue rectangle to signify burst times
numBursts = length(burstTimes);
if numBursts > 0
burstGroup = hggroup;
for n = 1:numBursts
tLow = burstTimes(n);
tHigh = tLow + burstLen(n);
fill([tLow, tHigh, tHigh, tLow], [bottom, bottom, top, top], 'b', ...
'Parent', burstGroup);
end
set(get(get(burstGroup,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','on');
legendEntries = [legendEntries, 'Burst'];
end
end
numSpikes = length(spikeTimes);
if options.timesOnly
% no shape information, just mark spikes
for n=1:numSpikes
% overlay red lines indicating spikes
plot([spikeTimes(n), spikeTimes(n)], [bottom,top], 'r-');
end
% finally draw the voltage trace in white:
plot(t, v, 'w-');
else
% we have shape information, show some of it
% draw the voltage trace in white:
voltageLine = plot(t, v, 'w-');
set(get(get(voltageLine,'Annotation'),'LegendInformation'),...
'IconDisplayStyle','off'); % Exclude line from legend
plot( spike.preMaxCurve.t ./ 1000, vPreMaxK, 'yo', ...
'MarkerSize', 6, 'MarkerFaceColor', 'y' )
plot( spike.postMaxCurve.t ./ 1000, vPostMaxK, 'ys', ...
'MarkerSize', 6, 'MarkerFaceColor', 'y' )
plot( spike.maxDeriv.t ./ 1000, vMaxDeriv, 'co', ...
'MarkerSize', 6, 'MarkerFaceColor', 'c' )
plot( spike.minDeriv.t ./ 1000, vMinDeriv, 'cs', ...
'MarkerSize', 6, 'MarkerFaceColor', 'c')
n1 = spike.n1List; n2 = spike.n2List;
plot( t(n1), v(n1), 'mo', ...
'MarkerSize', 6, 'MarkerFaceColor', 'm' )
plot( t(n2), v(n2), 'ms', ...
'MarkerSize', 6, 'MarkerFaceColor', 'm' )
plot( spike.maxV.t ./ 1000, vMax, 'ro', ...
'MarkerSize', 6, 'MarkerFaceColor', 'r' )
end
if numSpikes > 0
if options.timesOnly
legendEntries = [legendEntries, 'spike'];
else
legendEntries = [legendEntries, ...
{ 'pre max K', 'post max K', 'max dV', 'min dV', ...
'bracketStart', 'bracketEnd', 'maxV' } ];
end
end
if ~isempty(legendEntries)
legend(legendEntries{:}, 'Location', 'SouthOutside', 'Orientation', 'Horizontal')
end
% axis labels and title
xlabel('Time (s)', 'FontSize', 18)
ylabel('Voltage (mV)', 'FontSize', 18)
title(RealUnderscores(titleStr), 'FontSize', 18)
hold off;
if nargout
varargout = {h};
end
return