Skip to content

Add feature histogram widget #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
Changelog
=========
1.0.3
1.1.0
-----
Additions
~~~~~~~~~
- Added a widget to draw a histogram of features.

Changes
~~~~~~~
- The slice widget is now limited to slicing along the x/y dimensions. Support
Expand Down
1 change: 1 addition & 0 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ These widgets plot the data stored in the ``.features`` attribute of individual
Currently available are:

- 2D scatter plots of two features against each other.
- Histograms of individual features.

To use these:

Expand Down
1 change: 1 addition & 0 deletions src/napari_matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
__version__ = "unknown"


from .feature_histogram import * # NoQA
from .histogram import * # NoQA
from .scatter import * # NoQA
from .slice import * # NoQA
91 changes: 91 additions & 0 deletions src/napari_matplotlib/feature_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from typing import Optional

import numpy as np
from qtpy.QtWidgets import QCheckBox, QComboBox, QLabel, QWidget

from napari_matplotlib.base import SingleAxesWidget

__all__ = ["FeatureHistogramWidget"]

import napari

from .util import Interval


class FeatureHistogramWidget(SingleAxesWidget):
"""
Display a histogram of the features stored in the currently selected layer.
"""

n_layers_input = Interval(1, 1)
input_layer_types = (
napari.layers.Image,
napari.layers.Labels,
napari.layers.Points,
napari.layers.Surface,
)

def __init__(
self,
napari_viewer: napari.viewer.Viewer,
parent: Optional[QWidget] = None,
):
super().__init__(napari_viewer)

# Feature selection
self.layout().addWidget(QLabel("Feature:"))
self.plot_column_name = QComboBox()
self.plot_column_name.currentIndexChanged.connect(self._draw)
self.layout().addWidget(self.plot_column_name)

# Logarithmic plot yes/no
self.logarithmic_plot = QCheckBox("Logarithmic")
self.logarithmic_plot.stateChanged.connect(self._draw)
self.layout().addWidget(self.logarithmic_plot)
Comment on lines +41 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove this for now? Having a log-axes option is more widely applicable to all the plots napari-matplotlib produces, so I'd like to think about it in more general terms. Would be good to open an issue with this as a feature request so we can discuss a bit before implementing it.


# listen to laer changed
napari_viewer.layers.selection.events.changed.connect(
self.update_available_columns
)

# setup GUI
self._update_layers(None)
self.update_available_columns()

def update_available_columns(self) -> None:
"""
Update the feature list pulldown as soon as the user changes the selected layer
"""
selected_layer = self.layers[0]
self.plot_column_name.currentIndex()

if selected_layer is not None:
features = selected_layer.features
if features is not None:
self.plot_column_name.clear()
feats = list(features.keys())
print(f"Updating features list: {feats}")
self.plot_column_name.addItems(feats)
self.plot_column_name.setCurrentIndex(0)

def draw(self) -> None:
"""
Clear the axes and histogram the currently selected feature.
"""
layer = self.layers[0]
if layer is None:
self.clear()
return

selected_column = self.plot_column_name.currentText()
if selected_column is not None and len(selected_column) > 0:
data = layer.features[selected_column]
bins = np.linspace(np.min(data), np.max(data), 100)
self.clear()
self.axes.hist(
data,
bins=bins,
label=layer.name + " / " + selected_column,
log=self.logarithmic_plot.isChecked(),
)
self.axes.legend()
7 changes: 7 additions & 0 deletions src/napari_matplotlib/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: napari-matplotlib
display_name: napari Matplotlib
contributions:
commands:
- id: napari-matplotlib.feature_histogram
python_name: napari_matplotlib:FeatureHistogramWidget
title: Make a feature histogram

- id: napari-matplotlib.histogram
python_name: napari_matplotlib:HistogramWidget
title: Make a histogram
Expand All @@ -19,6 +23,9 @@ contributions:
title: Plot a 1D slice

widgets:
- command: napari-matplotlib.feature_histogram
display_name: Feature Histogram

- command: napari-matplotlib.histogram
display_name: Histogram

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions src/napari_matplotlib/tests/test_feature_histogram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from copy import deepcopy

import numpy as np
import pytest

from napari_matplotlib import FeatureHistogramWidget


@pytest.mark.mpl_image_compare
def test_feature_histogram(make_napari_viewer):
# Smoke test adding a histogram widget
viewer = make_napari_viewer()
viewer.theme = "light"

image = np.asarray([[0, 1], [2, 1]])

viewer.add_image(image)
labels_layer = viewer.add_labels(image.astype(int))
labels_layer.features = {
"labels": [1, 2],
"area": [2, 1],
"aspect_ratio": [2, 1],
}

fig = FeatureHistogramWidget(viewer).figure
return deepcopy(fig)