-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from 3 commits
b290a2c
7269487
210a35a
bff4529
8c3bba6
7d01cef
80f6d5a
c2d6099
52b0ad0
46d9d18
f7e57f8
15d65d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import numpy as np | ||
|
||
from .base import NapariMPLWidget | ||
from qtpy.QtWidgets import QComboBox, QLabel, QCheckBox | ||
from qtpy.QtCore import QSize | ||
|
||
__all__ = ["FeatureHistogramWidget"] | ||
|
||
import napari | ||
|
||
from .util import Interval | ||
|
||
|
||
class FeatureHistogramWidget(NapariMPLWidget): | ||
""" | ||
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, column_name: str = None): | ||
dstansby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super().__init__(napari_viewer) | ||
self.axes = self.canvas.figure.subplots() | ||
|
||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
# listen to laer changed | ||
napari_viewer.layers.selection.events.changed.connect(self.update_available_columns) | ||
|
||
# setup GUI | ||
self.setMinimumSize(QSize(400, 400)) | ||
dstansby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.update_layers(None) | ||
dstansby marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.update_available_columns() | ||
|
||
def update_available_columns(self): | ||
""" | ||
Update the feature list pulldown as soon as the user changes the selected layer | ||
""" | ||
selected_layer = self.layers[0] | ||
|
||
former_plot_column_index = 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() | ||
self.plot_column_name.addItems(list(features.keys())) | ||
|
||
self.plot_column_name.setCurrentIndex(former_plot_column_index) | ||
|
||
def clear(self) -> None: | ||
self.axes.clear() | ||
|
||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from napari_matplotlib import FeatureHistogramWidget | ||
import numpy as np | ||
|
||
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] | ||
} | ||
|
||
FeatureHistogramWidget(viewer) |
Uh oh!
There was an error while loading. Please reload this page.