-
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
Closed
haesleinhuepf
wants to merge
12
commits into
matplotlib:main
from
haesleinhuepf:add_feature_histograms
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b290a2c
add FeatureHistogramWidget
haesleinhuepf 7269487
add example
haesleinhuepf 210a35a
added test
haesleinhuepf bff4529
Merge branch 'main' into add_feature_histograms
dstansby 8c3bba6
Remove minimum size
dstansby 7d01cef
Run pre-commit
dstansby 80f6d5a
Fix some typing
dstansby c2d6099
Remove clear
dstansby 52b0ad0
Fix __init__ signature
dstansby 46d9d18
Remove notebook
dstansby f7e57f8
Fixup test
dstansby 15d65d5
Update docs
dstansby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.