Executable script generation for napari plugins
Project description
napari-macrokit
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file napari-macrokit-0.0.1.tar.gz
.
File metadata
- Download URL: napari-macrokit-0.0.1.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.13.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f49f629f22d0088d482b7292415a76205723b4bbac82319cd5e0fd43022b507 |
|
MD5 | e6a1f71f371a35fe54256acbc88d966c |
|
BLAKE2b-256 | 9c42581a9a5c5fa5260d272e4a33d4fa677705af1ef31f386befc6cb13def2b2 |
File details
Details for the file napari_macrokit-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: napari_macrokit-0.0.1-py3-none-any.whl
- Upload date:
- Size: 25.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.13.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d878d54bf4c544466b27a49e7356c693f466953dd5507bb9ea0d31b51323e64 |
|
MD5 | 93b5cad6cfca302eb6fe407b6b846a69 |
|
BLAKE2b-256 | 31d4210013297eb67d1b3dabf2f3c835b683a5b553d7e39d15f057fcb12964db |