Skip to content

Commit 2dae719

Browse files
committed
enh:add pick_rois function to nifti/roi.py
1 parent 4bec0d5 commit 2dae719

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

boyle/nifti/roi.py

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# -------------------------------------------------------------------------------
1111

1212
import numpy as np
13+
import nibabel as nib
1314
import scipy.ndimage as scn
1415
from collections import OrderedDict
1516

@@ -59,6 +60,36 @@ def drain_rois(img):
5960
return out
6061

6162

63+
def pick_rois(rois_img, roi_values, bg_val=0):
64+
""" Return the `rois_img` only with the ROI values from `roi_values`.
65+
Parameters
66+
----------
67+
rois_img: niimg-like
68+
69+
roi_values: list of int or float
70+
The list of values from rois_img.
71+
72+
bg_val: int or float
73+
The background value of `rois_img`.
74+
75+
Returns
76+
-------
77+
subset_rois_img: nibabel.Nifti2Image
78+
"""
79+
img = read_img(rois_img)
80+
img_data = img.get_data()
81+
82+
if bg_val == 0:
83+
out = np.zeros(img_data.shape, dtype=img_data.dtype)
84+
else:
85+
out = np.ones(img_data.shape, dtype=img_data.dtype) * bg_val
86+
87+
for r in roi_values:
88+
out[img_data == r] = r
89+
90+
return nib.Nifti2Image(out, affine=img.affine, header=img.header)
91+
92+
6293
def largest_connected_component(volume):
6394
"""Return the largest connected component of a 3D array.
6495

0 commit comments

Comments
 (0)