Skip to content

Commit

Permalink
fix and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Jul 29, 2024
1 parent e3e33bc commit 3918ad5
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 257 deletions.
24 changes: 10 additions & 14 deletions src/batches/stats/setBatchFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
assert(~checkSpmMat(rfxDir, opt));

if strcmp(glmType, 'two_sample_t_test')
factorialDesign = returnTwoSampleTTestBatch(opt, nodeName);
factorialDesign = returnTwoSampleTTestBatch(rfxDir);
else
factorialDesign = returnOneWayAnovaBatch(rfxDir);
end
Expand Down Expand Up @@ -267,7 +267,9 @@

assert(iscellstr(icell.scans));

matlabbatch = returnFactorialDesignBatch(matlabbatch, rfxDir, icell, thisGroup);
factorialDesign = returnFactorialDesignBatch(rfxDir, icell, thisGroup);

matlabbatch{end + 1}.spm.stats.factorial_design = factorialDesign;

mask = getInclusiveMask(opt, nodeName);
matlabbatch{end}.spm.stats.factorial_design.masking.em = {mask};
Expand All @@ -278,8 +280,7 @@
'before estimation')));
end

function matlabbatch = returnFactorialDesignBatch(matlabbatch, directory, icell, thisGroup)

function factorialDesign = returnFactorialDesignBatch(directory, icell, thisGroup)
if isempty(thisGroup)
thisGroup = 'GROUP';
end
Expand All @@ -294,13 +295,9 @@
% If 2 groups, then number of levels = 2
factorialDesign.des.fd.fact.name = thisGroup;
factorialDesign.des.fd.fact.levels = numel(icell);

matlabbatch{end + 1}.spm.stats.factorial_design = factorialDesign;

end

function factorialDesign = returnTwoSampleTTestBatch(directory)

factorialDesign = commonFaxtorialDesignBatch(directory);

factorialDesign.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});
Expand All @@ -310,11 +307,9 @@

factorialDesign.des.t2.scans1 = {};
factorialDesign.des.t2.scans2 = {};

end

function factorialDesign = returnOneWayAnovaBatch(directory)

factorialDesign = commonFaxtorialDesignBatch(directory);

factorialDesign.cov = struct('c', {}, 'cname', {}, 'iCFI', {}, 'iCC', {});
Expand All @@ -323,7 +318,6 @@
factorialDesign.des.anova = varianceStruct();

factorialDesign.des.anova.icell(1).scans = {};

end

function factorialDesign = commonFaxtorialDesignBatch(directory)
Expand Down Expand Up @@ -360,7 +354,7 @@
[glmType, groupBy] = bm.groupLevelGlmType(nodeName, participants);

% only certain type of model supported for now
if ismember(glmType, {'unknown', 'two_sample_t_test'})
if ismember(glmType, {'unknown'})
% TODO update message to with better info for 2 sample T-Test
msg = sprintf(['Models other than group average / comparisons ', ...
'not implemented yet %s'], commonMsg);
Expand All @@ -384,11 +378,13 @@
msg = sprintf('For one-sample t-test only DummyContrasts are implemented %s', commonMsg);
notImplemented(mfilename(), msg, opt);
status = false;
return
end

if ismember(glmType, {'one_way_anova'}) && ...
if ismember(glmType, {'one_way_anova', 'two_sample_t_test'}) && ...
(isempty(datasetLvlContrasts) || not(isempty(datasetLvlDummyContrasts)))
msg = sprintf('For one-way ANOVA only contrasts are yet implemented %s', commonMsg);
msg = sprintf(['For one-way ANOVA or 2 samples t-test ', ...
'only contrasts are implemented %s'], commonMsg);
notImplemented(mfilename(), msg, opt);
status = false;
end
Expand Down
27 changes: 1 addition & 26 deletions src/batches/stats/setBatchGroupLevelContrasts.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

end

case 'one_way_anova'
case {'two_sample_t_test', 'one_way_anova' }

% T test for ANOVA
%
Expand Down Expand Up @@ -123,31 +123,6 @@

end

case 'two_sample_t_test'

edge = bm.get_edge('Destination', nodeName);
contrastsList = edge.Filter.contrast;

for j = 1:numel(contrastsList)

thisContrast = bm.get_contrasts('Name', nodeName);

spmMatFile = fullfile(getRFXdir(opt, nodeName, contrastsList{j}), 'SPM.mat');

if ~opt.dryRun
assert(exist(spmMatFile, 'file') == 2);
end

for iCon = 1:numel(thisContrast)
consess{iCon}.tcon.name = thisContrast{iCon}.Name;
consess{iCon}.tcon.convec = thisContrast{iCon}.Weights;
consess{iCon}.tcon.sessrep = 'none';
end

matlabbatch = setBatchContrasts(matlabbatch, opt, spmMatFile, consess);

end

otherwise
msg = sprintf('Node %s has has model type I cannot handle.\n', nodeName);
notImplemented(mfilename(), msg, opt);
Expand Down
4 changes: 3 additions & 1 deletion src/bids_model/getContrastsListForFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@

% if no specific dummy contrasts mentioned also include all contrasts from previous levels
% or if contrasts are mentioned we grab them
if ~isfield(node.DummyContrasts, 'Contrasts') || isfield(node, 'Contrasts')
if ~isfield(node, 'DummyContrasts') || ...
~isfield(node.DummyContrasts, 'Contrasts') || ...
isfield(node, 'Contrasts')
tmp = getContrastsList(bm, nodeName, participants);
for i = 1:numel(tmp)
contrastsList{end + 1} = tmp{i}.Name;
Expand Down
2 changes: 1 addition & 1 deletion src/bids_model/getDummyContrastsList.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
end
end

