Skip to content

Commit fda9a50

Browse files
authored
Merge pull request #500 from oesteban/fix/499-extract-wm
FIX: Better generalization and renaming+relocation in the API of ``extract_wm``
2 parents 7b28b4f + 6b3a451 commit fda9a50

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

niworkflows/interfaces/images.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -585,22 +585,6 @@ def reorient(in_file, newpath=None):
585585
return out_file
586586

587587

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-
604588
def normalize_xform(img):
605589
"""
606590
Set identical, valid qform and sform matrices in an image.

niworkflows/utils/images.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,23 @@ def update_header_fields(fname, **kwargs):
107107
for field, value in kwargs.items():
108108
img.header[field] = value
109109
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

niworkflows/utils/tests/test_images.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44
import pytest
5-
from ..images import update_header_fields, overwrite_header
5+
from ..images import update_header_fields, overwrite_header, dseg_label
66

77

88
def random_image():
@@ -71,3 +71,18 @@ def test_overwrite_header_reject_mmap(tmp_path):
7171
img = nb.load(fname, mmap=True)
7272
with pytest.raises(ValueError):
7373
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))

0 commit comments

Comments
 (0)