napari macro-kit

napari-macrokit

Executable script generation for napari plugins

License BSD-3 PyPI Python Version tests codecov napari hub

Executable script generation for napari plugins.

Example showing the real-time recording of GUI operation.

This napari plugin aims at making image analysis reproducible with arbitrary input/output types.

Usage

Create a macro object, decorate functions with record method and run!

from napari_macrokit import get_macro

macro = get_macro("my-plugin-specifier")  # get macro object

# define a function
@macro.record
def add(a: float, b: float) -> float:
    return a + b

# run
result = add(3.2, 5.4)
add(result, 1.0)

macro

# Out:
# >>> float0 = add(3.2, 5.4)
# >>> float1 = add(float0, 1.0)

Record GUI Operations

You can use recordable functions in your widgets to keep tracks of GUI operations. More simply, you can double-decorate functions with record and magicgui.

import numpy as np
from magicgui import magicgui
import napari
from napari.types import ImageData
from napari_macrokit import get_macro

macro = get_macro("my-plugin-specifier")  # get macro object

# define recordable magicgui
@magicgui
@macro.record
def add(image: ImageData, b: float) -> ImageData:
    return image + b

viewer = napari.Viewer()  # launch a viewer
viewer.add_image(np.random.random((100, 100)))  # image data
viewer.window.add_dock_widget(add)  # add magicgui to the viewer

Running add twice in GUI and you'll find macro updated like below.

macro
# Out
# >>> image0 = add(viewer.layers['Image'].data, 0.06)
# >>> image1 = add(image0, 0.12)

Combining Plugins

Suppose you have two modules that use napari-macrokit.

# napari_module_0.py

from napari.types import ImageData
from scipy import ndimage as ndi
from napari_macrokit import get_macro

macro = get_macro("napari-module-0")

@macro.record
def gaussian_filter(image: ImageData, sigma: float) -> ImageData:
    return ndi.gaussian_filter(image, sigma=sigma)

@macro.record
def threshold(image: ImageData, value: float) -> ImageData:
    return image > value
# napari_module_1.py

from napari.types import ImageData
import numpy as np
from napari_macrokit import get_macro
macro = get_macro("napari-module-1")

@macro.record
def estimate_background(image: ImageData) -> float:
    return np.percentile(image, 10.0)

You can use functions from both modules to build an analysis workflow by collecting existing macro objects with collect_macro function. All the recordable actions in the modules will also be recorded to the returned macro object.

import numpy as np
from napari_macrokit import collect_macro
from napari_module_0 import gaussian_filter, threshold
from napari_module_1 import estimate_background

# global_macro will record all the macro available at this point
global_macro = collect_macro()

# start image analysis!
image = np.random.random((100, 100))

out = gaussian_filter(image, 2.0)
thresh = estimate_background(out)
binary = threshold(out, thresh)

macro
# Out
# >>> image0 = gaussian_filter(arr0, 2.0)
# >>> float0 = estimate_background(image0)
# >>> image1 = threshold(image1, float0)

This napari plugin was generated with Cookiecutter using @napari's cookiecutter-napari-plugin template.

Installation

You can install napari-macrokit via pip:

pip install napari-macrokit

To install latest development version :

pip install git+https://github.com/hanjinliu/napari-macrokit.git

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the BSD-3 license, "napari-macrokit" is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Version:

  • 0.0.1

Last updated:

  • 27 January 2023

First released:

  • 27 January 2023

License:

Supported data:

  • Information not submitted

Plugin type:

GitHub activity:

  • Stars: 4
  • Forks: 1
  • Issues + PRs: 0

Python versions supported:

Operating system:

Requirements:

  • numpy
  • magicgui
  • qtpy
  • macro-kit (>=0.4.0)

Sign up to receive updates