Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Jul 25, 2024
1 parent 40fc905 commit 7f0d048
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 32 deletions.
55 changes: 32 additions & 23 deletions src/batches/stats/setBatchFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
return
end

[BIDS, opt] = getData(opt, opt.dir.preproc);

printBatchName(sprintf('specify group level fmri model for node "%s"', nodeName), opt);

% now we fetch the contrast for each subject and allocate them in the batch
Expand All @@ -59,10 +61,10 @@
contrastsList = {};
groups = {};

[BIDS, opt] = getData(opt, opt.dir.preproc);

contrasts = getContrastsListForFactorialDesign(opt, nodeName);

% TODO refactor

designMatrix = opt.model.bm.get_design_matrix('Name', nodeName);
designMatrix = cellfun(@(x) num2str(x), designMatrix, 'uniformoutput', false);

Expand Down Expand Up @@ -176,6 +178,7 @@
node = opt.model.bm.get_nodes('Name', nodeName);
groupBy = node.GroupBy;

% TODO refactor
groupColumnHdr = setxor(groupBy, {'contrast'});
groupColumnHdr = groupColumnHdr{1};

Expand Down Expand Up @@ -344,39 +347,45 @@

commonMsg = sprintf('for the dataset level node: "%s"', nodeName);

% TODO refactor
participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
columns = fieldnames(participants);
status = checkGroupBy(thisNode, columns);

[glmType, ~, groupBy] = groupLevelGlmType(opt, nodeName, participants);

% only certain type of model supported for now
if ismember(glmType, {'unknown'})
% should probably exit if we are on 2 sample T test

msg = sprintf('Models other than group average /comparisons not implemented yet %s', commonMsg);
if ismember(glmType, {'unknown', 'two_sample_t_test'})
% 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);
notImplemented(mfilename(), msg, opt);
status = false;
return

Check warning on line 364 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L364

Added line #L364 was not covered by tests
end

datasetLvlContrasts = opt.model.bm.get_contrasts('Name', nodeName);
datasetLvlDummyContrasts = opt.model.bm.get_dummy_contrasts('Name', nodeName);

if isempty(datasetLvlContrasts) && isempty(datasetLvlDummyContrasts)
msg = sprintf('No contrast specified %s', commonMsg);
logger('WARNING', msg, 'id', id, 'filename', mfilename(), 'options', opt);

Check warning on line 372 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L371-L372

Added lines #L371 - L372 were not covered by tests
status = false;
return

Check warning on line 374 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L374

Added line #L374 was not covered by tests
end

if ismember(glmType, {'one_sample_t_test'}) && ...
(not(isempty(datasetLvlContrasts)) || isempty(datasetLvlDummyContrasts))
msg = sprintf('For one-sample t-test only DummyContrasts are implemented %s', commonMsg);

Check warning on line 379 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L379

Added line #L379 was not covered by tests
notImplemented(mfilename(), msg, opt);
status = false;

Check warning on line 381 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L381

Added line #L381 was not covered by tests
end

% datasetLvlDummyContrasts = opt.model.bm.get_dummy_contrasts('Name', nodeName);
% if isempty(datasetLvlDummyContrasts) || ~isfield(datasetLvlDummyContrasts, 'Test')
%
% msg = sprintf('Only DummyContrasts are implemented %s', commonMsg);
% notImplemented(mfilename(), msg, opt);
%
% status = false;
%
% end
%
% datasetLvlContrasts = opt.model.bm.get_contrasts('Name', nodeName);
% if ~isempty(datasetLvlContrasts)
%
% msg = sprintf('Contrasts are not yet implemented %s', commonMsg);
% notImplemented(mfilename(), msg, opt);
%
% end
if ismember(glmType, {'one_way_anova'}) && ...
(isempty(datasetLvlContrasts) || not(isempty(datasetLvlDummyContrasts)))
msg = sprintf('For one-way ANOVA only contrasts are yet implemented %s', commonMsg);
notImplemented(mfilename(), msg, opt);
status = false;

Check warning on line 388 in src/batches/stats/setBatchFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/batches/stats/setBatchFactorialDesign.m#L386-L388

Added lines #L386 - L388 were not covered by tests
end

end
1 change: 1 addition & 0 deletions src/batches/stats/setBatchGroupLevelContrasts.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

printBatchName('group level contrast estimation', opt);

% TODO refactor
participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
[groupGlmType, designMatrix, groupBy] = groupLevelGlmType(opt, nodeName, participants);

Expand Down
1 change: 1 addition & 0 deletions src/batches/stats/setBatchTwoSampleTTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
group1 = group1{2};
group2 = group2{2};

% TODO refactor
availableGroups = unique(BIDS.raw.participants.content.(groupField));

