Skip to content

Commit

Permalink
🔧 Update config to allow choosing mri_robust_template for longitudi…
Browse files Browse the repository at this point in the history
…nal (new default)

[skip ci]
  • Loading branch information
shnizzedy committed Nov 13, 2024
1 parent 37fc070 commit 9d09358
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 43 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Required positional parameter "wf" in input and output of `ingress_pipeconfig_paths` function, where a node to reorient templates is added to the `wf`.
- Required positional parameter "orientation" to `resolve_resolution`.
- Optional positional argument "cfg" to `create_lesion_preproc`.
- Added `mri_robust_template` for longitudinal template generation.

### Changed

- Moved `pygraphviz` from requirements to `graphviz` optional dependencies group.
- Automatically tag untagged `subject_id` and `unique_id` as `!!str` when loading data config files.
- Made orientation configurable (was hard-coded as "RPI").
- Made `mri_robust_template` default implementation for longitudinal template generation.

### Fixed

Expand Down
18 changes: 18 additions & 0 deletions CPAC/longitudinal/robust_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2024 C-PAC Developers

# This file is part of C-PAC.

# C-PAC is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.

# C-PAC is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
"""Create longitudinal template using ``mri_robust_template``."""
16 changes: 13 additions & 3 deletions CPAC/longitudinal/wf/anat.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
name="mask_T1w_longitudinal_template",
config=["longitudinal_template_generation"],
switch=["run"],
option_key="using",
option_val="C-PAC legacy",
inputs=["desc-brain_T1w"],
outputs=["space-T1w_desc-brain_mask"],
)
Expand Down Expand Up @@ -97,6 +99,8 @@ def select_session(
name="mask_longitudinal_T1w_brain",
config=["longitudinal_template_generation"],
switch=["run"],
option_key="using",
option_val="C-PAC legacy",
inputs=["space-longitudinal_desc-brain_T1w"],
outputs=["space-longitudinal_desc-brain_mask"],
)
Expand All @@ -122,6 +126,8 @@ def mask_longitudinal_T1w_brain(
name="warp_longitudinal_T1w_to_template",
config=["longitudinal_template_generation"],
switch=["run"],
option_key="using",
option_val="C-PAC legacy",
inputs=[
(
"space-longitudinal_desc-brain_T1w",
Expand Down Expand Up @@ -178,6 +184,8 @@ def warp_longitudinal_T1w_to_template(
name="warp_longitudinal_seg_to_T1w",
config=["longitudinal_template_generation"],
switch=["run"],
option_key="using",
option_val="C-PAC legacy",
inputs=[
(
"from-longitudinal_to-T1w_mode-image_desc-linear_xfm",
Expand Down Expand Up @@ -376,12 +384,14 @@ def anat_longitudinal_wf(
template_node.inputs.set(
avg_method=config.longitudinal_template_generation["average_method"],
dof=config.longitudinal_template_generation["dof"],
interp=config.longitudinal_template_generation["interp"],
cost=config.longitudinal_template_generation["cost"],
interp=config.longitudinal_template_generation["legacy-specific"]["interp"],
cost=config.longitudinal_template_generation["legacy-specific"]["cost"],
convergence_threshold=config.longitudinal_template_generation[
"convergence_threshold"
],
thread_pool=config.longitudinal_template_generation["thread_pool"],
thread_pool=config.longitudinal_template_generation["legacy-specific"][
"thread_pool"
],
unique_id_list=list(session_wfs.keys()),
)

Expand Down
43 changes: 32 additions & 11 deletions CPAC/pipeline/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,22 +848,32 @@ def sanitize(filename):
},
"longitudinal_template_generation": {
"run": bool1_1,
"using": In({"mri_robust_template", "C-PAC legacy"}),
"average_method": In({"median", "mean", "std"}),
"dof": In({12, 9, 7, 6}),
"interp": In({"trilinear", "nearestneighbour", "sinc", "spline"}),
"cost": In(
"convergence_threshold": Number,
"max_iter": int,
"legacy-specific": Maybe(
{
"corratio",
"mutualinfo",
"normmi",
"normcorr",
"leastsq",
"labeldiff",
"bbr",
"interp": Maybe(
In({"trilinear", "nearestneighbour", "sinc", "spline"})
),
"cost": Maybe(
In(
{
"corratio",
"mutualinfo",
"normmi",
"normcorr",
"leastsq",
"labeldiff",
"bbr",
}
)
),
"thread_pool": Maybe(int),
}
),
"thread_pool": int,
"convergence_threshold": Number,
},
"functional_preproc": {
"run": bool1_1,
Expand Down Expand Up @@ -1418,6 +1428,17 @@ def schema(config_dict):
raise OSError(msg) from error
except KeyError:
pass
try:
# check for incompatible longitudinal options
lgt = partially_validated["longitudinal_template_generation"]
if lgt["using"] == "mri_robust_template":
error_msg = "{value} is not implemented for longitudinal {key} in `mri_robust_template`."
for key, value in [("average_method", "std"), ("dof", 9), ("max_iter", -1)]:
if lgt[key] == value:
msg = error_msg.format(key=key, value=value)
raise NotImplementedError(msg)
except KeyError:
pass
return partially_validated


Expand Down
3 changes: 3 additions & 0 deletions CPAC/resources/configs/1.7-1.8-nesting-mappings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,15 @@ longitudinal_template_dof:
- dof
longitudinal_template_interp:
- longitudinal_template_generation
- legacy-specific
- interp
longitudinal_template_cost:
- longitudinal_template_generation
- legacy-specific
- cost
longitudinal_template_thread_pool:
- longitudinal_template_generation
- legacy-specific
- thread_pool
longitudinal_template_convergence_threshold:
- longitudinal_template_generation
Expand Down
42 changes: 27 additions & 15 deletions CPAC/resources/configs/pipeline_config_blank.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pipeline_setup:

# Desired orientation for the output data. "RPI", "LPI", "RAI", "LAI", "RAS", "LAS", "RPS", "LPS"
desired_orientation: RPI

output_directory:

# Quality control outputs
Expand Down Expand Up @@ -1515,30 +1514,43 @@ longitudinal_template_generation:
# at once.
run: Off

# Freesurfer longitudinal template algorithm using FSL FLIRT
# Implementation to use
# Options: mri_robust_template, C-PAC legacy
using: mri_robust_template

# Method to average the dataset at each iteration of the template creation
# Options: median, mean or std
# Options: median, mean
# Additional option if using "C-PAC legacy": std
average_method: median

# Degree of freedom for FLIRT in the template creation
# Options: 12 (affine), 9 (traditional), 7 (global rescale) or 6 (rigid body)
# Options: 12 (affine), 7 (global rescale), or 6 (rigid body)
# Additional option if using "C-PAC legacy": 9 (traditional)
dof: 12

# Interpolation parameter for FLIRT in the template creation
# Options: trilinear, nearestneighbour, sinc or spline
interp: trilinear

# Cost function for FLIRT in the template creation
# Options: corratio, mutualinfo, normmi, normcorr, leastsq, labeldiff or bbr
cost: corratio

# Number of threads used for one run of the template generation algorithm
thread_pool: 2

# Threshold of transformation distance to consider that the loop converged
# (-1 means numpy.finfo(np.float64).eps and is the default)
convergence_threshold: -1

# Maximum iterations
# Stop after this many iterations, even if still above convergence_threshold
# Additional option if using "C-PAC legacy": -1 means loop forever until reaching convergence threshold
max_iter: 5

# Options for C-PAC legacy implementation that are not configurable in mri_robust_template
legacy-specific:

# Interpolation parameter for FLIRT in the template creation
# Options: trilinear, nearestneighbour, sinc or spline
interp:

# Cost function for FLIRT in the template creation
# Options: corratio, mutualinfo, normmi, normcorr, leastsq, labeldiff or bbr
cost:

# Number of threads used for one run of the template generation algorithm
thread_pool:

# OUTPUTS AND DERIVATIVES
# -----------------------
post_processing:
Expand Down
42 changes: 28 additions & 14 deletions CPAC/resources/configs/pipeline_config_default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pipeline_setup:
# This string will be sanitized and used in filepaths
pipeline_name: cpac-default-pipeline

# Desired orientation for the output data. "RPI", "LPI", "RAI", "LAI", "RAS", "LAS", "RPS", "LPS"
desired_orientation: RPI
output_directory:

# Directory where C-PAC should write out processed data, logs, and crash reports.
Expand Down Expand Up @@ -248,30 +250,42 @@ longitudinal_template_generation:
# at once.
run: Off

# Freesurfer longitudinal template algorithm using FSL FLIRT
# Implementation to use
# Options: mri_robust_template, C-PAC legacy
using: mri_robust_template

# Method to average the dataset at each iteration of the template creation
# Options: median, mean or std
# Options: median, mean
# Additional option if using "C-PAC legacy": std
average_method: median

# Degree of freedom for FLIRT in the template creation
# Options: 12 (affine), 9 (traditional), 7 (global rescale) or 6 (rigid body)
# Options: 12 (affine), 7 (global rescale), or 6 (rigid body)
# Additional option if using "C-PAC legacy": 9 (traditional)
dof: 12

# Interpolation parameter for FLIRT in the template creation
# Options: trilinear, nearestneighbour, sinc or spline
interp: trilinear

# Cost function for FLIRT in the template creation
# Options: corratio, mutualinfo, normmi, normcorr, leastsq, labeldiff or bbr
cost: corratio

# Number of threads used for one run of the template generation algorithm
thread_pool: 2

# Threshold of transformation distance to consider that the loop converged
# (-1 means numpy.finfo(np.float64).eps and is the default)
convergence_threshold: -1

# Maximum iterations
# Stop after this many iterations, even if still above convergence_threshold
# Additional option if using "C-PAC legacy": -1 means loop forever until reaching convergence threshold
max_iter: 5

# Options for C-PAC legacy implementation that are not configurable in mri_robust_template
legacy-specific:

# Interpolation parameter for FLIRT in the template creation
# Options: trilinear, nearestneighbour, sinc or spline
interp: trilinear

# Cost function for FLIRT in the template creation
# Options: corratio, mutualinfo, normmi, normcorr, leastsq, labeldiff or bbr
cost: corratio

# Number of threads used for one run of the template generation algorithm
thread_pool: 2

anatomical_preproc:

Expand Down

0 comments on commit 9d09358

Please sign in to comment.