Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Make number of frames available after rendering using generator.
Browse files Browse the repository at this point in the history
The APNG pipeline requires the number of frames up front, and other
converters might benefit as well.  Once the frames are rendered (and don't
get re-rendered), the number of frames is obviously fixed.

As an alternative, we could turn the generator into a list when rendering,
thus avoiding the extra attribute.  But the downside of this is that the
list might be around for considerable time, eating memory.

The example animation is changed since the previous example would generate
warnings during rendering, due to negative values in the default range of x.
  • Loading branch information
gagern committed Jul 16, 2014
1 parent 780e614 commit 10f8e52
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/sage/plot/animate.py
Expand Up @@ -185,10 +185,15 @@ class Animation(SageObject):
....: for k in srange(0,2*pi,0.3)])
sage: a.show() # optional -- ImageMagick
Do not convert input iterator to a list::
Do not convert input iterator to a list, but ensure that
the frame count is known after rendering the frames::
sage: a = animate((x^p for p in sxrange(1,2,.1))); a
sage: a = animate((plot(x^p, (x,0,2)) for p in sxrange(1,2,.1))); a
Animation with unknown number of frames
sage: a.png() # long time
'.../'
sage: len(a) # long time
10
sage: a._frames
<generator object ...
Expand Down Expand Up @@ -313,7 +318,7 @@ def __add__(self, other):
kwds = self._combine_kwds(self._kwds, other._kwds)

#Combine the frames
m = max(len(self._frames), len(other._frames))
m = max(len(self), len(other))
frames = [a+b for a,b in zip(self._frames, other._frames)]
frames += self._frames[m:] + other._frames[m:]

Expand Down Expand Up @@ -357,7 +362,10 @@ def __len__(self):
sage: len(a)
5
"""
return len(self._frames)
try:
return self._num_frames
except AttributeError:
return len(self._frames)

def make_image(self, frame, filename, **kwds):
r"""
Expand Down Expand Up @@ -428,18 +436,20 @@ def png(self, dir=None):
return self._png_dir
except AttributeError:
pass
d = tmp_dir()
else:
d = dir
G = self._frames
for i, frame in enumerate(self._frames):
filename = '%s/%s'%(d,sage.misc.misc.pad_zeros(i,8))
dir = tmp_dir()
i = 0
for frame in self._frames:
filename = '%s/%08d.png'%(dir,i)
try:
frame.save_image(filename + '.png', **self._kwds)
save_image = frame.save_image
except AttributeError:
self.make_image(frame, filename + '.png', **self._kwds)
self._png_dir = d
return d
self.make_image(frame, filename, **self._kwds)
else:
save_image(filename, **self._kwds)
i += 1
self._num_frames = i
self._png_dir = dir
return dir

def graphics_array(self, ncols=3):
r"""
Expand Down

0 comments on commit 10f8e52

Please sign in to comment.