if isfield(node.DummyContrasts, 'Contrasts')
if isfield(node, 'DummyContrasts') && isfield(node.DummyContrasts, 'Contrasts1')

dummyContrastsList = node.DummyContrasts.Contrasts;

Expand Down
4 changes: 2 additions & 2 deletions src/workflows/stats/bidsRFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@

participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));

matlabbatch = {};

for i = 1:numel(datasetNodes)

msg = sprintf('\n PROCESSING NODE: %s\n', nodeName);
logger('INFO', msg, 'options', opt, 'filename', mfilename());

matlabbatch = {};

nodeName = datasetNodes{i}.Name;

switch bm.groupLevelGlmType(nodeName, participants)
Expand Down
128 changes: 0 additions & 128 deletions tests/tests_slow/tests_workflows/stats/test_bidsRFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,104 +45,6 @@ function test_bidsRFX_rfx()

end

function test_bidsRFX_no_overwrite_smoke_test()

markTestAs('slow');

opt = setOptions('vislocalizer', '', 'pipelineType', 'stats');
opt.model.file = spm_file(opt.model.file, ...
'basename', ...
'model-vismotionNoOverWrite_smdl');
opt.model.bm = BidsModel('file', opt.model.file);

matlabbatch = bidsRFX('RFX', opt);

expectedNbBatch = 7;
if bids.internal.is_octave()
expectedNbBatch = 5;
end
assertEqual(numel(matlabbatch), expectedNbBatch);

end

function test_bidsRFX_within_group_ttest()

markTestAs('slow');

opt = setOptions('vislocalizer', '', 'pipelineType', 'stats');
opt.model.file = spm_file(opt.model.file, ...
'basename', ...
'model-vislocalizerWithinGroup_smdl');
opt.model.bm = BidsModel('file', opt.model.file);

matlabbatch = bidsRFX('RFX', opt);

if bids.internal.is_octave()
assertEqual(numel(matlabbatch), 10);
else
assertEqual(numel(matlabbatch), 14);
end

% creates 1 batch for
% - specify
% - figure
% - estimate
% - MACS: model space
% - MACS: information criteria
% - review
% - figure
% for each group

batchOrder = {'stats', 'factorial_design'; ...
'util', 'print'; ...
'stats', 'factorial_design'; ...
'util', 'print'};
batchOrder = extendBatchOrder(batchOrder);
batchOrder = extendBatchOrder(batchOrder);
summary = batchSummary(matlabbatch);
assertEqual(summary, batchOrder);

assertEqual(matlabbatch{1}.spm.stats.factorial_design.dir{1}, ...
fileparts(matlabbatch{5}.spm.stats.fmri_est.spmmat{1}));
if bids.internal.is_octave()
assertEqual(matlabbatch{3}.spm.stats.factorial_design.dir{1}, ...
fileparts(matlabbatch{8}.spm.stats.fmri_est.spmmat{1}));
else
assertEqual(matlabbatch{3}.spm.stats.factorial_design.dir{1}, ...
fileparts(matlabbatch{10}.spm.stats.fmri_est.spmmat{1}));
end

end

function test_bidsRFX_two_sample_ttest()

markTestAs('slow');

opt = setOptions('vislocalizer', '', 'pipelineType', 'stats');
opt.model.file = spm_file(opt.model.file, ...
'basename', ...
'model-vislocalizer2sampleTTest_smdl');
opt.model.bm = BidsModel('file', opt.model.file);

matlabbatch = bidsRFX('RFX', opt);

summary = batchSummary(matlabbatch);

% creates 1 batch for (specify, figure, estimate, review, figure)
batchOrder = {'stats', 'factorial_design'; ...
'util', 'print'};
batchOrder = extendBatchOrder(batchOrder);
assertEqual(summary, batchOrder);

assertEqual(matlabbatch{1}.spm.stats.factorial_design.dir{1}, ...
fileparts(matlabbatch{3}.spm.stats.fmri_est.spmmat{1}));

% 2 blind and 1 ctrl
assertEqual(numel(matlabbatch{1}.spm.stats.factorial_design.des.t2.scans1), 2);
assertEqual(numel(matlabbatch{1}.spm.stats.factorial_design.des.t2.scans2), 1);

end

function test_bidsRFX_select_datasets_level_to_run()

markTestAs('slow');
Expand All @@ -168,36 +70,6 @@ function test_bidsRFX_select_datasets_level_to_run()

end

function test_bidsRFX_several_datasets_level()

markTestAs('slow');

opt = setOptions('vislocalizer', '', 'pipelineType', 'stats');
opt.model.file = spm_file(opt.model.file, ...
'basename', ...
'model-vislocalizerSeveralDatasetLevels_smdl');
opt.model.bm = BidsModel('file', opt.model.file);

matlabbatch = bidsRFX('RFX', opt);

summary = batchSummary(matlabbatch);

% only the batches from the last node is returned
% creates 1 batch for (specify, figure, estimate, review, figure)
batchOrder = {'stats', 'factorial_design'; ...
'util', 'print'};
batchOrder = extendBatchOrder(batchOrder);
assertEqual(summary, batchOrder);

nbGroupLevelModelsReturned = 1;
nbBatchPerModel = 7;
if bids.internal.is_octave()
nbBatchPerModel = 5;
end
assertEqual(numel(matlabbatch), nbGroupLevelModelsReturned * nbBatchPerModel);

end

function test_bidsRFX_rfx_on_empty_dir()

markTestAs('slow');
Expand Down
Loading

0 comments on commit 3918ad5

Please sign in to comment.