Skip to content

Commit

Permalink
Merge pull request #425 from CerenB/cer-bidsConcatFix
Browse files Browse the repository at this point in the history
bidsConcatBetaTmaps. fix
  • Loading branch information
Remi-Gau authored Nov 1, 2021
2 parents f58b6f1 + ccf3256 commit 56746e1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 15 deletions.
44 changes: 31 additions & 13 deletions src/workflows/bidsConcatBetaTmaps.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)
% :param funcFWHM: smoothing (FWHM) applied to the the normalized EPI
% :type funcFWHM: (scalar)
% :param deleteIndBeta: decide to delete beta-maps
% :type funcFWHM: (boolean)
% :type deleteIndBeta: (boolean)
% :param deleteIndTmaps: decide to delete t-maps
% :type funcFWHM: (boolean)
% :type deleteIndTmaps: (boolean)
%
% When concatenating betamaps:
%
Expand All @@ -22,12 +22,21 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)
%
% (C) Copyright 2019 CPP_SPM developers

if nargin < 3
deleteIndBeta = 1;
deleteIndTmaps = 1;
[~, opt] = setUpWorkflow(opt, 'merge beta images and t-maps');

% TODO temporary check: will be removed on the dev branch
if ~isfield(opt, 'dryRun')
opt.dryRun = false;
end

[~, opt] = setUpWorkflow(opt, 'merge beta images and t-maps');
if nargin < 3
deleteIndBeta = true;
deleteIndTmaps = true;
end
if opt.dryRun
deleteIndBeta = false;
deleteIndTmaps = false;
end

RT = 0;

Expand All @@ -41,21 +50,25 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)

load(fullfile(ffxDir, 'SPM.mat'));

contrasts = specifyContrasts(ffxDir, opt.taskName, opt);
model = spm_jsonread(opt.model.file);

contrasts = specifyContrasts(SPM, opt.taskName, model);

beta_maps = cell(length(contrasts), 1);
t_maps = cell(length(contrasts), 1);

% path to beta and t-map files.
fprintf(1, '\nConcatenating the following contrasts:');
for iContrast = 1:length(beta_maps)

fprintf(1, '\n\t%s', contrasts(iContrast).name);
betasIndices = find(contrasts(iContrast).C);

if numel(betasIndices) > 1
error('Supposed to concatenate one beta image per contrast.');
end

% for this beta iamge we identify
% for this beta image we identify
% - which run it came from
% - the exact condition name stored in the SPM.mat
% so they can be saved in a tsv for for "label" and "fold" for MVPA
Expand All @@ -70,7 +83,6 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)
fileName = validationInputFile(ffxDir, fileName);
beta_maps{iContrast, 1} = [fileName, ',1'];

% while the contrastes (t-maps) are not from the index. They were created
fileName = sprintf('spmT_%04d.nii', iContrast);
fileName = validationInputFile(ffxDir, fileName);
t_maps{iContrast, 1} = [fileName, ',1'];
Expand All @@ -81,15 +93,17 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)
nameStructure = struct( ...
'ext', '.tsv', ...
'suffix', 'labelfold', ...
'entities' struct('sub', subLabel, ...
'task', opt.taskName, ...
'space', opt.space));
'entities', struct('sub', subLabel, ...
'task', opt.taskName, ...
'space', opt.space));
nameStructure.use_schema = false;
tsvName = bids.create_filename(nameStructure);

tsvContent = struct('folds', runs, 'labels', {conditions});

spm_save(fullfile(ffxDir, tsvName), tsvContent);

% TODO in the dev branch make those output filenames "BIDS derivatives" compliant
% beta maps
outputName = ['4D_beta_', num2str(funcFWHM), '.nii'];

Expand All @@ -101,7 +115,10 @@ function bidsConcatBetaTmaps(opt, funcFWHM, deleteIndBeta, deleteIndTmaps)

matlabbatch = setBatch3Dto4D(matlabbatch, t_maps, RT, outputName);

saveAndRunWorkflow(matlabbatch, 'concat_betaImg_tMaps', opt, subLabel);
% TODO temporary: remove on dev branch
if ~opt.dryRun
saveAndRunWorkflow(matlabbatch, 'concat_betaImg_tMaps', opt, subLabel);
end

removeBetaImgTmaps(t_maps, deleteIndBeta, deleteIndTmaps, ffxDir);

Expand Down Expand Up @@ -136,6 +153,7 @@ function removeBetaImgTmaps(t_maps, deleteIndBeta, deleteIndTmaps, ffxDir)
delete(t_maps{iTmap}(1:end - 2));
end
fprintf('Done. \n\n\n ');

end

end
9 changes: 8 additions & 1 deletion tests/createDummyDataSet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@ do
touch $ThisDir/mask.nii

touch $ThisDir/spmT_0001.nii
touch $ThisDir/spmT_0002.nii
touch $ThisDir/spmT_0002.nii
touch $ThisDir/spmT_0003.nii
touch $ThisDir/spmT_0004.nii

touch $ThisDir/beta_0001.nii
touch $ThisDir/beta_0002.nii
touch $ThisDir/beta_0003.nii
touch $ThisDir/beta_0004.nii

touch $ThisDir/con_0001.nii
touch $ThisDir/con_0002.nii
Expand Down
16 changes: 16 additions & 0 deletions tests/dummyData/models/model-vismotionMVPA_smdl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Name": "vismotion",
"Description": "This model only has simple contrasts for MVPA",
"Input": {
"task": "vismotion"
},
"Steps": [
{
"Level": "subject",
"AutoContrasts": [
"trial_type.VisMot",
"trial_type.VisStat"
]
}
]
}
33 changes: 33 additions & 0 deletions tests/test_bidsConcatBetaTmaps.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function test_suite = test_bidsConcatBetaTmaps %#ok<*STOUT>
%
% (C) Copyright 2019 CPP_SPM developers
try % assignment of 'localfunctions' is necessary in Matlab >= 2016
test_functions = localfunctions(); %#ok<*NASGU>
catch % no problem; early Matlab versions can use initTestSuite fine
end
initTestSuite;
end

function test_bidsConcatBetaTmapsBasic()

subLabel = '01';
funcFWHM = 6;

opt = setOptions('vismotion', subLabel);
opt.dryRun = true;

opt.model.file = spm_file(opt.model.file, 'filename', 'model-vismotionMVPA_smdl.json');

bidsConcatBetaTmaps(opt, funcFWHM);

ffxDir = getFFXdir(subLabel, funcFWHM, opt);
content = bids.util.tsvread(fullfile(ffxDir, ...
'sub-01_task-vismotion_space-MNI_labelfold.tsv'));

expectedContent = struct('labels', {{'VisMot*bf(1)'; 'VisStat*bf(1)'}}, 'folds', [1; 1]);

assertEqual(content, expectedContent);

delete(fullfile(ffxDir, 'sub-01_task-vismotion_space-MNI_labelfold.tsv'));

end
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.2
v1.1.3

0 comments on commit 56746e1

Please sign in to comment.