You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FuncAnimation with generator defaults to arbitrary save_count=100 #8278
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
importnumpyasnpfrommatplotlib.animationimportFuncAnimationdefget_frames():
t=0.0x=np.linspace(0, 2*np.pi, 100)
l, =plt.plot(x, np.sin(x+t))
txt=plt.title("")
yieldl, # Initial frame... not storedfornt, tinenumerate(np.linspace(0,2*np.pi, 200)):
l.set_data(x, np.sin(x+t))
txt.set_text("frame: {}".format(nt))
yieldl,
anim=FuncAnimation(plt.gcf(), lambdaartists:artists, frames=get_frames())
# To view, use a notebook for examplefromIPython.displayimportHTMLHTML(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.
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.
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.
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?
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.
On Apr 15, 2017 3:55 PM, "Antony Lee" ***@***.***> wrote:
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.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#8278 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-ErLHgp0COobD6cY9Eo8x0z0QQUjks5rwSCmgaJpZM4Maxt6>
.
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.
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 ?
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
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.
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.
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?
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.
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.
Activity
dopplershift commentedon Mar 13, 2017
Not a bug. Generators are not guaranteed to ever raise
StopIteration, which is one of the reasonssave_countexists. 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 untilStopIteration. I've not dug in enough to know the full ramifications of this though.anntzer commentedon Apr 15, 2017
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.dopplershift commentedon Apr 15, 2017
You can disagree with whether
save_countwas a good idea, but speaking as the designer, there's no argument:save_countis 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_countto signal that we should just go untilStopIteration?anntzer commentedon Apr 15, 2017
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.
WeatherGod commentedon Apr 17, 2017
tacaswell commentedon Apr 17, 2017
True, but
'none'isn't super meaningful here, going with-1meaning '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 forNonein this sort of case.dopplershift commentedon Apr 17, 2017
I'm fine in principle with
Noneas the sentinel value. We have a complication here whereNonemaps 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 utilizeNonefor "just keep reading" without breaking the existing use ofNone. Unless we don't think it's important to preserve... @tacaswell ?tacaswell commentedon Apr 17, 2017
Un-related change:
save_countas an upper limit and actually set tomin(save_count, len(frames))assuming it has a length, currently it is completely ignored iflen(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 astepkwarg).option 1
Nonemean length of frames or un-boundedThe 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=Noneand 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
len(frames)is a thingThis is simpler, but will break anyone who has been passing in an un-bounded generator.
option 3
UNBOUNDEDsentinel 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
Sorry this is rambly.
anntzer commentedon Apr 24, 2017
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.
anntzer commentedon Jun 10, 2018
Behavior deprecated with a warning in #10301.
v-goncharenko commentedon Dec 18, 2019
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
6 remaining items
timhoffm commentedon Feb 16, 2020
#8278 (comment)
Unfortunately it's not that simple.
We initialize
save_countexplicitly to 100 if we don't have a better idea:matplotlib/lib/matplotlib/animation.py
Line 1643 in 29fe439
We rely on a finite value of
save_count.matplotlib/lib/matplotlib/animation.py
Line 1721 in 29fe439
What was the intention of happening after the deprecation? Should it run as lon as
framesyields values (maybe case forever)? Or do we want to require an explicit values for iterators and generators?anntzer commentedon Feb 16, 2020
My opinion is yes (it's just like calling
liston an iterator: we assume that the user is responsible for ensuring that the thing is finite, callinglist()on an infinite iterator just hangs).Yeah, I'm not sure I put in the deprecation correctly, we likely need another deprecation round if we want to be safe.
timhoffm commentedon Feb 16, 2020
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:matplotlib/lib/matplotlib/animation.py
Line 1721 in 29fe439
QuLogic commentedon Apr 25, 2020
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_seqwhensave_count is None:dopplershift commentedon Apr 25, 2020
I think I lean towards don't trim, which is the most consistent with the original, albeit flawed, design and the current operation.
QuLogic commentedon Jan 26, 2023
The 100
save_countdefault was removed in #24131