/usr/share/pyshared/nipype/interfaces/dcmstack.py is in python-nipype 0.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | """Provides interfaces to various commands provided by freeusrfer
Change directory to provide relative paths for doctests
>>> import os
>>> filepath = os.path.dirname( os.path.realpath( __file__ ) )
>>> datadir = os.path.realpath(os.path.join(filepath, '../testing/data'))
>>> os.chdir(datadir)
"""
from __future__ import absolute_import
import os, string
from os import path
from glob import glob
from nipype.interfaces.base import (TraitedSpec,
DynamicTraitedSpec,
InputMultiPath,
File,
Directory,
traits,
BaseInterface,
)
import nibabel as nb
from nipype.interfaces.traits_extension import isdefined, Undefined
have_dcmstack = True
try:
import dicom
import dcmstack
from dcmstack.dcmmeta import NiftiWrapper
except ImportError:
have_dcmstack = False
def sanitize_path_comp(path_comp):
result = []
for char in path_comp:
if not char in string.letters + string.digits + '-_.':
result.append('_')
else:
result.append(char)
return ''.join(result)
class NiftiGeneratorBaseInputSpec(TraitedSpec):
out_format = traits.Str(desc="String which can be formatted with "
"meta data to create the output filename(s)")
out_ext = traits.Str('.nii.gz',
usedefault=True,
desc="Determines output file type")
class NiftiGeneratorBase(BaseInterface):
'''Base class for interfaces that produce Nifti files, potentially with
embeded meta data.'''
def _get_out_path(self, meta, idx=None):
'''Return the output path for the gernerated Nifti.'''
if self.inputs.out_format:
out_fmt = self.inputs.out_format
else:
#If no out_format is specified, use a sane default that will work
#with the provided meta data.
out_fmt = []
if not idx is None:
out_fmt.append('%03d' % idx)
if 'SeriesNumber' in meta:
out_fmt.append('%(SeriesNumber)03d')
if 'ProtocolName' in meta:
out_fmt.append('%(ProtocolName)s')
elif 'SeriesDescription' in meta:
out_fmt.append('%(SeriesDescription)s')
else:
out_fmt.append('sequence')
out_fmt = '-'.join(out_fmt)
out_fn = (out_fmt % meta) + self.inputs.out_ext
out_fn = sanitize_path_comp(out_fn)
return path.join(os.getcwd(), out_fn)
class DcmStackInputSpec(NiftiGeneratorBaseInputSpec):
dicom_files = traits.Either(InputMultiPath(File(exists=True)),
Directory(exists=True),
traits.Str(),
mandatory=True)
embed_meta = traits.Bool(desc="Embed DICOM meta data into result")
exclude_regexes = traits.List(desc="Meta data to exclude, suplementing "
"any default exclude filters")
include_regexes = traits.List(desc="Meta data to include, overriding any "
"exclude filters")
class DcmStackOutputSpec(TraitedSpec):
out_file = File(exists=True)
class DcmStack(NiftiGeneratorBase):
'''Create one Nifti file from a set of DICOM files. Can optionally embed
meta data.
Example
-------
>>> from nipype.interfaces.dcmstack import DcmStack
>>> stacker = DcmStack()
>>> stacker.inputs.dicom_files = 'path/to/series/'
>>> stacker.run() # doctest: +SKIP
>>> result.outputs.out_file # doctest: +SKIP
'/path/to/cwd/sequence.nii.gz'
'''
input_spec = DcmStackInputSpec
output_spec = DcmStackOutputSpec
def _get_filelist(self, trait_input):
if isinstance(trait_input, str):
if path.isdir(trait_input):
return glob(path.join(trait_input, '*.dcm'))
else:
return glob(trait_input)
return trait_input
def _run_interface(self, runtime):
src_paths = self._get_filelist(self.inputs.dicom_files)
include_regexes = dcmstack.default_key_incl_res
if isdefined(self.inputs.include_regexes):
include_regexes += self.inputs.include_regexes
exclude_regexes = dcmstack.default_key_excl_res
if isdefined(self.inputs.exclude_regexes):
exclude_regexes += self.inputs.exclude_regexes
meta_filter = dcmstack.make_key_regex_filter(exclude_regexes,
include_regexes)
stack = dcmstack.DicomStack(meta_filter=meta_filter)
for src_path in src_paths:
src_dcm = dicom.read_file(src_path, force=True)
stack.add_dcm(src_dcm)
nii = stack.to_nifti(embed_meta=True)
nw = NiftiWrapper(nii)
self.out_path = \
self._get_out_path(nw.meta_ext.get_class_dict(('global', 'const')))
if not self.inputs.embed_meta:
nw.remove_extension()
nb.save(nii, self.out_path)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_file"] = self.out_path
return outputs
class GroupAndStackOutputSpec(TraitedSpec):
out_list = traits.List(desc="List of output nifti files")
class GroupAndStack(DcmStack):
'''Create (potentially) multiple Nifti files for a set of DICOM files.
'''
input_spec = DcmStackInputSpec
output_spec = GroupAndStackOutputSpec
def _run_interface(self, runtime):
src_paths = self._get_filelist(self.inputs.dicom_files)
stacks = dcmstack.parse_and_stack(src_paths)
self.out_list = []
for key, stack in stacks.iteritems():
nw = NiftiWrapper(stack.to_nifti(embed_meta=True))
const_meta = nw.meta_ext.get_class_dict(('global', 'const'))
out_path = self._get_out_path(const_meta)
if not self.inputs.embed_meta:
nw.remove_extension()
nb.save(nw.nii_img, out_path)
self.out_list.append(out_path)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs["out_list"] = self.out_list
return outputs
class LookupMetaInputSpec(TraitedSpec):
in_file = File(mandatory=True,
exists=True,
desc='The input Nifti file')
meta_keys = traits.Either(traits.List(),
traits.Dict(),
mandatory=True,
desc=("List of meta data keys to lookup, or a "
"dict where keys specify the meta data keys to "
"lookup and the values specify the output names")
)
class LookupMeta(BaseInterface):
'''Lookup meta data values from a Nifti with embeded meta data.
Example
-------
>>> from nipype.interfaces import dcmstack
>>> lookup = dcmstack.LookupMeta()
>>> lookup.inputs.in_file = 'functional.nii'
>>> lookup.inputs.meta_keys = {'RepetitionTime' : 'TR', \
'EchoTime' : 'TE'}
>>> result = lookup.run() # doctest: +SKIP
>>> result.outputs.TR # doctest: +SKIP
9500.0
>>> result.outputs.TE # doctest: +SKIP
95.0
'''
input_spec = LookupMetaInputSpec
output_spec = DynamicTraitedSpec
def _make_name_map(self):
if isinstance(self.inputs.meta_keys, list):
self._meta_keys = {}
for key in self.inputs.meta_keys:
self._meta_keys[key] = key
else:
self._meta_keys = self.inputs.meta_keys
def _outputs(self):
self._make_name_map()
outputs = super(LookupMeta, self)._outputs()
undefined_traits = {}
for out_name in self._meta_keys.values():
outputs.add_trait(out_name, traits.Any)
undefined_traits[out_name] = Undefined
outputs.trait_set(trait_change_notify=False, **undefined_traits)
#Not sure why this is needed
for out_name in self._meta_keys.values():
_ = getattr(outputs, out_name)
return outputs
def _run_interface(self, runtime):
#If the 'meta_keys' input is a list, covert it to a dict
self._make_name_map()
nw = NiftiWrapper.from_filename(self.inputs.in_file)
self.result = {}
for meta_key, out_name in self._meta_keys.iteritems():
self.result[out_name] = nw.meta_ext.get_values(meta_key)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs.update(self.result)
return outputs
class CopyMetaInputSpec(TraitedSpec):
src_file = File(mandatory=True, exists=True)
dest_file = File(mandatory=True, exists=True)
include_classes = traits.List(desc="List of specific meta data "
"classifications to include. If not "
"specified include everything.")
exclude_classes = traits.List(desc="List of meta data "
"classifications to exclude")
class CopyMetaOutputSpec(TraitedSpec):
dest_file = File(exists=True)
class CopyMeta(BaseInterface):
'''Copy meta data from one Nifti file to another. Useful for preserving
meta data after some processing steps.'''
input_spec = CopyMetaInputSpec
output_spec = CopyMetaOutputSpec
def _run_interface(self, runtime):
src_nii = nb.load(self.inputs.src_file)
src = NiftiWrapper(src_nii, make_empty=True)
dest_nii = nb.load(self.inputs.dest_file)
dest = NiftiWrapper(dest_nii, make_empty=True)
classes = src.meta_ext.get_valid_classes()
if self.inputs.include_classes:
classes = [cls
for cls in classes
if cls in self.inputs.include_classes
]
if self.inputs.exclude_classes:
classes = [cls
for cls in classes
if not cls in self.inputs.exclude_classes
]
for cls in classes:
src_dict = src.meta_ext.get_class_dict(cls)
dest_dict = dest.meta_ext.get_class_dict(cls)
dest_dict.update(src_dict)
# Update the shape and slice dimension to reflect the meta extension update.
dest.meta_ext.slice_dim = src.meta_ext.slice_dim
dest.meta_ext.shape = src.meta_ext.shape
self.out_path = path.join(os.getcwd(),
path.basename(self.inputs.dest_file))
dest.to_filename(self.out_path)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs['dest_file'] = self.out_path
return outputs
class MergeNiftiInputSpec(NiftiGeneratorBaseInputSpec):
in_files = traits.List(mandatory=True,
desc="List of Nifti files to merge")
sort_order = traits.Either(traits.Str(),
traits.List(),
desc="One or more meta data keys to "
"sort files by.")
merge_dim = traits.Int(desc="Dimension to merge along. If not "
"specified, the last singular or "
"non-existant dimension is used.")
class MergeNiftiOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="Merged Nifti file")
def make_key_func(meta_keys, index=None):
def key_func(src_nii):
result = [src_nii.get_meta(key, index) for key in meta_keys]
return result
return key_func
class MergeNifti(NiftiGeneratorBase):
'''Merge multiple Nifti files into one. Merges together meta data
extensions as well.'''
input_spec = MergeNiftiInputSpec
output_spec = MergeNiftiOutputSpec
def _run_interface(self, runtime):
niis = [nb.load(fn)
for fn in self.inputs.in_files
]
nws = [NiftiWrapper(nii, make_empty=True)
for nii in niis
]
if self.inputs.sort_order:
sort_order = self.inputs.sort_order
if isinstance(sort_order, str):
sort_order = [sort_order]
nws.sort(key=make_key_func(sort_order))
if self.inputs.merge_dim == traits.Undefined:
merge_dim = None
else:
merge_dim = self.inputs.merge_dim
merged = NiftiWrapper.from_sequence(nws, merge_dim)
const_meta = merged.meta_ext.get_class_dict(('global', 'const'))
self.out_path = self._get_out_path(const_meta)
nb.save(merged.nii_img, self.out_path)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = self.out_path
return outputs
class SplitNiftiInputSpec(NiftiGeneratorBaseInputSpec):
in_file = File(exists=True, mandatory=True, desc="Nifti file to split")
split_dim = traits.Int(desc="Dimension to split along. If not "
"specified, the last dimension is used.")
class SplitNiftiOutputSpec(TraitedSpec):
out_list = traits.List(File(exists=True),
desc="Split Nifti files")
class SplitNifti(NiftiGeneratorBase):
'''Split one Nifti file into many along the specified dimension. Each
result has an updated meta data extension as well.'''
input_spec = SplitNiftiInputSpec
output_spec = SplitNiftiOutputSpec
def _run_interface(self, runtime):
self.out_list = []
nii = nb.load(self.inputs.in_file)
nw = NiftiWrapper(nii, make_empty=True)
split_dim = None
if self.inputs.split_dim == traits.Undefined:
split_dim = None
else:
split_dim = self.inputs.split_dim
for split_idx, split_nw in enumerate(nw.split(split_dim)):
const_meta = split_nw.meta_ext.get_class_dict(('global', 'const'))
out_path = self._get_out_path(const_meta, idx=split_idx)
nb.save(split_nw.nii_img, out_path)
self.out_list.append(out_path)
return runtime
def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_list'] = self.out_list
return outputs
|