Skip to content

Blocked set_clim() callbacks to prevent inconsistent state (#29522) #29590

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

Merged
merged 4 commits into from
Feb 28, 2025

Conversation

prafulgulani
Copy link
Contributor

closes #29522

  • Why is this change necessary? What problem does it solve? What is the reasoning for this implementation?
    Previously when norm's limits were updated, self.changed() was called through the callbacks attached to the norm, these callbacks were fired immediately leading to inconsistent state and causing the bug.
    This change blocks the immediate callbacks and emits a update signal after both the limits have been set.

PR checklist

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

Can you add a test for this?

This is still a band-aid over the underlying problem in my opinion, but probably worth adding still. This won't help people setting vmin/vmax manually outside of the set_clim() call.

The issue is still with who should own the updates to the norm, the colorbar or the norm setting and does one take precedence over the other. My guess is that we need to try and push the colorbar's auto-expansion logic up into the initializer somehow rather than at draw time.

@prafulgulani
Copy link
Contributor Author

prafulgulani commented Feb 9, 2025

Not sure if the test would meet the expectations because this is my first open-source contribution, can I give it a try?
Just to be sure I need to write a test which tests if the set_clim() function now updates to new limits correctly right?

@greglucas
Copy link
Contributor

@prafulgulani, yes please do give it a shot and if you have any questions feel free to ask here.

You want to make sure that only one "changed" signal gets sent when calling set_clim(). Here is an example test we already have for testing the callbacks from the norm directly. You should add a similar mock.assert_called_once() to your test.

def test_norm_callback():
increment = unittest.mock.Mock(return_value=None)
norm = mcolors.Normalize()
norm.callbacks.connect('changed', increment)
# Haven't updated anything, so call count should be 0
assert increment.call_count == 0
# Now change vmin and vmax to test callbacks
norm.vmin = 1
assert increment.call_count == 1
norm.vmax = 5
assert increment.call_count == 2
# callback shouldn't be called if setting to the same value
norm.vmin = 1
assert increment.call_count == 2
norm.vmax = 5
assert increment.call_count == 2
# We only want autoscale() calls to send out one update signal
increment.call_count = 0
norm.autoscale([0, 1, 2])
assert increment.call_count == 1

@prafulgulani prafulgulani force-pushed the bugfix-for-issue-29522 branch 2 times, most recently from 256882f to 295e5b7 Compare February 12, 2025 05:40
@prafulgulani prafulgulani marked this pull request as ready for review February 12, 2025 09:02
@prafulgulani prafulgulani force-pushed the bugfix-for-issue-29522 branch from 295e5b7 to bf5370d Compare February 12, 2025 16:22
Copy link
Contributor

@greglucas greglucas 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, thanks for updating this!

Note to second reviewer, please squash merge.

Comment on lines 1625 to 1626
plt.close(fig)

Copy link
Member

Choose a reason for hiding this comment

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

Not necessary; tests always close figures.

Suggested change
plt.close(fig)

Comment on lines 1616 to 1617
# Initial callback count should be zero
assert callback.call_count == 0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Initial callback count should be zero
assert callback.call_count == 0
callback.assert_not_called()

@github-actions github-actions bot added topic: color/color & colormaps topic: images topic: axes CI: Run cibuildwheel Run wheel building tests on a PR Documentation: examples files in galleries/examples Documentation: devdocs files in doc/devel Documentation: user guide files in galleries/users_explain or doc/users labels Feb 22, 2025
@prafulgulani prafulgulani force-pushed the bugfix-for-issue-29522 branch from 2cbfdb3 to 49a0ab2 Compare February 22, 2025 10:20
@github-actions github-actions bot removed topic: color/color & colormaps topic: images topic: axes CI: Run cibuildwheel Run wheel building tests on a PR Documentation: examples files in galleries/examples Documentation: devdocs files in doc/devel Documentation: user guide files in galleries/users_explain or doc/users labels Feb 22, 2025
@timhoffm timhoffm added this to the v3.10.2 milestone Feb 28, 2025
@timhoffm timhoffm merged commit 07371db into matplotlib:main Feb 28, 2025
42 of 45 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 28, 2025
@timhoffm
Copy link
Member

Thanks @prafulgulani and congratulations on your first contribution to Matploblib! 🎉

@prafulgulani prafulgulani deleted the bugfix-for-issue-29522 branch February 28, 2025 15:34
@prafulgulani prafulgulani restored the bugfix-for-issue-29522 branch February 28, 2025 15:34
@prafulgulani prafulgulani deleted the bugfix-for-issue-29522 branch February 28, 2025 15:35
@QuLogic QuLogic moved this from Waiting for author to Merged in First Time Contributors Feb 28, 2025
QuLogic added a commit that referenced this pull request Feb 28, 2025
…590-on-v3.10.x

Backport PR #29590 on branch v3.10.x (Blocked set_clim() callbacks to prevent inconsistent state (#29522))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
4 participants