Skip to content

Reference

This is the reference material for the project API.

You can write any markdown here as you would in any other markdown file. You can also include images, links, and other media. Check out the list of plugins to see what extra functionality is available.

API

load_sample_data()

Load sample data.

Source code in src/napari_workshop_plugin/_sample_data.py
10
11
12
13
def load_sample_data() -> List[tuple]:
    """Load sample data."""
    path = str(here / "data" / "sample.npy")
    return read_numpy_file(path)

napari_get_reader(path)

Find the reader contribution for a path.

If path is recognised as a readable extension, return the appropriate function to read from that path.

Parameters:

Name Type Description Default
path str or list of str

Path to file, or list of paths.

required

Returns:

Type Description
function or None

If the path is a recognized format, return a function that accepts the same path or list of paths, and returns a list of layer data tuples.

Source code in src/napari_workshop_plugin/_reader.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def napari_get_reader(
    path: Union[str, List[str]]) -> Union[None, callable]:
    """Find the reader contribution for a path.

    If path is recognised as a readable extension, return the appropriate function to read from that path.

    Parameters
    ----------
    path : str or list of str
        Path to file, or list of paths.

    Returns
    -------
    function or None
        If the path is a recognized format,
        return a function that accepts the same path or
        list of paths, and returns a list of layer data tuples.
    """
    if isinstance(path, list):
        # reader plugins may be handed single path, or a list of paths.
        # if it is a list, it is assumed to be an image stack...
        # so we are only going to look at the first file.
        path = path[0]

    # if we know we cannot read the file, we immediately return None.
    if not path.endswith(".npy"):
        return None

    # otherwise we return the *function* that can read ``path``.
    return read_numpy_file

read_numpy_file(path)

Take a path or list of paths and return a list of LayerData tuples.

Readers are expected to return data as a list of tuples, where each tuple is (data, [add_kwargs, [layer_type]]), "add_kwargs" and "layer_type" are both optional.

Parameters:

Name Type Description Default
path str or list of str

Path to file, or list of paths.

required

Returns:

Name Type Description
layer_data list of tuples

A list of LayerData tuples where each tuple in the list contains (data, metadata, layer_type), where data is a numpy array, metadata is a dict of keyword arguments for the corresponding viewer.add_* method in napari, and layer_type is a lower-case string naming the type of layer. Both "meta", and "layer_type" are optional. napari will default to layer_type=="image" if not provided

Source code in src/napari_workshop_plugin/_reader.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def read_numpy_file(path: Union[str, List[str]]) -> List[tuple]:
    """Take a path or list of paths and return a list of LayerData tuples.

    Readers are expected to return data as a list of tuples, where each tuple
    is (data, [add_kwargs, [layer_type]]), "add_kwargs" and "layer_type" are
    both optional.

    Parameters
    ----------
    path : str or list of str
        Path to file, or list of paths.

    Returns
    -------
    layer_data : list of tuples
        A list of LayerData tuples where each tuple in the list contains
        (data, metadata, layer_type), where data is a numpy array, metadata is
        a dict of keyword arguments for the corresponding viewer.add_* method
        in napari, and layer_type is a lower-case string naming the type of
        layer. Both "meta", and "layer_type" are optional. napari will
        default to layer_type=="image" if not provided
    """
    # handle both a string and a list of strings
    paths = [path] if isinstance(path, str) else path
    # load all files into array
    arrays = [np.load(_path) for _path in paths]
    # stack arrays into single array
    data = np.squeeze(np.stack(arrays))

    # optional kwargs for the corresponding viewer.add_* method
    add_kwargs = {}

    layer_type = "image"  # optional, default is "image"
    return [(data, add_kwargs, layer_type)]

segmentation_widget(viewer, image, blur_sigma=2.0, disk_size=4, classes=3)

Segment cells from calcium image max projection.

  1. Grayscale and blur the image.
  2. Perform a multiotsu to separate the image into three classes: background, non-cell tissue, and cells.
  3. Smooth the multiotsu segmentation via disks.

Parameters:

Name Type Description Default
viewer napari.Viewer

A viewer instance from napari

required
image napari.layers.Image

The image to segment from. Can be one channel or more. If more than one channel, is converted to grayscale.

required
blur_sigma float

The standard deviation of the gaussian blur to be applied. A higher sigma indicates more blur. By default 2.0.

2.0
disk_size int

The size of the disk in pixels for smoothing. A higher disk_size indicates more smoothing. By default 4.

4
classes int

The number of classes to segment into. One of the classes will be background. By default 3. Going past 5 classes is not recommended for speed.

3
Source code in src/napari_workshop_plugin/_widget.py
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@magic_factory(persist=True)
def segmentation_widget(
    viewer: "napari.Viewer",
    image: "napari.layers.Image",
    blur_sigma: float = 2.0,
    disk_size: int = 4,
    classes: int = 3,
):
    """Segment cells from calcium image max projection.

    1. Grayscale and blur the image.
    2. Perform a multiotsu to separate the image into three classes:
        background, non-cell tissue, and cells.
    3. Smooth the multiotsu segmentation via disks.

    Parameters
    ----------
    viewer : napari.Viewer
        A viewer instance from napari
    image : napari.layers.Image
        The image to segment from. Can be one channel or more.
        If more than one channel, is converted to grayscale.
    blur_sigma : float, optional
        The standard deviation of the gaussian blur to be applied.
        A higher sigma indicates more blur.
        By default 2.0.
    disk_size : int, optional
        The size of the disk in pixels for smoothing.
        A higher disk_size indicates more smoothing.
        By default 4.
    classes : int, optional
        The number of classes to segment into.
        One of the classes will be background.
        By default 3. Going past 5 classes is not recommended for speed.

    """
    def inner_display_layer(args) -> None:
        display_layer(viewer, args)


    worker = segment(image, blur_sigma, disk_size, classes)
    worker.yielded.connect(inner_display_layer)
    worker.start()

    return worker