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 1 commit
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
Prev Previous commit
Next Next commit
Run pre-commit
  • Loading branch information
dstansby committed Aug 25, 2023
commit 7d01ceff0ceaeb0036431d415f303607b64d2dbc
30 changes: 20 additions & 10 deletions src/napari_matplotlib/feature_histogram.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import numpy as np
from qtpy.QtWidgets import QCheckBox, QComboBox, QLabel

from .base import NapariMPLWidget
from qtpy.QtWidgets import QComboBox, QLabel, QCheckBox
from qtpy.QtCore import QSize

__all__ = ["FeatureHistogramWidget"]

Expand All @@ -17,9 +16,16 @@ class FeatureHistogramWidget(NapariMPLWidget):
"""

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, column_name: str = None):
input_layer_types = (
napari.layers.Image,
napari.layers.Labels,
napari.layers.Points,
napari.layers.Surface,
)

def __init__(
self, napari_viewer: napari.viewer.Viewer, column_name: str = None
):
super().__init__(napari_viewer)
self.axes = self.canvas.figure.subplots()

Expand All @@ -35,7 +41,9 @@ def __init__(self, napari_viewer: napari.viewer.Viewer, column_name: str = None)
self.layout().addWidget(self.logarithmic_plot)

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

# setup GUI
self.update_layers(None)
Expand Down Expand Up @@ -74,8 +82,10 @@ def draw(self) -> None:
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.hist(
data,
bins=bins,
label=layer.name + " / " + selected_column,
log=self.logarithmic_plot.isChecked(),
)
self.axes.legend()
11 changes: 6 additions & 5 deletions src/napari_matplotlib/tests/test_feature_histogram.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from napari_matplotlib import FeatureHistogramWidget
import numpy as np

from napari_matplotlib import FeatureHistogramWidget


def test_example_q_widget(make_napari_viewer):
# Smoke test adding a histogram widget
viewer = make_napari_viewer()

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


viewer.add_image(image)

labels_layer = viewer.add_labels(labels)

labels_layer.features = {
'labels': [1, 2],
'area': [2, 1],
'aspect_ratio': [2, 1]
"labels": [1, 2],
"area": [2, 1],
"aspect_ratio": [2, 1],
}

FeatureHistogramWidget(viewer)