Skip to content

Commit

Permalink
Improve performance on applywarp with atlases by converting 4D data i…
Browse files Browse the repository at this point in the history
…n a single step
  • Loading branch information
mcraig-ibme committed Feb 5, 2024
1 parent c8add98 commit b203245
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions oxasl/region_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,34 @@ def add_roi(wsp, rois, name, roi, threshold=0.5):
})
wsp.log.write("DONE\n")

def add_rois(wsp, rois, names, roi_list, header, threshold=0.5):
"""
Add a list of ROIs
:param rois: Current list of ROIs
:param names: Array of ROI names
:param roi_set: sequence of ROI Numpy arrays (may be binary, probabilistic, percentage...)
:param header: Nifti header for ROI list arrays
:param threshold: Threshold for generating binary mask
"""
roi_4d = Image(np.stack(roi_list, axis=3), header=header)
roi_space = reg.get_img_space(wsp, roi_4d)
roi_asl = reg.change_space(wsp, roi_4d, "asl")
if threshold:
mask_asl = roi_asl.data > threshold
else:
mask_asl = roi_asl.data

for idx, name in enumerate(names):
wsp.log.write(" - %s..." % name)
rois.append({
"name" : name,
"roi_asl" : Image(roi_asl.data[..., idx], header=roi_asl.header),
"mask_asl" : (mask_asl[..., idx] > threshold).astype(np.int32),
"roi_%s" % roi_space : Image(roi_list[idx], header=header),
})
wsp.log.write("DONE\n")

def add_roi_set(wsp, rois, set_name, names, roi_set, threshold=None):
"""
Add an ROI set
Expand Down Expand Up @@ -358,7 +386,7 @@ def add_roi_set(wsp, rois, set_name, names, roi_set, threshold=None):
def add_roi_set_from_fsl_atlas(wsp, rois, atlas_name, resolution=2, threshold=0.5):
"""
Get ROIs from an FSL atlas
:param rois: Current list of ROIs
:param atlas_name: Name of the FSL atlas
:param resolution: Resolution in mm
Expand All @@ -374,21 +402,20 @@ def add_roi_set_from_fsl_atlas(wsp, rois, atlas_name, resolution=2, threshold=0.
roi_region = atlas.get(label=label)
# Convert to probability
roi_region = Image(roi_region.data / 100.0, header=roi_region.header)
if not wsp.fuzzy_sets and wsp.psf is None:
add_roi(wsp, rois, label.name, roi_region, threshold=threshold)
else:
roi_set.append(roi_region.data)
names.append(label.name)
roi_set.append(roi_region.data)
names.append(label.name)

if wsp.fuzzy_sets or wsp.psf is not None:
# When treating as an ROI set, do not threshold, allow it to be fuzzy
roi_set = Image(np.stack(roi_set, axis=3), header=roi_region.header)
add_roi_set(wsp, rois, atlas_name, names, roi_set)
else:
add_rois(wsp, rois, names, roi_set, roi_region.header, threshold=threshold)

def add_rois_from_3d_label_atlas(wsp, rois, atlas_img, region_names):
"""
Get ROIs from an atlas described by a 3D label image
This is a 3D integer image where each unizue nonzero voxel value defines an
ROI region
Expand Down

0 comments on commit b203245

Please sign in to comment.