Skip to main content

A configurable features table widget for Napari.

Project description

EPFL Center for Imaging logo

Configurable Features Table for Napari

demo.webm

This is an extended version of Napari's built-in features table widget, with extra options to:

  • Sort table values
  • Display a subset of table columns
  • Colorize Labels based on feature values

The table is essentially a graphical view of the features attribute of a layer.

The table is optimized for usage with 2D and 3D Labels layers. It can probably be used to display features from other layer types as well, but this hasn't been tested.

Displaying the table

Open the table from Plugins > Features Table in Napari.

The table displays features from the currently selected layer in the layers list. It will automatically update when the layer selection changes. If multiple layers are selected, only features from the first selected layer will be displayed.

Computing custom features

By default, the table displays a label column for the selected Labels layer, along with any pre-existing features that can be matched with the label column (they should be in Pandas DataFrame format with at least a 'label' column).

It is possible to compute features automatically when a new Labels layer is selected, based on a provided featurizer function. Featurizer functions will receive as input the labels layer data as a Numpy array, and should return a Pandas DataFrame with at least a label column, along with any other feature columns.

For example, the following code extends the behaviour of the table to compute and display the area (or volume) of a labels layer:

import pandas as pd
from skimage.measure import regionprops_table

def area_featurizer(labels: np.ndarray) -> pd.DataFrame:
    return pd.DataFrame(regionprops_table(labels, properties=["label", "area"]))

if __name__ == "__main__":
    import napari
    from napari_label_focus import ConfigurableFeaturesTableWidget
    viewer = napari.Viewer()
    widget = ConfigurableFeaturesTableWidget(viewer, featurizers=[area_featurizer])
    viewer.window.add_dock_widget(widget)
    napari.run()

If more than one featurizer is provided, featurizers will be run one by one and the results from each will be merged into a single features DataFrame.

Controlling what clicking on a table row does

By default, clicking on a table row selects the corresponding label in the Labels layer. This behaviour can be extended by adding callback functions to the table_click_callbacks parameter of the table widget. The callback functions receive a selection context object with references to the viewer, selected layer, selected table row, and the table itself.

The following example illustrates how this works:

from napari_label_focus._context import SelectionContext

def print_selection_context(ctx: SelectionContext):
    print(f"Napari viewer: {ctx.viewer}")
    print(f"Selected layer: {ctx.selected_layer}")
    print(f"Selected table row: {ctx.selected_table_idx}")
    print(f"Features table: {ctx.features_table}")

if __name__ == "__main__":
    import napari
    from napari_label_focus import ConfigurableFeaturesTableWidget
    viewer = napari.Viewer()
    widget = ConfigurableFeaturesTableWidget(viewer, table_click_callbacks=[print_selection_context])
    viewer.window.add_dock_widget(widget)
    napari.run()

In this case, the function print_selection_context gets called whenever users click on a table row.

Installation

You can install napari-label-focus via [pip]:

pip install napari-label-focus

Contributing

Contributions are very welcome.

License

This software is distributed under the terms of the BSD-3 license.

Issues

If you encounter any problems, please file an issue along with a detailed description.

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

napari_label_focus-0.2.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

napari_label_focus-0.2.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file napari_label_focus-0.2.0.tar.gz.

File metadata

  • Download URL: napari_label_focus-0.2.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for napari_label_focus-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ed9ad2c4711032738f7e1566f22d4566b292b686f5045145db64568e5c44c73e
MD5 a8ab3d138dab5ec261f62130c3283549
BLAKE2b-256 f4dec20cc0c52b6bcc9a6d9b62f6fc5312f66a2e9512f951d0caae5c3733713c

See more details on using hashes here.

File details

Details for the file napari_label_focus-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for napari_label_focus-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0368b3ba5d57fa91482c01fb494947fd829e3ede1d0827cf5ad4814b2633378
MD5 3f366f9766255abe782e05de04835c2a
BLAKE2b-256 a404c188951e1ea4ceb273f66508a86c413c07309f8c6eb433ed2a0c5cc7c636

See more details on using hashes here.

Supported by

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