Skip to content
Snippets Groups Projects
Commit 261e9159 authored by Andrey Aristov's avatar Andrey Aristov
Browse files

interactive

parent 0b9e6e72
No related branches found
No related tags found
No related merge requests found
...@@ -7,14 +7,21 @@ see: https://napari.org/plugins/stable/guides.html#widgets ...@@ -7,14 +7,21 @@ see: https://napari.org/plugins/stable/guides.html#widgets
Replace code below according to your needs. Replace code below according to your needs.
""" """
from functools import partial from functools import partial
from multiprocessing import Pool
import dask
import napari import napari
import numpy as np import numpy as np
from magicgui import magic_factory from magicgui import magic_factory
from qtpy.QtWidgets import QHBoxLayout, QPushButton, QWidget from qtpy.QtWidgets import QHBoxLayout, QPushButton, QWidget
from scipy.ndimage import label, binary_dilation, gaussian_filter, binary_erosion, binary_fill_holes from scipy.ndimage import (
binary_dilation,
binary_erosion,
binary_fill_holes,
gaussian_filter,
label,
)
from skimage.measure import regionprops_table from skimage.measure import regionprops_table
# import matplotlib.pyplot as plt # import matplotlib.pyplot as plt
...@@ -42,69 +49,70 @@ def example_magic_widget(img_layer: "napari.layers.Image"): ...@@ -42,69 +49,70 @@ def example_magic_widget(img_layer: "napari.layers.Image"):
print(f"you have selected {img_layer}") print(f"you have selected {img_layer}")
def save_values(val):
print(f"Saving {val}")
# Uses the `autogenerate: true` flag in the plugin manifest # Uses the `autogenerate: true` flag in the plugin manifest
# to indicate it should be wrapped as a magicgui to autogenerate # to indicate it should be wrapped as a magicgui to autogenerate
# a widget. # a widget.
@magic_factory() @magic_factory(
auto_call=True,
thr={
"label": "Threshold",
"widget_type": "FloatSlider",
"min": 0.1,
"max": 0.5,
},
erode={"widget_type": "Slider", "min": 1, "max": 10},
)
def segment_organoid( def segment_organoid(
BF_layer: "napari.layers.Image", BF_layer: "napari.layers.Image",
fluo_layer: "napari.layers.Image", fluo_layer: "napari.layers.Image",
donut=10,
thr: float = 0.4, thr: float = 0.4,
erode: int = 10,
donut=10,
) -> napari.types.LayerDataTuple: ) -> napari.types.LayerDataTuple:
# frame = napari.current_viewer().cursor.position[0] # frame = napari.current_viewer().cursor.position[0]
kwargs = {} kwargs = {}
ddata = BF_layer.data
try: labels = ddata.map_blocks(
p = Pool() partial(segment_bf, thr=thr, erode=erode), dtype=ddata.dtype
stack = p.map(
partial(seg_frame, thr=thr, donut=donut),
zip(BF_layer.data.compute(), fluo_layer.data.compute()),
) )
print((out := np.array(stack)).shape) selected_labels = dask.array.map_blocks(filter_biggest, labels, donut)
return [(out, {"name": "label", **kwargs}, 'labels')] print(selected_labels.shape)
return [
except Exception as e: (labels, {"name": "raw_labels", "visible": False, **kwargs}, "labels"),
print(e.args) (selected_labels, {"name": "selected labels", **kwargs}, "labels"),
raise e ]
finally:
p.close()
def seg_frame(data, thr, donut): def filter_biggest(labels, donut=10):
bf, fluo = data
labels = segment_bf(bf, thr=thr, plot=False)
print(".",)
props = regionprops_table( props = regionprops_table(
labels, labels[0],
intensity_image=fluo,
properties=( properties=(
"label", "label",
"centroid",
"bbox",
"mean_intensity",
"area", "area",
"major_axis_length",
), ),
) )
# print(props)
biggest_prop_index = np.argmax(props["area"]) biggest_prop_index = np.argmax(props["area"])
label_of_biggest_object = props["label"][biggest_prop_index] label_of_biggest_object = props["label"][biggest_prop_index]
spheroid_mask = labels == label_of_biggest_object spheroid_mask = labels[0] == label_of_biggest_object
# bg_mask = np.bitwise_xor( bg_mask = np.bitwise_xor(
# binary_dilation(spheroid_mask, structure=np.ones((donut, donut))), binary_dilation(spheroid_mask, structure=np.ones((donut, donut))),
# spheroid_mask, spheroid_mask,
# ) )
return spheroid_mask.astype("uint16")# + 2 * bg_mask.astype("uint8") return (
spheroid_mask.astype("uint16") + 2 * bg_mask.astype("uint16")
).reshape(labels.shape)
def segment_bf(well, thr=0.2, smooth=10, erode=10, fill=True, plot=False): def segment_bf(well, thr=0.2, smooth=10, erode=10, fill=True, plot=False):
''' """
Serments input 2d array using thresholded gradient with filling Serments input 2d array using thresholded gradient with filling
Returns SegmentedImage object Returns SegmentedImage object
''' """
grad = get_2d_gradient(well) grad = get_2d_gradient(well[0])
sm = gaussian_filter(grad, smooth) sm = gaussian_filter(grad, smooth)
# sm = multiwell.gaussian_filter(well, smooth) # sm = multiwell.gaussian_filter(well, smooth)
...@@ -117,7 +125,8 @@ def segment_bf(well, thr=0.2, smooth=10, erode=10, fill=True, plot=False): ...@@ -117,7 +125,8 @@ def segment_bf(well, thr=0.2, smooth=10, erode=10, fill=True, plot=False):
regions = binary_erosion(regions, iterations=erode) regions = binary_erosion(regions, iterations=erode)
labels, _ = label(regions) labels, _ = label(regions)
return labels return labels.reshape(well.shape)
def get_2d_gradient(xy): def get_2d_gradient(xy):
gx, gy = np.gradient(xy) gx, gy = np.gradient(xy)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment