-
Notifications
You must be signed in to change notification settings - Fork 287
/
nilearn.py
155 lines (124 loc) · 5.72 KB
/
nilearn.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
# -*- 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:
"""
Image tools interfaces
~~~~~~~~~~~~~~~~~~~~~~
"""
import nibabel as nb
import numpy as np
from skimage import morphology as sim
from scipy.ndimage.morphology import binary_fill_holes
from nilearn.masking import compute_epi_mask
from nilearn.image import concat_imgs
from nipype import logging
from nipype.utils.filemanip import fname_presuffix
from nipype.interfaces.base import (
traits, isdefined, TraitedSpec, BaseInterfaceInputSpec,
File, InputMultiPath, SimpleInterface
)
LOGGER = logging.getLogger('nipype.interface')
class MaskEPIInputSpec(BaseInterfaceInputSpec):
in_files = InputMultiPath(File(exists=True), mandatory=True,
desc='input EPI or list of files')
lower_cutoff = traits.Float(0.2, usedefault=True)
upper_cutoff = traits.Float(0.85, usedefault=True)
connected = traits.Bool(True, usedefault=True)
enhance_t2 = traits.Bool(False, usedefault=True,
desc='enhance T2 contrast on image')
opening = traits.Int(2, usedefault=True)
closing = traits.Bool(True, usedefault=True)
fill_holes = traits.Bool(True, usedefault=True)
exclude_zeros = traits.Bool(False, usedefault=True)
ensure_finite = traits.Bool(True, usedefault=True)
target_affine = traits.Either(None, traits.File(exists=True),
default=None, usedefault=True)
target_shape = traits.Either(None, traits.File(exists=True),
default=None, usedefault=True)
no_sanitize = traits.Bool(False, usedefault=True)
class MaskEPIOutputSpec(TraitedSpec):
out_mask = File(exists=True, desc='output mask')
class MaskEPI(SimpleInterface):
input_spec = MaskEPIInputSpec
output_spec = MaskEPIOutputSpec
def _run_interface(self, runtime):
in_files = self.inputs.in_files
if self.inputs.enhance_t2:
in_files = [_enhance_t2_contrast(f, newpath=runtime.cwd)
for f in in_files]
masknii = compute_epi_mask(
in_files,
lower_cutoff=self.inputs.lower_cutoff,
upper_cutoff=self.inputs.upper_cutoff,
connected=self.inputs.connected,
opening=self.inputs.opening,
exclude_zeros=self.inputs.exclude_zeros,
ensure_finite=self.inputs.ensure_finite,
target_affine=self.inputs.target_affine,
target_shape=self.inputs.target_shape
)
if self.inputs.closing:
closed = sim.binary_closing(masknii.get_data().astype(
np.uint8), sim.ball(1)).astype(np.uint8)
masknii = masknii.__class__(closed, masknii.affine,
masknii.header)
if self.inputs.fill_holes:
filled = binary_fill_holes(masknii.get_data().astype(
np.uint8), sim.ball(6)).astype(np.uint8)
masknii = masknii.__class__(filled, masknii.affine,
masknii.header)
if self.inputs.no_sanitize:
in_file = self.inputs.in_files
if isinstance(in_file, list):
in_file = in_file[0]
nii = nb.load(in_file)
qform, code = nii.get_qform(coded=True)
masknii.set_qform(qform, int(code))
sform, code = nii.get_sform(coded=True)
masknii.set_sform(sform, int(code))
self._results['out_mask'] = fname_presuffix(
self.inputs.in_files[0], suffix='_mask', newpath=runtime.cwd)
masknii.to_filename(self._results['out_mask'])
return runtime
class MergeInputSpec(BaseInterfaceInputSpec):
in_files = InputMultiPath(File(exists=True), mandatory=True,
desc='input list of files to merge')
dtype = traits.Enum('f4', 'f8', 'u1', 'u2', 'u4', 'i2', 'i4',
usedefault=True, desc='numpy dtype of output image')
header_source = File(exists=True, desc='a Nifti file from which the header should be copied')
compress = traits.Bool(True, usedefault=True, desc='Use gzip compression on .nii output')
class MergeOutputSpec(TraitedSpec):
out_file = File(exists=True, desc='output merged file')
class Merge(SimpleInterface):
input_spec = MergeInputSpec
output_spec = MergeOutputSpec
def _run_interface(self, runtime):
ext = '.nii.gz' if self.inputs.compress else '.nii'
self._results['out_file'] = fname_presuffix(
self.inputs.in_files[0], suffix='_merged' + ext, newpath=runtime.cwd, use_ext=False)
new_nii = concat_imgs(self.inputs.in_files, dtype=self.inputs.dtype)
if isdefined(self.inputs.header_source):
src_hdr = nb.load(self.inputs.header_source).header
new_nii.header.set_xyzt_units(t=src_hdr.get_xyzt_units()[-1])
new_nii.header.set_zooms(list(new_nii.header.get_zooms()[:3]) +
[src_hdr.get_zooms()[3]])
new_nii.to_filename(self._results['out_file'])
return runtime
def _enhance_t2_contrast(in_file, newpath=None, offset=0.5):
"""
Performs a logarithmic transformation of intensity that
effectively splits brain and background and makes the
overall distribution more Gaussian.
"""
out_file = fname_presuffix(in_file, suffix='_t1enh',
newpath=newpath)
nii = nb.load(in_file)
data = nii.get_data()
maxd = data.max()
newdata = np.log(offset + data / maxd)
newdata -= newdata.min()
newdata *= maxd / newdata.max()
nii = nii.__class__(newdata, nii.affine, nii.header)
nii.to_filename(out_file)
return out_file