Skip to content

FuncAnimation with generator defaults to arbitrary save_count=100 #8278

Closed

Description

@mforbes

Bug report

Bug summary

When using matplotlib.animation.FuncAnimation with a generator and without specifying save_count, a default value of save_count=100 is used. Instead, the generator should be run until it raises a StopIteration exception.

Code for reproduction

import numpy as np
from matplotlib.animation import FuncAnimation

def get_frames():
    t = 0.0
    x = np.linspace(0, 2*np.pi, 100)
    l, = plt.plot(x, np.sin(x+t))
    txt = plt.title("")
    yield l,  # Initial frame... not stored
    for nt, t in enumerate(np.linspace(0,2*np.pi, 200)):
        l.set_data(x, np.sin(x+t))
        txt.set_text("frame: {}".format(nt))
        yield l,
        
anim = FuncAnimation(plt.gcf(), lambda artists:artists, frames=get_frames())

# To view, use a notebook for example
from IPython.display import HTML
HTML(anim.to_html5_video())

Actual outcome

Only frames 0 to 99 are drawn even though the generator will produce 200 frames (up to 199).

Expected outcome

An animation that runs until the generator raises StopIteration.

Matplotlib version

It looks like this will affect the latest version on GitHub. I tested this with version 1.5.3 from anaconda.

