Skip to main content

Auto-generate python code from within napari to make napari-assistant compatible plugins

Project description

napari-assistant-plugin-generator

License PyPI Python Version tests codecov Development Status napari hub DOI

The napari-assistant-plugin-generator is a napari plugin to generate python code which can be pip-installed and serve as napari-plugin compatible with the napari-assistant.

Usage

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 naparia from the command line.

  • Open the blobs example image Blobs, e.g. after downloading it from here.

  • In the Assistant, click on the Remove noise button and select Gaussian (scikit-image, nsbatwm) from the operation pulldown.

  • Click the Binarize button and select Threshold (Otsu 1979, scikit-image, nsbatwm) operation.

  • Click the Label button and select 'Connected component labeling (scikit-image, nsbatwm)' from the operation pulldown.

Afterwards, your Napari with the configured workflow should look like this:

img.png

Plugin generation

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 in the Generate code... of the Assistant:

img.png

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 _ instead.
  • 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 Tools where your plugin will be found after installing it.
  • menu name The menu entry will have this title.

img.png

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 requirements.txt files:

img.png

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:

img.png

The 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 editable. 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 <your_plugin_folder>/<your_plugin_folder>/_function.py:

img.png

img.png

Installation

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:

pip install napari-assistant-plugin-generator

Also make sure you have git installed. E.g. using mamba/conda:

mamba install git

Feedback welcome!

The napari-assistant is developed in the open because we believe in the open source community. Feel free to drop feedback as github issue or via image.sc

Contributing

Contributions are very welcome. Please ensure the test coverage at least stays the same before you submit a pull request.

License

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

Acknowledgements

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.

Project details


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-assistant-plugin-generator-0.1.0.tar.gz.

File metadata

File hashes

Hashes for napari-assistant-plugin-generator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ea33b52858288b1d739d807af6f28df25686703d86c951baf4ed0a3f77d03811
MD5 8ff6b0c5125ab411df1833900f0cc22c
BLAKE2b-256 8e091ce0c2a26fc04a8ee2e0da2fbbe361829c99ea3979657a5194cf708cd245

See more details on using hashes here.

File details

Details for the file napari_assistant_plugin_generator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for napari_assistant_plugin_generator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa5d9d25fc92945139f1673469024e519e5e8b4c176a75785b7ece2dff2526c5
MD5 52c33e73dd14172c9fb6fd7e6d9f6909
BLAKE2b-256 6243b1c180cb301746a661400424b3d894e4753a31cc9c8b671c15023b205d90

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page