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

Create tests for utils/plotting.py (#302) #323

Merged
merged 5 commits into from
May 3, 2023

Conversation

kingjuno
Copy link
Contributor

This PR creates tests for #302.

@kingjuno
Copy link
Contributor Author

@klieret For now, I have only created tests for EventPlotter to confirm whether I am doing things correctly. If so, I will start working on the others.

@codecov-commenter
Copy link

Codecov Report

Patch coverage: 100.00% and project coverage change: +3.17 🎉

Comparison is base (afbeefd) 69.67% compared to head (cf12d69) 72.84%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #323      +/-   ##
==========================================
+ Coverage   69.67%   72.84%   +3.17%     
==========================================
  Files          44       45       +1     
  Lines        2463     2504      +41     
==========================================
+ Hits         1716     1824     +108     
+ Misses        747      680      -67     
Impacted Files Coverage Δ
src/gnn_tracking/test_data/__init__.py 100.00% <100.00%> (ø)
src/gnn_tracking/utils/plotting.py 26.74% <100.00%> (+26.74%) ⬆️
src/gnn_tracking/utils/test_plotting.py 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@klieret klieret self-requested a review May 1, 2023 17:07
Copy link
Member

@klieret klieret left a comment

Choose a reason for hiding this comment

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

Thanks a lot for all of your work. I'm impressed by how thoroughly you started this. I was mostly thinking about ensuring that the plots run without failure, rather than testing the results.

This is also where I see the following issue: We duplicate a lot of code from the EventPlotter. That means we test that we get the same result with the code copied over from its origin (with the intermediate step of plotting it and retrieving it again), which then doesn't show all that much.

So while I can see that a lot of thinking went into this, would you be open to simplifying this?

So we could do something really silly, like just testing len(fig.axes[i].get_lines()[0].get_xydata())).

If we want to test the content, we could simply do something like

fig.axes[i].get_lines()[0].get_xydata()[0] == approx(hard coded number here)

from gnn_tracking.utils.plotting import EventPlotter


@patch("matplotlib.pyplot.show")
Copy link
Member

Choose a reason for hiding this comment

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

nice :)

fig, _ = plotter.plot_ep_rv_uv(evtid=evtid)
plotted_data_points = []
original_data_points = []
original_data_points.append(hits[["eta", "phi"]].values)
Copy link
Member

Choose a reason for hiding this comment

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

it's recommended to use DataFrame.to_numpy() rather than .values

for i in range(3):
plotted_data_points.append(np.array(fig.axes[i].get_lines()[0].get_xydata()))
plt.close(fig)
assert (plotted_data_points[0] == original_data_points[0]).all()
Copy link
Member

Choose a reason for hiding this comment

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

since these are floating points, it's probably best to use pytest.approx or something similar

original_data_points.append(hits[["z", "r"]].values)
original_data_points.append(hits[["u", "v"]].values)
for i in range(3):
plotted_data_points.append(np.array(fig.axes[i].get_lines()[0].get_xydata()))
Copy link
Member

@klieret klieret May 1, 2023

Choose a reason for hiding this comment

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

Nice! That's a cool way of actually testing the real content

src/gnn_tracking/utils/test_plotting.py Show resolved Hide resolved
@kingjuno
Copy link
Contributor Author

kingjuno commented May 2, 2023

So, am I wrong in assuming that you need the test to look something like this?

def test_event_plotter(mock_show):
    assert mock_show is plt.show
    path = trackml_test_data_dir
    evtid = 1

    plotter = EventPlotter(indir=path)
    fig, _ = plotter.plot_ep_rv_uv(evtid=evtid)
    original_data_points = []

    original_data_points.append(
        [
            [-3.65964174, -2.99420524],
            [-3.14542103, -2.94588566],
            [-3.65947032, -2.99451041],
            [-3.14519644, -2.94551826],
            [-2.90377235, -2.63840151],
            [-3.21849656, -2.51005006],
            [-3.2187562, -2.50965071],
            [-3.48590827, -2.21800613],
            [-2.85355783, -2.35087061],
            [-3.21562123, -2.23991871],
        ]
    )
    original_data_points.append(
        [
            [-1502.5, 77.40522003],
            [-1498.0, 129.21363831],
            [-1498.0, 77.18669128],
            [-1502.0, 129.58804321],
            [-1498.0, 164.72381592],
            [-1502.0, 120.39829254],
            [-1498.0, 120.04631805],
            [-1498.0, 91.84147644],
            [-1502.0, 173.72445679],
            [-1498.0, 120.42472839],
        ]
    )
    original_data_points.append(
        [
            [-0.01277896, -0.00189722],
            [-0.00759138, -0.00150495],
            [-0.01281572, -0.00189868],
            [-0.0075689, -0.00150338],
            [-0.00531829, -0.00292747],
            [-0.00670373, -0.00490364],
            [-0.00672142, -0.00492071],
            [-0.00656526, -0.00868637],
            [-0.00404855, -0.00409189],
            [-0.00515092, -0.00651333],
        ]
    )
    for i in range(3):
        assert_approx_equal(
            np.array(fig.axes[i].get_lines()[0].get_xydata()[:10]),
            np.array(original_data_points[i]),
        )
    plt.close(fig)

@klieret
Copy link
Member

klieret commented May 2, 2023

Yes, I think that looks perfect :)

Some code quality checkers will probably want you to define original_data_points in one go (rather than appending to it).

I propose to merge this once you're done with this test and to create a new PR for any other tests.

@kingjuno
Copy link
Contributor Author

kingjuno commented May 2, 2023

I have pushed the changes. Let me know if any changes are required.

Copy link
Member

@klieret klieret 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 to me! Thank you so much :)

I'll merge once the tests run through


        for i in range(3):
>           assert_approx_equal(
                np.array(fig.axes[i].get_lines()[0].get_xydata()[:10]),
                np.array(original_data_points[i]),
            )

src/gnn_tracking/utils/test_plotting.py:60: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (array([[-3.65964174, -2.99420524],
       [-3.14542103, -2.94588566],
       [-3.65947032, -2.99451065],
       [-3.1...2.50965071],
       [-3.48590827, -2.21800613],
       [-2.85355783, -2.35087061],
       [-3.21562123, -2.23991871]]))
kwds = {}

    @wraps(func)
    def inner(*args, **kwds):
        with self._recreate_cm():
>           return func(*args, **kwds)
E           TypeError: only size-1 arrays can be converted to Python scalars

I don't immediately see the route cause, does this happen locally?

@kingjuno
Copy link
Contributor Author

kingjuno commented May 2, 2023

oops test failed because I forgot to change approx_equal to almost_equal 🥲 .

@klieret klieret merged commit b900b53 into gnn-tracking:main May 3, 2023
@klieret
Copy link
Member

klieret commented May 4, 2023

@all-contributors please add @kingjuno for test

@allcontributors
Copy link

@klieret

We had trouble processing your request. Please try again later.

@klieret
Copy link
Member

klieret commented May 5, 2023

@all-contributors please add @kingjuno for test

@allcontributors
Copy link

@klieret

We had trouble processing your request. Please try again later.

@klieret
Copy link
Member

klieret commented May 5, 2023

Hmmm :/ I wonder if I broke the .all-contributorsrc...

Added manually

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.

3 participants