Skip to content

Commit b8bba00

Browse files
committed
meg compatibility and loading fiducials from coordsystem file
1 parent b6e52bb commit b8bba00

File tree

3 files changed

+135
-5
lines changed

3 files changed

+135
-5
lines changed

bids_export_example2.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@
175175

176176
% Task information for xxxx-eeg.json file
177177
% ---------------------------------------
178-
tInfo.InstitutionAddress = 'Pavillon Baudot CHU Purpan, BP 25202, 31052 Toulouse Cedex';
179-
tInfo.InstitutionName = 'Paul Sabatier Universite';
180-
tInfo.InstitutionalDepartmentName = 'Centre de Recherche Cerveau et Cognition';
178+
tInfo.InstitutionAddress = '';
179+
tInfo.InstitutionName = '';
180+
tInfo.InstitutionalDepartmentName = '';
181181
tInfo.PowerLineFrequency = 50;
182182
tInfo.ManufacturersModelName = 'Biosemi Active 2';
183183

bids_getinfofromfolder.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
sessions = union(sessions, sessionTmp);
4646
runs = union(runs , runsTmp);
4747
else
48-
if ~isempty(strfind(files(iFile).name, 'eeg')) && ~isempty(strfind(files(iFile).name, 'task'))
48+
if (~isempty(strfind(files(iFile).name, 'eeg')) || ~isempty(strfind(files(iFile).name, 'meg'))) && ~isempty(strfind(files(iFile).name, 'task'))
4949
pos = strfind(files(iFile).name, 'task');
5050
tmpStr = files(iFile).name(pos+5:end);
5151
underS = find(tmpStr == '_');
5252
newTask = tmpStr(1:underS(1)-1);
5353
tasklist = union( tasklist, { newTask });
5454
end
55-
if ~isempty(strfind(files(iFile).name, 'eeg')) && ~isempty(strfind(files(iFile).name, 'run'))
55+
if (~isempty(strfind(files(iFile).name, 'eeg')) || ~isempty(strfind(files(iFile).name, 'meg'))) && ~isempty(strfind(files(iFile).name, 'run'))
5656
pos = strfind(files(iFile).name, 'run');
5757
tmpStr = files(iFile).name(pos+4:end);
5858
underS = find(tmpStr == '_');

