[matplotlib-devel] New Animation Class

2010-07-09 Thread Ryan May
Hi,

I've been hard at work over the last couple of months putting
together a set of classes that simplifies the creation of animations
in matplotlib. This started when I resurrected some old code for
animations to give to a colleague, when I realized just how bad the
old code was and how much better I could do. The result of this
afternoon hack is what I'm ready to put forth.  Some of the goals:

* Run independently of the backend, unlike the examples we have now
(This is really accomplished by the Timer object we now have)
* Remove the boilerplate code of setting up loops
* Facilitate saving out animations as a movie file
* Provide a simple API that integrates well with the rest of Matplotlib
* Provide (optional) blitting support so that users don't have to
learn the ins and outs of blitting

Overall, I think I accomplished my goals, so I'm putting this out
there for wider comments. I've attached the python module which, when
run, displays two animated figures. There is also a git repository at:

http://github.com/dopplershift/Animation

which has some more examples, including ports of our old examples.
(The examples assume animation.py is in your python path somewhere,
which you'll have to do by hand. This can be as simple as dropping
animation.py into the directory).

Some things to note:
* The flow is broken into *a lot* of member functions. This is to
provide sufficient entry points for subclasses so that they really
only need to reimplement the parts they override. Optional blitting
support drove a lot of this.
* There are two main classes for end users:
  * FuncAnimation -- provide a callback which draws the next frame of animation
  * ArtistAnimation -- provide a sequence of collections of artists
which are turned off and on for each frame of animation
* There is support for saving movies with either mencoder or ffmpeg.
The config for this is really rough, and the place I could *really*
use suggestions. I'm not sure how best to go about it. I've been
unable to find a (currently maintained) python library for saving
movie files, so system calls to the utilities is the best I can do at
the moment. I'm not sure what to use on windows, since I'm not sure of
the state (and requirements) of mencode/ffmpeg on windows.

TODOs:
* Configuring saving movie files (formats, programs, etc.) (see above)
* Documentation (I promise not to commit until this is written)
* More examples (could use some more procedural examples, like
animating using data read from a socket, or inotify)

I welcome feedback on this, because I really want to see this become
an easy and bulletproof way of doing animations in matplotlib. This
seems to be an area of frequent question on the mailing list, and I
want this framework to lessen the questions, not increase them.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
# TODO:
# * Documentation -- this will need a new section of the User's Guide.
#  Both for Animations and just timers.
#   - Also need to update http://www.scipy.org/Cookbook/Matplotlib/Animations
# * Blit
#   * Currently broken with Qt4 for widgets that don't start on screen
#   * Still a few edge cases that aren't working correctly
#   * Can this integrate better with existing matplotlib animation artist flag?
# * Example
#   * Frameless animation - pure procedural with no loop
#   * Need example that uses something like inotify or subprocess
#   * Complex syncing examples
# * Movies
#   * Library to make movies?
#   * RC parameter for config?
# * Need to consider event sources to allow clicking through multiple figures
from datetime import datetime

def traceme(func):
def wrapper(*args):
print '%s -- Calling: %s %s' % (datetime.now(), func.__name__, str(args))
ret = func(*args)
print 'Returned: %s' % func.__name__
return ret
return wrapper

from matplotlib.cbook import iterable

class Animation(object):
'''
This class wraps the creation of an animation using matplotlib. It is
only a base class which should be subclassed to provide needed behavior.

*fig* is the figure object that is used to get draw, resize, and any
other needed events.

*event_source* is a class that can run a callback when desired events
are generated, as well as be stopped and started. Examples include timers
(see :class:`TimedAnimation`) and file system notifications.

*blit* is a boolean that controls whether blitting is used to optimize
drawing.
'''
def __init__(self, fig, event_source=None, blit=False):
self._fig = fig
self._blit = blit

# These are the basics of the animation.  The frame sequence represents
# information for each frame of the animation and depends on how the
# drawing is handled by the subclasses. The event source fires events
# that cause the frame sequence to be iterated.
self.frame_seq = self.new_frame_seq()
self.event_source = 

Re: [matplotlib-devel] New Animation Class

2010-07-09 Thread Ryan May
On Fri, Jul 9, 2010 at 5:22 PM, Ryan May rma...@gmail.com wrote:
 Hi,

 I've been hard at work over the last couple of months putting
 together a set of classes that simplifies the creation of animations
 in matplotlib. This started when I resurrected some old code for
 animations to give to a colleague, when I realized just how bad the
 old code was and how much better I could do. The result of this
 afternoon hack is what I'm ready to put forth.  Some of the goals:

In my haste to get this out, I forgot to mention that thanks go to Ben
Root for already banging on this quite a bit, giving quite a bit of
useful design feedback and helping me find some bugs.

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma

--
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] New Animation Class

2010-07-09 Thread John Hunter
On Fri, Jul 9, 2010 at 5:22 PM, Ryan May rma...@gmail.com wrote:

 I've been hard at work over the last couple of months putting
 together a set of classes that simplifies the creation of animations
 in matplotlib. This started when I resurrected some old code for

