-
Notifications
You must be signed in to change notification settings - Fork 16
/
bids_writeelectrodefile.m
62 lines (56 loc) · 2.56 KB
/
bids_writeelectrodefile.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
% BIDS_WRITEELECTRODEFILE - write electrodes.tsv and coordsystem.json from single EEG dataset
%
% Usage:
% bids_writeelectrodefile(EEG, fileOut, flagExport)
%
% Inputs:
% 'EEG' - [struct] the EEG structure
% 'fileOut' - [string] filepath of the desired output location with file basename
% e.g. ~/BIDS_EXPORT/sub-01/ses-01/eeg/sub-01_ses-01_task-GoNogo
%
% Optional inputs:
% 'Export' - ['on'|'off'|'auto']
%
% Authors: Dung Truong, Arnaud Delorme, 2022
function bids_writeelectrodefile(EEG, fileOut, varargin)
if nargin > 2
flagExport = varargin{2};
else
flagExport = 'auto';
end
if isfield(EEG.chaninfo, 'filename') && isequal(flagExport, 'auto')
templates = {'GSN-HydroCel-32.sfp', 'GSN65v2_0.sfp', 'GSN129.sfp', 'GSN-HydroCel-257.sfp', 'standard-10-5-cap385.elp', 'standard_1005.elc', 'standard_1005.ced'};
if any(contains(EEG.chaninfo.filename, templates))
flagExport = 'off';
disp('Template channel location detected, not exporting electrodes.tsv file');
end
end
if any(strcmp(flagExport, {'auto', 'on'})) && ~isempty(EEG.chanlocs) && isfield(EEG.chanlocs, 'X') && any(cellfun(@(x)~isempty(x), { EEG.chanlocs.X }))
fid = fopen( [ fileOut '_electrodes.tsv' ], 'w');
fprintf(fid, 'name\tx\ty\tz\n');
for iChan = 1:EEG.nbchan
if isempty(EEG.chanlocs(iChan).X) || isnan(EEG.chanlocs(iChan).X) || contains(fileOut, 'ieeg')
fprintf(fid, '%s\tn/a\tn/a\tn/a\n', EEG.chanlocs(iChan).labels );
else
fprintf(fid, '%s\t%2.6f\t%2.6f\t%2.6f\n', EEG.chanlocs(iChan).labels, EEG.chanlocs(iChan).X, EEG.chanlocs(iChan).Y, EEG.chanlocs(iChan).Z );
end
end
fclose(fid);
% Write coordinate file information (coordsystem.json)
if isfield(EEG.chaninfo, 'BIDS') && isfield(EEG.chaninfo.BIDS, 'EEGCoordinateUnits')
coordsystemStruct.EEGCoordinateUnits = EEG.chaninfo.BIDS.EEGCoordinateUnits;
else
coordsystemStruct.EEGCoordinateUnits = 'mm';
end
if isfield(EEG.chaninfo, 'BIDS') &&isfield(EEG.chaninfo.BIDS, 'EEGCoordinateSystem')
coordsystemStruct.EEGCoordinateSystem = EEG.chaninfo.BIDS.EEGCoordinateSystem;
else
coordsystemStruct.EEGCoordinateSystem = 'CTF';
end
if isfield(EEG.chaninfo, 'BIDS') &&isfield(EEG.chaninfo.BIDS, 'EEGCoordinateSystemDescription')
coordsystemStruct.EEGCoordinateSystemDescription = EEG.chaninfo.BIDS.EEGCoordinateSystemDescription;
else
coordsystemStruct.EEGCoordinateSystemDescription = 'EEGLAB';
end
jsonwrite( [ fileOut '_coordsystem.json' ], coordsystemStruct);
end