if any(~ismember({group1, group2}, availableGroups))
Expand Down
8 changes: 6 additions & 2 deletions src/bids_model/checkGroupBy.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%
% Only certain type of GroupBy supported for now for each level
%
% This helps doing some defensive programming
% This helps doing some defensive programming.
%

% (C) Copyright 2022 bidspm developers
Expand Down Expand Up @@ -53,19 +53,23 @@

case 'dataset'

supportedGroupBy = {'["contrast"]', '["contrast", "group"]'};
supportedGroupBy = { ...
'["contrast"]', ...
'["contrast", "x"] for "x" being a participant.tsv column name.'};

% only certain type of GroupBy supported for now
status = false;
if numel(groupBy) == 1 && all(ismember(lower(groupBy), {'contrast'}))
status = true;

elseif numel(groupBy) == 2 && iscellstr(extraVar) && numel(extraVar) > 0
for i = 1:numel(extraVar)
if all(ismember(groupBy, {'contrast', extraVar{i}}))
status = true;
break
end
end

end

end
Expand Down
8 changes: 6 additions & 2 deletions src/bids_model/getContrastsList.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function contrastsList = getContrastsList(model, node)
function contrastsList = getContrastsList(model, node, extraVar)
%
% Get list of names of Contrast from this Node
% or gets its from the previous Nodes
Expand All @@ -18,6 +18,10 @@

% (C) Copyright 2022 bidspm developers

if nargin < 3
extraVar = {};
end

contrastsList = {};

if ischar(node)
Expand All @@ -44,7 +48,7 @@
% TODO relax those assumptions

% assumptions
assert(checkGroupBy(node));
assert(checkGroupBy(node, extraVar));
assert(node.Model.X == 1);

contrastsList = getContrastsFromParentNode(model, node);
Expand Down
12 changes: 9 additions & 3 deletions src/bids_model/getContrastsListForFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
% (C) Copyright 2022 bidspm developers

% assuming we want to only average at the group level

% TODO refactor
participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
columns = fieldnames(participants);

[groupGlmType, ~, ~] = groupLevelGlmType(opt, nodeName, participants);

if ismember(groupGlmType, {'one_sample_t_test', 'one_way_anova'})

edge = opt.model.bm.get_edge('Destination', nodeName);
Expand All @@ -30,14 +35,14 @@
else

% this assumes DummyContrasts exist
contrastsList = getDummyContrastsList(opt.model.bm, nodeName);
contrastsList = getDummyContrastsList(opt.model.bm, nodeName, columns);

node = opt.model.bm.get_nodes('Name', nodeName);

% 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')
tmp = getContrastsList(opt.model.bm, nodeName);
tmp = getContrastsList(opt.model.bm, nodeName, columns);
for i = 1:numel(tmp)
contrastsList{end + 1} = tmp{i}.Name;
end
Expand All @@ -48,7 +53,8 @@
else

commonMsg = sprintf('for the dataset level node: "%s"', nodeName);
msg = sprintf('Models other than group average not implemented yet %s', commonMsg);
msg = sprintf(['Models other than group average / comparisons ', ...

Check warning on line 56 in src/bids_model/getContrastsListForFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/bids_model/getContrastsListForFactorialDesign.m#L56

Added line #L56 was not covered by tests
'not implemented yet %s'], commonMsg);
notImplemented(mfilename(), msg, opt);

contrastsList = {};

Check warning on line 60 in src/bids_model/getContrastsListForFactorialDesign.m

View check run for this annotation

Codecov / codecov/patch

src/bids_model/getContrastsListForFactorialDesign.m#L60

Added line #L60 was not covered by tests
Expand Down
8 changes: 6 additions & 2 deletions src/bids_model/getDummyContrastsList.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function dummyContrastsList = getDummyContrastsList(model, node)
function dummyContrastsList = getDummyContrastsList(model, node, extraVar)
%
% Get list of names of DummyContrast from this Node or gets its from the
% previous Nodes
Expand All @@ -18,6 +18,10 @@

% (C) Copyright 2022 bidspm developers

if nargin < 3
extraVar = {};
end

dummyContrastsList = {};

if ischar(node)
Expand All @@ -33,7 +37,7 @@

else

assert(checkGroupBy(node));
assert(checkGroupBy(node, extraVar));

switch lower(node.Level)

Expand Down
2 changes: 2 additions & 0 deletions src/stats/group_level/groupLevelGlmType.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
if nargin < 3
participants = struct();
end

% TODO refactor
columns = fieldnames(participants);

srcDesignMatrix = opt.model.bm.getBidsDesignMatrix('Name', nodeName);
Expand Down

0 comments on commit 7f0d048

Please sign in to comment.