File tree Expand file tree Collapse file tree 3 files changed +36
-17
lines changed Expand file tree Collapse file tree 3 files changed +36
-17
lines changed Original file line number Diff line number Diff line change @@ -585,22 +585,6 @@ def reorient(in_file, newpath=None):
585
585
return out_file
586
586
587
587
588
- def extract_wm (in_seg , wm_label = 3 , newpath = None ):
589
- import nibabel as nb
590
- import numpy as np
591
- from nipype .utils .filemanip import fname_presuffix
592
-
593
- nii = nb .load (in_seg )
594
- data = np .zeros (nii .shape , dtype = np .uint8 )
595
- data [np .asanyarray (nii .dataobj ) == wm_label ] = 1
596
-
597
- out_file = fname_presuffix (in_seg , suffix = '_wm' , newpath = newpath )
598
- new = nb .Nifti1Image (data , nii .affine , nii .header )
599
- new .set_data_dtype (np .uint8 )
600
- new .to_filename (out_file )
601
- return out_file
602
-
603
-
604
588
def normalize_xform (img ):
605
589
"""
606
590
Set identical, valid qform and sform matrices in an image.
Original file line number Diff line number Diff line change @@ -107,3 +107,23 @@ def update_header_fields(fname, **kwargs):
107
107
for field , value in kwargs .items ():
108
108
img .header [field ] = value
109
109
overwrite_header (img , fname )
110
+
111
+
112
+ def dseg_label (in_seg , label , newpath = None ):
113
+ """Extract a particular label from a discrete segmentation."""
114
+ from pathlib import Path
115
+ import nibabel as nb
116
+ import numpy as np
117
+ from nipype .utils .filemanip import fname_presuffix
118
+
119
+ newpath = Path (newpath or '.' )
120
+
121
+ nii = nb .load (in_seg )
122
+ data = np .int16 (nii .dataobj ) == label
123
+
124
+ out_file = fname_presuffix (in_seg , suffix = '_mask' ,
125
+ newpath = str (newpath .absolute ()))
126
+ new = nii .__class__ (data , nii .affine , nii .header )
127
+ new .set_data_dtype (np .uint8 )
128
+ new .to_filename (out_file )
129
+ return out_file
Original file line number Diff line number Diff line change 2
2
import numpy as np
3
3
4
4
import pytest
5
- from ..images import update_header_fields , overwrite_header
5
+ from ..images import update_header_fields , overwrite_header , dseg_label
6
6
7
7
8
8
def random_image ():
@@ -71,3 +71,18 @@ def test_overwrite_header_reject_mmap(tmp_path):
71
71
img = nb .load (fname , mmap = True )
72
72
with pytest .raises (ValueError ):
73
73
overwrite_header (img , fname )
74
+
75
+
76
+ def test_dseg_label (tmp_path ):
77
+ fname = str (tmp_path / 'test_file.nii.gz' )
78
+
79
+ data = np .dstack ((
80
+ np .zeros ((20 , 20 ), dtype = 'int16' ),
81
+ np .ones ((20 , 20 ), dtype = 'int16' ),
82
+ np .ones ((20 , 20 ), dtype = 'int16' ) * 2 ,
83
+ np .ones ((20 , 20 ), dtype = 'int16' ) * 3 ,
84
+ ))
85
+ nb .Nifti1Image (data , np .eye (4 ), None ).to_filename (fname )
86
+
87
+ new_im = nb .load (dseg_label (fname , label = 2 , newpath = tmp_path ))
88
+ assert np .all ((data == 2 ).astype ('int16' ) == np .int16 (new_im .dataobj ))
You can’t perform that action at this time.
0 commit comments