Skip to content

Commit

Permalink
Merge pull request #409 from mgxd/tst/workflow-construction
Browse files Browse the repository at this point in the history
TST: Build workflow across different conditions
  • Loading branch information
mgxd authored Oct 15, 2024
2 parents 8cc8db2 + e5124cc commit 7efc8c9
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 45 deletions.
67 changes: 28 additions & 39 deletions nibabies/data/tests/config.toml
Original file line number Diff line number Diff line change
@@ -1,78 +1,67 @@
[environment]
cpu_count = 8
exec_docker_version = "20.10.12"
exec_env = "nibabies-docker"
free_mem = 0.4
exec_env = "posix"
free_mem = 2.2
overcommit_policy = "heuristic"
overcommit_limit = "50%"
nipype_version = "1.6.1"
templateflow_version = "0.7.2"
version = "21.1.0"
nipype_version = "1.5.0"
templateflow_version = "24.2.2"
version = "24.1.0"

[execution]
bids_dir = "/data"
bids_database_dir = "/tmp/bids_db"
bids_description_hash = "c47e9ebb943ca662556808b2aeac3f6c8bb2a242696c32850c64ec47aba80d9e"
bids_dir = "ds000005/"
bids_description_hash = "5d42e27751bbc884eca87cb4e62b9a0cca0cd86f8e578747fe89b77e6c5b21e5"
boilerplate_only = false
sloppy = true
debug = []
derivatives = [ "/opt/derivatives/precomputed",]
fs_license_file = "/opt/freesurfer/license.txt"
fs_subjects_dir = "/opt/subjects"
layout = "BIDS Layout: .../data | Subjects: 1 | Sessions: 1 | Runs: 1"
log_dir = "/tmp/logs"
log_level = 20
fs_subjects_dir = "/opt/freesurfer/subjects"
log_dir = "/tmp/fmriprep/logs"
log_level = 40
low_mem = false
md_only_boilerplate = false
nibabies_dir = "/out"
notrack = false
output_dir = "/out"
me_output_echos = false
output_layout = "bids"
output_spaces = "MNIInfant:cohort-1:res-native"
notrack = true
output_dir = "/tmp"
output_spaces = "MNIInfant:cohort-1"
reports_only = false
run_uuid = "20220323-202555_01a7d80d-7ff4-4b13-a99c-ec399045e9ff"
segmentation_atlases_dir = "/opt/segmentations"
run_uuid = "20200306-105302_d365772b-fd60-4741-a722-372c2f558b50"
participant_label = [ "01",]
templateflow_home = "/home/nibabies/.cache/templateflow"
work_dir = "/scratch"
templateflow_home = "~/.cache/templateflow"
work_dir = "work/"
write_graph = false

[workflow]
anat_only = false
bold2t1w_dof = 6
bold2t1w_init = "register"
cifti_output = false
bold2anat_dof = 6
fd_radius = 45
fmap_bspline = false
force_syn = false
hires = true
ignore = [ "slicetiming",]
ignore = []
longitudinal = false
medial_surface_nan = false
project_goodvoxels = false
regressors_all_comps = false
regressors_dvars_th = 1.5
regressors_fd_th = 0.5
run_reconall = true
skull_strip_fixed_seed = false
skull_strip_template = "UNCInfant:cohort-1"
skull_strip_t1w = "force"
slice_time_ref = 0.5
spaces = "MNIInfant:cohort-1:res-native MNIInfant:cohort-1"
use_bbr = false
use_syn_sdc = false
surface_recon_method = "auto"

[nipype]
crashfile_format = "txt"
get_linked_libs = false
memory_gb = 8
nprocs = 4
omp_nthreads = 2
memory_gb = 32
nprocs = 8
omp_nthreads = 8
plugin = "MultiProc"
resource_monitor = false
stop_on_first_crash = false

[nipype.plugin_args]
maxtasksperchild = 1
raise_insufficient = false

[execution.bids_filters.t1w]
reconstruction = "<Query.NONE: 1>"

