Skip to content

Commit 9ec91b9

Browse files
committed
Merge pull request #1304 from carlohamalainen/carlo-minc-interface
ENH: Interfaces for MINC tools.
2 parents 47f0326 + 2f8c7bd commit 9ec91b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+6081
-1
lines changed

CHANGES

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Next release
1010
* ENH: New ANTs interface: antsBrainExtraction (https://github.com/nipy/nipype/pull/1231)
1111
* API: Default model level for the bedpostx workflow has been set to "2" following FSL 5.0.9 lead
1212
* ENH: New interfaces for interacting with AWS S3: S3DataSink and S3DataGrabber (https://github.com/nipy/nipype/pull/1201)
13-
13+
* ENH: Interfaces for MINC tools (https://github.com/nipy/nipype/pull/1304)
1414

1515
Release 0.11.0 (September 15, 2015)
1616
============

nipype/interfaces/minc/__init__.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"""The minc module provides classes for interfacing with the `MINC
4+
<http://www.bic.mni.mcgill.ca/ServicesSoftware/MINC>`_ command line tools. This
5+
module was written to work with MINC version 2.2.00.
6+
7+
Author: Carlo Hamalainen <[email protected]>
8+
http://carlo-hamalainen.net
9+
"""
10+
11+
12+
from .base import (Info)
13+
14+
15+
from .minc import (Average,
16+
BBox,
17+
Beast,
18+
BestLinReg,
19+
BigAverage,
20+
Blob,
21+
Blur,
22+
Calc,
23+
Convert,
24+
Copy,
25+
Dump,
26+
Extract,
27+
Gennlxfm,
28+
Math,
29+
NlpFit,
30+
Norm,
31+
Pik,
32+
Resample,
33+
Reshape,
34+
ToEcat,
35+
ToRaw,
36+
Volcentre,
37+
Voliso,
38+
Volpad,
39+
VolSymm,
40+
XfmAvg,
41+
XfmConcat,
42+
XfmInvert, )

nipype/interfaces/minc/base.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
"""The minc module provides classes for interfacing with the `MINC
4+
<http://www.bic.mni.mcgill.ca/ServicesSoftware/MINC>`_ command line tools. This
5+
module was written to work with MINC version 2.2.00.
6+
7+
Author: Carlo Hamalainen <[email protected]>
8+
http://carlo-hamalainen.net
9+
"""
10+
11+
from ..base import (
12+
TraitedSpec,
13+
CommandLineInputSpec,
14+
CommandLine,
15+
StdOutCommandLineInputSpec,
16+
StdOutCommandLine,
17+
File,
18+
Directory,
19+
InputMultiPath,
20+
OutputMultiPath,
21+
traits,
22+
isdefined,
23+
)
24+
25+
import glob
26+
import os
27+
import os.path
28+
import re
29+
30+
import warnings
31+
warn = warnings.warn
32+
warnings.filterwarnings('always', category=UserWarning)
33+
34+
35+
def check_minc():
36+
"""Returns True if and only if MINC is installed.'
37+
"""
38+
39+
return Info.version() is not None
40+
41+
42+
def no_minc():
43+
"""Returns True if and only if MINC is *not* installed.
44+
"""
45+
return not check_minc()
46+
47+
48+
class Info(object):
49+
"""Handle MINC version information.
50+
51+
version refers to the version of MINC on the system
52+
"""
53+
54+
@staticmethod
55+
def version():
56+
"""Check for minc version on the system
57+
58+
Parameters
59+
----------
60+
None
61+
62+
Returns
63+
-------
64+
version : dict
65+
Version number as dict or None if MINC not found
66+
67+
"""
68+
try:
69+
clout = CommandLine(command='mincinfo',
70+
args='-version',
71+
terminal_output='allatonce').run()
72+
except IOError:
73+
return None
74+
75+
out = clout.runtime.stdout
76+
77+
def read_program_version(s):
78+
if 'program' in s:
79+
return s.split(':')[1].strip()
80+
return None
81+
82+
def read_libminc_version(s):
83+
if 'libminc' in s:
84+
return s.split(':')[1].strip()
85+
return None
86+
87+
def read_netcdf_version(s):
88+
if 'netcdf' in s:
89+
return ' '.join(s.split(':')[1:]).strip()
90+
return None
91+
92+
def read_hdf5_version(s):
93+
if 'HDF5' in s:
94+
return s.split(':')[1].strip()
95+
return None
96+
97+
versions = {'minc': None,
98+
'libminc': None,
99+
'netcdf': None,
100+
'hdf5': None, }
101+
102+
for l in out.split('\n'):
103+
for (name, f) in [('minc', read_program_version),
104+
('libminc', read_libminc_version),
105+
('netcdf', read_netcdf_version),
106+
('hdf5', read_hdf5_version), ]:
107+
if f(l) is not None:
108+
versions[name] = f(l)
109+
110+
return versions
111+
112+
113+
def aggregate_filename(files, new_suffix):
114+
"""
115+
Try to work out a sensible name given a set of files that have
116+
been combined in some way (e.g. averaged). If we can't work out a
117+
sensible prefix, we use the first filename in the list.
118+
119+
Examples
120+
--------
121+
122+
>>> from nipype.interfaces.minc.base import aggregate_filename
123+
>>> f = aggregate_filename(['/tmp/foo1.mnc', '/tmp/foo2.mnc', '/tmp/foo3.mnc'], 'averaged')
124+
>>> os.path.split(f)[1] # This has a full path, so just check the filename.
125+
'foo_averaged.mnc'
126+
127+
>>> f = aggregate_filename(['/tmp/foo1.mnc', '/tmp/blah1.mnc'], 'averaged')
128+
>>> os.path.split(f)[1] # This has a full path, so just check the filename.
129+
'foo1_averaged.mnc'
130+
131+
"""
132+
133+
path = os.path.split(files[0])[0]
134+
names = [os.path.splitext(os.path.split(x)[1])[0] for x in files]
135+
common_prefix = os.path.commonprefix(names)
136+
137+
path = os.getcwd()
138+
139+
if common_prefix == '':
140+
return os.path.abspath(os.path.join(path, os.path.splitext(files[0])[0] + '_' + new_suffix + '.mnc'))
141+
else:
142+
return os.path.abspath(os.path.join(path, common_prefix + '_' + new_suffix + '.mnc'))

0 commit comments

Comments
 (0)