Skip to content
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

added a Feature Histogram Widget #148

Merged
merged 26 commits into from
Aug 25, 2023

Conversation

jo-mueller
Copy link
Contributor

Linked to issue #58

New features:

This PR introduces a new widget to create Histograms from feature data in Labels/Points/Shapes/Tracks/Vectors layers. It's strongly based off of the ScatterWidget. I wasn't sure whether scatter.py was the right place to put it, but it felt like a better fit than histogram.py, which essentially just works with image intensity data rather than tabular data.

Example code snippet

This code snipped demonstrates what it does:

import numpy as np
import napari
from napari_matplotlib import HistogramWidget, FeaturesHistogramWidget

n_points = 1000
random_points = np.random.random((n_points,3))*10
feature1 = np.random.random(n_points)
feature2 = np.random.normal(size=n_points)

viewer = make_napari_viewer()
viewer.add_points(random_points, properties={'feature1': feature1, 'feature2': feature2}, face_color='feature1', size=1)

widget = FeaturesHistogramWidget(viewer)
viewer.window.add_dock_widget(widget)
widget._set_axis_keys('feature1')
widget._key_selection_widget()
widget._set_axis_keys('feature2')
widget._key_selection_widget()

This code is essentially also what I added to the tests (tests are passing locally).

Looking forward to hear your thoughts on this! :)

@jo-mueller
Copy link
Contributor Author

PS: @dstansby I didn't see that there was ongoing work on a different PR because it wasn't linked to the issue. Feel free to treat this as stale for while.

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

Looking good - a few major changes to make though. I have time to revise this myself, or I can leave you to do it- let me know either way!

src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
@jo-mueller
Copy link
Contributor Author

Hi @dstansby ,

thanks a lot for taking the time to review this! I think I can definitely get those changes done myself. I'll be on vacation in the coming week so PR should come sometime in the second-to-next week.

@jo-mueller jo-mueller closed this Jun 26, 2023
@jo-mueller jo-mueller force-pushed the add-feature-histogram branch 2 times, most recently from 524fdbd to 83747f9 Compare June 26, 2023 11:14
@jo-mueller jo-mueller reopened this Jun 26, 2023
@jo-mueller
Copy link
Contributor Author

jo-mueller commented Jun 26, 2023

Hi @dstansby ,

I added your suggestions from above and replaced the magicgui widgets with pyqt code as has been done in the scatter widget in the meantime. Also sorry for the messy commit history - let me know if I should put the changes on a clean branch and re-start the PR.

@dstansby
Copy link
Member

dstansby commented Jun 26, 2023

pre-commit.ci autofix

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

Looks good! I've left one requst for a change. In addition, could you add a figure test? Hopefully the other test files help with that, but let me know if you need any pointers.

Otherwise I'm 👍 to merge.

src/napari_matplotlib/scatter.py Outdated Show resolved Hide resolved
@jo-mueller
Copy link
Contributor Author

Hi @dstansby ,

I'm not sure what you mean by figure test? I did add the following to the tests. This adds two points layers to the viewer with two sets of features assigned to each. I check whether the figure in the widget changes upon changing the key for the histogram and whether the plot changes as it should when the selected layer is changed.

    n_points = 1000
    random_points = np.random.random((n_points, 3)) * 10
    feature1 = np.random.random(n_points)
    feature2 = np.random.normal(size=n_points)

    viewer = make_napari_viewer()
    viewer.add_points(
        random_points,
        properties={"feature1": feature1, "feature2": feature2},
        name="points1",
    )
    viewer.add_points(
        random_points,
        properties={"feature1": feature1, "feature2": feature2},
        name="points2",
    )

    widget = FeaturesHistogramWidget(viewer)
    viewer.window.add_dock_widget(widget)

    # Check whether changing the selected key changes the plot
    widget._set_axis_keys("feature1")
    fig1 = deepcopy(widget.figure)

    widget._set_axis_keys("feature2")
    assert_figures_not_equal(widget.figure, fig1)

    # check whether selecting a different layer produces the same plot
    viewer.layers.selection.clear()
    viewer.layers.selection.add(viewer.layers[1])
    assert_figures_equal(widget.figure, fig1)

removed `reset_choices()` statement

Revert "removed `reset_choices()` statement"

This reverts commit ed8fc0a.

removed `reset_choices()` from feature histogram
@dstansby
Copy link
Member

For a figure test, see e.g.,

@pytest.mark.mpl_image_compare
def test_histogram_2D(make_napari_viewer, astronaut_data):
viewer = make_napari_viewer()
viewer.theme = "light"
viewer.add_image(astronaut_data[0], **astronaut_data[1])
fig = HistogramWidget(viewer).figure
# Need to return a copy, as original figure is too eagerley garbage
# collected by the widget
return deepcopy(fig)

This using the pytest-mpl plugin to compare the generated image with an image stored in the repository in this directory: https://github.com/matplotlib/napari-matplotlib/tree/main/src/napari_matplotlib/tests/baseline

@jo-mueller
Copy link
Contributor Author

For the figure test: So @pytest.mark.mpl_image_compare will compare the returned image to any image in the baseline directory?

@dstansby
Copy link
Member

No, you'll need to generate the new test image - see https://github.com/matplotlib/pytest-mpl#usage for instructions.

@jo-mueller
Copy link
Contributor Author

@dstansby

No, you'll need to generate the new test image - see matplotlib/pytest-mpl#usage for instructions.

Ok, I added the baseline image, but for some reason, the generated baseline is not detected by the test...Sorry for the back and forth, I'm not so familiar with these testing schemes 🙈

Just as a sidenote: Setting everything up for pytest --mpl seemed quite complicated to me. It requires a few additional installation steps (pip install pyqt6 tinycss2) that did not seem evident to me from the.

@dstansby
Copy link
Member

No worries! If you don't mind, I'd be happy to pick this up and get the test working?

@jo-mueller
Copy link
Contributor Author

No worries! If you don't mind, I'd be happy to pick this up and get the test working?

Not at all! Would be cool though if it were documented somewhere for future contributions 👍

@dstansby
Copy link
Member

dstansby commented Aug 25, 2023

Looks like the test image was just in the wrong directory :) I also did a few other minor fixes, will merge this and do a new release of napari-matplotlib if the tests pass. Thanks a lot for the contribution, and sorry for how long it took me to merge it...

@dstansby
Copy link
Member

Looks like it's just code coverage failing

@dstansby dstansby marked this pull request as ready for review August 25, 2023 16:07
@dstansby dstansby merged commit dc1f3bb into matplotlib:main Aug 25, 2023
8 of 11 checks passed
@jo-mueller jo-mueller deleted the add-feature-histogram branch August 26, 2023 23:50
@jo-mueller jo-mueller mentioned this pull request Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants