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 3 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
304 changes: 304 additions & 0 deletions examples/feature_histogram.ipynb

Large diffs are not rendered by default.

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
82 changes: 82 additions & 0 deletions src/napari_matplotlib/feature_histogram.py
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):
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
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.setMinimumSize(QSize(400, 400))
self.update_layers(None)
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()
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
22 changes: 22 additions & 0 deletions src/napari_matplotlib/tests/test_feature_histogram.py
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)