This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
plot_spike_trial_example.m
151 lines (130 loc) · 6.03 KB
/
plot_spike_trial_example.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
function plot_spike_trial_example(cfg, spiketrials, waveformstats, ipart, markername, itrial, toi)
% Use as :
% plot_spike_trial_example(cfg, spiketrials, waveformstats, ipart, markername, itrial, maxsize)
%
% plot_spike_trial_example plots, for one trial, the raw LFP, the LFP
% high-pass filtered, and the LFP high-pass filtered colored at the moments
% where Spiking-Circus found spikes.
% Input:
% - spiketrials : output from readSpikeTrials.m
% - waveformstats : output from readSpikeWaveforms.m
% - ipart : nr. of the part to use
% - markername : name of the analysis to use
% - itrial : number of the trial to use
% - maxsize : (optional, default = "all") time to plot in the trial,
% see cfg.latency in ft_selectdata
% This file is part of EpiCode, see
% http://www.github.com/stephenwhitmarsh/EpiCode for documentation and details.
%
% EpiCode is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% EpiCode is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with EpiCode. If not, see <http://www.gnu.org/licenses/>.
%
if nargin < 7
toi = "all";
end
for ichannel = 1:size(cfg.circus.channel, 2)
%find trial samples to load filtered data
idir = spiketrials{ipart}.(markername).trialinfo.idir(itrial);
channame = cfg.circus.channel{ichannel};
temp = dir(fullfile(cfg.rawdir, cfg.directorylist{ipart}{idir}, sprintf('*%s.ncs', channame)));
if strcmp(channame(1), '_')
channame = channame(2:end);
end
datapath = fullfile(temp.folder, temp.name);
cfgtemp = [];
cfgtemp.trl(1) = spiketrials{ipart}.(markername).trialinfo.begsample_dir(itrial);
cfgtemp.trl(2) = cfgtemp.trl(1) + (spiketrials{ipart}.(markername).trialinfo.endsample(itrial) - spiketrials{ipart}.(markername).trialinfo.begsample(itrial));
cfgtemp.trl(3) = spiketrials{ipart}.(markername).trialinfo.offset(itrial);
cfgtemp.dataset = datapath;
cfgtemp.bsfilter = 'yes';
cfgtemp.bsfreq = [49 51];
cfgtemp.bsinstabilityfix = 'reduce';
dat = ft_preprocessing(cfgtemp);
cfgtemp.hpfilter = 'yes';
cfgtemp.hpfreq = 300;
cfgtemp.hpfiltord = 3;
dat_hpfilt = ft_preprocessing(cfgtemp, dat);
Fs = dat.fsample;
iplot = 1;
fig = figure;
sgtitle(sprintf('%s %s (%s, trial %d)', cfg.prefix(1:end-1), markername, channame, itrial),...
'interpreter', 'none', 'FontWeight', 'bold', 'Fontsize', 18);
%plot LFP
subplot(3,1,iplot); hold on;
flip = -waveformstats{ipart}.peak_direction(1);
p = plot(dat.time{1}, dat.trial{1}*flip, 'k', 'linewidth', 1);
l1 = legend(p, channame, 'location', 'eastoutside', 'interpreter', 'none');
set(gca, 'tickdir', 'out', 'fontsize', 15);
ylabel('uV');
axis tight
if string(toi) ~= "all"
xlim(toi);
end
%plot MUA without and with higlights
for do_highlight = [false true]
iplot = iplot +1;
%plot data
subplot(3,1,iplot); hold on;
flip = -waveformstats{ipart}.peak_direction(1);
p = plot(dat_hpfilt.time{1}, dat_hpfilt.trial{1}*flip, 'k', 'linewidth', 1);
if do_highlight
unitssel = ichannel == spiketrials{ipart}.(markername).template_maxchan +1;
C = linspecer(sum(unitssel));
icolor = 0;
ileg = 0;
leg = [];
for i_unit = find(unitssel)
icolor = icolor+1;
idx = spiketrials{ipart}.(markername).trial{i_unit} == itrial;
spikes = spiketrials{ipart}.(markername).time{i_unit}(idx);
first_spike = true;
for ispike = 1 : size(spikes, 2)
if spikes(ispike) - 0.02 < dat_hpfilt.time{1}(1) || spikes(ispike) + 0.02 > dat_hpfilt.time{1}(end)
continue
end
if first_spike
ileg = ileg +1;
first_spike = false;
end
temp = round((spikes(ispike) - 0.001) * Fs) : round((spikes(ispike) + 0.02) * Fs);
sel = double(temp) - double(spiketrials{ipart}.(markername).trialinfo.offset(itrial));
leg{ileg} = plot(dat_hpfilt.time{1}(sel), dat_hpfilt.trial{1}(sel)*flip, 'color', C(icolor, :), 'linewidth', 1.5);%[1 0.6 0.2]);
leg_name{ileg} = spiketrials{ipart}.(markername).label{i_unit};
end
end
if ~isempty(leg)
l3 = legend([leg{:}], leg_name{:}, 'Location', 'EastOutside', 'interpreter', 'none');
else
l3 = legend(p, channame, 'Location', 'EastOutside', 'interpreter', 'none');
end
else
l2 = legend(p, channame, 'Location', 'EastOutside', 'interpreter', 'none');
end
set(gca, 'tickdir', 'out', 'fontsize', 15);
ylabel('uV');
axis tight
if string(toi) ~= "all"
xlim(toi);
end
end
xlabel('time (s)');
%make legend box the same size to have aligned x axis
maxlen = max(cellfun(@length, l3.String));
difflen = maxlen - length(l1.String{1});
l1.String{1} = [l1.String{1}, repmat(' ', 1, difflen+3), '.'];
l2.String{1} = l1.String{1};
l1.FontSize = l3.FontSize;
l2.FontSize = l3.FontSize;
figname = fullfile(cfg.imagesavedir, 'example_trial_spikes', sprintf('%s%s_%s_trial%d', cfg.prefix, channame, markername, itrial));
savefigure_own(fig, figname, 'landscape', 'png', 'pdf', 'close');
end