ImaGIN_FiringRate.m
5.87 KB
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
function ImaGIN_FiringRate(S)
% Compute mean firing rate
%
% USAGE: D = ImaGIN_FiringRate(S)
% -=============================================================================
% This function is part of the ImaGIN software:
% https://f-tract.eu/
%
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE AUTHORS
% DO NOT ASSUME ANY LIABILITY OR RESPONSIBILITY FOR ITS USE IN ANY CONTEXT.
%
% Copyright (c) 2000-2018 Inserm U1216
% =============================================================================-
%
% Authors: Olivier David
%% ===== LOAD INPUT FILE =====
% Get input file
try
DD = S.D;
catch
DD = spm_select(inf, '\.mat$', 'Select EEG mat file');
end
% Change mouse pointer
spm('Pointer', 'Watch'); drawnow;
% Load meeg object
Dmat = load(DD);
% If the spike field is not available, try to load it with the "load" function
if ~isfield(Dmat, 'D') || ~isfield(Dmat.D, 'spike')
error('No spike data');
end
% Copy spike field
Dspike = Dmat.D.spike;
% Convert to MEEG object
D = meeg(Dmat.D);
%% ===== OPTIONS =====
%Recalculate seizure
NElectrode=length(Dspike.name);
try
SelChan = S.Channel;
catch
SelChan = spm_input('Select channel(s) ', 1, 'i',1:NElectrode);
end
try
SelNeuro = S.Neurone;
catch
for i1=1:length(SelChan)
NNeurone=max(Dspike.markers{SelChan(i1)}(:,1,1,1));
SelNeuro{i1} = spm_input(['Select neurone(s) in channel ' num2str(SelChan(i1)) ' '], '+1', 'i',1:NNeurone);
end
end
Time=time(D);
try
Instantaneous=S.Instantaneous;
catch
Ctype = {
'Yes',...
'No'};
str = 'Instantaneous';
Sel = spm_input(str, '+1', 'm', Ctype);
if Sel==1
Instantaneous = 1;
else
Instantaneous = 0;
end
end
try
Causal=S.Causal;
catch
Ctype = {
'Yes',...
'No'};
str = 'Causal';
Sel = spm_input(str, '+1', 'm', Ctype);
if Sel==1
Causal = 1;
else
Causal = 0;
end
end
if Instantaneous==0;
try
TimeWindow=S.TimeWindow;
catch
TimeWindow = spm_input('Time window positions [sec]', '+1', 'r','[]');
end
else
TimeWindow=[];
end
if isempty(TimeWindow)
try
Start = S.Start;
catch
Start=spm_input('Start of analysis window [sec]', '+1', 'r',Time(1));
end
try
End = S.End;
catch
End=spm_input('End of analysis window [sec]', '+1', 'r',Time(end));
end
if Instantaneous==0;
try
TimeResolution=S.TimeResolution;
catch
TimeResolution = spm_input('Time sampling rate [sec]', '+1', 'r',.1,1);
end
end
else
Start=min(TimeWindow);
End=max(TimeWindow);
end
if Instantaneous==0;
try
TimeWindowWidth=S.TimeWindowWidth;
catch
TimeWindowWidth = spm_input('Time window width [sec]', '+1', 'r',2,1);
end
else
TimeWindowWidth=0;
TimeResolution=0;
try
TimeBinDuration=S.TimeBinDuration;
catch
TimeBinDuration = spm_input('Timebin duration [sec]', '+1', 'r',0.05,1);
end
end
try
TemporalSmoothing=S.TemporalSmoothing;
catch
TemporalSmoothing = spm_input('Temporal smoothing [sec]', '+1', 'r',0,1);
end
%% ===== COMPUTATION =====
%Save parameter analysis
for i1=1:length(SelChan)
Dspike.Analysis{SelChan(i1)}.SelNeuro=SelNeuro;
Dspike.Analysis{SelChan(i1)}.TimeWindow=TimeWindow;
Dspike.Analysis{SelChan(i1)}.TimeWindowWidth=TimeWindowWidth;
if isempty(TimeWindow)
Dspike.Analysis{SelChan(i1)}.TimeResolution=TimeResolution;
Dspike.Analysis{SelChan(i1)}.Start=Start;
Dspike.Analysis{SelChan(i1)}.End=End;
end
end
%Compute mean firing rate
if Instantaneous==0;
if isempty(TimeWindow)
TimeWindow=Start:TimeResolution:End;
end
end
N=0;
for i4=1:length(SelNeuro)
N=N+length(SelNeuro{i4});
end
if Instantaneous
Data=zeros(N,length(Time));
Data=zeros(N,length(min(Time):TimeBinDuration:max(Time)));
else
Data=zeros(N,length(TimeWindow));
end
n=0;
for i1=1:length(SelChan)
%Crop the data
Index = find(Dspike.timings{SelChan(i1)}>=Start & Dspike.timings{SelChan(i1)}<=End);
timings = Dspike.timings{SelChan(i1)}(Index);
markers = double(Dspike.markers{SelChan(i1)}(Index,1,1,1));
if Instantaneous
%Instantaneous firing rate for each neurone
for i3=1:length(SelNeuro{i1})
n=n+1;
D.Firing.Name{n}=[Dspike.name{i1} ' - ' num2str(SelNeuro{i1}(i3))];
Data(n,:)=hist(timings,min(Time):TimeBinDuration:max(Time))/TimeBinDuration;
end
else
%Mean firing rate for each neurone
for i3=1:length(SelNeuro{i1})
n=n+1;
for i2=1:length(TimeWindow)
win=TimeWindow(i2)+[-TimeWindowWidth TimeWindowWidth]/2;
Data(n,i2)=length(find(markers(find(timings>=win(1)&timings<=win(2)))==SelNeuro{i1}(i3)))/TimeWindowWidth;
end
D.Firing.Name{n}=[Dspike.name{i1} ' - ' num2str(SelNeuro{i1}(i3))];
end
end
end
if Instantaneous
S=zeros(size(Data,1),length(Time));
for i1=1:size(Data,1)
S(i1,:) = interp1(min(Time):TimeBinDuration:max(Time),Data(i1,:),Time,'linear');
end
else
S=zeros(size(Data,1),length(Time));
for i1=1:size(Data,1)
S(i1,:) = interp1(TimeWindow,Data(i1,:),Time,'linear');
end
end
if TemporalSmoothing>0
windowSize = round(TemporalSmoothing*fsample(D));
for i1=1:size(Data,1)
S(i1,:)= filter(ones(1,windowSize)/windowSize,1,S(i1,:));
end
end
%% ===== SAVE FILE =====
% Get file name
Dfile = fullfile(D);
% Copy results of spike analysis in the file structure
Dmat.D.Firing.data = S;
D = Dmat.D;
% Save file
save(Dfile, 'D');
% Restore mouse pointer
spm('Pointer', 'Arrow');