Skip to content

Commit 3389a28

Browse files
committed
Adding back base.py for NiftySegCommand because of version control on the software.
1 parent c40b673 commit 3389a28

15 files changed

+138
-54
lines changed

nipype/interfaces/niftyreg/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def __init__(self, required_version=None, **inputs):
6464
_version = self.get_version()
6565
if _version:
6666
_version = _version.decode("utf-8")
67-
if StrictVersion(_version) < StrictVersion(self._min_version):
67+
if self._min_version is not None and \
68+
StrictVersion(_version) < StrictVersion(self._min_version):
6869
msg = 'A later version of Niftyreg is required (%s < %s)'
6970
warn(msg % (_version, self._min_version))
7071
if required_version is not None:

nipype/interfaces/niftyseg/base.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
4+
"""
5+
The niftyseg module provides classes for interfacing with `niftyseg
6+
<https://sourceforge.net/projects/niftyseg/>`_ command line tools.
7+
These are the base tools for working with niftyseg.
8+
EM Statistical Segmentation tool is found in niftyseg/em.py
9+
Fill lesions tool is found in niftyseg/lesions.py
10+
Mathematical operation tool is found in niftyseg/maths.py
11+
Patch Match tool is found in niftyseg/patchmatch.py
12+
Statistical operation tool is found in niftyseg/stats.py
13+
Label Fusion and CalcTopNcc tools are in niftyseg/steps.py
14+
Examples
15+
--------
16+
See the docstrings of the individual classes for examples.
17+
"""
18+
19+
from nipype.interfaces.niftyreg.base import NiftyRegCommand, no_nifty_package
20+
import subprocess
21+
import warnings
22+
23+
24+
warn = warnings.warn
25+
warnings.filterwarnings('always', category=UserWarning)
26+
27+
28+
class NiftySegCommand(NiftyRegCommand):
29+
"""
30+
Base support interface for NiftySeg commands.
31+
"""
32+
_suffix = '_ns'
33+
_min_version = None
34+
35+
def __init__(self, **inputs):
36+
super(NiftySegCommand, self).__init__(**inputs)
37+
38+
def get_version(self):
39+
if no_nifty_package(cmd=self.cmd):
40+
return None
41+
# exec_cmd = ''.join((self.cmd, ' --version'))
42+
exec_cmd = 'seg_EM --version'
43+
# Using seg_EM for version (E.G: seg_stats --version doesn't work)
44+
return subprocess.check_output(exec_cmd, shell=True).strip('\n')

nipype/interfaces/niftyseg/em.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
from ..base import (TraitedSpec, File, traits, CommandLineInputSpec,
2222
InputMultiPath)
23-
from ..niftyreg.base import NiftyRegCommand, get_custom_path
23+
from .base import NiftySegCommand
24+
from ..niftyreg.base import get_custom_path
2425

2526

2627
class EMInputSpec(CommandLineInputSpec):
@@ -109,7 +110,7 @@ class EMOutputSpec(TraitedSpec):
109110
out_outlier_file = File(desc='Output outlierness image')
110111

111112

112-
class EM(NiftyRegCommand):
113+
class EM(NiftySegCommand):
113114
"""Interface for executable seg_EM from NiftySeg platform.
114115
115116
seg_EM is a general purpose intensity based image segmentation tool. In

nipype/interfaces/niftyseg/label_fusion.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
from ..base import (TraitedSpec, File, traits, isdefined, CommandLineInputSpec,
2020
NipypeInterfaceError)
21-
from ..niftyreg.base import get_custom_path, NiftyRegCommand
21+
from .base import NiftySegCommand
22+
from ..niftyreg.base import get_custom_path
2223
from ...utils.filemanip import load_json, save_json, split_filename
2324

2425

@@ -111,7 +112,7 @@ class LabelFusionOutput(TraitedSpec):
111112
out_file = File(exists=True, desc='image written after calculations')
112113

113114

114-
class LabelFusion(NiftyRegCommand):
115+
class LabelFusion(NiftySegCommand):
115116
"""Interface for executable seg_LabelFusion from NiftySeg platform using
116117
type STEPS as classifier Fusion.
117118
@@ -288,7 +289,7 @@ class CalcTopNCCOutputSpec(TraitedSpec):
288289
out_files = traits.Any(File(exists=True))
289290

290291

291-
class CalcTopNCC(NiftyRegCommand):
292+
class CalcTopNCC(NiftySegCommand):
292293
"""Interface for executable seg_CalcTopNCC from NiftySeg platform.
293294
294295
Examples

nipype/interfaces/niftyseg/lesions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import warnings
2222

2323
from ..base import TraitedSpec, File, traits, CommandLineInputSpec
24-
from ..niftyreg.base import NiftyRegCommand, get_custom_path
24+
from .base import NiftySegCommand
25+
from ..niftyreg.base import get_custom_path
2526

2627

2728
warn = warnings.warn
@@ -93,7 +94,7 @@ class FillLesionsOutputSpec(TraitedSpec):
9394
out_file = File(desc="Output segmentation")
9495

9596

96-
class FillLesions(NiftyRegCommand):
97+
class FillLesions(NiftySegCommand):
9798
"""Interface for executable seg_FillLesions from NiftySeg platform.
9899
99100
Fill all the masked lesions with WM intensity average.