eeg_importcoordsystemfiles.m

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
% eeg_importcoordsystemfiles - import coordinate information
2+
%
3+
% Usage:
4+
% [EEG, bids] = eeg_importcoordsystemfiles(EEG, coordfile, 'key', value)
5+
%
6+
% Inputs:
7+
% 'EEG' - [struct] the EEG structure to which event information will be imported
8+
% coordfile - [string] path to the coordsystem.json file.
9+
% e.g. ~/BIDS_EXPORT/sub-01/ses-01/eeg/sub-01_ses-01_task-GoNogo_coordsystem.json
10+
%
11+
% Optional inputs:
12+
% 'bids' - [struct] structure that saves imported BIDS information. Default is []
13+
%
14+
% Outputs:
15+
% EEG - [struct] the EEG structure with event info imported
16+
% bids - [struct] structure that saves BIDS information with event information
17+
%
18+
% Authors: Arnaud Delorme, 2022
19+
20+
function [EEG, bids] = eeg_importcoordsystemfiles(EEG, coordfile, varargin)
21+
22+
if nargin < 2
23+
help eeg_importcoordsystemfiles;
24+
return;
25+
end
26+
27+
g = finputcheck(varargin, {'bids' 'struct' [] struct([]) }, 'eeg_importcoordsystemfiles', 'ignore');
28+
if isstr(g), error(g); end
29+
30+
bids = g.bids;
31+
32+
% coordinate information
33+
bids(1).coordsystem = loadfile( coordfile, '');
34+
35+
if isfield(bids.coordsystem, 'AnatomicalLandmarkCoordinates') && ~isempty(bids.coordsystem.AnatomicalLandmarkCoordinates)
36+
fieldNames = fieldnames(bids.coordsystem.AnatomicalLandmarkCoordinates);
37+
if ~isfield(EEG.chaninfo, 'nodatchans')
38+
EEG.chaninfo.nodatchans = [];
39+
end
40+
for iField = 1:length(fieldNames)
41+
EEG.chaninfo.nodatchans(end+1).labels = fieldNames{iField};
42+
EEG.chaninfo.nodatchans(end).X = bids.coordsystem.AnatomicalLandmarkCoordinates.(fieldNames{iField})(1);
43+
EEG.chaninfo.nodatchans(end).Y = bids.coordsystem.AnatomicalLandmarkCoordinates.(fieldNames{iField})(2);
44+
EEG.chaninfo.nodatchans(end).Z = bids.coordsystem.AnatomicalLandmarkCoordinates.(fieldNames{iField})(3);
45+
end
46+
EEG.chaninfo.nodatchans = convertlocs(EEG.chaninfo.nodatchans);
47+
end
48+
49+
% import JSON or TSV file
50+
% -----------------------
51+
function data = loadfile(localFile, globalFile)
52+
[~,~,ext] = fileparts(localFile);
53+
data = [];
54+
localFile = dir(localFile);
55+
if ~isempty(localFile)
56+
if strcmpi(ext, '.tsv')
57+
data = importtsv( fullfile(localFile(1).folder, localFile(1).name));
58+
else
59+
if exist('jsondecode.m','file')
60+
data = jsondecode( importalltxt( fullfile(localFile(1).folder, localFile(1).name) ));
61+
else
62+
data = jsonread(fullfile(localFile(1).folder, localFile(1).name));
63+
end
64+
end
65+
elseif ~isempty(globalFile)
66+
if strcmpi(ext, '.tsv')
67+
data = importtsv( fullfile(globalFile(1).folder, globalFile(1).name));
68+
else
69+
if exist('jsondecode.m','file')
70+
data = jsondecode( importalltxt( fullfile(globalFile(1).folder, globalFile(1).name) ));
71+
else
72+
data = jsonread(fullfile(globalFile(1).folder, globalFile(1).name));
73+
end
74+
end
75+
end
76+
77+
% Import full text file
78+
% ---------------------
79+
function str = importalltxt(fileName)
80+
81+
str = [];
82+
fid =fopen(fileName, 'r');
83+
while ~feof(fid)
84+
str = [str 10 fgetl(fid) ];
85+
end
86+
str(1) = [];
87+
88+
function nameout = cleanvarname(namein)
89+
90+
% Routine to remove unallowed characters from strings
91+
% nameout can be use as a variable or field in a structure
92+
93+
% custom change
94+
if strcmp(namein,'#')
95+
namein = 'nb';
96+
end
97+
98+
% 1st char must be a letter
99+
for l=1:length(namein)
100+
if isletter(namein(l))
101+
nameout = namein(l:end);
102+
break % exist for loop
103+
end
104+
end
105+
106+
% remove usual suspects
107+
stringcheck = [strfind(namein,'('), ...
108+
strfind(namein,')') ...
109+
strfind(namein,'[') ...
110+
strfind(namein,']') ...
111+
strfind(namein,'{') ...
112+
strfind(namein,'}') ...
113+
strfind(namein,'-') ...
114+
strfind(namein,'+') ...
115+
strfind(namein,'*') ...
116+
strfind(namein,'/') ...
117+
strfind(namein,'#') ...
118+
strfind(namein,'%') ...
119+
strfind(namein,'&') ...
120+
strfind(namein,'@') ...
121+
];
122+
123+
if ~isempty(stringcheck)
124+
nameout(stringcheck) = [];
125+
end
126+
127+
% last check
128+
if ~isvarname(nameout)
129+
error('the variable name to use is still invalid, check chars to remove')
130+
end

0 commit comments

Comments
 (0)