Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Jul 29, 2024
1 parent 0e0cafe commit b6aa23f
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 208 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/run_tests_cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ jobs:
coverage run --source src -m pytest
coverage xml
- name: Code coverage
uses: codecov/codecov-action@v4
with:
file: coverage.xml
flags: cli
name: codecov-cli
fail_ci_if_error: false
# - name: Code coverage
# uses: codecov/codecov-action@v4
# with:
# file: coverage.xml
# flags: cli
# name: codecov-cli
# fail_ci_if_error: false
2 changes: 1 addition & 1 deletion miss_hit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ tab_width: 2

# metrics limit for the code quality (https://florianschanda.github.io/miss_hit/metrics.html)
metric "cnest": limit 5
metric "file_length": limit 800
metric "file_length": limit 1000
metric "cyc": limit 22
metric "parameters": limit 7
48 changes: 18 additions & 30 deletions src/batches/stats/setBatchFactorialDesign.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,11 @@
% TODO all contrasts should have the same name
contrasts = getContrastsListForFactorialDesign(opt, nodeName);

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

groupColumnHdr = setxor(designMatrix, {'1'});
groupColumnHdr = groupColumnHdr{1};

% 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.
groupColumnHdr = opt.model.bm.getGroupColumnHdrFromDesignMatrix(nodeName);
availableGroups = getAvailableGroups(opt, groupColumnHdr);

