Skip to content

Commit 8a0bf9d

Browse files
committed
fix: move Nipype and spaces to config module
1 parent 868b80c commit 8a0bf9d

File tree

5 files changed

+78
-71
lines changed

5 files changed

+78
-71
lines changed

fmriprep/cli/parser.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,3 @@ def parse_args(args=None, namespace=None):
403403

404404
config.execution.participant_label = sorted(participant_label)
405405
config.workflow.skull_strip_template = config.workflow.skull_strip_template[0]
406-
407-
# Ensure user-defined spatial references for outputs are correctly parsed.
408-
# Certain options require normalization to a space not explicitly defined by users.
409-
# These spaces will not be included in the final outputs.
410-
internal_spaces = []
411-
if config.workflow.use_aroma:
412-
# Make sure there's a normalization to FSL for AROMA to use.
413-
internal_spaces.append('MNI152NLin6Asym:res-2')
414-
415-
cifti_output = config.workflow.cifti_output
416-
if cifti_output:
417-
# CIFTI grayordinates to corresponding FSL-MNI resolutions.
418-
vol_res = '2' if cifti_output == '91k' else '1'
419-
internal_spaces += ['fsaverage:den-164k', 'MNI152NLin6Asym:res-%s' % vol_res]
420-
421-
# These arguments implicitly signal expected output
422-
spaces = config.execution.output_spaces
423-
# Add the default standard space if not already present (required by several sub-workflows)
424-
if "MNI152NLin2009cAsym" not in spaces.get_spaces(nonstandard=False, dim=(3,)):
425-
internal_spaces.append("MNI152NLin2009cAsym")
426-
427-
config.workflow.internal_spaces = ' '.join(internal_spaces) or None

fmriprep/cli/run.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ def main():
99
import os
1010
import sys
1111
import gc
12-
from nipype import config as ncfg
1312
from multiprocessing import Process, Manager
1413
from .parser import parse_args
1514
from ..utils.bids import write_derivative_description
@@ -62,31 +61,6 @@ def main():
6261
# Clean up master process before running workflow, which may create forks
6362
gc.collect()
6463

65-
# Configure resource_monitor
66-
if config.nipype.resource_monitor:
67-
ncfg.update_config({
68-
'monitoring': {
69-
'enabled': config.nipype.resource_monitor,
70-
'sample_frequency': '0.5',
71-
'summary_append': True,
72-
}
73-
})
74-
ncfg.enable_resource_monitor()
75-
76-
# Nipype config (logs and execution)
77-
ncfg.update_config({
78-
'logging': {
79-
'log_directory': str(config.execution.log_dir),
80-
'log_to_file': True
81-
},
82-
'execution': {
83-
'crashdump_dir': str(config.execution.log_dir),
84-
'crashfile_format': config.nipype.crashfile_format,
85-
'get_linked_libs': config.nipype.get_linked_libs,
86-
'stop_on_first_crash': config.nipype.stop_on_first_crash,
87-
}
88-
})
89-
9064
# Sentry tracking
9165
if sentry_sdk is not None:
9266
with sentry_sdk.configure_scope() as scope:

fmriprep/config.py

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
force_syn = false
6161
hires = true
6262
ignore = []
63-
internal_spaces = "MNI152NLin2009cAsym"
6463
longitudinal = false
6564
medial_surface_nan = false
6665
run_reconall = true
@@ -368,7 +367,8 @@ class execution(_Config):
368367
output_dir = None
369368
"""Folder where derivatives will be stored."""
370369
output_spaces = None
371-
"""List of (non)standard spaces designated as spatial references for outputs."""
370+
"""List of (non)standard spaces designated (with the ``--output-spaces`` flag of
371+
the command line) as spatial references for outputs."""
372372
reports_only = False
373373
"""Only build the reports, based on the reportlets found in a cached working directory."""
374374
run_uuid = '%s_%s' % (strftime('%Y%m%d-%H%M%S'), uuid4())
@@ -435,8 +435,6 @@ class workflow(_Config):
435435
"""Run FreeSurfer ``recon-all`` with the ``-hires`` flag."""
436436
ignore = None
437437
"""Ignore particular steps for *fMRIPrep*."""
438-
internal_spaces = None
439-
"""Standard and nonstandard spaces."""
440438
longitudinal = False
441439
"""Run FreeSurfer ``recon-all`` with the ``-logitudinal`` flag."""
442440
medial_surface_nan = None
@@ -454,7 +452,8 @@ class workflow(_Config):
454452
skull_strip_template = "OASIS30ANTs"
455453
"""Change default brain extraction template."""
456454
spaces = None
457-
"""Standard and nonstandard spaces."""
455+
"""Keeps the :py:class:`~niworkflows.utils.spaces.SpatialReferences`
456+
instance keeping standard and nonstandard spaces."""
458457
t2s_coreg = None
459458
"""Co-register echos before generating the T2\\* reference of
460459
:abbr:`ME-EPI (multi-echo echo-planar imaging)`."""
@@ -513,6 +512,7 @@ def load(filename):
513512
if sectionname != 'environment':
514513
section = getattr(sys.modules[__name__], sectionname)
515514
section.load(configs)
515+
init_nipype()
516516
init_loggers()
517517
init_spaces()
518518
init_layout()
@@ -564,6 +564,7 @@ def init_layout():
564564

565565
def init_loggers():
566566
"""Set the current log level to all loggers."""
567+
from nipype import config as ncfg
567568
_handler = logging.StreamHandler(stream=sys.stdout)
568569
_handler.setFormatter(
569570
logging.Formatter(fmt=loggers._fmt, datefmt=loggers._datefmt)
@@ -574,35 +575,78 @@ def init_loggers():
574575
loggers.interface.setLevel(execution.log_level)
575576
loggers.workflow.setLevel(execution.log_level)
576577
loggers.utils.setLevel(execution.log_level)
578+
ncfg.update_config({
579+
'logging': {
580+
'log_directory': str(execution.log_dir),
581+
'log_to_file': True
582+
},
583+
})
577584

578585

579586
def init_spaces(checkpoint=True):
580587
"""Initialize the :attr:`~workflow.spaces` setting."""
581588
from niworkflows.utils.spaces import Reference, SpatialReferences
582-
if (
583-
getattr(workflow, 'spaces')
584-
and isinstance(workflow.spaces, SpatialReferences)
585-
):
586-
return
587-
588589
spaces = execution.output_spaces
589-
if spaces is not None and not isinstance(spaces, _SRs):
590+
if not isinstance(spaces, SpatialReferences):
590591
spaces = SpatialReferences(
591592
[ref for s in execution.output_spaces.split(' ')
592593
for ref in Reference.from_string(s)]
593594
)
594-
if spaces is None:
595-
spaces = _SRs()
595+
596+
# Add the default standard space if not already present (required by several sub-workflows)
597+
if "MNI152NLin2009cAsym" not in spaces.get_spaces(nonstandard=False, dim=(3,)):
598+
spaces.add(
599+
Reference("MNI152NLin2009cAsym", {"res": "native"})
600+
)
596601

597602
if checkpoint:
598603
spaces.checkpoint()
599604

600-
if workflow.internal_spaces:
601-
internal = [
602-
Reference.from_string(ref)
603-
for ref in workflow.internal_spaces.strip().split(' ')
604-
]
605-
spaces += [
606-
ref[0] for ref in internal if ref[0].fullname not in spaces
607-
]
605+
# Ensure user-defined spatial references for outputs are correctly parsed.
606+
# Certain options require normalization to a space not explicitly defined by users.
607+
# These spaces will not be included in the final outputs.
608+
if workflow.use_aroma:
609+
# Make sure there's a normalization to FSL for AROMA to use.
610+
spaces.add(
611+
Reference("MNI152NLin6Asym", {"res": "2"})
612+
)
613+
614+
cifti_output = workflow.cifti_output
615+
if cifti_output:
616+
# CIFTI grayordinates to corresponding FSL-MNI resolutions.
617+
vol_res = '2' if cifti_output == '91k' else '1'
618+
spaces.add(
619+
Reference("fsaverage", {"den": "164k"})
620+
)
621+
spaces.add(
622+
Reference("MNI152NLin6Asym", {"res": vol_res})
623+
)
624+
625+
# Make the SpatialReferences object available
608626
workflow.spaces = spaces
627+
628+
629+
def init_nipype():
630+
"""Set NiPype configurations."""
631+
from nipype import config as ncfg
632+
633+
# Configure resource_monitor
634+
if nipype.resource_monitor:
635+
ncfg.update_config({
636+
'monitoring': {
637+
'enabled': nipype.resource_monitor,
638+
'sample_frequency': '0.5',
639+
'summary_append': True,
640+
}
641+
})
642+
ncfg.enable_resource_monitor()
643+
644+
# Nipype config (logs and execution)
645+
ncfg.update_config({
646+
'execution': {
647+
'crashdump_dir': str(execution.log_dir),
648+
'crashfile_format': nipype.crashfile_format,
649+
'get_linked_libs': nipype.get_linked_libs,
650+
'stop_on_first_crash': nipype.stop_on_first_crash,
651+
}
652+
})

fmriprep/data/tests/config.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ fmap_bspline = false
3737
force_syn = false
3838
hires = true
3939
ignore = []
40-
internal_spaces = "fsaverage:den-164k MNI152NLin6Asym:res-2"
4140
longitudinal = false
4241
medial_surface_nan = false
4342
regressors_all_comps = false

fmriprep/tests/test_config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ def test_config_spaces():
1919
config.init_spaces()
2020

2121
spaces = config.workflow.spaces
22+
assert "MNI152NLin6Asym:res-2" not in [
23+
str(s) for s in spaces.get_standard(full_spec=True)]
24+
25+
assert "MNI152NLin6Asym_res-2" not in [
26+
format_reference((s.fullname, s.spec))
27+
for s in spaces.references if s.standard and s.dim == 3
28+
]
29+
30+
config.workflow.use_aroma = True
31+
config.init_spaces()
32+
spaces = config.workflow.spaces
33+
2234
assert "MNI152NLin6Asym:res-2" in [
2335
str(s) for s in spaces.get_standard(full_spec=True)]
2436

0 commit comments

Comments
 (0)