Activity

  1. dopplershift commented on Mar 13, 2017

    @dopplershift
    Contributor

    Not a bug. Generators are not guaranteed to ever raise StopIteration, which is one of the reasons save_count exists. Just blindly looking for that could lead to an infinite loop.

    I'd be open to a pull request that uses a sentinel value for save_count (-1?) to indicate that the generator will finish and to keep iterating until StopIteration. I've not dug in enough to know the full ramifications of this though.

  2. anntzer commented on Apr 15, 2017

    @anntzer
    Contributor

    I agree with the OP and disagree with @dopplershift. The general pattern in Python when someone feeds you an infinite generator is to just keep advancing it forever and ever, and consider the infinite look a user-level bug. np.fromiter(itertools.count(), int) just loops forever, it does not truncate its output after 200 elements.

  3. dopplershift commented on Apr 15, 2017

    @dopplershift
    Contributor

    You can disagree with whether save_count was a good idea, but speaking as the designer, there's no argument: save_count is operating exactly as I intended in this case. Ergo, not a bug.

    Since we don't want to break backwards compatibility, how do you feel about allowing a save_count to signal that we should just go until StopIteration?

  4. anntzer commented on Apr 15, 2017

    @anntzer
    Contributor

    At least then the default value of save_count should be switched to 100 (instead of None) and this should be reflected in the docs. Setting save_count to None should be understood as no limit.

  5. added this to the milestone on Apr 17, 2017
  6. WeatherGod commented on Apr 17, 2017

    @WeatherGod
    Member
  7. tacaswell commented on Apr 17, 2017

    @tacaswell
    Member

    True, but 'none' isn't super meaningful here, going with -1 meaning 'forever' is a c-ism I am not a huge fan of, and as @anntzer points out 'just keep going' is the expected python behavior for None in this sort of case.

  8. dopplershift commented on Apr 17, 2017

    @dopplershift
    Contributor

    I'm fine in principle with None as the sentinel value. We have a complication here where None maps to default behavior, which depends on whether we're given a sequence (known length) vs. iterator (unknown length). It's a little convoluted at the moment, and I'm not sure how to utilize None for "just keep reading" without breaking the existing use of None. Unless we don't think it's important to preserve... @tacaswell ?

  9. tacaswell commented on Apr 17, 2017

    @tacaswell
    Member

    Un-related change:

    • change the internal logic to treat save_count as an upper limit and actually set to min(save_count, len(frames)) assuming it has a length, currently it is completely ignored if len(frames) is a thing. You could argue that it should be the users job to slice the input how ever they want before handing it to us (which is what I would say to argue against adding a step kwarg).

    option 1

    • change default to our own sentinal, if we get that clip to 100
    • let None mean length of frames or un-bounded

    The only place this will break things is will really break things is if someone is passing in an un-bounded generator + explicitly passing in save_count=None and relying on us to implicitly clip to 100 (rather than clipping them selves). That seems like it should be rare and is a bad idea in any case.

    option 2

    • leave default as None
    • treat None as un-bounded, but still extract a reasonable length len(frames) is a thing

    This is simpler, but will break anyone who has been passing in an un-bounded generator.

    option 3

    • add a UNBOUNDED sentinel to animation, but I like both 1 and 2 better.

    won't break anything, but adds extra complexity to get 'expected pythonic behavior'.

    not-an-option

    • changing the default to 100 as we sometimes ignore this and would interfere with the unrelated thing at the top

    Sorry this is rambly.

  10. anntzer commented on Apr 24, 2017

    @anntzer
    Contributor

    Option 2 will only break code that used to pass an unbounded generator and expected it to be clipped at 100, which is likely broken as well (although an infinite loop may indeed qualify "more broken"). So I'd favor that.

  11. modified the milestones: , on Sep 24, 2017
  12. anntzer commented on Jun 10, 2018

    @anntzer
    Contributor

    Behavior deprecated with a warning in #10301.

  13. v-goncharenko commented on Dec 18, 2019

    @v-goncharenko

    Seems like conversation eded with some merges, but documentation still have nothing about this kind of behaviour. It's quite confusing especially considering that even StackOverflow have dedicated topic.

    Maybe it's worth to just add some note to docs while complex solutions is being developed.

    Thanks

  14. 6 remaining items

  15. timhoffm commented on Feb 16, 2020

    @timhoffm
    Member

    #8278 (comment)

    Looks like the deprecation is still in place, "just" needs to be expired. I guess it's a simple

    Unfortunately it's not that simple.

    We initialize save_count explicitly to 100 if we don't have a better idea:

    self.save_count = 100

    We rely on a finite value of save_count.

    self._save_seq = self._save_seq[-self.save_count:]

    What was the intention of happening after the deprecation? Should it run as lon as frames yields values (maybe case forever)? Or do we want to require an explicit values for iterators and generators?

  16. anntzer commented on Feb 16, 2020

    @anntzer
    Contributor

    Should it run as lon as frames yields values (maybe case forever)?

    My opinion is yes (it's just like calling list on an iterator: we assume that the user is responsible for ensuring that the thing is finite, calling list() on an infinite iterator just hangs).

    Looks like the deprecation is still in place, "just" needs to be expired. I guess it's a simple...

    Yeah, I'm not sure I put in the deprecation correctly, we likely need another deprecation round if we want to be safe.

  17. timhoffm commented on Feb 16, 2020

    @timhoffm
    Member

    I also think that we need a second deprecation.

    But more importantly, we have to understand how this should be modified if we don't have any save_count:

    self._save_seq = self._save_seq[-self.save_count:]

  18. QuLogic commented on Apr 25, 2020

    @QuLogic
    Member

    I was going to remove this in 3.3, but I see we have some outstanding decisions. The way I see it, we have 3 options for handling _save_seq when save_count is None:

    • Don't trim the list; this is the minimal fixup, but wastes memory if the animation is intended to be infinite.
    • Don't save anything; this doesn't waste any memory, but means you can't watch, then save a movie.
    • Add an option to do either one (somewhere.)
  19. dopplershift commented on Apr 25, 2020

    @dopplershift
    Contributor

    I think I lean towards don't trim, which is the most consistent with the original, albeit flawed, design and the current operation.

  20. modified the milestones: needs sorting, v3.4.0 on Jul 14, 2020
  21. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  22. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  23. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  24. QuLogic commented on Jan 26, 2023

    @QuLogic
    Member

    The 100 save_count default was removed in #24131

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions