/
process_events_import_Neuroscope.m
369 lines (308 loc) · 15 KB
/
process_events_import_Neuroscope.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
362
363
364
365
366
367
368
369
function varargout = process_events_import_Neuroscope( varargin )
% PROCESS_CHANNEL_SETSEEG: Convert Neuroscope events to Brainstorm and
% attach them to the "link to raw file"
% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c)2000-2020 University of Southern California & McGill University
% 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
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Konstantinos Nasiotis, 2020
eval(macro_method);
end
%% ===== GET DESCRIPTION =====
function sProcess = GetDescription() %#ok<DEFNU>
% Description the process
sProcess.Comment = 'Import Neuroscope events';
sProcess.Category = 'Custom';
sProcess.SubGroup = {'Peyrache Lab', 'Ripples'};
sProcess.Index = 2224;
sProcess.Description = '';
% Definition of the input accepted by this process
sProcess.InputTypes = {'data', 'raw'};
sProcess.OutputTypes = {'data', 'raw'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;
sProcess.isSeparator = 1;
SelectOptions = {...
'', ... % Filename
'', ... % FileFormat
'open', ... % Dialog type: {open,save}
'Import anatomy folder...', ... % Window title
'ImportAnat', ... % LastUsedDir: {ImportData,ImportChannel,ImportAnat,ExportChannel,ExportData,ExportAnat,ExportProtocol,ExportImage,ExportScript}
'single', ... % Selection mode: {single,multiple}
'dirs', ... % Selection mode: {files,dirs,files_and_dirs}
bst_get('FileFilters', 'AnatIn'), ... % Available file formats
'AnatIn'}; % DefaultFormats: {ChannelIn,DataIn,DipolesIn,EventsIn,AnatIn,MriIn,NoiseCovIn,ResultsIn,SspIn,SurfaceIn,TimefreqIn}
% Option: Neuroscope Folder
sProcess.options.neuroscopeFolder.Comment = 'Folder to import:';
sProcess.options.neuroscopeFolder.Type = 'filename';
sProcess.options.neuroscopeFolder.Value = SelectOptions;
% Option: Neuroscope Fs
sProcess.options.fs.Comment = 'Sampling rate of the original .dat file:';
sProcess.options.fs.Type = 'value';
sProcess.options.fs.Value = {20000, [], 0};
end
%% ===== FORMAT COMMENT =====
function Comment = FormatComment(sProcess) %#ok<DEFNU>
Comment = sProcess.Comment;
end
%% ===== RUN =====
function OutputFiles = Run(sProcess, sInput) %#ok<DEFNU>
OutputFiles = {};
if length(sInput)>1
error('This function can only be used on a single file. Don''t add more in the processing window');
end
% Get options
% Get channel studies
[tmp, iChanStudies] = bst_get('ChannelForStudy', [sInput.iStudy]);
iChanStudies = unique(iChanStudies);
% Get channel study
sStudy = bst_get('Study', iChanStudies);
if isempty(sStudy.Channel)
bst_report('Error', sProcess, [], 'No channel file available.');
return
end
% Read channel file
ChannelFile = sStudy.Channel(1).FileName;
DataMat = in_bst_data(sInput.FileName);
% Get channel file
[sStudy, iStudy] = bst_get('ChannelFile', ChannelFile);
%% Load channel file
ChannelFile = file_fullpath(ChannelFile);
ChannelMat = in_bst_channel(ChannelFile);
folder = sProcess.options.neuroscopeFolder.Value{1};
%% Get the unique Montages / Shank that are present in the channel file
montages = unique({ChannelMat.Channel.Group}, 'stable');
montages = montages(find(~cellfun(@isempty, montages)));
%% Get the number of Montages that exist in the Neuroscope files
directoryContents = dir(folder);
iCluFiles = find(contains({directoryContents.name}, '.clu.'));
iResFiles = find(contains({directoryContents.name}, '.res.'));
% iFetFiles = find(contains({directoryContents.name}, '.fet'));
nMontages = length(iCluFiles); % How many "montages exist"
if (length(iCluFiles)~= length(iResFiles)) || length(montages) ~= length(iCluFiles)
error('Something is off. You should have the same number of files for .res, .fet, .res filetypes and also the same number of Montages')
elseif length(iCluFiles)==0
error('No files found. Probably the wrong folder was selected.')
end
%% Start converting
events = struct();
index = 0;
for iMontage = 1:nMontages
% Information about the Neuroscope file can be found here:
% http://neurosuite.sourceforge.net/formats.html
%% Load necessary files
% Extract filename from 'filename.fet.1'
general_file = fullfile(directoryContents(1).folder, directoryContents(iCluFiles(1)).name);
general_file = general_file(1:end-5);
clu = load([general_file 'clu.' num2str(iMontage);]);
res = load([general_file 'res.' num2str(iMontage);]);
fet = dlmread([general_file 'fet.' num2str(iMontage);]);
ChannelsInMontage = ChannelMat.Channel(strcmp({ChannelMat.Channel.Group}, montages{iMontage})); % Only the channels from the Montage should be loaded here to be used in the spike-events
%% The combination of the .clu files and the .fet file is enough to use on the converter.
% Brainstorm assign each spike to a SINGLE NEURON on each electrode. This
% converter picks up the electrode that showed the strongest (absolute)
% component on the .fet file and assigns the spike to that electrode. Consecutively, it
% checks the .clu file to assign the spike to a specific neuron. If more
% than one clusters are assigned to that electrode, different labels will
% be created for each neuron.
iChannels = zeros(size(fet,1),1);
for iSpike = 2:size(fet,1) % The first entry will be zeros. Ignore
[tmp,iPCA] = max(abs(fet(iSpike,1:end-3)));
iChannels(iSpike) = ceil(iPCA/3);
end
% Now iChannels holds the Channel that each spike belongs to, and clu
% holds the cluster that each spike belongs to. Assign unique labels to
% multiple neurons on the same electrode.
% Initialize output structure
spikesPrefix = process_spikesorting_supervised('GetSpikesEventPrefix');
uniqueClusters = unique(clu(2:end))'; % The first entry is just the number of clusters
for iCluster = 1:length(uniqueClusters)
disp(num2str(iCluster))
selectedSpikes = find(clu==uniqueClusters(iCluster));
[tmp,iMaxFeature] = max(sum(abs(fet(selectedSpikes,1:end-3))));
iElectrode = ceil(iMaxFeature/3);
index = index+1;
% Write the packet to events
if uniqueClusters(iCluster)==0
events(index).label = ['Spikes Noise ' montages{iMontage} ' |' num2str(uniqueClusters(iCluster)) '|'];
elseif uniqueClusters(iCluster)==1
events(index).label = ['Spikes MUA ' montages{iMontage} ' |' num2str(uniqueClusters(iCluster)) '|'];
else
events(index).label = [spikesPrefix ' ' ChannelsInMontage(iElectrode).Name ' |' num2str(uniqueClusters(iCluster)) '|'];
end
events(index).color = rand(1,3);
events(index).times = fet(selectedSpikes,end)' ./ sProcess.options.fs.Value{1}(1); % The timestamps are in SAMPLES
events(index).epochs = ones(1,length(events(index).times));
events(index).reactTimes = [];
events(index).select = 1;
events(index).channels = cell(1, size(events(index).times, 2));
events(index).notes = cell(1, size(events(index).times, 2));
end
end
%% Attach the events to the raw file
% Load the raw file descriptor
isRaw = strcmpi(sInput.FileType, 'raw');
if isRaw
DataMat = in_bst_data(sInput.FileName, 'F');
sFile = DataMat.F;
else
sFile = in_fopen(sInput.FileName, 'BST-DATA');
end
newEvents = events;
% Fix events structure
if ~isempty(newEvents)
newEvents = struct_fix_events(newEvents);
end
if ~isempty(sFile.events)
sFile.events = struct_fix_events(sFile.events);
end
%% ===== MERGE EVENTS LISTS =====
% Add each new event
for iNew = 1:length(newEvents)
% Look for an existing event
if ~isempty(sFile.events)
iEvt = find(strcmpi(newEvents(iNew).label, {sFile.events.label}));
else
iEvt = [];
end
% Make sure that the sample indices are round values
newEvents(iNew).times = round(newEvents(iNew).times * sFile.prop.sfreq) ./ sFile.prop.sfreq;
% If event does not exist yet: add it at the end of the list
if isempty(iEvt)
if isempty(sFile.events)
iEvt = 1;
sFile.events = newEvents(iNew);
else
iEvt = length(sFile.events) + 1;
sFile.events(iEvt) = newEvents(iNew);
end
% Event exists: merge occurrences
else
% Convert new event type if required
sizeTimeWindow = size(sFile.events(iEvt).times, 1);
sizeNewTimeWindow = size(newEvents(iNew).times, 1);
if sizeTimeWindow ~= sizeNewTimeWindow
if sizeTimeWindow == 1
% Convert to single event
disp(['BST> Warning: Event type of "', ...
sFile.events(iEvt).label, ...
'" inconsistent, converting to single event using start time.']);
newEvents(iNew).times = newEvents(iNew).times(1,:);
else
% Convert to extended event
disp(['BST> Warning: Event type of "', ...
sFile.events(iEvt).label, ...
'" inconsistent, converting to extended event.']);
newEvents(iNew).times = [newEvents(iNew).times; newEvents(iNew).times + 0.001];
end
end
% Merge events occurrences
sFile.events(iEvt).times = [sFile.events(iEvt).times, newEvents(iNew).times];
sFile.events(iEvt).epochs = [sFile.events(iEvt).epochs, newEvents(iNew).epochs];
sFile.events(iEvt).reactTimes = [sFile.events(iEvt).reactTimes, newEvents(iNew).reactTimes];
sFile.events(iEvt).channels = [sFile.events(iEvt).channels, newEvents(iNew).channels];
sFile.events(iEvt).notes = [sFile.events(iEvt).notes, newEvents(iNew).notes];
% Sort by time
if (size(sFile.events(iEvt).times, 2) > 1)
[tmp__, iSort] = unique(bst_round(sFile.events(iEvt).times(1,:), 9));
sFile.events(iEvt).times = sFile.events(iEvt).times(:,iSort);
sFile.events(iEvt).epochs = sFile.events(iEvt).epochs(iSort);
if ~isempty(sFile.events(iEvt).reactTimes)
sFile.events(iEvt).reactTimes = sFile.events(iEvt).reactTimes(iSort);
end
sFile.events(iEvt).channels = sFile.events(iEvt).channels(iSort);
sFile.events(iEvt).notes = sFile.events(iEvt).notes(iSort);
end
end
% Add color if does not exist yet
if isempty(sFile.events(iEvt).color)
% Get the default color for this new event
% sFile.events(iEvt).color = panel_record('GetNewEventColor', iEvt, sFile.events);
% Same code, but without dependencies
AllEvents = sFile.events;
ColorTable = ...
[0 1 0
.4 .4 1
1 .6 0
0 1 1
.56 .01 .91
0 .5 0
.4 0 0
1 0 1
.02 .02 1
.5 .5 .5];
% Attribute the first color that of the colortable that is not in the existing events
for iColor = 1:length(ColorTable)
if isempty(AllEvents) || ~isstruct(AllEvents) || ~any(cellfun(@(c)isequal(c, ColorTable(iColor,:)), {AllEvents.color}))
break;
end
end
% If all the colors of the color table are taken: attribute colors cyclically
if (iColor == length(ColorTable))
iColor = mod(iEvt-1, length(ColorTable)) + 1;
end
sFile.events(iEvt).color = ColorTable(iColor,:);
end
end
%%
% Only save changes if something was change
if ~isempty(newEvents)
% Report changes in .mat structure
if isRaw
DataMat.F = sFile;
else
DataMat.Events = sFile.events;
end
% Save file definition
bst_save(file_fullpath(sInput.FileName), DataMat, 'v6', 1);
% Report number of detected events
bst_report('Info', sProcess, sInput, sprintf('Added to file: %d events in %d different categories', size([newEvents.epochs],2), length(newEvents)));
else
bst_report('Error', sProcess, sInput, 'No events read from file.');
end
% Return all the files in input
OutputFiles = {sInput.FileName};
end
%% ===== CONVERT =====
function Compute(ChannelFile)
% Get channel file
[sStudy, iStudy] = bst_get('ChannelFile', ChannelFile);
% Load channel file
ChannelFile = file_fullpath(ChannelFile);
ChannelMat = in_bst_channel(ChannelFile);
% Get channels classified as EEG
iEEG = channel_find(ChannelMat.Channel, 'EEG,SEEG,ECOG,ECG,EKG');
% If there are no channels classified at EEG, take all the channels
if isempty(iEEG)
warning('Warning: No EEG channels identified, trying to use all the channels...');
iEEG = 1:length(ChannelMat.Channel);
end
% Detect channels of interest
[iSelEeg, iEcg] = ImaGIN_select_channels({ChannelMat.Channel(iEEG).Name}, 1);
% Set channels as SEEG
if ~isempty(iSelEeg)
[ChannelMat.Channel(iEEG(iSelEeg)).Type] = deal(Modality);
end
if ~isempty(iEcg)
[ChannelMat.Channel(iEEG(iEcg)).Type] = deal('ECG');
end
% Save modified file
bst_save(ChannelFile, ChannelMat, 'v7');
% Update database reference
[sStudy.Channel.Modalities, sStudy.Channel.DisplayableSensorTypes] = channel_get_modalities(ChannelMat.Channel);
bst_set('Study', iStudy, sStudy);
end