-
Notifications
You must be signed in to change notification settings - Fork 15
/
utility.py
334 lines (257 loc) · 10.7 KB
/
utility.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
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
"""Utility interfaces for ASLPrep."""
import os
import nibabel as nb
import pandas as pd
from nilearn import image
from nipype.interfaces.base import (
BaseInterfaceInputSpec,
File,
SimpleInterface,
TraitedSpec,
traits,
)
from nipype.interfaces.fsl.base import FSLCommand, FSLCommandInputSpec
from nipype.utils.filemanip import fname_presuffix, load_json, save_json
from aslprep import config
from aslprep.utils.asl import reduce_metadata_lists
class _ReduceASLFilesInputSpec(BaseInterfaceInputSpec):
asl_file = File(exists=True, mandatory=True, desc="ASL file to split.")
aslcontext = File(exists=True, mandatory=True, desc="aslcontext TSV.")
processing_target = traits.Str()
metadata = traits.Dict()
class _ReduceASLFilesOutputSpec(TraitedSpec):
asl_file = File(exists=True, desc="Modified ASL file.")
aslcontext = File(exists=True, desc="Modified aslcontext file.")
metadata = traits.Dict()
class ReduceASLFiles(SimpleInterface):
"""Split ASL data into different files."""
input_spec = _ReduceASLFilesInputSpec
output_spec = _ReduceASLFilesOutputSpec
def _run_interface(self, runtime):
aslcontext = pd.read_table(self.inputs.aslcontext)
asl_img = nb.load(self.inputs.asl_file)
assert asl_img.shape[3] == aslcontext.shape[0]
if self.inputs.processing_target == "controllabel":
files_to_keep = ["control", "label", "m0scan"]
elif self.inputs.processing_target == "deltam":
files_to_keep = ["deltam", "m0scan"]
else:
files_to_keep = ["cbf", "m0scan"]
asl_idx = aslcontext.loc[aslcontext["volume_type"].isin(files_to_keep)].index.values
asl_idx = asl_idx.astype(int)
self._results["metadata"] = reduce_metadata_lists(self.inputs.metadata, asl_idx)
asl_img = image.index_img(asl_img, asl_idx)
self._results["asl_file"] = fname_presuffix(
self.inputs.asl_file,
suffix="_reduced",
newpath=runtime.cwd,
use_ext=True,
)
asl_img.to_filename(self._results["asl_file"])
aslcontext = aslcontext.loc[asl_idx]
self._results["aslcontext"] = fname_presuffix(
self.inputs.aslcontext,
suffix="_reduced",
newpath=runtime.cwd,
use_ext=True,
)
aslcontext.to_csv(self._results["aslcontext"], sep="\t", index=False)
return runtime
class _RMSDiffInputSpec(FSLCommandInputSpec):
matrixfile1 = File(
exists=True,
position=0,
argstr="%s",
desc="First matrix file.",
mandatory=True,
)
matrixfile2 = File(
exists=True,
position=1,
argstr="%s",
desc="Second matrix file.",
mandatory=True,
)
ref_vol = File(
exists=True,
position=2,
argstr="%s",
desc="Reference volume.",
mandatory=True,
)
class _RMSDiffOutputSpec(TraitedSpec):
rmsd = traits.Float()
class RMSDiff(FSLCommand):
"""Run rmsdiff."""
_cmd = "rmsdiff"
input_spec = _RMSDiffInputSpec
output_spec = _RMSDiffOutputSpec
def aggregate_outputs(self, runtime=None, needed_outputs=None): # noqa: U100
"""Taken from nipype.interfaces.afni.preprocess.ClipLevel."""
outputs = self._outputs()
outfile = os.path.join(os.getcwd(), "stat_result.json")
if runtime is None:
try:
rmsd = load_json(outfile)["stat"]
except IOError:
return self.run().outputs
else:
rmsd = []
for line in runtime.stdout.split("\n"):
if line:
values = line.split()
if len(values) > 1:
rmsd.append([float(val) for val in values])
else:
rmsd.extend([float(val) for val in values])
if len(rmsd) == 1:
rmsd = rmsd[0]
save_json(outfile, dict(stat=rmsd))
outputs.rmsd = rmsd
return outputs
class _PairwiseRMSDiffInputSpec(BaseInterfaceInputSpec):
in_files = traits.List(
File(exists=True),
desc="Matrix files to compare with each other.",
mandatory=True,
)
ref_file = File(
exists=True,
desc="Reference volume.",
mandatory=True,
)
class _PairwiseRMSDiffOutputSpec(TraitedSpec):
out_file = File(exists=True, desc="Output txt file.")
class PairwiseRMSDiff(SimpleInterface):
"""Run rmsdiff on each contiguous pair of transform files to build a txt file of rmsd values.
This interface uses :class:`~aslprep.interfaces.utility.RMSDiff` internally, which may not be
a proper nipype pattern.
"""
input_spec = _PairwiseRMSDiffInputSpec
output_spec = _PairwiseRMSDiffOutputSpec
def _run_interface(self, runtime):
rmsd = []
for i_file in range(len(self.inputs.in_files) - 1):
j_file = i_file + 1
file1 = self.inputs.in_files[i_file]
file2 = self.inputs.in_files[j_file]
# run rmsdiff
rmsdiff = RMSDiff(matrixfile1=file1, matrixfile2=file2, ref_vol=self.inputs.ref_file)
res = rmsdiff.run()
assert isinstance(res.outputs.rmsd, float)
rmsd.append(str(res.outputs.rmsd))
self._results["out_file"] = fname_presuffix(
self.inputs.ref_file,
suffix="_rmsd.txt",
newpath=runtime.cwd,
use_ext=False,
)
with open(self._results["out_file"], "w") as fo:
fo.write("\n".join(rmsd))
return runtime
class _CombineMotionParametersInputSpec(BaseInterfaceInputSpec):
m0type = traits.Str()
processing_target = traits.Str()
aslcontext = File(exists=True)
control_mat_files = traits.Either(traits.List(File(exists=True)), None)
control_par_file = traits.Either(File(exists=True), None)
label_mat_files = traits.Either(traits.List(File(exists=True)), None)
label_par_file = traits.Either(File(exists=True), None)
deltam_mat_files = traits.Either(traits.List(File(exists=True)), None)
deltam_par_file = traits.Either(File(exists=True), None)
cbf_mat_files = traits.Either(traits.List(File(exists=True)), None)
cbf_par_file = traits.Either(File(exists=True), None)
m0scan_mat_files = traits.Either(traits.List(File(exists=True)), None)
m0scan_par_file = traits.Either(File(exists=True), None)
class _CombineMotionParametersOutputSpec(TraitedSpec):
mat_file_list = traits.List(File(exists=True))
combined_par_file = File(exists=True)
class CombineMotionParameters(SimpleInterface):
"""Combine motion parameter files from MCFLIRT across image types."""
input_spec = _CombineMotionParametersInputSpec
output_spec = _CombineMotionParametersOutputSpec
def _run_interface(self, runtime):
aslcontext = pd.read_table(self.inputs.aslcontext)
files_to_combine = sorted(list(set(aslcontext["volume_type"].tolist())))
out_par = [None] * aslcontext.shape[0]
out_mat_files = [None] * aslcontext.shape[0]
for file_to_combine in files_to_combine:
mat_files = getattr(self.inputs, f"{file_to_combine}_mat_files")
par_file = getattr(self.inputs, f"{file_to_combine}_par_file")
idx = aslcontext.loc[aslcontext["volume_type"] == file_to_combine].index.values
with open(par_file, "r") as fo:
par = fo.readlines()
for i_vol, vol_idx in enumerate(idx):
out_par[vol_idx] = par[i_vol]
out_mat_files[vol_idx] = mat_files[i_vol]
self._results["combined_par_file"] = fname_presuffix(
par_file,
suffix="_combined",
newpath=runtime.cwd,
use_ext=True,
)
with open(self._results["combined_par_file"], "w") as fo:
fo.write("".join(out_par))
self._results["mat_file_list"] = out_mat_files
return runtime
class _SplitOutVolumeTypeInputSpec(BaseInterfaceInputSpec):
volumetype = traits.Str()
aslcontext = File(exists=True)
asl_file = File(exists=True)
class _SplitOutVolumeTypeOutputSpec(TraitedSpec):
out_file = File(exists=True)
class SplitOutVolumeType(SimpleInterface):
"""Split out a specific volume type from the ASL file."""
input_spec = _SplitOutVolumeTypeInputSpec
output_spec = _SplitOutVolumeTypeOutputSpec
def _run_interface(self, runtime):
aslcontext = pd.read_table(self.inputs.aslcontext)
volumetype_df = aslcontext.loc[aslcontext["volume_type"] == self.inputs.volumetype]
volumetype_idx = volumetype_df.index.tolist()
if len(volumetype_idx) == 0:
raise ValueError(f"No volumes found for {self.inputs.volumetype}")
out_img = image.index_img(self.inputs.asl_file, volumetype_idx)
self._results["out_file"] = fname_presuffix(
self.inputs.asl_file,
suffix=f"_{self.inputs.volumetype}",
newpath=runtime.cwd,
use_ext=True,
)
out_img.to_filename(self._results["out_file"])
return runtime
class _SplitReferenceTargetInputSpec(BaseInterfaceInputSpec):
aslcontext = File(exists=True, required=True)
asl_file = File(exists=True, required=True)
class _SplitReferenceTargetOutputSpec(TraitedSpec):
out_file = File(exists=True)
class SplitReferenceTarget(SimpleInterface):
"""Split out just the optimal volume type for reference files from the overall ASL file.
This means grabbing M0 volumes if they're available, or control volumes if *they're* available,
or deltams, or cbfs.
"""
input_spec = _SplitReferenceTargetInputSpec
output_spec = _SplitReferenceTargetOutputSpec
def _run_interface(self, runtime):
aslcontext = pd.read_table(self.inputs.aslcontext)
volume_types = aslcontext["volume_type"].values
if "m0scan" in volume_types:
ref_target = "m0scan"
elif "control" in volume_types:
ref_target = "control"
elif "deltam" in volume_types:
ref_target = "deltam"
elif "cbf" in volume_types:
ref_target = "cbf"
else:
raise ValueError(volume_types)
config.loggers.interface.warning(f"Selected {ref_target} for reference.")
volumetype_idx = aslcontext["volume_type"].loc[volume_types == ref_target].index.tolist()
out_img = image.index_img(self.inputs.asl_file, volumetype_idx)
self._results["out_file"] = fname_presuffix(
self.inputs.asl_file,
suffix=f"_{ref_target}",
newpath=runtime.cwd,
use_ext=True,
)
out_img.to_filename(self._results["out_file"])
return runtime