Auto-generate python code from within napari to make napari-assistant compatible plugins
For demonstrating how one can generate a Napari plugin from an existing workflow, we demonstrate the procedure by reusing function from napari-segment-blobs-and-things-with-membranes (nsbatwm) for generating a plugin.
After installing nsbatwm you can start the assistant from the menu
Tools > Utilities > Assistant (na)or run
napariafrom the command line.
Open the blobs example image
Blobs, e.g. after downloading it from here.
In the Assistant, click on the
Remove noisebutton and select
Gaussian (scikit-image, nsbatwm)from the operation pulldown.
Binarizebutton and select
Threshold (Otsu 1979, scikit-image, nsbatwm)operation.
Labelbutton and select 'Connected component labeling (scikit-image, nsbatwm)' from the operation pulldown.
Afterwards, your Napari with the configured workflow should look like this:
Before running the plugin-generator, make sure you are connected to the internet,
because a plugin template will be downloaded.
The plugin generator can be found in the menu
Tools > Utilities > Generate Napari plugin from workflow (na) and also
Generate code... of the Assistant:
In the plugin generator dialog, please enter this information:
- output dir: Folder where the Napari plugin code should be saved. If not specified, the plugin will be stored in the current directory Napari was started from.
- plugin name: Name of the plugin. A folder with this name will be generated in the folder specified above. Plugin names must not contain special characters or spaces. Use
- developer name: Your name as it will be displayed later on the napari-hub in case you decide to publish your plugin.
- developer email: Your email as it will be stored in the configuration of your plugin. This email-address is visible to the public.
- github username: Your username on github. URLs in the plugin documentation will point to your profile on github.
- short description: Please write one sentence explaining what the plugin is doing.
- license: Choose the open-source license your plugin code will be licensed. If you are not sure which one to use, consult choosealicense.com.
- tools menu The menu under
Toolswhere your plugin will be found after installing it.
- menu name The menu entry will have this title.
After the Napari plugin code has been generated, open it in the integrated development environment (IDE) of your choice.
Go through the files in the directory and search for
TODO entries. Start with the
readme.md and the
Highly relevant might be the python file
my_napari_assistant_plugin/_function.py. It contains a function with python code representing the workflow we designed by clicking above:
from napari_plugin_engine import napari_hook_implementation from napari_tools_menu import register_function from napari_time_slicer import time_slicer @napari_hook_implementation def napari_experimental_provide_function(): return [process_image] @register_function(menu="Segmentation / labeling > Segment image") @time_slicer def process_image(image0_b: "napari.types.ImageData", gaussian_blur_sigma_2: float = 1.0, connected_component_labeling_exclude_on_edges_3: bool = False) -> "napari.types.LabelsData": """ Short plugin description # TODO: Provide more detailed documentation here. E.g. specify the parameters and what values users should enter. """ # TODO: Check the list of parameters of the function definition above. # If there are parameters that should not be editable by the end user, move their definition and values here instead. import napari_segment_blobs_and_things_with_membranes as nsbatwm # version 0.3.3 # gaussian blur image1_G = nsbatwm.gaussian_blur(image0_b, sigma=gaussian_blur_sigma_2) # threshold otsu image2_T = nsbatwm.threshold_otsu(image1_G) # connected component labeling image3_C = nsbatwm.connected_component_labeling( image2_T, exclude_on_edges=connected_component_labeling_exclude_on_edges_3) return image3_C
It is recommended to inspect the generated code and rename variables to be more meaningful. For renaming variables, make use of your IDE's tools. For example variables can be renamed conveniently in pycharm using the right-click menu:
readme.md file also contains instructions for how to install and distribute your plugin.
TL:DR: As a plugin developer you typically execute this command from the terminal within your plugin's root directory to install your plugin in an
This command allows you to modify the code and test it without the need for re-installing your plugin.
pip install -e .
If installation was successful, you will find your plugin in the menu you specified and a dialog will open requesting the parameters of the generated Python function in
It is recommended to install devbio-napari first. It comes with many image processing functions that can be combined in workflows and where it is easy to generate plugins from.
Afterwards, the plugin generator can be installed using
pip install napari-assistant-plugin-generator
Also make sure you have
git installed. E.g. using
mamba install git
Contributions are very welcome. Please ensure the test coverage at least stays the same before you submit a pull request.
Distributed under the terms of the BSD-3 license, "napari-assistant-plugin-generator" is free and open source software
This project was supported by the Deutsche Forschungsgemeinschaft under Germany’s Excellence Strategy – EXC2068 - Cluster of Excellence "Physics of Life" of TU Dresden. This project has been made possible in part by grant number 2021-240341 (Napari plugin accelerator grant) from the Chan Zuckerberg Initiative DAF, an advised fund of the Silicon Valley Community Foundation.
- 09 November 2022
- 09 November 2022
- Information not submitted
- Information not submitted
- Stars: 1
- Forks: 0
- Issues + PRs: 0