-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_spike_histogram.m
executable file
·214 lines (179 loc) · 6.66 KB
/
plot_spike_histogram.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
function handles = plot_spike_histogram (spHistData, spHistParams, varargin)
%% Plots a spike histogram from the results of compute_spike_histogram.m
% Usage: handles = plot_spike_histogram (spHistData, spHistParams, varargin)
% Explanation:
% TODO
%
% Example(s):
% TODO
%
% Outputs:
% handles - a structure with fields:
% TODO
% specified as a scalar structure
%
% Arguments:
% spHistData - spike histogram data
% must be a scalar structure with fields:
% spikeCounts
% edgesSec
% timeBurstStartsSec
% timeBurstEndsSec
% spHistParams - spike histogram parameters
% must be a scalar structure with fields:
% oscDurationSec
% histLeftSec
% timeOscEndSec
% binWidthSec
% maxInterBurstIntervalSec
% nSpikesInOsc
% figTitleBase
% varargin - 'XLimits': x-axis limits
% suppress by setting value to 'suppress'
% must be 'suppress' or a 2-element increasing numeric vector
% default == minimum and maximum edges of bins
% - 'YLimits': limits of y axis,
% suppress by setting value to 'suppress'
% must be 'suppress' or a 2-element increasing numeric vector
% default == 'suppress'
% - Any other parameter-value pair for plot_histogram()
%
% Requires:
% cd/alternate_elements.m
% cd/apply_iteratively.m
% cd/create_error_for_nargin.m
% cd/extract_subvectors.m
% cd/plot_histogram.m
% cd/plot_horizontal_line.m
%
% Used by:
% cd/m3ha_network_analyze_spikes.m
% cd/m3ha_oscillations_analyze.m
% cd/parse_multiunit.m
% File History:
% 2019-12-02 Moved from parse_multiunit.m
%
%% Hard-coded parameters
minNXTicks = 5;
% TODO: Make optional arguments
xLabel = 'Time (seconds)';
yLabel = '';
figTitle = '';
%% Default values for optional arguments
xLimitsDefault = []; % set later
yLimitsDefault = []; % set later
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Deal with arguments
% Check number of required arguments
if nargin < 2
error(create_error_for_nargin(mfilename));
end
% Set up Input Parser Scheme
iP = inputParser;
iP.FunctionName = mfilename;
iP.KeepUnmatched = true; % allow extraneous options
% Add required inputs to the Input Parser
addRequired(iP, 'spHistData', ...
@(x) validateattributes(x, {'struct'}, {'scalar'}));
addRequired(iP, 'spHistParams', ...
@(x) validateattributes(x, {'struct'}, {'scalar'}));
% Add parameter-value pairs to the Input Parser
addParameter(iP, 'XLimits', xLimitsDefault, ...
@(x) isempty(x) || ischar(x) && strcmpi(x, 'suppress') || ...
isvector(x) && length(x) == 2);
addParameter(iP, 'YLimits', yLimitsDefault, ...
@(x) isempty(x) || ischar(x) && strcmpi(x, 'suppress') || ...
isnumeric(x) && isvector(x) && length(x) == 2);
% Read from the Input Parser
parse(iP, spHistData, spHistParams, varargin{:});
xLimits = iP.Results.XLimits;
yLimits = iP.Results.YLimits;
% Keep unmatched arguments for the plot_histogram() function
otherArguments = iP.Unmatched;
%% Preparation
% Extract from spHistData
spikeCounts = spHistData.spikeCounts;
edgesSec = spHistData.edgesSec;
timeBurstStartsSec = spHistData.timeBurstStartsSec;
timeBurstEndsSec = spHistData.timeBurstEndsSec;
% Extract from spHistParams
oscDurationSec = spHistParams.oscDurationSec;
nSpikesInOsc = spHistParams.nSpikesInOsc;
% Decide on figure title base
if isfield(spHistParams, 'figTitleBase')
figTitleBase = spHistParams.figTitleBase;
else
figTitleBase = 'unnamed data';
end
% Compute burst windows
burstWindows = alternate_elements(timeBurstStartsSec, timeBurstEndsSec, ...
'ReturnNaNIfEmpty', true);
% Compute default x limits
if isempty(xLimits)
% Extract parameters
histLeftSec = spHistParams.histLeftSec;
timeOscEndSec = spHistParams.timeOscEndSec;
maxInterBurstIntervalSec = spHistParams.maxInterBurstIntervalSec;
% Compute left and right ends of histogram to show
histLeft = min(histLeftSec);
histRight = timeOscEndSec + 1.5 * max(maxInterBurstIntervalSec);
% histRight = 10;
% Find appropriate x limits
xLimits = [histLeft, histRight];
end
% Compute x tick locations
xTickLocs = linspace(xLimits(1), xLimits(2), minNXTicks);
% Compute default y limits
if isempty(yLimits)
% Extract parameters
binWidthSec = spHistParams.binWidthSec;
% Find the last bin to show for all traces
lastBinToShow = floor(range(xLimits) ./ binWidthSec) + 1;
% Find appropriate y limits
spikeCountsOfInterest = extract_subvectors(spikeCounts, ...
'IndexEnd', lastBinToShow);
largestSpikeCount = apply_iteratively(@max, spikeCountsOfInterest);
yLimits = [0, largestSpikeCount * 1.2];
end
% Decide on y axis label
if isempty(yLabel)
binWidthMs = spHistParams.binWidthMs;
yLabel = ['Spike Count per ', num2str(binWidthMs), ' ms'];
end
% Decide on figure title
if isempty(figTitle)
figTitle = ['Spike histogram for ', figTitleBase];
end
%% Plot histogram
% Hold on
wasHold = hold_on;
% Plot histogram
[histBars, histFig] = ...
plot_histogram([], 'Counts', spikeCounts, 'Edges', edgesSec, ...
'XLimits', xLimits, 'YLimits', yLimits, ...
'XTickLocs', xTickLocs, 'XLabel', xLabel, ...
'YLabel', yLabel, 'FigTitle', figTitle, ...
otherArguments);
% Show oscillation duration
texts(1) = text(0.2, 0.95, sprintf('Oscillation Duration = %.2g seconds', ...
oscDurationSec), 'Units', 'normalized');
% Show number of spikes in oscillation
texts(2) = text(0.2, 0.9, sprintf('Number of spikes in oscillation = %d', ...
nSpikesInOsc), 'Units', 'normalized');
% Plot burst windows
horzLines = plot_horizontal_line(0, 'XLimits', burstWindows, ...
'Color', 'g', 'LineStyle', '-', 'LineWidth', 3);
% Hold off
hold_off(wasHold);
%% Output results
handles.fig = histFig;
handles.bars = histBars;
handles.texts = texts;
if exist('horzLines', 'var')
handles.horzLines = horzLines;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%{
OLD CODE:
%}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%