From 7ae2ed6353c22428ca20e780d3c53c9e055fdec5 Mon Sep 17 00:00:00 2001 From: WillForan Date: Sat, 28 Oct 2023 20:13:55 -0400 Subject: [PATCH 1/3] [BUG] only one el in BIDS.participants.meta --- spm_BIDS_App.m | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spm_BIDS_App.m b/spm_BIDS_App.m index f5af1bc..6665588 100644 --- a/spm_BIDS_App.m +++ b/spm_BIDS_App.m @@ -260,7 +260,13 @@ if ~isempty(BIDS.participants) idx = ismember(BIDS.participants.participant_id,BIDS_App.participants); for fn=fieldnames(BIDS.participants)' - BIDS.participants.(char(fn)) = BIDS.participants.(char(fn))(idx); + replace = BIDS.participants.(char(fn)); + % BIDS.participants.meta is 1x1. other fields are 1xN + if(length(replace) < length(idx)) + warning('%s: idx len %d > number of values %d; ignored', char(fn), length(idx), length(replace)) + continue + end + BIDS.participants.(char(fn)) = replace(idx); end end From cabf8a03b91ac7901ea7f9f5bc7010e1fbd8861d Mon Sep 17 00:00:00 2001 From: WillForan Date: Sat, 28 Oct 2023 21:44:58 -0400 Subject: [PATCH 2/3] [WIP] narrow_participants.m and test, run before copy --- narrow_participants.m | 25 +++++++++++++++++++++++++ spm_BIDS_App.m | 20 +++----------------- test_narrow_participants.m | 30 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 narrow_participants.m create mode 100644 test_narrow_participants.m diff --git a/narrow_participants.m b/narrow_participants.m new file mode 100644 index 0000000..01f9bcd --- /dev/null +++ b/narrow_participants.m @@ -0,0 +1,25 @@ +function BIDS = narrow_participants(BIDS, labels) +% NARROW_PARTICIPANTS - restrict BIDS struct to only requested labels +% + +% nothing to do if no labels to change? +if isempty(labels) + return +end + +idx = ismember({BIDS.subjects.name},labels); +BIDS.subjects = BIDS.subjects(idx); + +idx = ismember(BIDS.participants.participant_id,labels); +for fn=fieldnames(BIDS.participants)' + replace = BIDS.participants.(char(fn)); + % BIDS.participants.meta is 1x1. other fields are 1xN + if(length(replace) < length(idx)) + warning('BIDS participants field "%s" too short: ID idx len %d > number of field values %d; ignored', ... + char(fn), length(idx), length(replace)) + continue + end + BIDS.participants.(char(fn)) = replace(idx); +end + +end diff --git a/spm_BIDS_App.m b/spm_BIDS_App.m index 6665588..d83f038 100644 --- a/spm_BIDS_App.m +++ b/spm_BIDS_App.m @@ -201,6 +201,9 @@ spm('defaults','fmri'); spm_jobman('initcfg'); +% if we used --participant_label PARTICIPANT_LABEL [PARTICIPANT_LABEL ...] +BIDS = narrow_participants(BIDS, BIDS_App.participants); + %========================================================================== %-(Temporary) Copy of input data and uncompress image files %========================================================================== @@ -253,23 +256,6 @@ BIDS = spm_changepath(BIDS,'.nii.gz','.nii',false); end -%-Simplify BIDS structure to only contain participants under study -%-------------------------------------------------------------------------- -idx = ismember({BIDS.subjects.name},BIDS_App.participants); -BIDS.subjects = BIDS.subjects(idx); -if ~isempty(BIDS.participants) - idx = ismember(BIDS.participants.participant_id,BIDS_App.participants); - for fn=fieldnames(BIDS.participants)' - replace = BIDS.participants.(char(fn)); - % BIDS.participants.meta is 1x1. other fields are 1xN - if(length(replace) < length(idx)) - warning('%s: idx len %d > number of values %d; ignored', char(fn), length(idx), length(replace)) - continue - end - BIDS.participants.(char(fn)) = replace(idx); - end -end - %========================================================================== %-Analysis level: participant* %========================================================================== diff --git a/test_narrow_participants.m b/test_narrow_participants.m new file mode 100644 index 0000000..58049f6 --- /dev/null +++ b/test_narrow_participants.m @@ -0,0 +1,30 @@ +% +% script based testing for narrow_participants +% participants.json adds a 'meta' field that can be avoided when selecting only some participants +% + +bids_dir = [tempname '_bids_test']; +try + % build minimal BIDS filesystem: participants.json and 1 anat file pair + system(['mkdir -p ' bids_dir '/sub-{a,b}/ses-{1,2}/anat/']); + system([ 'touch ' bids_dir '/sub-{a,b}/ses-{1,2}/anat/T1.{json,nii.gz}']); + system(['echo -e ''participant_id\nsub-a\nsub-a\nsub-b\nsub-b'' > ' bids_dir '/participants.tsv']); + system(['echo ''{ "Name": "test", "BIDSVersion": "1.4.1"}'' > ' bids_dir '/dataset_description.json']); + system(['echo ''{ "age": { "Description": "xyz" } }'' > ' bids_dir '/participants.json']); + + % get what we built -- matches expectations + BIDS = spm_BIDS(bids_dir); + assert(length(BIDS.participants.participant_id) == 4) + + % narrowing works + BIDS_narrow = narrow_participants(BIDS, {'sub-a'}) + assert(length(BIDS_narrow.participants.participant_id) == 2) + + % no change on empty + BIDS_nochange = narrow_participants(BIDS, {}); % TODO: {{}}? + assert(length(BIDS_nochange.participants.participant_id) == 4) + +catch me + rmdir(bids_dir,'s') + rethrow(me) +end From a73da7c11f7a3a19d1fee636b8085d52524a5735 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Wed, 8 Nov 2023 13:39:06 +0100 Subject: [PATCH 3/3] Update narrow_participants.m --- narrow_participants.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/narrow_participants.m b/narrow_participants.m index 01f9bcd..d952e99 100644 --- a/narrow_participants.m +++ b/narrow_participants.m @@ -1,6 +1,6 @@ function BIDS = narrow_participants(BIDS, labels) % NARROW_PARTICIPANTS - restrict BIDS struct to only requested labels -% +% % nothing to do if no labels to change? if isempty(labels)