Skip to content

Commit 34b40c4

Browse files
committed
FIX: Better generalization and renaming+relocation in the API of extract_wm
Revise this buggy function with a more appropriate implementation. Resolves: #499
1 parent 602a7ca commit 34b40c4

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,25 @@ 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+
if newpath is None:
120+
newpath = Path()
121+
newpath = Path()
122+
123+
nii = nb.load(in_seg)
124+
data = np.asanyarray(nii.dataobj, dtype='int16') == label
125+
126+
out_file = fname_presuffix(in_seg, suffix='_mask',
127+
newpath=str(newpath.absolute()))
128+
new = nb.Nifti1Image(data, nii.affine, nii.header)
129+
new.set_data_dtype(np.uint8)
130+
new.to_filename(out_file)
131+
return out_file

0 commit comments

Comments
 (0)