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

open nd2 and segment bf

parent 2e6f213c
No related branches found
No related tags found
No related merge requests found
repos: repos:
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1 rev: v4.1.0
hooks: hooks:
- id: check-docstring-first - id: check-docstring-first
- id: end-of-file-fixer - id: end-of-file-fixer
...@@ -24,11 +24,11 @@ repos: ...@@ -24,11 +24,11 @@ repos:
hooks: hooks:
- id: isort - id: isort
- repo: https://github.com/psf/black - repo: https://github.com/psf/black
rev: 21.11b1 rev: 22.1.0
hooks: hooks:
- id: black - id: black
- repo: https://github.com/asottile/pyupgrade - repo: https://github.com/asottile/pyupgrade
rev: v2.29.1 rev: v2.31.1
hooks: hooks:
- id: pyupgrade - id: pyupgrade
args: [--py38-plus, --keep-runtime-typing] args: [--py38-plus, --keep-runtime-typing]
......
...@@ -5,6 +5,7 @@ It implements the Reader specification, but your plugin may choose to ...@@ -5,6 +5,7 @@ It implements the Reader specification, but your plugin may choose to
implement multiple readers or even other plugin contributions. see: implement multiple readers or even other plugin contributions. see:
https://napari.org/plugins/stable/guides.html#readers https://napari.org/plugins/stable/guides.html#readers
""" """
import nd2
import numpy as np import numpy as np
...@@ -29,13 +30,30 @@ def napari_get_reader(path): ...@@ -29,13 +30,30 @@ def napari_get_reader(path):
path = path[0] path = path[0]
# if we know we cannot read the file, we immediately return None. # if we know we cannot read the file, we immediately return None.
if not path.endswith(".npy"): if path.endswith(".nd2"):
return None return read_nd2
# otherwise we return the *function* that can read ``path``. # otherwise we return the *function* that can read ``path``.
return reader_function return reader_function
def read_nd2(path):
data = nd2.ND2File(path)
ddata = data.to_dask()
return [
(
ddata,
dict(
channel_axis=1,
name=["BF", "fluo"],
colormap=["gray", "green"],
contrast_limits=[(8500, 35000), (150, 20000)],
),
"image",
)
]
def reader_function(path): def reader_function(path):
"""Take a path or list of paths and return a list of LayerData tuples. """Take a path or list of paths and return a list of LayerData tuples.
...@@ -54,7 +72,8 @@ def reader_function(path): ...@@ -54,7 +72,8 @@ def reader_function(path):
A list of LayerData tuples where each tuple in the list contains A list of LayerData tuples where each tuple in the list contains
(data, metadata, layer_type), where data is a numpy array, metadata is (data, metadata, layer_type), where data is a numpy array, metadata is
a dict of keyword arguments for the corresponding viewer.add_* method a dict of keyword arguments for the corresponding viewer.add_* method
in napari, and layer_type is a lower-case string naming the type of layer. in napari, and layer_type is a lower-case string naming the type of
layer.
Both "meta", and "layer_type" are optional. napari will default to Both "meta", and "layer_type" are optional. napari will default to
layer_type=="image" if not provided layer_type=="image" if not provided
""" """
......
...@@ -6,8 +6,16 @@ see: https://napari.org/plugins/stable/guides.html#widgets ...@@ -6,8 +6,16 @@ see: https://napari.org/plugins/stable/guides.html#widgets
Replace code below according to your needs. Replace code below according to your needs.
""" """
from qtpy.QtWidgets import QWidget, QHBoxLayout, QPushButton from functools import partial
from multiprocessing import Pool
import napari
import numpy as np
from magicgui import magic_factory from magicgui import magic_factory
from qtpy.QtWidgets import QHBoxLayout, QPushButton, QWidget
from scipy.ndimage import binary_dilation
from segment.seg import segment_bf
from skimage.measure import regionprops_table
class ExampleQWidget(QWidget): class ExampleQWidget(QWidget):
...@@ -37,5 +45,53 @@ def example_magic_widget(img_layer: "napari.layers.Image"): ...@@ -37,5 +45,53 @@ def example_magic_widget(img_layer: "napari.layers.Image"):
# 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.
def example_function_widget(img_layer: "napari.layers.Image"): @magic_factory()
print(f"you have selected {img_layer}") def segment_organoid(
BF_layer: "napari.layers.Image",
fluo_layer: "napari.layers.Image",
donut=10,
thr: float = 0.4,
) -> napari.types.LabelsData:
# frame = napari.current_viewer().cursor.position[0]
try:
p = Pool()
stack = list(
p.map(
partial(seg_frame, thr=thr, donut=donut),
zip(BF_layer.data.compute(), fluo_layer.data.compute()),
)
)
except Exception as e:
print(e.args)
finally:
p.close()
return np.array(stack)
# (bg_mask.astype('uint8'), {"name": "donut", **kwargs}, 'image')
def seg_frame(data, thr, donut):
bf, fluo = data
label = segment_bf(bf, thr=thr, plot=False)
print(bf.dtype, bf.shape)
props = regionprops_table(
label,
intensity_image=fluo,
properties=(
"label",
"centroid",
"bbox",
"mean_intensity",
"area",
"major_axis_length",
),
)
# print(props)
biggest_prop_index = np.argmax(props["area"])
label_of_biggest_object = props["label"][biggest_prop_index]
spheroid_mask = label == label_of_biggest_object
bg_mask = np.bitwise_xor(
binary_dilation(spheroid_mask, structure=np.ones((donut, donut))),
spheroid_mask,
)
return spheroid_mask.astype("uint16") + 2 * bg_mask.astype("uint8")
...@@ -13,36 +13,36 @@ contributions: ...@@ -13,36 +13,36 @@ contributions:
title: Save image data with Segment organoid title: Save image data with Segment organoid
- id: napari-segment.make_sample_data - id: napari-segment.make_sample_data
python_name: napari_segment._sample_data:make_sample_data python_name: napari_segment._sample_data:make_sample_data
title: Load sample data from Segment organoid title: Load sample data from Segment organoid
- id: napari-segment.make_qwidget # - id: napari-segment.make_qwidget
python_name: napari_segment._widget:ExampleQWidget # python_name: napari_segment._widget:ExampleQWidget
title: Make example QWidget # title: Make example QWidget
- id: napari-segment.make_magic_widget # - id: napari-segment.make_magic_widget
python_name: napari_segment._widget:example_magic_widget # python_name: napari_segment._widget:example_magic_widget
title: Make example magic widget # title: Make example magic widget
- id: napari-segment.make_func_widget - id: napari-segment.segment_organoid
python_name: napari_segment._widget:example_function_widget python_name: napari_segment._widget:segment_organoid
title: Make example function widget title: Segment organoid
readers: readers:
- command: napari-segment.get_reader - command: napari-segment.get_reader
accepts_directories: false accepts_directories: false
filename_patterns: ['*.npy'] filename_patterns: ['*.npy', '*.nd2']
writers: writers:
- command: napari-segment.write_multiple - command: napari-segment.write_multiple
layer_types: ['image*','labels*'] layer_types: ['image*','labels*']
filename_extensions: [] filename_extensions: []
- command: napari-segment.write_single_image - command: napari-segment.write_single_image
layer_types: ['image'] layer_types: ['image']
filename_extensions: ['.npy'] filename_extensions: ['.npy']
sample_data: sample_data:
- command: napari-segment.make_sample_data - command: napari-segment.make_sample_data
display_name: Segment organoid display_name: Segment organoid
key: unique_id.1 key: unique_id.1
widgets: widgets:
- command: napari-segment.make_qwidget # - command: napari-segment.make_qwidget
display_name: Example QWidget # display_name: Example QWidget
- command: napari-segment.make_magic_widget # - command: napari-segment.make_magic_widget
display_name: Example Magic Widget # display_name: Example Magic Widget
- command: napari-segment.make_func_widget - command: napari-segment.segment_organoid
autogenerate: true # autogenerate: true
display_name: Example Function Widget display_name: Segment organoid
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment