Skip to content

Commit

Permalink
fix: do not run subprocess at import
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Sep 22, 2020
1 parent 7b9ea4f commit 70fbadf
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions niworkflows/anat/ants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from collections import OrderedDict
from multiprocessing import cpu_count
from pkg_resources import resource_filename as pkgr_fn
from packaging.version import parse as parseversion, Version
from warnings import warn

# nipype
Expand Down Expand Up @@ -40,8 +39,6 @@
"FLAIR": OrderedDict([("nclasses", 3), ("csf", 1), ("gm", 3), ("wm", 2)]),
}

_ants_version = Registration().version


def init_brain_extraction_wf(
name="brain_extraction_wf",
Expand Down Expand Up @@ -169,6 +166,7 @@ def init_brain_extraction_wf(
Output :abbr:`TPMs (tissue probability maps)` by ATROPOS
"""
from packaging.version import parse as parseversion, Version
from templateflow.api import get as get_template

wf = pe.Workflow(name)
Expand Down Expand Up @@ -270,8 +268,11 @@ def init_brain_extraction_wf(
)

# Tolerate missing ANTs at construction time
if _ants_version and parseversion(_ants_version) >= Version("2.3.0"):
try:
init_aff.inputs.search_grid = (40, (0, 40, 40))
except ValueError:
warn("antsAI's option --search-grid was added in ANTS 2.3.0 "
f"({init_aff.interface.version} found.)")

# Set up spatial normalization
settings_file = (
Expand All @@ -289,7 +290,8 @@ def init_brain_extraction_wf(
)
norm.inputs.float = use_float
fixed_mask_trait = "fixed_image_mask"
if _ants_version and parseversion(_ants_version) >= Version("2.2.0"):

if norm.interface.version and parseversion(norm.interface.version) >= Version("2.2.0"):
fixed_mask_trait += "s"

map_brainmask = pe.Node(
Expand Down Expand Up @@ -324,17 +326,12 @@ def init_brain_extraction_wf(
name="inu_n4_final",
iterfield=["input_image"],
)
if _ants_version and parseversion(_ants_version) >= Version("2.1.0"):
try:
inu_n4_final.inputs.rescale_intensities = True
else:
warn(
"""\
Found ANTs version %s, which is too old. Please consider upgrading to 2.1.0 or \
greater so that the --rescale-intensities option is available with \
N4BiasFieldCorrection."""
% _ants_version,
DeprecationWarning,
)
except ValueError:
warn("N4BiasFieldCorrection's --rescale-intensities option was added in ANTS 2.1.0 "
f"({inu_n4_final.interface.version} found.) Please consider upgrading.",
DeprecationWarning)

# Apply mask
apply_mask = pe.MapNode(ApplyMask(), iterfield=["in_file"], name="apply_mask")
Expand Down Expand Up @@ -742,17 +739,13 @@ def init_atropos_wf(
name="inu_n4_final",
iterfield=["input_image"],
)
if _ants_version and parseversion(_ants_version) >= Version("2.1.0"):

try:
inu_n4_final.inputs.rescale_intensities = True
else:
warn(
"""\
Found ANTs version %s, which is too old. Please consider upgrading to 2.1.0 or \
greater so that the --rescale-intensities option is available with \
N4BiasFieldCorrection."""
% _ants_version,
DeprecationWarning,
)
except ValueError:
warn("N4BiasFieldCorrection's --rescale-intensities option was added in ANTS 2.1.0 "
f"({inu_n4_final.interface.version} found.) Please consider upgrading.",
DeprecationWarning)

# Apply mask
apply_mask = pe.MapNode(ApplyMask(), iterfield=["in_file"], name="apply_mask")
Expand Down Expand Up @@ -979,11 +972,9 @@ def init_n4_only_wf(
try:
inu_n4_final.inputs.rescale_intensities = True
except ValueError:
warn(
"The installed ANTs version too old. Please consider upgrading to "
"2.1.0 or greater.",
DeprecationWarning,
)
warn("N4BiasFieldCorrection's --rescale-intensities option was added in ANTS 2.1.0 "
f"({inu_n4_final.interface.version} found.) Please consider upgrading.",
DeprecationWarning)

# fmt: off
wf.connect([
Expand Down

0 comments on commit 70fbadf

Please sign in to comment.