Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sccn/bids-matlab-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodelorme committed Aug 3, 2022
2 parents 4b2fa63 + fea021e commit c4831ad
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
21 changes: 17 additions & 4 deletions eeg_importeventsfiles.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
%
% 'eventtype' - [string] BIDS event column to be used as type in EEG.event. Default is 'value'
%
% 'usevislab' - [string] Whether to use VisLab's functions. Default 'off'
% Outputs:
%

Expand All @@ -34,8 +35,15 @@
function [EEG, bids, eventData, eventDesc] = eeg_importeventsfiles(EEG, eventfile, varargin)
g = finputcheck(varargin, {'eventDescFile' 'string' [] '';
'bids' 'struct' [] struct([]);
'eventtype' 'string' [] 'value' });
'eventtype' 'string' [] 'value' ;
'usevislab' 'string' { 'on' 'off'} 'off' }, 'eeg_importeventsfiles', 'ignore');
if isstr(g), error(g); end

if strcmpi(g.usevislab, 'on')
[EEG, bids, eventData, eventDesc] = eeg_importeventsfiles(EEG, eventfile, varargin{:}); %change to Kay's function
return;
end

bids = g.bids;

% ---------
Expand Down Expand Up @@ -89,15 +97,20 @@
% end
end
EEG.event = events;
EEG = eeg_checkset(EEG, 'makeur'); % add urevent
EEG = eeg_checkset(EEG, 'eventconsistency');
end

% import HED tags if exists
if ~isempty(g.eventDescFile)
if plugin_status('HEDTools')
fMap = fieldMap.createfMapFromJson(g.eventDescFile);
if fMap.hasAnnotation()
EEG.etc.tags = fMap.getStruct();
try
fMap = fieldMap.createfMapFromJson(g.eventDescFile);
if fMap.hasAnnotation()
EEG.etc.tags = fMap.getStruct();
end
catch ME
warning('Unable to import HED tags. Check your _events.json file');
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions eeg_writeelectrodesfiles.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function eeg_writeelectrodesfiles(EEG, fileOut)
end
end

if ~isTemplate && ~isempty(EEG.chanlocs) && isfield(EEG.chanlocs, 'X') && ~isempty(EEG.chanlocs(2).X)
if ~isTemplate && ~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');

Expand All @@ -40,4 +40,4 @@ function eeg_writeelectrodesfiles(EEG, fileOut)
coordsystemStruct.EEGCoordinateSystem = 'CTF'; % Change as soon as possible to EEGLAB
coordsystemStruct.EEGCoordinateSystemDescription = 'EEGLAB';
jsonwrite( [ fileOut '_coordsystem.json' ], coordsystemStruct);
end
end
36 changes: 29 additions & 7 deletions pop_importbids.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@
if isempty(res), return; end

if ~isempty(type_fields) && ~strcmpi(type_fields{res.typefield}, 'n/a'), options = { 'eventtype' type_fields{res.typefield} }; else options = {}; end
% options = { 'eventtype' type_fields{res.typefield} };
if res.events, options = { options{:} 'bidsevent' 'on' }; else options = { options{:} 'bidsevent' 'off' }; end
if res.chanlocs, options = { options{:} 'bidschanloc' 'on' }; else options = { options{:} 'bidschanloc' 'off' }; end
if ~isempty(res.folder), options = { options{:} 'outputdir' res.folder }; end
Expand Down Expand Up @@ -191,7 +190,7 @@
inconsistentChannels = 0;
inconsistentEvents = 0;
if isempty(opt.subjects)
opt.subjects = 2:size(bids.participants,1);
opt.subjects = 2:size(bids.participants,1); % indices into the participants.tsv file, ignoring first header row
else
opt.subjects = opt.subjects+1;
end
Expand Down Expand Up @@ -405,10 +404,14 @@
end
if exist([ eegFileRaw(1:end-8) '_events.json' ], 'file')
selected_eventdescfile = [ eegFileRaw(1:end-8) '_events.json' ];
elseif exist(fullfile(bidsFolder,[eegFileRaw(strfind(eegFileRaw,'task'):end-8) '_events.json' ]), 'file')
selected_eventdescfile = fullfile(bidsFolder,[eegFileRaw(strfind(eegFileRaw,'task'):end-8) '_events.json' ]);
elseif exist(eventDescFile, 'file')
selected_eventdescfile = eventDescFile;
elseif ~isempty(eventDescFile)
if ischar(eventDescFile) && exist(eventDescFile, 'file')
selected_eventdescfile = eventDescFile;
elseif isstruct(eventDescFile) && isfield(eventDescFile, 'folder') && isfield(eventDescFile, 'name') % sanity check
if exist(fullfile(eventDescFile.folder, eventDescFile.name), 'file')
selected_eventdescfile = fullfile(eventDescFile.folder, eventDescFile.name);
end
end
else
warning('No events.json found');
end
Expand Down Expand Up @@ -461,13 +464,14 @@
inconsistentChannels = inconsistentChannels+1;
end
end
%{
if ~isempty(bData.eventinfo)
if size(bData.eventinfo,1)-1 ~= length(bData.EEG.event)
fprintf(2, 'Warning: inconsistency detected, %d events in BIDS file vs %d in EEG file for %s\n', size(bData.eventinfo,1)-1, length(bData.EEG.event), [tmpFileName,fileExt]);
inconsistentEvents = inconsistentEvents+1;
end
end

%}
end % end for eegFileRaw
end
end
Expand Down Expand Up @@ -525,6 +529,24 @@
end
end

% import HED tags if exists in top level events.json
% -----------------------------
% scan for top level events.json
top_level_eventsjson = dir(fullfile(bidsFolder, '*_events.json'));
if ~isempty(top_level_eventsjson) && numel(top_level_eventsjson) == 1
top_level_eventsjson = fullfile(top_level_eventsjson.folder, top_level_eventsjson.name);
if plugin_status('HEDTools')
try
fMap = fieldMap.createfMapFromJson(top_level_eventsjson);
if fMap.hasAnnotation()
STUDY.etc.tags = fMap.getStruct();
end
catch ME
warning('Found top-level events.json file and tried importing HED tags but failed');
end
end
end

% check BIDS data field present
% -----------------------------
function res = checkBIDSfield(bids, fieldName)
Expand Down

0 comments on commit c4831ad

Please sign in to comment.