label = '1WayANOVA';
Expand All @@ -101,10 +95,9 @@
% Note that this will lead to different results
% depending on the requested subejcts
%
tsvFile = fullfile(opt.dir.raw, 'participants.tsv');
tsv = bids.util.tsvread(tsvFile);
subjectsInGroup = strcmp(tsv.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(tsv.participant_id(subjectsInGroup), '^sub-', '');
participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
subjectsInGroup = strcmp(participants.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(participants.participant_id(subjectsInGroup), '^sub-', '');
subjectsLabel = intersect(subjectsLabel, opt.subjects);

% collect all con images from all subjects
Expand Down Expand Up @@ -183,13 +176,8 @@

contrasts = getContrastsListForFactorialDesign(opt, nodeName);

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

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

participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
groupColumnHdr = opt.model.bm.getGroupColumnHdrFromGroupBy(nodeName, participants);
availableGroups = getAvailableGroups(opt, groupColumnHdr);

for iGroup = 1:numel(availableGroups)
Expand All @@ -202,10 +190,8 @@
% Note that this will lead to different results depending on the requested
% subejcts
%
tsvFile = fullfile(opt.dir.raw, 'participants.tsv');
tsv = bids.util.tsvread(tsvFile);
subjectsInGroup = strcmp(tsv.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(tsv.participant_id(subjectsInGroup), '^sub-', '');
subjectsInGroup = strcmp(participants.(groupColumnHdr), thisGroup);
subjectsLabel = regexprep(participants.participant_id(subjectsInGroup), '^sub-', '');
subjectsLabel = intersect(subjectsLabel, opt.subjects);

% collect all con images from all subjects
Expand Down Expand Up @@ -301,6 +287,7 @@
% If 2 groups, then number of levels = 2
factorialDesign.des.fd.fact.name = thisGroup;
factorialDesign.des.fd.fact.levels = numel(icell);

factorialDesign.des.fd.fact.dept = 0;

% 1: Assumes that the variance is not the same across groups
Expand Down Expand Up @@ -338,11 +325,11 @@

factorialDesign.masking.tm.tm_none = 1;
factorialDesign.masking.im = 1;

factorialDesign.masking.em = {''};

factorialDesign.globalc.g_omit = 1;
factorialDesign.globalm.gmsca.gmsca_no = 1;
factorialDesign.globalm.glonorm = 1;
factorialDesign = setBatchFactorialDesignImplicitMasking(factorialDesign);
factorialDesign = setBatchFactorialDesignGlobalCalcAndNorm(factorialDesign);

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

Expand All @@ -354,11 +341,12 @@

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

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

status = model.validateGroupBy(nodeName, participants);

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

% only certain type of model supported for now
if ismember(glmType, {'unknown', 'two_sample_t_test'})
Expand All @@ -370,8 +358,8 @@
return
end

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

if isempty(datasetLvlContrasts) && isempty(datasetLvlDummyContrasts)
msg = sprintf('No contrast specified %s', commonMsg);
Expand Down
39 changes: 17 additions & 22 deletions src/batches/stats/setBatchGroupLevelContrasts.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@

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

% TODO refactor
model = opt.model.bm;

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

groupColumnHdr = model.getGroupColumnHdrFromGroupBy(nodeName, participants);
availableGroups = getAvailableGroups(opt, groupColumnHdr);

[groupGlmType, groupBy] = model.groupLevelGlmType(nodeName, participants);
switch groupGlmType

case 'one_sample_t_test'

contrastsList = getContrastsListForFactorialDesign(opt, nodeName);

if all(ismember(lower(groupBy), {'contrast'}))
if numel(groupBy) == 1 && ismember(lower(groupBy), {'contrast'})

for j = 1:numel(contrastsList)

Expand All @@ -48,11 +52,7 @@

end

% TODO make more general than just with group
elseif all(ismember(lower(groupBy), {'contrast', 'group'}))
% TODO make more general than just with group
groupColumnHdr = groupBy{ismember(lower(groupBy), {'group'})};
availableGroups = unique(participants.(groupColumnHdr));
elseif numel(groupBy) == 2 && any(ismember(groupBy, fieldnames(participants)))

for j = 1:numel(contrastsList)

Expand Down Expand Up @@ -80,14 +80,10 @@
% through the Edge filter.
% Then generate the between group contrasts.

designMatrix = removeIntercept(designMatrix);

groups = unique(participants.(designMatrix{1}));

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

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

for j = 1:numel(contrastsList)

Expand All @@ -102,14 +98,14 @@
% Sort conditions and weights
[ConditionList, I] = sort(thisContrast{iCon}.ConditionList);
for iCdt = 1:numel(ConditionList)
ConditionList{iCdt} = strrep(ConditionList{iCdt}, [designMatrix{1}, '.'], '');
ConditionList{iCdt} = strrep(ConditionList{iCdt}, [groupColumnHdr, '.'], '');
end
Weights = thisContrast{iCon}.Weights(I);

% Create contrast vectors by what was passed in the model
convec = zeros(size(groups));
for iGroup = 1:numel(groups)
index = strcmp(groups{iGroup}, ConditionList);
convec = zeros(size(availableGroups));
for iGroup = 1:numel(availableGroups)
index = strcmp(availableGroups{iGroup}, ConditionList);
if any(index)
convec(iGroup) = Weights(index);
end
Expand All @@ -128,17 +124,16 @@

designMatrix = removeIntercept(designMatrix);

% TODO make more general than just with group
if ismember(lower(designMatrix), {'group'})
if any(ismember(designMatrix, fieldnames(participants)))
% TODO will this ignore the contrasts define at other levels
% and not passed through the filter ?
edge = opt.model.bm.get_edge('Destination', nodeName);
edge = model.get_edge('Destination', nodeName);
contrastsList = edge.Filter.contrast;
end

for j = 1:numel(contrastsList)

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/batches/stats/setBatchSubjectLevelContrasts.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%
% USAGE::
%
% matlabbatch = setBatchSubjectLevelContrasts(matlabbatch, opt, subLabel, funcFWHM)
% matlabbatch = setBatchSubjectLevelContrasts(matlabbatch, opt, subLabel, nodeName)
%
% :param matlabbatch:
% :type matlabbatch: structure
Expand Down
3 changes: 1 addition & 2 deletions src/batches/stats/setBatchTwoSampleTTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@
group1 = group1{2};
group2 = group2{2};

% TODO refactor
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 All @@ -85,6 +83,7 @@
conImages{iSub} = findSubjectConImage(opt, subLabel, contrastsList);
end

participants = bids.util.tsvread(fullfile(opt.dir.raw, 'participants.tsv'));
% set up the batch
for iCon = 1:numel(contrastsList)

Expand Down
Loading

0 comments on commit b6aa23f

Please sign in to comment.