From 953836b564f12554d6424180cd87ff45bd944673 Mon Sep 17 00:00:00 2001 From: Dung Truong Date: Tue, 2 Aug 2022 12:24:06 -0400 Subject: [PATCH 1/5] handle importing HED errors --- eeg_importeventsfiles.m | 21 +++++++++++++++++---- pop_importbids.m | 34 +++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/eeg_importeventsfiles.m b/eeg_importeventsfiles.m index 689584d..8145dc7 100644 --- a/eeg_importeventsfiles.m +++ b/eeg_importeventsfiles.m @@ -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: % @@ -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; % --------- @@ -85,15 +93,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 diff --git a/pop_importbids.m b/pop_importbids.m index ccc7f01..9385962 100644 --- a/pop_importbids.m +++ b/pop_importbids.m @@ -96,7 +96,7 @@ [~,~,~,res] = inputgui( 'geometry', geometry, 'geomvert', [1 0.5, 1 1 1 0.5 1], 'uilist', promptstr, 'helpcom', 'pophelp(''pop_importbids'')', 'title', 'Import BIDS data -- pop_importbids()'); if isempty(res), return; end - if ~isempty(type_fields), options = { 'eventtype' type_fields{res.typefield} }; else options = {}; end + if ~isempty(type_fields), 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 @@ -384,8 +384,14 @@ end if exist([ eegFileRaw(1:end-8) '_events.json' ], 'file') selected_eventdescfile = [ eegFileRaw(1: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 @@ -407,8 +413,8 @@ BIDS.gInfo.README = bids.README; BIDS.pInfo = [bids.participants(1,:); bids.participants(iSubject,:)]; % header -> iSubject info BIDS.pInfoDesc = bids.participantsJSON; - BIDS.eInfo = bids.eventInfo; - BIDS.eInfoDesc = bids.data.eventdesc; + %BIDS.eInfo = bids.eventInfo; + %BIDS.eInfoDesc = bids.data.eventdesc; BIDS.tInfo = infoData; EEG.BIDS = BIDS; @@ -438,13 +444,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 @@ -501,7 +508,20 @@ commands = sprintf('[STUDY, ALLEEG] = pop_importbids(''%s'');', bidsFolder); end end - +%{ +% import HED tags if exists +% ----------------------------- +if ~isempty(eventDescFile) + if plugin_status('HEDTools') + fMap = fieldMap.createfMapFromJson(eventDescFile); + if fMap.hasAnnotation() + STUDY.etc.tags = fMap.getStruct(); + % import STUDY design if exist + STUDY.design = hed2studydesign(fMap); + end + end +end +%} % check BIDS data field present % ----------------------------- function res = checkBIDSfield(bids, fieldName) From 660391eedb2faa26071a05602f91a0145ba633ff Mon Sep 17 00:00:00 2001 From: Dung Truong Date: Tue, 2 Aug 2022 13:07:14 -0400 Subject: [PATCH 2/5] import top-level HED tags to STUDY --- pop_importbids.m | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pop_importbids.m b/pop_importbids.m index 9385962..eb3369b 100644 --- a/pop_importbids.m +++ b/pop_importbids.m @@ -508,20 +508,25 @@ commands = sprintf('[STUDY, ALLEEG] = pop_importbids(''%s'');', bidsFolder); end end -%{ -% import HED tags if exists + +% import HED tags if exists in top level events.json % ----------------------------- -if ~isempty(eventDescFile) +% 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') - fMap = fieldMap.createfMapFromJson(eventDescFile); - if fMap.hasAnnotation() - STUDY.etc.tags = fMap.getStruct(); - % import STUDY design if exist - STUDY.design = hed2studydesign(fMap); + 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) From 57be88093cb7f185a7c79ab0ee40b2c9fe6d0e69 Mon Sep 17 00:00:00 2001 From: Dung Truong Date: Tue, 2 Aug 2022 13:09:17 -0400 Subject: [PATCH 3/5] re-enable importing event info into BIDS structure for EEG --- pop_importbids.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pop_importbids.m b/pop_importbids.m index eb3369b..9d58ad4 100644 --- a/pop_importbids.m +++ b/pop_importbids.m @@ -413,8 +413,8 @@ BIDS.gInfo.README = bids.README; BIDS.pInfo = [bids.participants(1,:); bids.participants(iSubject,:)]; % header -> iSubject info BIDS.pInfoDesc = bids.participantsJSON; - %BIDS.eInfo = bids.eventInfo; - %BIDS.eInfoDesc = bids.data.eventdesc; + BIDS.eInfo = bids.eventInfo; + BIDS.eInfoDesc = bids.data.eventdesc; BIDS.tInfo = infoData; EEG.BIDS = BIDS; From d47be92e73b788f35492eb14b606ce82db506d77 Mon Sep 17 00:00:00 2001 From: Dung Truong Date: Tue, 2 Aug 2022 10:17:13 -0700 Subject: [PATCH 4/5] check for empty electrode location --- eeg_writeelectrodesfiles.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eeg_writeelectrodesfiles.m b/eeg_writeelectrodesfiles.m index 79c6a7c..ef3a677 100644 --- a/eeg_writeelectrodesfiles.m +++ b/eeg_writeelectrodesfiles.m @@ -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'); @@ -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 \ No newline at end of file +end From fea021e79f564ed7557743a84e9d7ae4d68eb79e Mon Sep 17 00:00:00 2001 From: Dung Truong Date: Tue, 2 Aug 2022 16:52:51 -0400 Subject: [PATCH 5/5] add comment to code --- pop_importbids.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop_importbids.m b/pop_importbids.m index ee4d8ee..ebf8345 100644 --- a/pop_importbids.m +++ b/pop_importbids.m @@ -190,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