Very nice -- people are going to really like this. I've had several
requests personally to add something like this so thanks for doing it
:-)

Some rapid fire comments, in no particular order

  * on recent ubuntu linux with gtkagg, dynamic_image has a one
pixel bug on the right side if blit=True.  I suspect this has nothing
to do with your package and everything to do with gtkagg blitting code
having an off-by-one error somewhere.  I added this to the tracker:
https://sourceforge.net/tracker/?func=detailaid=3027636group_id=80706atid=560720

  * this is completely un-thought out, but could we define a subclass
of TimedAnimation to work like an iterator so users could do the
natural thing :

  line, = ax.plot(something)
  for frame in SomeTimedAnimation(fig, blit=True):
  line.set_data(mydata)
  frame.update()

or something along those lines

* when you integrate this into trunk, I would like to see widgets.py
upgraded to use it.  This is not a requirement and I would be happy to
help with it, but it is a good way to push on the new API and expand
the test cases.

* I am happy to see this pushed into trunk at any time.  I would not
push it into the branch, but we can do a 1.1 trunk release as soon as
we are ready (release early, release often).  Putting it in the trunk
will facilitate testing and other developer contributions.  But if
you'd rather leave it in github for a while, I have no problem with
that either.

* you hardcode the artist visible state True/False in ArtistAnimation,
which overrides any settings the users may be trying to control. Not
sure if this is a problem, but it's something to think about.  When
you set False in ArtistAnimation._init_draw, should you first store
the current state so you can restore the userland settings?  You
comment that maybe you should be integrating with the animated
property.  This is essentially what this is for, if animated is set it
should not be used in drawing the background.   Not sure if this
matters since it may be sensible to assume they are handing you
control of visibility in ArtistAnimation, just throwing it out there.

* in Animation.save, why do you set blit=False?  When making movies,
shouldn't we also depend on the efficiencies of blit?  Or was the
idea: blit is buggy so for production movies turn it off cause I'm
willing to sacrifice performance for quality?
If so, I'd rather try an fix the bugsor expose blit as a kwarg.

* a tutorial for the site docs would be awesome.  It's one of the big
missing pieces in the docs, so this would be a good time to add it.

* when you include animation.py in the trunk, would you write the examples as

  import matplotlib.animation as animation
  ani = animation.FuncAnimation(fig, ...)

  per the style guidelines in the coding guide.

* let's preserve the old gui specific examples in a subdir of
examples/animation, so people who need bare metal control will still
have examples to follow.  You can add a README in that dir suggesting
the use of the new API unless necessary.

Nice work.

JDH

--
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] ContourSet not an Artist?

2010-07-09 Thread John Hunter
On Fri, Jul 9, 2010 at 9:59 PM, Eric Firing efir...@hawaii.edu wrote:
 On 07/09/2010 11:45 AM, Ryan May wrote:
 Hi,

 I just noticed that ContourSet only inherits ScalarMappable and
 ContourLabeller, neither of which is an Artist subclass, which means
 ContourSet is not an Artist. (And is why it does not have a remove()
 method).  Anyone have any idea why and if we should correct that? I'm
 not sure what the ramifications would be.

 It's just historical.  Should every plotting command return an Artist,
 simple or compound?  If so, I suspect contouring is not the only thing
 that will need refactoring.  I haven't investigated what would be required.

Ryan, was there a particular use case in which you found that CS was
not an Artist to be a limitation, or were you just asking out of
curiosity?

JDH

--
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel


Re: [matplotlib-devel] ContourSet not an Artist?

2010-07-09 Thread Ryan May
On Jul 9, 2010, at 22:11, John Hunter jdh2...@gmail.com wrote:

 On Fri, Jul 9, 2010 at 9:59 PM, Eric Firing efir...@hawaii.edu wrote:
 On 07/09/2010 11:45 AM, Ryan May wrote:
 Hi,
 
 I just noticed that ContourSet only inherits ScalarMappable and
 ContourLabeller, neither of which is an Artist subclass, which means
 ContourSet is not an Artist. (And is why it does not have a remove()
 method).  Anyone have any idea why and if we should correct that? I'm
 not sure what the ramifications would be.
 
 It's just historical.  Should every plotting command return an Artist,
 simple or compound?  If so, I suspect contouring is not the only thing
 that will need refactoring.  I haven't investigated what would be required.
 
 Ryan, was there a particular use case in which you found that CS was
 not an Artist to be a limitation, or were you just asking out of
 curiosit

Looking at adding a remove method, per another thread. I was confused by the 
lack of standard machinery, and wasn't sure if I should just add remove method 
or make it an artist.

Ryan


 
 JDH
 
 --
 This SF.net email is sponsored by Sprint
 What will you do first with EVO, the first 4G phone?
 Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
 ___
 Matplotlib-devel mailing list
 Matplotlib-devel@lists.sourceforge.net
 https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

--
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
___
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel