Skip to content

Commit

Permalink
feat: split register parcellation into two pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Aug 5, 2021
1 parent 12f4e94 commit 2afb919
Show file tree
Hide file tree
Showing 9 changed files with 449 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
function [config] = BuildRegisterToStandardConfiguration()
%BUILDREGISTERTOSTANDARDCONFIGURATION Builds configuration for pipeline.
% Builds a configuration for a pipeline that registers volumes to a standard.
%
% Input:
% - ...
%
% Output:
% - config: Configuration.

config = struct();

% get data folder relative to this file
filePath = fileparts(which(mfilename));
pathToDataFolder = fullfile(filePath, '../../../../../neurochi/data/');
pathToWorkspace = fullfile(pathToDataFolder, 'w1');
pathToDataset = fullfile(pathToDataFolder, 'input');
pathToParcellations = fullfile(pathToDataFolder, 'parcs');
% for intermediary pipelines, send output to the transfer folder
pathToOutput = fullfile(pathToDataFolder, 'transfer');

%% pipeline: register to standard
% common configuration
config.verbose = true;
config.clobber = true;
config.pathToWorkspace = pathToWorkspace;
config.pathToDataset = pathToDataset;
config.pathToParcellations = pathToParcellations;
config.pathToOutput = pathToOutput;
% helps debug by not running all subjects
config.numSubjects = 1;


% step 1
config.step1.dof = 6;
config.step1.interp = 'spline';
config.step1.optional = false;
config.step1.clobber = config.clobber;
config.step1.verbose = config.verbose;
% step 2: flirt
config.step2.dof = 6;
config.step2.applyxfm = true;
config.step2.nosearch = true;
config.step2.interp = 'spline';
config.step2.optional = false;
config.step2.clobber = config.clobber;
config.step2.verbose = config.verbose;
% step 3
config.step3.optional = false;
config.step3.clobber = config.clobber;
config.step3.verbose = config.verbose;
% step 4
config.step4.dof = 12;
config.step4.interp = 'spline';
config.step4.optional = false;
config.step4.clobber = config.clobber;
config.step4.verbose = config.verbose;
% step 5
config.step5.dof = 12;
config.step5.applyxfm = true;
config.step5.nosearch = true;
config.step5.interp = 'spline';
config.step5.optional = false;
config.step5.clobber = config.clobber;
config.step5.verbose = config.verbose;
% step 6
config.step6.optional = false;
config.step6.clobber = config.clobber;
config.step6.verbose = config.verbose;
% step 7
config.step7.optional = false;
config.step7.clobber = config.clobber;
config.step7.verbose = config.verbose;
% step 8
config.step8.optional = false;
config.step8.clobber = config.clobber;
config.step8.verbose = config.verbose;

end
43 changes: 34 additions & 9 deletions src/matlab/builders/configurations/BuildSampleConfiguration.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [config] = BuildSampleConfiguration()
%BUILDSAMPLECONFIGURATION Summary of this function goes here
% Detailed explanation goes here
%BUILDSAMPLECONFIGURATION Builds configuration for pipeline.
% Builds a configuration for a sample pipeline.
%
% Input:
% - config: Base configuration.
% - ...
%
% Output:
% - config: Configuiration.
% - config: Configuration.


config = {};
Expand Down Expand Up @@ -47,8 +47,7 @@
% step configurations can also inherit from the sequence configurations
pipeline1.step3.verbose = pipeline1.verbose;

% you can also order this by pipeline / step
%% pipeline 2: register parcellations
%% pipeline 2: register to standard
pipeline2 = {};
pipeline2.verbose = common.verbose;
pipeline2.pathToOutput = fullfile(pathToDataFolder, 'o2');
Expand All @@ -57,7 +56,7 @@
pipeline2.step1.interp = 'spline';
pipeline2.step1.optional = true;
pipeline2.step1.verbose = pipeline2.verbose;
% step 2: flirt
% step 2: flirt
pipeline2.step2.dof = 6;
pipeline2.step2.applyxfm = true;
pipeline2.step2.nosearch = true;
Expand Down Expand Up @@ -141,10 +140,36 @@
pipeline3.step5.optional = true;
pipeline3.step5.verbose = pipeline3.verbose;

% step 6: apply mask
% step 6: invert subcortical mask
pipeline3.step6.optional = true;
pipeline3.step6.verbose = pipeline3.verbose;

% step 7: multiply segmented brain by inverted subcortical mask
pipeline3.step7.skip = true;
pipeline3.step7.verbose = pipeline3.verbose;

% step 8: tag subcortical mask as gray matter
pipeline3.step8.skip = true;
pipeline3.step8.verbose = pipeline3.verbose;

% step 9: add subcortical mask back to segmented brain
pipeline3.step9.skip = true;

% step 10: create tissue type masks
pipeline3.step10.skip = true;

% step 11: dilate wm mask
pipeline3.step11.skip = true;

% step 12: dilate csf mask
pipeline3.step12.skip = true;

% step 13: combine wm and csf mask
pipeline3.step13.skip = true;

% step 14: threshold wm and csf mask to keep intersect
pipeline3.step14 = struct(); %.skip = true;
pipeline3.step14.thr = 2;

%% prepare fmri configuration object
%
Expand All @@ -159,7 +184,7 @@
% fmri.fMin = .01; % MIPLAB configs
% fmri.fMax = .25; % task bandpass
% fmri.fhwm = 0; % Full Width at Half Maximum of the Gaussian kernel
%
%
% config.fmri = fmri;

config.pipeline1 = pipeline1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function [config] = BuildTransformParcellationConfiguration()
%BUILDTRANSFORMPARCELLATIONCONFIGURATION Builds configuration for pipeline.
% Builds a configuration for a pipeline that transforms a parcellation.
%
% Input:
% - ...
%
% Output:
% - config: Configuration.

config = struct();

% get data folder relative to this file
filePath = fileparts(which(mfilename));
pathToDataFolder = fullfile(filePath, '../../../../../neurochi/data/');
pathToWorkspace = fullfile(pathToDataFolder, 'w1');
pathToDataset = fullfile(pathToDataFolder, 'input');
pathToParcellations = fullfile(pathToDataFolder, 'parcs');
% for intermediary pipelines, send output to the transfer folder
pathToOutput = fullfile(pathToDataFolder, 'transfer');

%% pipeline: transform parcellation
% common configuration
config.verbose = true;
config.clobber = true;
config.pathToWorkspace = pathToWorkspace;
config.pathToDataset = pathToDataset;
config.pathToParcellations = pathToParcellations;
config.pathToOutput = pathToOutput;
% helps debug by not running all subjects
config.numSubjects = 1;
% select the parcellation to transform
% (must be present in `pathToParcellations`)
config.parcellation = 'mask_ventricles_MNIch2.nii.gz';

% step 1
config.step1.numDilations = 0;
config.step1.clobber = config.clobber;
config.step1.verbose = config.verbose;
% step 2
config.step2.interp = 'nn';
config.step2.clobber = config.clobber;
config.step2.verbose = config.verbose;
% step 3
config.step3.interp = 'nearestneighbour';
config.step3.applyxfm = true;
config.step3.nosearch = true;
config.step3.optional = true;
config.step3.clobber = config.clobber;
config.step3.verbose = config.verbose;

% step 4
config.step4.interp = 'nearestneighbour';
config.step4.applyxfm = true;
config.step4.nosearch = true;
config.step4.optional = true;
config.step4.clobber = config.clobber;
config.step4.verbose = config.verbose;


end

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function [pipeline] = BuildRegisterParcellationPipeline(pathToWorkspace, ...
pathToDataset, ...
pathToParcellations, ...
pathToOutputsFromPreviousPipelines, ...
pathToOutput, ...
numSubjects, ...
config)
function [pipeline] = BuildRegisterToStandardPipeline(pathToWorkspace, ...
pathToDataset, ...
pathToParcellations, ...
pathToIntermediaryOutputs, ...
pathToOutput, ...
numSubjects, ...
config)
%BUILDSAMPLEPIPELINE Example of a pipeline builder.
% This builder creates a pipeline with one sequence per subject, based
% on the format of a BIDS dataset's participants.tsv file.
Expand All @@ -21,7 +21,7 @@
pathToWorkspace char = '.'
pathToDataset char = '.'
pathToParcellations char = '.'
pathToOutputsFromPreviousPipelines char = '.'
pathToIntermediaryOutputs char = '.'
pathToOutput char = '.'
numSubjects int8 {mustBeNonnegative} = 0
config = {}
Expand All @@ -30,10 +30,10 @@
% names of inputs needed to start the sequence
inputs = { ...
fullfile(pathToDataset, '%s/anat/%s_T1w.nii.gz'), ...
fullfile(pathToOutputsFromPreviousPipelines, '%s/%s_T1w_brain_mul.nii.gz'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_T1w_brain_mul.nii.gz'), ...
fullfile(pathToParcellations, 'ch2bet.nii.gz'), ...
fullfile(pathToParcellations, 'ch2.nii.gz'), ...
fullfile(pathToParcellations, 'schaefer_2018_400_subc.nii') };
fullfile(pathToParcellations, 'ch2.nii.gz') ...
};
numInputs = length(inputs);

