magicgui parameter sweep#

Example showing how to accomplish a napari parameter sweep with magicgui.

It demonstrates: 1. overriding the default widget type with a custom class 2. the auto_call option, which calls the function whenever a parameter changes

Tags: gui

magic parameter sweep
import typing

import skimage.filters
from typing_extensions import Annotated

import napari

# Define our gaussian_blur function.
# Note that we can use forward references for the napari type annotations.
# You can read more about them here:
# In this example, because we have already imported napari anyway, it doesn't
# really matter. But this syntax would let you specify that a parameter is a
# napari object type without actually importing or depending on napari.
# We also use the `Annotated` type to pass an additional dictionary that can be used
# to aid widget generation. The keys of the dictionary are keyword arguments to
# the corresponding magicgui widget type. For more informaiton see
def gaussian_blur(
    layer: 'napari.layers.Image',
    sigma: Annotated[float, {'widget_type': 'FloatSlider', 'max': 6}] = 1.0,
    mode: Annotated[str, {'choices': ['reflect', 'constant', 'nearest', 'mirror', 'wrap']}]='nearest',
) -> 'typing.Optional[napari.types.ImageData]':
    """Apply a gaussian blur to ``layer``."""
    if layer:
        return skimage.filters.gaussian(, sigma=sigma, mode=mode)
    return None

# create a viewer and add some images
viewer = napari.Viewer()
viewer.add_image(, name='astronaut')
viewer.add_image('float'), name='grass')

# Add our magic function to napari

if __name__ == '__main__':

Gallery generated by Sphinx-Gallery