[execution.bids_filters.t2w]
reconstruction = "<Query.NONE: 1>"
4 changes: 2 additions & 2 deletions nibabies/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ def parse_bids_for_age_months(

# Play nice with sessions
subject = f'sub-{subject_id}'
session = f'ses-{session_id}' or ''
prefix = f'{subject}' + f'_{session}' if session else ''
session = f'ses-{session_id}' if session_id else ''
prefix = f'{subject}' + (f'_{session}' if session else '')

subject_level = session_level = Path(bids_root) / subject
if session_id:
Expand Down
6 changes: 5 additions & 1 deletion nibabies/workflows/anatomical/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def init_infant_anat_fit_wf(

workflow.__desc__ = desc

if not recon_method:
if recon_method is None:
LOGGER.info('ANAT Skipping Stages 6+')
return workflow

Expand Down Expand Up @@ -1765,6 +1765,10 @@ def init_infant_single_anat_fit_wf(

workflow.__desc__ = desc

if recon_method is None:
LOGGER.info('ANAT Skipping Stages 5+')
return workflow

# Stage 5: Surface reconstruction
if recon_method == 'mcribs':
if reference_anat == 'T1w':
Expand Down
6 changes: 4 additions & 2 deletions nibabies/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@
from bids.layout import BIDSLayout
from niworkflows.utils.spaces import SpatialReferences

SubjectSession = tuple[str, str | None]


AUTO_T2W_MAX_AGE = 8


def init_nibabies_wf(subworkflows_list):
def init_nibabies_wf(subworkflows_list: list[SubjectSession]):
"""
Build *NiBabies*'s pipeline.
Expand All @@ -92,7 +94,7 @@ def init_nibabies_wf(subworkflows_list):
from nibabies.workflows.tests import mock_config
from nibabies.workflows.base import init_nibabies_wf
with mock_config():
wf = init_nibabies_wf()
wf = init_nibabies_wf(['01', None])
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion nibabies/workflows/bold/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def init_fsl_bbr_wf(
flt_bbr.inputs.schedule = data.load('flirtsch/bbr.sch')
# fmt:off
workflow.connect([
(inputnode, wm_mask, [('t1w_dseg', 'in_seg')]),
(inputnode, wm_mask, [('anat_dseg', 'in_seg')]),
(inputnode, flt_bbr, [('in_file', 'in_file')]),
(lta_to_fsl, flt_bbr, [('out_fsl', 'in_matrix_file')]),
])
Expand Down
71 changes: 71 additions & 0 deletions nibabies/workflows/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""Utilities and mocks for testing and documentation building."""

import os
import shutil
from contextlib import contextmanager
from pathlib import Path
from tempfile import mkdtemp

from toml import loads

from nibabies import data
from nibabies.workflows.base import init_execution_spaces


@contextmanager
def mock_config(bids_dir=None):
"""Create a mock config for documentation and testing purposes."""
from ... import config

_old_fs = os.getenv('FREESURFER_HOME')
if not _old_fs:
os.environ['FREESURFER_HOME'] = mkdtemp()

settings = loads(data.load.readable('tests/config.toml').read_text())
for sectionname, configs in settings.items():
if sectionname != 'environment':
section = getattr(config, sectionname)
section.load(configs, init=False)
config.nipype.omp_nthreads = 1
config.nipype.init()
config.loggers.init()
init_execution_spaces()

bids_dir = bids_dir or data.load('tests/ds000005').absolute()

config.execution.work_dir = Path(mkdtemp())
config.execution.bids_dir = bids_dir
config.execution.nibabies_dir = Path(mkdtemp())
config.execution.bids_database_dir = None
config.execution._layout = None
config.execution.init()

yield

shutil.rmtree(config.execution.work_dir)
shutil.rmtree(config.execution.nibabies_dir)

if not _old_fs:
del os.environ['FREESURFER_HOME']
Loading

0 comments on commit 7efc8c9

Please sign in to comment.