% get information about participants
Expand Down Expand Up @@ -66,16 +66,19 @@
subjectInputs = cell(1, numInputs);
for j = 1 : numInputs
input = inputs{j};
subjectInputs{j} = sprintf(input, subjectName);
% get number of times subject name is needed
subjectNameOccurences = count(input, '%s');
[subjectNameArray{1:subjectNameOccurences}] = deal(subjectName);
subjectInputs{j} = sprintf(input, subjectNameArray{:});
end

pathToSubjectWorkspace = fullfile(pathToWorkspace, subjectName);
pathToSubjectOutput = fullfile(pathToOutput, subjectName);
sequences{i} = BuildRegisterParcellationSequence(subjectInputs, ...
subjectName, ...
pathToSubjectWorkspace, ...
pathToSubjectOutput, ...
config);
sequences{i} = BuildRegisterToStandardSequence(subjectInputs, ...
subjectName, ...
pathToSubjectWorkspace, ...
pathToSubjectOutput, ...
config);
end

% create a pipeline with the sequences
Expand Down
95 changes: 95 additions & 0 deletions src/matlab/builders/pipelines/BuildTransformParcellationPipeline.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function [pipeline] = BuildTransformParcellationPipeline(parcellation, ...
pathToWorkspace, ...
pathToDataset, ...
pathToParcellations, ...
pathToIntermediaryOutputs, ...
pathToOutput, ...
numSubjects, ...
config)
%BUILDSAMPLEPIPELINE Example of a pipeline builder.
% This builder creates a pipeline with one sequence per subject, based
% on the format of a BIDS dataset's participants.tsv file.
%
% Input:
% - pathToWorkspace: Path to the workspace.
% - pathToDataset: Path to input the root of the BIDS dataset.
% - pathToOutput: Path to where we will output the data.
%
% Output:
% - pipeline: Built pipeline.

arguments
parcellation char
pathToWorkspace char = '.'
pathToDataset char = '.'
pathToParcellations char = '.'
pathToIntermediaryOutputs char = '.'
pathToOutput char = '.'
numSubjects int8 {mustBeNonnegative} = 0
config = {}
end

% names of inputs needed to start the sequence
inputs = { ...
fullfile(pathToDataset, '%s/anat/%s_T1w.nii.gz'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_T1w_brain_mul.nii.gz'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_MNI2T1w_dof6.mat'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_MNI2T1w_dof12.mat'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_MNI2T1w_warp.nii.gz'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_T1w_brain_dof6.nii.gz'), ...
fullfile(pathToIntermediaryOutputs, '%s/%s_T1w_brain_dof12.nii.gz'), ...
fullfile(pathToParcellations, parcellation) };
numInputs = length(inputs);

% get information about participants
subjects = readtable(fullfile(pathToDataset, 'participants.tsv'), ...
'Delimiter', ...
'\t', ...
'FileType', ...
'delimitedtext', ...
'PreserveVariableNames', ...
true);

tableHeight = height(subjects);
if ~numSubjects
numSubjects = tableHeight;
else
numSubjects = min(numSubjects, tableHeight);
end

sequences = cell(1, numSubjects);

% create a sequence for each subject
for i = 1 : numSubjects
% get data for one participant
subject = subjects(i, :);

% participant_id is the column name and it's a cell
subjectName = subject.participant_id{1};

% create an input array for each sequence
subjectInputs = cell(1, numInputs);
for j = 1 : numInputs
input = inputs{j};
% get number of times subject name is needed
subjectNameOccurences = count(input, '%s');
[subjectNameArray{1:subjectNameOccurences}] = deal(subjectName);
subjectInputs{j} = sprintf(input, subjectNameArray{:});
end

pathToSubjectWorkspace = fullfile(pathToWorkspace, subjectName);
pathToSubjectOutput = fullfile(pathToOutput, subjectName);
sequences{i} = BuildTransformParcellationSequence(subjectInputs, ...
parcellation, ...
subjectName, ...
pathToSubjectWorkspace, ...
pathToSubjectOutput, ...
config);
end

% create a pipeline with the sequences
parallel = false;
pipeline = Pipeline(sequences, parallel);

end

Loading

0 comments on commit 2afb919

Please sign in to comment.