Skip to content

Commit 3701b57

Browse files
committed
RF: CMTK with LibraryBaseInterface
1 parent 37c7895 commit 3701b57

File tree

5 files changed

+56
-52
lines changed

5 files changed

+56
-52
lines changed

nipype/interfaces/cmtk/base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
3+
# vi: set ft=python sts=4 ts=4 sw=4 et:
4+
""" Base interface for cmtk """
5+
6+
from ..base import LibraryBaseInterface
7+
from ...utils.misc import package_check
8+
9+
10+
class CFFBaseInterface(LibraryBaseInterface):
11+
_pkg = 'cfflib'
12+
13+
14+
# Originally set in convert, nbs, nx, parcellation
15+
# Set here to be imported, in case anybody depends on its presence
16+
# Remove in 2.0
17+
have_cmp = True
18+
try:
19+
package_check('cmp')
20+
except ImportError:
21+
have_cmp = False
22+
23+
have_cfflib = True
24+
try:
25+
package_check('cfflib')
26+
except ImportError:
27+
have_cfflib = False
28+
29+
have_cv = True
30+
try:
31+
package_check('cviewer')
32+
except ImportError:
33+
have_cv = False

nipype/interfaces/cmtk/convert.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@
88
import string
99
import networkx as nx
1010

11-
from ...utils.misc import package_check
1211
from ...utils.filemanip import split_filename
13-
from ..base import (BaseInterface, BaseInterfaceInputSpec, traits, File,
12+
from ..base import (BaseInterfaceInputSpec, traits, File,
1413
TraitedSpec, InputMultiPath, isdefined)
15-
16-
have_cfflib = True
17-
try:
18-
package_check('cfflib')
19-
except Exception as e:
20-
have_cfflib = False
21-
else:
22-
import cfflib as cf
14+
from .base import CFFBaseInterface, have_cfflib
2315

2416

2517
class CFFConverterInputSpec(BaseInterfaceInputSpec):
@@ -67,7 +59,7 @@ class CFFConverterOutputSpec(TraitedSpec):
6759
connectome_file = File(exists=True, desc='Output connectome file')
6860

6961

70-
class CFFConverter(BaseInterface):
62+
class CFFConverter(CFFBaseInterface):
7163
"""
7264
Creates a Connectome File Format (CFF) file from input networks, surfaces, volumes, tracts, etcetera....
7365
@@ -87,6 +79,7 @@ class CFFConverter(BaseInterface):
8779
output_spec = CFFConverterOutputSpec
8880

8981
def _run_interface(self, runtime):
82+
import cfflib as cf
9083
a = cf.connectome()
9184

9285
if isdefined(self.inputs.title):
@@ -232,7 +225,7 @@ class MergeCNetworksOutputSpec(TraitedSpec):
232225
exists=True, desc='Output CFF file with all the networks added')
233226

234227

235-
class MergeCNetworks(BaseInterface):
228+
class MergeCNetworks(CFFBaseInterface):
236229
""" Merges networks from multiple CFF files into one new CFF file.
237230
238231
Example
@@ -248,6 +241,7 @@ class MergeCNetworks(BaseInterface):
248241
output_spec = MergeCNetworksOutputSpec
249242

250243
def _run_interface(self, runtime):
244+
import cfflib as cf
251245
extracted_networks = []
252246

253247
for i, con in enumerate(self.inputs.in_files):

nipype/interfaces/cmtk/nbs.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,11 @@
1010
import networkx as nx
1111

1212
from ... import logging
13-
from ...utils.misc import package_check
14-
from ..base import (BaseInterface, BaseInterfaceInputSpec, traits, File,
13+
from ..base import (LibraryBaseInterface, BaseInterfaceInputSpec, traits, File,
1514
TraitedSpec, InputMultiPath, OutputMultiPath, isdefined)
15+
from .base import have_cv
1616
iflogger = logging.getLogger('interface')
1717

18-
have_cv = True
19-
try:
20-
package_check('cviewer')
21-
except Exception as e:
22-
have_cv = False
23-
else:
24-
import cviewer.libs.pyconto.groupstatistics.nbs as nbs
25-
2618

2719
def ntwks_to_matrices(in_files, edge_key):
2820
first = nx.read_gpickle(in_files[0])
@@ -92,7 +84,7 @@ class NetworkBasedStatisticOutputSpec(TraitedSpec):
9284
desc='Output network with edges identified by the NBS')
9385

9486

95-
class NetworkBasedStatistic(BaseInterface):
87+
class NetworkBasedStatistic(LibraryBaseInterface):
9688
"""
9789
Calculates and outputs the average network given a set of input NetworkX gpickle files
9890
@@ -111,11 +103,10 @@ class NetworkBasedStatistic(BaseInterface):
111103
"""
112104
input_spec = NetworkBasedStatisticInputSpec
113105
output_spec = NetworkBasedStatisticOutputSpec
106+
_pkg = 'cviewer'
114107

115108
def _run_interface(self, runtime):
116-
117-
if not have_cv:
118-
raise ImportError("cviewer library is not available")
109+
from cviewer.libs.pyconto.groupstatistics import nbs
119110

120111
THRESH = self.inputs.threshold
121112
K = self.inputs.number_of_permutations

nipype/interfaces/cmtk/nx.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@
1414

1515
from ... import logging
1616
from ...utils.filemanip import split_filename
17-
from ...utils.misc import package_check
1817
from ..base import (BaseInterface, BaseInterfaceInputSpec, traits, File,
1918
TraitedSpec, InputMultiPath, OutputMultiPath, isdefined)
19+
from .base import have_cmp
2020

2121
iflogger = logging.getLogger('interface')
2222

23-
have_cmp = True
24-
try:
25-
package_check('cmp')
26-
except Exception as e:
27-
have_cmp = False
28-
else:
29-
import cmp
30-
3123

3224
def read_unknown_ntwk(ntwk):
3325
if not isinstance(ntwk, nx.classes.graph.Graph):

nipype/interfaces/cmtk/parcellation.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,21 @@
88
import os
99
import os.path as op
1010
import shutil
11-
import warnings
1211

1312
import numpy as np
1413
import nibabel as nb
1514
import networkx as nx
1615

1716
from ... import logging
18-
from ...utils.misc import package_check
1917
from ..base import (BaseInterface, BaseInterfaceInputSpec, traits, File,
2018
TraitedSpec, Directory, isdefined)
19+
from .base import have_cmp
2120
iflogger = logging.getLogger('interface')
2221

23-
have_cmp = True
24-
try:
25-
package_check('cmp')
26-
except Exception as e:
27-
have_cmp = False
28-
else:
29-
import cmp
30-
from cmp.util import runCmd
31-
3222

3323
def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
24+
import cmp
25+
from cmp.util import runCmd
3426
iflogger.info("Create the cortical labels necessary for our ROIs")
3527
iflogger.info("=================================================")
3628
fs_label_dir = op.join(op.join(subjects_dir, subject_id), 'label')
@@ -174,6 +166,8 @@ def create_annot_label(subject_id, subjects_dir, fs_dir, parcellation_name):
174166
def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name, dilation):
175167
""" Creates the ROI_%s.nii.gz files using the given parcellation information
176168
from networks. Iteratively create volume. """
169+
import cmp
170+
from cmp.util import runCmd
177171
iflogger.info("Create the ROIs:")
178172
output_dir = op.abspath(op.curdir)
179173
fs_dir = op.join(subjects_dir, subject_id)
@@ -306,6 +300,8 @@ def create_roi(subject_id, subjects_dir, fs_dir, parcellation_name, dilation):
306300

