-
Notifications
You must be signed in to change notification settings - Fork 128
/
anatomical.py
668 lines (545 loc) · 24.9 KB
/
anatomical.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
=======================
The anatomical workflow
=======================
.. image :: _static/anatomical_workflow_source.svg
The anatomical workflow follows the following steps:
#. Conform (reorientations, revise data types) input data and read
associated metadata.
#. Skull-stripping (AFNI).
#. Calculate head mask -- :py:func:`headmsk_wf`.
#. Spatial Normalization to MNI (ANTs)
#. Calculate air mask above the nasial-cerebelum plane -- :py:func:`airmsk_wf`.
#. Brain tissue segmentation (FAST).
#. Extraction of IQMs -- :py:func:`compute_iqms`.
#. Individual-reports generation -- :py:func:`individual_reports`.
This workflow is orchestrated by :py:func:`anat_qc_workflow`.
For the skull-stripping, we use ``afni_wf`` from ``niworkflows.anat.skullstrip``:
.. workflow::
from niworkflows.anat.skullstrip import afni_wf
from mriqc.testing import mock_config
with mock_config():
wf = afni_wf()
"""
from .. import config
from nipype.pipeline import engine as pe
from nipype.interfaces import io as nio
from nipype.interfaces import utility as niu
from nipype.interfaces import fsl, ants
from templateflow.api import get as get_template
from ..interfaces import (StructuralQC, ArtifactMask, ConformImage,
ComputeQI2, IQMFileSink, RotationMask)
from ..interfaces.reports import AddProvenance
from .utils import get_fwhmx
def anat_qc_workflow(name='anatMRIQC'):
"""
One-subject-one-session-one-run pipeline to extract the NR-IQMs from
anatomical images
.. workflow::
import os.path as op
from mriqc.workflows.anatomical import anat_qc_workflow
from mriqc.testing import mock_config
with mock_config():
wf = anat_qc_workflow()
"""
from niworkflows.anat.skullstrip import afni_wf as skullstrip_wf
dataset = config.workflow.inputs.get("T1w", []) \
+ config.workflow.inputs.get("T2w", [])
config.loggers.workflow.info(f"""\
Building anatomical MRIQC workflow for files: {', '.join(dataset)}.""")
# Initialize workflow
workflow = pe.Workflow(name=name)
# Define workflow, inputs and outputs
# 0. Get data
inputnode = pe.Node(niu.IdentityInterface(fields=['in_file']), name='inputnode')
inputnode.iterables = [('in_file', dataset)]
outputnode = pe.Node(niu.IdentityInterface(fields=['out_json']), name='outputnode')
# 1. Reorient anatomical image
to_ras = pe.Node(ConformImage(check_dtype=False), name='conform')
# 2. Skull-stripping (afni)
asw = skullstrip_wf(n4_nthreads=config.nipype.omp_nthreads, unifize=False)
# 3. Head mask
hmsk = headmsk_wf()
# 4. Spatial Normalization, using ANTs
norm = spatial_normalization()
# 5. Air mask (with and without artifacts)
amw = airmsk_wf()
# 6. Brain tissue segmentation
segment = pe.Node(fsl.FAST(segments=True, out_basename='segment'),
name='segmentation', mem_gb=5)
# 7. Compute IQMs
iqmswf = compute_iqms()
# Reports
repwf = individual_reports()
# Connect all nodes
workflow.connect([
(inputnode, to_ras, [('in_file', 'in_file')]),
(inputnode, iqmswf, [('in_file', 'inputnode.in_file')]),
(inputnode, norm, [(('in_file', _get_mod), 'inputnode.modality')]),
(inputnode, segment, [(('in_file', _get_imgtype), 'img_type')]),
(to_ras, asw, [('out_file', 'inputnode.in_file')]),
(asw, segment, [('outputnode.out_file', 'in_files')]),
(asw, hmsk, [('outputnode.bias_corrected', 'inputnode.in_file')]),
(segment, hmsk, [('tissue_class_map', 'inputnode.in_segm')]),
(asw, norm, [('outputnode.bias_corrected', 'inputnode.moving_image'),
('outputnode.out_mask', 'inputnode.moving_mask')]),
(norm, amw, [
('outputnode.inverse_composite_transform', 'inputnode.inverse_composite_transform')]),
(norm, iqmswf, [
('outputnode.inverse_composite_transform', 'inputnode.inverse_composite_transform')]),
(norm, repwf, ([
('outputnode.out_report', 'inputnode.mni_report')])),
(to_ras, amw, [('out_file', 'inputnode.in_file')]),
(asw, amw, [('outputnode.out_mask', 'inputnode.in_mask')]),
(hmsk, amw, [('outputnode.out_file', 'inputnode.head_mask')]),
(to_ras, iqmswf, [('out_file', 'inputnode.in_ras')]),
(asw, iqmswf, [('outputnode.bias_corrected', 'inputnode.inu_corrected'),
('outputnode.bias_image', 'inputnode.in_inu'),
('outputnode.out_mask', 'inputnode.brainmask')]),
(amw, iqmswf, [('outputnode.air_mask', 'inputnode.airmask'),
('outputnode.hat_mask', 'inputnode.hatmask'),
('outputnode.art_mask', 'inputnode.artmask'),
('outputnode.rot_mask', 'inputnode.rotmask')]),
(segment, iqmswf, [('tissue_class_map', 'inputnode.segmentation'),
('partial_volume_files', 'inputnode.pvms')]),
(hmsk, iqmswf, [('outputnode.out_file', 'inputnode.headmask')]),
(to_ras, repwf, [('out_file', 'inputnode.in_ras')]),
(asw, repwf, [('outputnode.bias_corrected', 'inputnode.inu_corrected'),
('outputnode.out_mask', 'inputnode.brainmask')]),
(hmsk, repwf, [('outputnode.out_file', 'inputnode.headmask')]),
(amw, repwf, [('outputnode.air_mask', 'inputnode.airmask'),
('outputnode.art_mask', 'inputnode.artmask'),
('outputnode.rot_mask', 'inputnode.rotmask')]),
(segment, repwf, [('tissue_class_map', 'inputnode.segmentation')]),
(iqmswf, repwf, [('outputnode.noisefit', 'inputnode.noisefit')]),
(iqmswf, repwf, [('outputnode.out_file', 'inputnode.in_iqms')]),
(iqmswf, outputnode, [('outputnode.out_file', 'out_json')])
])
# Upload metrics
if not config.execution.no_sub:
from ..interfaces.webapi import UploadIQMs
upldwf = pe.Node(UploadIQMs(), name='UploadMetrics')
upldwf.inputs.url = config.execution.webapi_url
upldwf.inputs.strict = config.execution.upload_strict
if config.execution.webapi_port:
upldwf.inputs.port = config.execution.webapi_port
workflow.connect([
(iqmswf, upldwf, [('outputnode.out_file', 'in_iqms')]),
(upldwf, repwf, [('api_id', 'inputnode.api_id')]),
])
return workflow
def spatial_normalization(name='SpatialNormalization', resolution=2):
"""Create a simplied workflow to perform fast spatial normalization."""
from niworkflows.interfaces.registration import (
RobustMNINormalizationRPT as RobustMNINormalization
)
# Have the template id handy
tpl_id = config.workflow.template_id
# Define workflow interface
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=[
'moving_image', 'moving_mask', 'modality']), name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=[
'inverse_composite_transform', 'out_report']), name='outputnode')
# Spatial normalization
norm = pe.Node(RobustMNINormalization(
flavor=['testing', 'fast'][config.execution.debug],
num_threads=config.nipype.omp_nthreads,
float=config.execution.ants_float,
template=tpl_id,
template_resolution=resolution,
generate_report=True,),
name='SpatialNormalization',
# Request all MultiProc processes when ants_nthreads > n_procs
num_threads=config.nipype.omp_nthreads,
mem_gb=3)
norm.inputs.reference_mask = str(
get_template(tpl_id, resolution=resolution, desc='brain', suffix='mask'))
workflow.connect([
(inputnode, norm, [('moving_image', 'moving_image'),
('moving_mask', 'moving_mask'),
('modality', 'reference')]),
(norm, outputnode, [('inverse_composite_transform', 'inverse_composite_transform'),
('out_report', 'out_report')]),
])
return workflow
def compute_iqms(name='ComputeIQMs'):
"""
Setup the workflow that actually computes the IQMs.
.. workflow::
from mriqc.workflows.anatomical import compute_iqms
from mriqc.testing import mock_config
with mock_config():
wf = compute_iqms()
"""
from niworkflows.interfaces.bids import ReadSidecarJSON
from .utils import _tofloat
from ..interfaces.anatomical import Harmonize
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=[
'in_file', 'in_ras',
'brainmask', 'airmask', 'artmask', 'headmask', 'rotmask', 'hatmask',
'segmentation', 'inu_corrected', 'in_inu', 'pvms', 'metadata',
'inverse_composite_transform']), name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=['out_file', 'noisefit']),
name='outputnode')
# Extract metadata
meta = pe.Node(ReadSidecarJSON(), name='metadata')
# Add provenance
addprov = pe.Node(AddProvenance(), name='provenance',
run_without_submitting=True)
# AFNI check smoothing
fwhm_interface = get_fwhmx()
fwhm = pe.Node(fwhm_interface, name='smoothness')
# Harmonize
homog = pe.Node(Harmonize(), name='harmonize')
# Mortamet's QI2
getqi2 = pe.Node(ComputeQI2(), name='ComputeQI2')
# Compute python-coded measures
measures = pe.Node(StructuralQC(), 'measures')
# Project MNI segmentation to T1 space
invt = pe.MapNode(ants.ApplyTransforms(
dimension=3, default_value=0, interpolation='Linear',
float=True),
iterfield=['input_image'], name='MNItpms2t1')
invt.inputs.input_image = [str(p) for p in get_template(
config.workflow.template_id, suffix='probseg', resolution=1,
label=['CSF', 'GM', 'WM'])]
datasink = pe.Node(IQMFileSink(
out_dir=config.execution.output_dir,
dataset=config.execution.dsname),
name='datasink', run_without_submitting=True)
def _getwm(inlist):
return inlist[-1]
workflow.connect([
(inputnode, meta, [('in_file', 'in_file')]),
(inputnode, datasink, [('in_file', 'in_file'),
(('in_file', _get_mod), 'modality')]),
(inputnode, addprov, [(('in_file', _get_mod), 'modality')]),
(meta, datasink, [('subject', 'subject_id'),
('session', 'session_id'),
('task', 'task_id'),
('acquisition', 'acq_id'),
('reconstruction', 'rec_id'),
('run', 'run_id'),
('out_dict', 'metadata')]),
(inputnode, addprov, [('in_file', 'in_file'),
('airmask', 'air_msk'),
('rotmask', 'rot_msk')]),
(inputnode, getqi2, [('in_ras', 'in_file'),
('hatmask', 'air_msk')]),
(inputnode, homog, [('inu_corrected', 'in_file'),
(('pvms', _getwm), 'wm_mask')]),
(inputnode, measures, [('in_inu', 'in_bias'),
('in_ras', 'in_file'),
('airmask', 'air_msk'),
('headmask', 'head_msk'),
('artmask', 'artifact_msk'),
('rotmask', 'rot_msk'),
('segmentation', 'in_segm'),
('pvms', 'in_pvms')]),
(inputnode, fwhm, [('in_ras', 'in_file'),
('brainmask', 'mask')]),
(inputnode, invt, [('in_ras', 'reference_image'),
('inverse_composite_transform', 'transforms')]),
(homog, measures, [('out_file', 'in_noinu')]),
(invt, measures, [('output_image', 'mni_tpms')]),
(fwhm, measures, [(('fwhm', _tofloat), 'in_fwhm')]),
(measures, datasink, [('out_qc', 'root')]),
(addprov, datasink, [('out_prov', 'provenance')]),
(getqi2, datasink, [('qi2', 'qi_2')]),
(getqi2, outputnode, [('out_file', 'noisefit')]),
(datasink, outputnode, [('out_file', 'out_file')]),
])
return workflow
def individual_reports(name='ReportsWorkflow'):
"""
Generate the components of the individual report.
.. workflow::
from mriqc.workflows.anatomical import individual_reports
from mriqc.testing import mock_config
with mock_config():
wf = individual_reports()
"""
from ..interfaces import PlotMosaic
from ..interfaces.reports import IndividualReport
verbose = config.execution.verbose_reports
pages = 2
extra_pages = int(verbose) * 7
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=[
'in_ras', 'brainmask', 'headmask', 'airmask', 'artmask', 'rotmask',
'segmentation', 'inu_corrected', 'noisefit', 'in_iqms',
'mni_report', 'api_id']),
name='inputnode')
mosaic_zoom = pe.Node(PlotMosaic(
out_file='plot_anat_mosaic1_zoomed.svg',
cmap='Greys_r'), name='PlotMosaicZoomed')
mosaic_noise = pe.Node(PlotMosaic(
out_file='plot_anat_mosaic2_noise.svg',
only_noise=True,
cmap='viridis_r'), name='PlotMosaicNoise')
mplots = pe.Node(niu.Merge(pages + extra_pages), name='MergePlots')
rnode = pe.Node(IndividualReport(), name='GenerateReport')
# Link images that should be reported
dsplots = pe.Node(nio.DataSink(base_directory=str(config.execution.output_dir),
parameterization=False),
name='dsplots', run_without_submitting=True)
workflow.connect([
(inputnode, rnode, [('in_iqms', 'in_iqms')]),
(inputnode, mosaic_zoom, [('in_ras', 'in_file'),
('brainmask', 'bbox_mask_file')]),
(inputnode, mosaic_noise, [('in_ras', 'in_file')]),
(mosaic_zoom, mplots, [('out_file', "in1")]),
(mosaic_noise, mplots, [('out_file', "in2")]),
(mplots, rnode, [('out', 'in_plots')]),
(rnode, dsplots, [('out_file', "@html_report")]),
])
if not verbose:
return workflow
from ..interfaces.viz import PlotContours
plot_segm = pe.Node(PlotContours(
display_mode='z', levels=[.5, 1.5, 2.5], cut_coords=10,
colors=['r', 'g', 'b']), name='PlotSegmentation')
plot_bmask = pe.Node(PlotContours(
display_mode='z', levels=[.5], colors=['r'], cut_coords=10,
out_file='bmask'), name='PlotBrainmask')
plot_airmask = pe.Node(PlotContours(
display_mode='x', levels=[.5], colors=['r'],
cut_coords=6, out_file='airmask'), name='PlotAirmask')
plot_headmask = pe.Node(PlotContours(
display_mode='x', levels=[.5], colors=['r'],
cut_coords=6, out_file='headmask'), name='PlotHeadmask')
plot_artmask = pe.Node(PlotContours(
display_mode='z', levels=[.5], colors=['r'], cut_coords=10,
out_file='artmask', saturate=True), name='PlotArtmask')
workflow.connect([
(inputnode, plot_segm, [('in_ras', 'in_file'),
('segmentation', 'in_contours')]),
(inputnode, plot_bmask, [('in_ras', 'in_file'),
('brainmask', 'in_contours')]),
(inputnode, plot_headmask, [('in_ras', 'in_file'),
('headmask', 'in_contours')]),
(inputnode, plot_airmask, [('in_ras', 'in_file'),
('airmask', 'in_contours')]),
(inputnode, plot_artmask, [('in_ras', 'in_file'),
('artmask', 'in_contours')]),
(inputnode, mplots, [('mni_report', f"in{pages + 1}")]),
(plot_bmask, mplots, [('out_file', f'in{pages + 2}')]),
(plot_segm, mplots, [('out_file', f'in{pages + 3}')]),
(plot_artmask, mplots, [('out_file', f'in{pages + 4}')]),
(plot_headmask, mplots, [('out_file', f'in{pages + 5}')]),
(plot_airmask, mplots, [('out_file', f'in{pages + 6}')]),
(inputnode, mplots, [('noisefit', f'in{pages + 7}')]),
])
return workflow
def headmsk_wf(name='HeadMaskWorkflow'):
"""
Computes a head mask as in [Mortamet2009]_.
.. workflow::
from mriqc.workflows.anatomical import headmsk_wf
from mriqc.testing import mock_config
with mock_config():
wf = headmsk_wf()
"""
use_bet = config.workflow.headmask.upper() == "BET"
has_dipy = False
if not use_bet:
try:
from dipy.denoise import nlmeans # noqa
has_dipy = True
except ImportError:
pass
if not use_bet and not has_dipy:
raise RuntimeError("DIPY is not installed and ``config.workflow.headmask`` is not BET.")
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=['in_file', 'in_segm']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=['out_file']), name='outputnode')
if use_bet:
# Alternative for when dipy is not installed
bet = pe.Node(fsl.BET(surfaces=True), name='fsl_bet')
workflow.connect([
(inputnode, bet, [('in_file', 'in_file')]),
(bet, outputnode, [('outskin_mask_file', 'out_file')])
])
else:
from nipype.interfaces.dipy import Denoise
enhance = pe.Node(niu.Function(
input_names=['in_file'], output_names=['out_file'], function=_enhance), name='Enhance')
estsnr = pe.Node(niu.Function(
input_names=['in_file', 'seg_file'], output_names=['out_snr'],
function=_estimate_snr), name='EstimateSNR')
denoise = pe.Node(Denoise(), name='Denoise')
gradient = pe.Node(niu.Function(
input_names=['in_file', 'snr'], output_names=['out_file'],
function=image_gradient), name='Grad')
thresh = pe.Node(niu.Function(
input_names=['in_file', 'in_segm'], output_names=['out_file'],
function=gradient_threshold), name='GradientThreshold')
workflow.connect([
(inputnode, estsnr, [('in_file', 'in_file'),
('in_segm', 'seg_file')]),
(estsnr, denoise, [('out_snr', 'snr')]),
(inputnode, enhance, [('in_file', 'in_file')]),
(enhance, denoise, [('out_file', 'in_file')]),
(estsnr, gradient, [('out_snr', 'snr')]),
(denoise, gradient, [('out_file', 'in_file')]),
(inputnode, thresh, [('in_segm', 'in_segm')]),
(gradient, thresh, [('out_file', 'in_file')]),
(thresh, outputnode, [('out_file', 'out_file')])
])
return workflow
def airmsk_wf(name='AirMaskWorkflow'):
"""
Implements the Step 1 of [Mortamet2009]_.
.. workflow::
from mriqc.workflows.anatomical import airmsk_wf
from mriqc.testing import mock_config
with mock_config():
wf = airmsk_wf()
"""
workflow = pe.Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(
fields=['in_file', 'in_mask', 'head_mask', 'inverse_composite_transform']),
name='inputnode')
outputnode = pe.Node(niu.IdentityInterface(fields=[
'hat_mask', 'air_mask', 'art_mask', 'rot_mask']), name='outputnode')
rotmsk = pe.Node(RotationMask(), name='RotationMask')
invt = pe.Node(ants.ApplyTransforms(
dimension=3, default_value=0, interpolation='MultiLabel', float=True),
name='invert_xfm')
invt.inputs.input_image = str(get_template(
'MNI152NLin2009cAsym', resolution=1, desc='head', suffix='mask'))
qi1 = pe.Node(ArtifactMask(), name='ArtifactMask')
workflow.connect([
(inputnode, rotmsk, [('in_file', 'in_file')]),
(inputnode, qi1, [('in_file', 'in_file'),
('head_mask', 'head_mask')]),
(rotmsk, qi1, [('out_file', 'rot_mask')]),
(inputnode, invt, [('in_mask', 'reference_image'),
('inverse_composite_transform', 'transforms')]),
(invt, qi1, [('output_image', 'nasion_post_mask')]),
(qi1, outputnode, [('out_hat_msk', 'hat_mask'),
('out_air_msk', 'air_mask'),
('out_art_msk', 'art_mask')]),
(rotmsk, outputnode, [('out_file', 'rot_mask')])
])
return workflow
def _binarize(in_file, threshold=0.5, out_file=None):
import os.path as op
import numpy as np
import nibabel as nb
if out_file is None:
fname, ext = op.splitext(op.basename(in_file))
if ext == '.gz':
fname, ext2 = op.splitext(fname)
ext = ext2 + ext
out_file = op.abspath('{}_bin{}'.format(fname, ext))
nii = nb.load(in_file)
data = nii.get_data()
data[data <= threshold] = 0
data[data > 0] = 1
hdr = nii.header.copy()
hdr.set_data_dtype(np.uint8)
nb.Nifti1Image(data.astype(np.uint8), nii.affine, hdr).to_filename(
out_file)
return out_file
def _estimate_snr(in_file, seg_file):
import numpy as np
import nibabel as nb
from mriqc.qc.anatomical import snr
data = nb.load(in_file).get_data()
mask = nb.load(seg_file).get_data() == 2 # WM label
out_snr = snr(np.mean(data[mask]), data[mask].std(), mask.sum())
return out_snr
def _enhance(in_file, out_file=None):
import os.path as op
import numpy as np
import nibabel as nb
if out_file is None:
fname, ext = op.splitext(op.basename(in_file))
if ext == '.gz':
fname, ext2 = op.splitext(fname)
ext = ext2 + ext
out_file = op.abspath(f'{fname}_enhanced{ext}')
imnii = nb.load(in_file)
data = imnii.get_data().astype(np.float32) # pylint: disable=no-member
range_max = np.percentile(data[data > 0], 99.98)
range_min = np.median(data[data > 0])
# Resample signal excess pixels
excess = np.where(data > range_max)
data[excess] = 0
data[excess] = np.random.choice(data[data > range_min], size=len(excess[0]))
nb.Nifti1Image(data, imnii.affine, imnii.header).to_filename(
out_file)
return out_file
def image_gradient(in_file, snr, out_file=None):
"""Computes the magnitude gradient of an image using numpy"""
import os.path as op
import numpy as np
import nibabel as nb
from scipy.ndimage import gaussian_gradient_magnitude as gradient
if out_file is None:
fname, ext = op.splitext(op.basename(in_file))
if ext == '.gz':
fname, ext2 = op.splitext(fname)
ext = ext2 + ext
out_file = op.abspath(f'{fname}_grad{ext}')
imnii = nb.load(in_file)
data = imnii.get_data().astype(np.float32) # pylint: disable=no-member
datamax = np.percentile(data.reshape(-1), 99.5)
data *= 100 / datamax
grad = gradient(data, 3.0)
gradmax = np.percentile(grad.reshape(-1), 99.5)
grad *= 100.
grad /= gradmax
nb.Nifti1Image(grad, imnii.affine, imnii.header).to_filename(out_file)
return out_file
def gradient_threshold(in_file, in_segm, thresh=1.0, out_file=None):
""" Compute a threshold from the histogram of the magnitude gradient image """
import os.path as op
import numpy as np
import nibabel as nb
from scipy import ndimage as sim
struc = sim.iterate_structure(sim.generate_binary_structure(3, 2), 2)
if out_file is None:
fname, ext = op.splitext(op.basename(in_file))
if ext == '.gz':
fname, ext2 = op.splitext(fname)
ext = ext2 + ext
out_file = op.abspath(f'{fname}_gradmask{ext}')
imnii = nb.load(in_file)
hdr = imnii.header.copy()
hdr.set_data_dtype(np.uint8) # pylint: disable=no-member
data = imnii.get_data().astype(np.float32)
mask = np.zeros_like(data, dtype=np.uint8) # pylint: disable=no-member
mask[data > 15.] = 1
segdata = nb.load(in_segm).get_data().astype(np.uint8)
segdata[segdata > 0] = 1
segdata = sim.binary_dilation(
segdata, struc, iterations=2, border_value=1).astype(np.uint8)
mask[segdata > 0] = 1
mask = sim.binary_closing(mask, struc, iterations=2).astype(np.uint8)
# Remove small objects
label_im, nb_labels = sim.label(mask)
artmsk = np.zeros_like(mask)
if nb_labels > 2:
sizes = sim.sum(mask, label_im, list(range(nb_labels + 1)))
ordered = list(reversed(sorted(zip(sizes, list(range(nb_labels + 1))))))
for _, label in ordered[2:]:
mask[label_im == label] = 0
artmsk[label_im == label] = 1
mask = sim.binary_fill_holes(mask, struc).astype(np.uint8) # pylint: disable=no-member
nb.Nifti1Image(mask, imnii.affine, hdr).to_filename(out_file)
return out_file
def _get_imgtype(in_file):
from pathlib import Path
return int(
Path(in_file).name.rstrip(".gz").rstrip(".nii").split("_")[-1][1]
)
def _get_mod(in_file):
from pathlib import Path
return Path(in_file).name.rstrip(".gz").rstrip(".nii").split("_")[-1]