nipype/interfaces/niftyseg/maths.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222

2323
from ..base import (TraitedSpec, File, traits, isdefined, CommandLineInputSpec,
2424
NipypeInterfaceError)
25-
from ..niftyreg.base import NiftyRegCommand, get_custom_path
25+
from .base import NiftySegCommand
26+
from ..niftyreg.base import get_custom_path
2627
from ...utils.filemanip import split_filename
2728

2829

@@ -53,7 +54,7 @@ class MathsOutput(TraitedSpec):
5354
out_file = File(desc='image written after calculations')
5455

5556

56-
class MathsCommand(NiftyRegCommand):
57+
class MathsCommand(NiftySegCommand):
5758
"""
5859
Base Command Interface for seg_maths interfaces.
5960

nipype/interfaces/niftyseg/patchmatch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import warnings
1616

1717
from ..base import TraitedSpec, File, traits, CommandLineInputSpec
18-
from ..niftyreg.base import NiftyRegCommand, get_custom_path
18+
from .base import NiftySegCommand
19+
from ..niftyreg.base import get_custom_path
1920

2021

2122
warn = warnings.warn
@@ -80,7 +81,7 @@ class PatchMatchOutputSpec(TraitedSpec):
8081
out_file = File(desc="Output segmentation")
8182

8283

83-
class PatchMatch(NiftyRegCommand):
84+
class PatchMatch(NiftySegCommand):
8485
"""Interface for executable seg_PatchMatch from NiftySeg platform.
8586
8687
The database file is a text file and in each line we have a template

nipype/interfaces/niftyseg/stats.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
import numpy as np
1616

1717
from ..base import TraitedSpec, File, traits, CommandLineInputSpec
18-
from ..niftyreg.base import NiftyRegCommand, get_custom_path
18+
from .base import NiftySegCommand
19+
from ..niftyreg.base import get_custom_path
1920

2021

2122
class StatsInput(CommandLineInputSpec):
@@ -45,7 +46,7 @@ class StatsOutput(TraitedSpec):
4546
output = traits.Array(desc='Output array from seg_stats')
4647

4748

48-
class StatsCommand(NiftyRegCommand):
49+
class StatsCommand(NiftySegCommand):
4950
"""
5051
Base Command Interface for seg_stats interfaces.
5152
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT
2+
from __future__ import unicode_literals
3+
from ..base import NiftySegCommand
4+
5+
6+
def test_NiftySegCommand_inputs():
7+
input_map = dict(args=dict(argstr='%s',
8+
),
9+
environ=dict(nohash=True,
10+
usedefault=True,
11+
),
12+
ignore_exception=dict(nohash=True,
13+
usedefault=True,
14+
),
15+
terminal_output=dict(nohash=True,
16+
),
17+
)
18+
inputs = NiftySegCommand.input_spec()
19+
20+
for key, metadata in list(input_map.items()):
21+
for metakey, value in list(metadata.items()):
22+
assert getattr(inputs.traits()[key], metakey) == value
23+

nipype/interfaces/niftyseg/tests/test_em_interfaces.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import pytest
55

6-
from nipype.interfaces.niftyreg import no_nifty_package
7-
from nipype.interfaces.niftyseg import get_custom_path, EM
6+
from nipype.interfaces.niftyreg import no_nifty_package, get_custom_path
7+
from nipype.interfaces.niftyseg import EM
88
from nipype.testing import example_data
99

1010

@@ -16,7 +16,8 @@ def test_seg_em():
1616
seg_em = EM()
1717

1818
# Check if the command is properly defined
19-
assert seg_em.cmd == get_custom_path('seg_EM')
19+
cmd = get_custom_path('seg_EM', env_dir='NIFTYSEGDIR')
20+
assert seg_em.cmd == cmd
2021

2122
# test raising error with mandatory args absent
2223
with pytest.raises(ValueError):
@@ -30,7 +31,7 @@ def test_seg_em():
3031
cmd_tmp = '{cmd} -in {in_file} -nopriors 4 -bc_out {bc_out} -out \
3132
{out_file} -out_outlier {out_outlier}'
3233
expected_cmd = cmd_tmp.format(
33-
cmd=get_custom_path('seg_EM'),
34+
cmd=cmd,
3435
in_file=in_file,
3536
out_file='im1_em.nii.gz',
3637
bc_out='im1_bc_em.nii.gz',

0 commit comments

Comments
 (0)