307301

308302
def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
303+
import cmp
304+
import scipy.ndimage.morphology as nd
309305
iflogger.info("Create white matter mask")
310306
fs_dir = op.join(subjects_dir, subject_id)
311307
cmp_config = cmp.configuration.PipelineConfiguration()
@@ -328,11 +324,6 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
328324
aseg = nb.load(op.join(fs_dir, 'mri', 'aseg.nii.gz'))
329325
asegd = aseg.get_data()
330326

331-
try:
332-
import scipy.ndimage.morphology as nd
333-
except ImportError:
334-
raise Exception('Need scipy for binary erosion of white matter mask')
335-
336327
# need binary erosion function
337328
imerode = nd.binary_erosion
338329

@@ -438,6 +429,7 @@ def create_wm_mask(subject_id, subjects_dir, fs_dir, parcellation_name):
438429

439430
def crop_and_move_datasets(subject_id, subjects_dir, fs_dir, parcellation_name,
440431
out_roi_file, dilation):
432+
from cmp.util import runCmd
441433
fs_dir = op.join(subjects_dir, subject_id)
442434
cmp_config = cmp.configuration.PipelineConfiguration()
443435
cmp_config.parcellation_scheme = "Lausanne2008"
@@ -549,7 +541,7 @@ class ParcellateOutputSpec(TraitedSpec):
549541
)
550542

551543

552-
class Parcellate(BaseInterface):
544+
class Parcellate(LibraryBaseInterface):
553545
"""Subdivides segmented ROI file into smaller subregions
554546
555547
This interface implements the same procedure as in the ConnectomeMapper's
@@ -571,6 +563,8 @@ class Parcellate(BaseInterface):
571563

572564
input_spec = ParcellateInputSpec
573565
output_spec = ParcellateOutputSpec
566+
_pkg = 'cmp'
567+
imports = ('scipy', )
574568

575569
def _run_interface(self, runtime):
576570
if self.inputs.subjects_dir:

0 commit comments

Comments
 (0)