-
Notifications
You must be signed in to change notification settings - Fork 520
/
base.py
138 lines (112 loc) · 4.76 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
The niftyreg module provides classes for interfacing with `niftyreg
<http://sourceforge.net/projects/niftyreg/>`_ command line tools.
These are the base tools for working with niftyreg.
Registration tools are found in niftyreg/reg.py
Every other tool is found in niftyreg/regutils.py
Examples
--------
See the docstrings of the individual classes for examples.
"""
from distutils.version import StrictVersion
import os
from ... import logging
from ..base import (CommandLine, CommandLineInputSpec, traits, Undefined,
PackageInfo)
from ...utils.filemanip import split_filename
iflogger = logging.getLogger("nipype.interface")
def get_custom_path(command, env_dir="NIFTYREGDIR"):
return os.path.join(os.getenv(env_dir, ""), command)
class Info(PackageInfo):
version_cmd = get_custom_path('reg_aladin') + ' --version'
@staticmethod
def parse_version(raw_info):
return raw_info
class NiftyRegCommandInputSpec(CommandLineInputSpec):
"""Input Spec for niftyreg interfaces."""
# Set the number of omp thread to use
omp_core_val = traits.Int(
int(os.environ.get("OMP_NUM_THREADS", "1")),
desc="Number of openmp thread to use",
argstr="-omp %i",
usedefault=True,
)
class NiftyRegCommand(CommandLine):
"""
Base support interface for NiftyReg commands.
"""
_suffix = "_nr"
_min_version = "1.5.30"
input_spec = NiftyRegCommandInputSpec
def __init__(self, required_version=None, **inputs):
self.num_threads = 1
super(NiftyRegCommand, self).__init__(**inputs)
self.required_version = required_version
_version = self.version
if _version:
if self._min_version is not None and StrictVersion(
_version
) < StrictVersion(self._min_version):
msg = "A later version of Niftyreg is required (%s < %s)"
iflogger.warning(msg, _version, self._min_version)
if required_version is not None:
if StrictVersion(_version) != StrictVersion(required_version):
msg = "The version of NiftyReg differs from the required"
msg += "(%s != %s)"
iflogger.warning(msg, _version, self.required_version)
self.inputs.on_trait_change(self._omp_update, "omp_core_val")
self.inputs.on_trait_change(self._environ_update, "environ")
self._omp_update()
def _omp_update(self):
if self.inputs.omp_core_val:
self.inputs.environ["OMP_NUM_THREADS"] = str(self.inputs.omp_core_val)
self.num_threads = self.inputs.omp_core_val
else:
if "OMP_NUM_THREADS" in self.inputs.environ:
del self.inputs.environ["OMP_NUM_THREADS"]
self.num_threads = 1
def _environ_update(self):
if self.inputs.environ:
if "OMP_NUM_THREADS" in self.inputs.environ:
self.inputs.omp_core_val = int(self.inputs.environ["OMP_NUM_THREADS"])
else:
self.inputs.omp_core_val = Undefined
else:
self.inputs.omp_core_val = Undefined
def check_version(self):
_version = self.version
if not _version:
raise Exception("Niftyreg not found")
if StrictVersion(_version) < StrictVersion(self._min_version):
err = "A later version of Niftyreg is required (%s < %s)"
raise ValueError(err % (_version, self._min_version))
if self.required_version:
if StrictVersion(_version) != StrictVersion(self.required_version):
err = "The version of NiftyReg differs from the required"
err += "(%s != %s)"
raise ValueError(err % (_version, self.required_version))
@property
def version(self):
return Info.version()
def exists(self):
return self.version is not None
def _format_arg(self, name, spec, value):
if name == "omp_core_val":
self.numthreads = value
return super(NiftyRegCommand, self)._format_arg(name, spec, value)
def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None):
if basename == "":
msg = "Unable to generate filename for command %s. " % self.cmd
msg += "basename is not set!"
raise ValueError(msg)
_, final_bn, final_ext = split_filename(basename)
if out_dir is None:
out_dir = os.getcwd()
if ext is not None:
final_ext = ext
if suffix is not None:
final_bn = "".join((final_bn, suffix))
return os.path.abspath(os.path.join(out_dir, final_bn + final_ext))