Skip to content

Add pause() and resume() methods to the base Animation class - #17826

Merged
dopplershift merged 14 commits into
matplotlib:masterfrom
DCtheTall:pauseresume
Jul 28, 2020
Merged

dopplershift merged 14 commits into
matplotlib:masterfrom
DCtheTall:pauseresume

Conversation

@DCtheTall

@DCtheTall DCtheTall commented Jul 3, 2020 •

Copy link
Copy Markdown
Contributor

PR Summary

PR Checklist

  • Has Pytest style unit tests (omitted, see comments below)
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/next_api_changes/* if API changed in a backward-incompatible way

@DCtheTall DCtheTall changed the title Pauseresume Add pause() and resume() methods to the base Animation class Jul 3, 2020
@DCtheTall

DCtheTall commented Jul 3, 2020 •

Copy link
Copy Markdown
Contributor Author

@tacaswell here is my first pass at adding the pause() and resume() methods you mentioned in #17689.

I had a few questions/issues before proceeding and wanted to open this to get feedback:

  1. This code assumes pause() and resume() are always called on the same thread (or a sufficient time apart for it to not matter). Is this a safe assumption? Should I make this more thread-safe?

  2. I am not sure if I am using set_animation() right.

  3. To test this, it makes sense to me to use plt.show(), but this causes test_animation.py to crash in pytest. Is there a better way to test these methods? Is there a way to get plt.show() to work?

Thanks again for the opportunity to contribute to this awesome library!

@tacaswell tacaswell added this to the v3.4.0 milestone Jul 3, 2020
@tacaswell
tacaswell requested a review from dopplershift July 3, 2020 18:43
Comment thread lib/matplotlib/animation.py Resolved
@tacaswell

Copy link
Copy Markdown
Member

I would not worry too much about thread safety, Matplotlib is deeply non-thread safe (if you are trying to work on the same figure from multiple threads it is going to go badly in many many other places).

While testing everything is good, there as a trade off when the effort of getting the testing working is worth the cost of getting it working. For this to work right you really need to have a running GUI event loop to manage the asynchronous nature of things. I would start by writing stand alone scripts that use timers (https://matplotlib.org/3.2.2/gallery/event_handling/timers.html) to start / stop a running animation. Once that is working we can sort out the best way to fold that into the test suite (or if having those scripts as an example is the practical option).

@DCtheTall

DCtheTall commented Jul 7, 2020 •

Copy link
Copy Markdown
Contributor Author

I would not worry too much about thread safety, Matplotlib is deeply non-thread safe

Ack'd. I won't worry about it then.

I would start by writing stand alone scripts that use timers to start/stop a running animation. Once that is working we can sort out the best way to fold that into the test suite (or if having those scripts as an example is the practical option).

Makes sense, what path in the project do these scripts normally go?

@dopplershift dopplershift left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 to the functionality. I think this would work, but would love to see an existence proof--even an example that uses this would be good.

Comment thread lib/matplotlib/animation.py Resolved
Comment thread lib/matplotlib/animation.py Resolved
@DCtheTall

Copy link
Copy Markdown
Contributor Author

I think this would work, but would love to see an existence proof--even an example that uses this would be good.

I will get working on an example script then!

@DCtheTall

DCtheTall commented Jul 12, 2020 •

Copy link
Copy Markdown
Contributor Author

@dopplershift example script for pausing/resuming animations in the latest set of commits.

The example is a simplified version of the example used in #17689

@dopplershift dopplershift left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Example works well from me. Just one minor comment to address and I'll be good with this.

Comment thread examples/animation/pause_resume.py Resolved
@DCtheTall
DCtheTall requested a review from dopplershift July 19, 2020 00:44
Comment thread examples/animation/pause_resume.py Resolved
Comment thread examples/animation/pause_resume.py Resolved
Comment thread examples/animation/pause_resume.py Resolved
@DCtheTall
DCtheTall requested a review from dopplershift July 21, 2020 15:40
@DCtheTall
DCtheTall requested a review from QuLogic July 21, 2020 15:40

@dopplershift dopplershift left a comment

Copy link
Copy Markdown
Contributor

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 working with us on the changes.

@QuLogic

QuLogic commented Jul 24, 2020

Copy link
Copy Markdown
Member

This seems good, except that the example doesn't animate?

@DCtheTall

DCtheTall commented Jul 24, 2020 •

Copy link
Copy Markdown
Contributor Author

@QuLogic hmm, it worked for me on Linux (and I assume it worked for @dopplershift since he gave an LGTM), what platform are you using?

@dopplershift

Copy link
Copy Markdown
Contributor

It's working (starting up with an animation and responding to clicks for pause/resume) for me with both the macOS and Qt5Agg backends on my Mac running 10.15.

@jklymak

jklymak commented Jul 27, 2020

Copy link
Copy Markdown
Member

You have a number of documentation build errors.

/home/circleci/project/doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.ArtistAnimation.pause
/home/circleci/project/doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.ArtistAnimation.resume
/home/circleci/project/lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.FuncAnimation.pause
/home/circleci/project/lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.FuncAnimation.resume
/home/circleci/project/doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:23:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.TimedAnimation.pause
/home/circleci/project/doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:23:<autosummary>:1: WARNING: py:obj reference target not found: matplotlib.animation.TimedAnimation.resume

Probably need to use double back quotes instead of single?

@QuLogic

QuLogic commented Jul 27, 2020

Copy link
Copy Markdown
Member

I figured it out; it's the usual garbage collection issue for animations. You need to assign the PauseAnimation() to a variable, or else it's garbage collected and with it, so is the animation, and nothing gets animated.

@QuLogic

QuLogic commented Jul 27, 2020 •

Copy link
Copy Markdown
Member

I don't know why docs are failing though; can you try rebasing to the latest master commit to get the current CI config? You probably want to add your example somewhere in doc/api/animation_api.rst too, maybe in the Examples heading.

@QuLogic

QuLogic commented Jul 27, 2020 •

Copy link
Copy Markdown
Member

Oh, this is actually some missing references because the autosummary tries to put in base methods in subclasses. This is already ignored, but these new methods changed things a bit.

You will need to update doc/missing-references.json to add the pause/resume methods. This file is a bit out-of-date, so the instructions will make a really big diff. Instead, here is what needs to be added:

diff --git a/doc/missing-references.json b/doc/missing-references.json
index 77244902b..f6fffc11d 100644
--- a/doc/missing-references.json
+++ b/doc/missing-references.json
@@ -1136,6 +1136,12 @@
     "matplotlib.animation.ArtistAnimation.to_jshtml": [
       "doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:21:<autosummary>:1"
     ],
+    "matplotlib.animation.ArtistAnimation.pause": [
+      "doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
+    ],
+    "matplotlib.animation.ArtistAnimation.resume": [
+      "doc/api/_as_gen/matplotlib.animation.ArtistAnimation.rst:23:<autosummary>:1"
+    ],
     "matplotlib.animation.FFMpegFileWriter.args_key": [
       "lib/matplotlib/animation.py:docstring of matplotlib.animation.FFMpegFileWriter.supported_formats:1:<autosummary>:1"
     ],
@@ -1235,6 +1241,12 @@
     "matplotlib.animation.FuncAnimation.to_jshtml": [
       "lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1"
     ],
+    "matplotlib.animation.FuncAnimation.pause": [
+      "lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1:"
+    ],
+    "matplotlib.animation.FuncAnimation.resume": [
+      "lib/matplotlib/animation.py:docstring of matplotlib.animation.FuncAnimation.new_frame_seq:1:<autosummary>:1:"
+    ],
     "matplotlib.animation.HTMLWriter.bin_path": [
       "doc/api/_as_gen/matplotlib.animation.HTMLWriter.rst:28:<autosummary>:1"
     ],
@@ -1361,6 +1373,12 @@
     "matplotlib.animation.TimedAnimation.to_jshtml": [
       "doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:21:<autosummary>:1"
     ],
+    "matplotlib.animation.TimedAnimation.pause": [
+      "doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:23:<autosummary>:1"
+    ],
+    "matplotlib.animation.TimedAnimation.resume": [
+      "doc/api/_as_gen/matplotlib.animation.TimedAnimation.rst:23:<autosummary>:1"
+    ],
     "matplotlib.cbook.ls_mapper": [
       "doc/api/prev_api_changes/api_changes_1.5.0.rst:11",
       "doc/api/prev_api_changes/api_changes_1.5.0.rst:8"

or I can push these changes for you.

@dopplershift

Copy link
Copy Markdown
Contributor

Now I don't understand why it was working for me in the first place. Oh well.

@DCtheTall

Copy link
Copy Markdown
Contributor Author

You need to assign the PauseAnimation() to a variable, or else it's garbage collected and with it, so is the animation, and nothing gets animated.

Done.

You will need to update doc/missing-references.json to add the pause/resume methods.

Done.

Now I don't understand why it was working for me in the first place.

I'm willing to chalk it up to dark magic for now 😅

@dopplershift dopplershift left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good pending CI passing.

@QuLogic

QuLogic commented Jul 29, 2020

Copy link
Copy Markdown
Member

Thanks @DCtheTall! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

@tacaswell

Copy link
Copy Markdown
Member

Thanks for this @DCtheTall ! Sorry I instigated this work and then dropped off entirely.

@DCtheTall

Copy link
Copy Markdown
Contributor Author

Thanks all! Happy to have contributed to a library I use pretty often these days, hope to be back for more in the future 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants