Skip to content

Commit

Permalink
bugfix: support OCL-backed layers and re-computing of labeling results
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Apr 2, 2022
1 parent 688b586 commit 43057b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
46 changes: 30 additions & 16 deletions napari_skimage_regionprops/_all_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from toolz import curry
from typing import Callable
from functools import wraps
import time
import inspect
import numpy as np
import pandas as pd
# most imports here are just for backwards compatbility
#from ._workflow import WorkflowManager, CURRENT_TIME_FRAME_DATA, _get_layer_from_data, _break_down_4d_to_2d_kwargs, _viewer_has_layer

from ._utilities import isimage

@curry
def analyze_all_frames(function: Callable) -> Callable:
Expand All @@ -35,17 +32,28 @@ def worker_function(*args, **kwargs):
viewer_key = key

labels_layer = None
image_layer = None
original_args = copy_dict(bound.arguments)

if viewer is not None:
variable_timepoint = list(viewer.dims.current_step)
current_timepoint = variable_timepoint[0]
max_time = int(viewer.dims.range[-4][1])

# find a labels layer to attach result
for key, value in original_args.items():
if isimage(value):
layer = _get_layer_from_data(viewer, value)
if isinstance(layer, napari.layers.Labels):
labels_layer = layer
labels_layer_key = key
if isinstance(layer, napari.layers.Image):
image_layer = layer
image_layer_key = key
else:
max_time = 0
for key, value in original_args.items():
if isinstance(value, np.ndarray) or str(type(value)) in ["<class 'cupy._core.core.ndarray'>",
"<class 'dask.array.core.Array'>"]:
if isimage(value):
if len(value.shape) == 4 and max_time < value.shape[0]:
max_time = value.shape[0]

Expand All @@ -59,8 +67,7 @@ def worker_function(*args, **kwargs):

if viewer is None:
for key, value in args.items():
if isinstance(value, np.ndarray) or str(type(value)) in ["<class 'cupy._core.core.ndarray'>",
"<class 'dask.array.core.Array'>"]:
if isimage(value):
if len(value.shape) == 4:
new_value = value[f]
if new_value.shape[0] == 1:
Expand All @@ -79,8 +86,11 @@ def worker_function(*args, **kwargs):

variable_timepoint[0] = f
viewer.dims.current_step = variable_timepoint
_refresh_viewer(viewer)

from napari_workflows._workflow import _break_down_4d_to_2d_kwargs
args[labels_layer_key] = labels_layer.data
args[image_layer_key] = image_layer.data
_break_down_4d_to_2d_kwargs(args, f, viewer)
args[viewer_key] = None
bound.arguments = args
Expand All @@ -98,14 +108,7 @@ def worker_function(*args, **kwargs):
# reset viewer
variable_timepoint[0] = current_timepoint
viewer.dims.current_step = variable_timepoint

# find a labels layer to attach result
for key, value in original_args.items():
if isinstance(value, np.ndarray) or str(type(value)) in ["<class 'cupy._core.core.ndarray'>",
"<class 'dask.array.core.Array'>"]:
layer = _get_layer_from_data(viewer, value)
if isinstance(layer, napari.layers.Labels):
labels_layer = layer
_refresh_viewer(viewer)

if labels_layer is not None:
labels_layer.properties = result.to_dict(orient='list')
Expand All @@ -125,3 +128,14 @@ def copy_dict(source, result=None):
for k, v in source.items():
result[k] = v
return result

def _refresh_viewer(viewer):
if viewer is None:
return

from napari_workflows import WorkflowManager
wm = WorkflowManager.install(viewer)
w = wm.workflow

while(wm._search_first_invalid_layer (w.roots()) is not None):
wm._update_invalid_layer()
2 changes: 1 addition & 1 deletion napari_skimage_regionprops/_regionprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def regionprops(image_layer : napari.layers.Layer, labels_layer: napari.layers.L
regionprops_table(image_data, labels_layer.data, napari_viewer, size, intensity, perimeter, shape, position, moments)

@register_function(menu="Measurement > Regionprops (nsr)")
def regionprops_table(image : napari.types.LayerData, labels: napari.types.LabelsData, napari_viewer : Viewer = None, size : bool = True, intensity : bool = True, perimeter : bool = False, shape : bool = False, position : bool = False, moments : bool = False):
def regionprops_table(image : napari.types.ImageData, labels: napari.types.LabelsData, napari_viewer : Viewer = None, size : bool = True, intensity : bool = True, perimeter : bool = False, shape : bool = False, position : bool = False, moments : bool = False):
"""
Adds a table widget to a given napari viewer with quantitative analysis results derived from an image-label pair.
"""
Expand Down
7 changes: 6 additions & 1 deletion napari_skimage_regionprops/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ def duplicate_current_frame(layer : LayerInput, napari_viewer : Viewer, axis : i
else:
result = Image(new_image, name=new_name)

return result
return result

def isimage(value):
return isinstance(value, np.ndarray) or str(type(value)) in ["<class 'cupy._core.core.ndarray'>",
"<class 'dask.array.core.Array'>",
"<class 'pyclesperanto_prototype._tier0._pycl.OCLArray'>"]

0 comments on commit 43057b6

Please sign in to comment.