Skip to content

FIX/RF: Nipype interfaces #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions nipreps/synthstrip/wrappers/nipype.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#
"""SynthStrip interface."""

import logging
import os
from pathlib import Path

Expand All @@ -30,15 +31,11 @@
CommandLineInputSpec,
File,
TraitedSpec,
Undefined,
isdefined,
traits,
)

_fs_home = os.getenv('FREESURFER_HOME', None)
_default_model_path = Path(_fs_home) / 'models' / 'synthstrip.1.pt' if _fs_home else Undefined

if _fs_home and not _default_model_path.exists():
_default_model_path = Undefined
IFLOGGER = logging.getLogger('nipype.interface')


class _SynthStripInputSpec(CommandLineInputSpec):
Expand All @@ -50,8 +47,6 @@ class _SynthStripInputSpec(CommandLineInputSpec):
)
use_gpu = traits.Bool(False, usedefault=True, argstr='-g', desc='Use GPU', nohash=True)
model = File(
str(_default_model_path),
usedefault=True,
exists=True,
argstr='--model %s',
desc="file containing model's weights",
Expand All @@ -78,6 +73,26 @@ class _SynthStripOutputSpec(TraitedSpec):


class SynthStrip(CommandLine):
"""NiPrep implementation of FreeSurfer's SynthStrip."""

_cmd = 'nipreps-synthstrip'
input_spec = _SynthStripInputSpec
output_spec = _SynthStripOutputSpec

def _parse_inputs(self, skip=None):
if not isdefined(self.inputs.model):
self._set_default_model()
return super()._parse_inputs(skip or [])

def _set_default_model(self):
model = None
fs_home = os.getenv('FREESURFER_HOME', None)
if fs_home and Path(fs_home).exists():
model = Path(fs_home) / 'models' / 'synthstrip.1.pt'

if not model or not model.exists():
IFLOGGER.warning('No SynthStrip model was found.')
return

IFLOGGER.info('Using default SynthStrip model: %s', model.name)
self.inputs.model = model
Empty file.
45 changes: 45 additions & 0 deletions nipreps/synthstrip/wrappers/tests/test_nipype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pathlib import Path

import pytest

try:
from nipreps.synthstrip.wrappers.nipype import SynthStrip
except ImportError:
pytest.skip('Nipype not installed.')


@pytest.fixture
def t1w(tmp_path):
t1w = tmp_path / 'T1w.nii.gz'
t1w.touch()
return t1w


@pytest.fixture
def fsdir(tmp_path):
fs = tmp_path / 'freesurfer'
(fs / 'models').mkdir(parents=True)
(fs / 'models' / 'synthstrip.1.pt').touch()
return fs


def test_nipype_synthstrip_fs(t1w, fsdir, monkeypatch):
# First with FS environment
monkeypatch.setenv('FREESURFER_HOME', str(fsdir))
syn = SynthStrip(in_file=t1w)
assert 'synthstrip.1.pt' in syn.cmdline

# now remove the default file
(fsdir / 'models' / 'synthstrip.1.pt').unlink()
syn = SynthStrip(in_file=t1w)
assert 'synthstrip.1.pt' not in syn.cmdline

monkeypatch.delenv('FREESURFER_HOME')
syn = SynthStrip(in_file=t1w)
assert 'synthstrip.1.pt' not in syn.cmdline

model = Path('model.pt')
model.touch()
syn = SynthStrip(in_file=t1w, model=model)
assert 'model.pt' in syn.cmdline
model.unlink()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ignore = [
"S311", # We are not using random for cryptographic purposes
"ISC001",
"S603",
"S101",
]

[tool.ruff.lint.flake8-quotes]
Expand Down
Loading