Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Jul 28, 2024
1 parent 51ceec6 commit 0e0cafe
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 41 deletions.
23 changes: 10 additions & 13 deletions src/batches/stats/setBatchFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
return
end

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

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

Expand Down Expand Up @@ -71,13 +71,11 @@
groupColumnHdr = setxor(designMatrix, {'1'});
groupColumnHdr = groupColumnHdr{1};

checkColumnParticipantsTsv(BIDS, groupColumnHdr);

% Sorting is important so that we know in which order
% the groups are entered in the design matrix.
% Otherwise it will be harder to properly design
% the contrast vectors later.
availableGroups = sort(unique(BIDS.raw.participants.content.(groupColumnHdr)));
availableGroups = getAvailableGroups(opt, groupColumnHdr);

label = '1WayANOVA';

Expand All @@ -103,7 +101,8 @@
% Note that this will lead to different results
% depending on the requested subejcts
%
tsv = BIDS.raw.participants.content;
tsvFile = fullfile(opt.dir.raw, 'participants.tsv');
tsv = bids.util.tsvread(tsvFile);
subjectsInGroup = strcmp(tsv.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(tsv.participant_id(subjectsInGroup), '^sub-', '');
subjectsLabel = intersect(subjectsLabel, opt.subjects);
Expand Down Expand Up @@ -180,7 +179,7 @@
contrastsList = {};
groups = {};

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

contrasts = getContrastsListForFactorialDesign(opt, nodeName);

Expand All @@ -191,9 +190,7 @@
groupColumnHdr = setxor(groupBy, {'contrast'});
groupColumnHdr = groupColumnHdr{1};

checkColumnParticipantsTsv(BIDS, groupColumnHdr);

availableGroups = unique(BIDS.raw.participants.content.(groupColumnHdr));
availableGroups = getAvailableGroups(opt, groupColumnHdr);

for iGroup = 1:numel(availableGroups)

Expand All @@ -205,7 +202,8 @@
% Note that this will lead to different results depending on the requested
% subejcts
%
tsv = BIDS.raw.participants.content;
tsvFile = fullfile(opt.dir.raw, 'participants.tsv');
tsv = bids.util.tsvread(tsvFile);
subjectsInGroup = strcmp(tsv.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(tsv.participant_id(subjectsInGroup), '^sub-', '');
subjectsLabel = intersect(subjectsLabel, opt.subjects);
Expand Down Expand Up @@ -352,14 +350,13 @@

function [status, groupBy, glmType] = checks(opt, nodeName)

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

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);

status = opt.model.bm.validateGroupBy(nodeName, columns);

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

Expand Down
9 changes: 5 additions & 4 deletions src/batches/stats/setBatchTwoSampleTTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
%%
printBatchName('specify group level paired T-test fmri model', opt);

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

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

Expand All @@ -67,7 +67,8 @@
group2 = group2{2};

% TODO refactor
availableGroups = unique(BIDS.raw.participants.content.(groupField));
availableGroups = getAvailableGroups(opt, groupField);
participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));

if any(~ismember({group1, group2}, availableGroups))
error(['Some requested group is not present: %s.', ...
Expand Down Expand Up @@ -103,8 +104,8 @@

subLabel = opt.subjects{iSub};

idx = strcmp(BIDS.raw.participants.content.participant_id, ['sub-' subLabel]);
participantGroup = BIDS.raw.participants.content.(groupField){idx};
idx = strcmp(participants.participant_id, ['sub-' subLabel]);
participantGroup = participants.(groupField){idx};

if numel(contrastsList) == 1
file = conImages{iSub};
Expand Down
20 changes: 20 additions & 0 deletions src/bids/getAvailableGroups.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function availableGroups = getAvailableGroups(opt, groupColumnHdr)

% (C) Copyright 2024 bidspm developers
tsvFile = fullfile(opt.dir.raw, 'participants.tsv');

assert(exist(tsvFile, 'file') == 2);

participants = bids.util.tsvread(tsvFile);

columns = fieldnames(participants);
if ~ismember(groupColumnHdr, columns)
msg = sprintf(['Could not find column "%s" in participants.tsv.\n', ...

Check warning on line 12 in src/bids/getAvailableGroups.m

View check run for this annotation

Codecov / codecov/patch

src/bids/getAvailableGroups.m#L12

Added line #L12 was not covered by tests
'Available columns are: %s'], ...
columnHdr, ...
bids.internal.create_unordered_list(columns));
logger('ERROR', msg, 'filename', mfilename, 'id', 'missingColumn');

Check warning on line 16 in src/bids/getAvailableGroups.m

View check run for this annotation

Codecov / codecov/patch

src/bids/getAvailableGroups.m#L16

Added line #L16 was not covered by tests
end

availableGroups = sort(unique(participants.(groupColumnHdr)));
end
9 changes: 3 additions & 6 deletions src/stats/group_level/groupLevelGlmType.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
% TODO refactor
columns = fieldnames(participants);

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

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

groupBy = node.GroupBy;
srcDesignMatrix = node.Model.X;

type = 'unknown';
if isnumeric(designMatrix) && designMatrix == 1
if isnumeric(srcDesignMatrix) && srcDesignMatrix == 1
type = 'one_sample_t_test';

elseif iscell(designMatrix) && numel(designMatrix) == 2
elseif iscell(srcDesignMatrix) && numel(srcDesignMatrix) == 2

designMatrix = cellfun(@(x) num2str(x), srcDesignMatrix, 'uniformoutput', false);

Expand Down
19 changes: 3 additions & 16 deletions src/workflows/stats/bidsFFX.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,10 @@ function checkRootNode(opt)
% This only concerns 'specify' and 'specifyAndEstimate'
%

thisNode = opt.model.bm.get_root_node;

if ismember(lower(thisNode.Level), {'session', 'subject'})

notImplemented(mfilename(), ...
'"session" and "subject" level Node not implemented yet');
opt.model.bm.validateRootNode();

elseif ismember(lower(thisNode.Level), {'dataset'})

msg = sprintf(['Your model seems to be having dataset Node at its root\n.', ...
'Validate it: https://bids-standard.github.io/stats-models/validator.html\n']);
id = 'wrongLevel';
logger('ERROR', msg, 'id', id, 'filename', mfilename());

end

checkGroupBy(thisNode);
thisNode = opt.model.bm.get_root_node;
opt.model.bm.validateGroupBy(thisNode);

% should not be necessary
% mostly in case users did not validate the model inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
function test_setBatchFactorialDesign_between_3_groups()

opt = setOptions('3_groups', '', 'pipelineType', 'stats');
opt.verbosity = 3;
opt.verbosity = 0;

matlabbatch = {};
[matlabbatch, ~, ~] = setBatchFactorialDesign(matlabbatch, ...
opt, ...
'between_groups');
matlabbatch;

end

0 comments on commit 0e0cafe

Please sign in to comment.