waitforbuttonpress function addition

Hi,

Following Gael Varoquaux's lead on adding a ginput command to matplotlib
(nice job!), I added a waitforbuttonpress function to matplotlib. The
patch is attached (generate using svn diff from the
matplotlib/trunk/matplotlib directory). waitforbuttonpress is a simple
function with a matlab equivalent that returns a list of true/false's -
true for a keyboard click and false for a mouse click. I use it in
matlab regularly as a simple yes/no question (often to decide whether or
not to save a figure).

The way I have implemented it is by adding an additional class
BlockingKeyMouseInput, which is quite similar to BlockingMouseInput, but
waits for both key and mouse events. A smarter person than I could
probably combine these two classes and make something that would serve
both functions. But I am basically new to python and don't feel
comfortable. Perhaps someone else could take a look and make
improvements/simplifications?

The other thing that I have noticed with both ginput and
waitforbuttonpress is that if you use ctrl-c to break out of either,
then the callback functions remain attached to their respective events
(e.g., try ginput(n=-1,timeout=-1,verbose=True) and hit ctrl-c). This
probably isn't a huge problem, but it would be nice if there was a way
to say "if ctrl-c is pressed, cleanup nicely". Does someone know if
that is possible?

Cheers,
David

waitforbuttonpress.patch (4.69 KB)

···

--
**********************************
David M. Kaplan
Charge de Recherche 1
Institut de Recherche pour le Developpement
Centre de Recherche Halieutique Mediterraneenne et Tropicale
av. Jean Monnet
B.P. 171
34203 Sete cedex
France

Phone: +33 (0)4 99 57 32 27
Fax: +33 (0)4 99 57 32 95
http://www.ur097.ird.fr/team/dkaplan/index.html
**********************************

--
**********************************
David M. Kaplan
Assistant Researcher
UCSC / Institute of Marine Sciences
Ocean Sciences
1156 High St.
SC, CA 95064

Phone: 831-459-4789
Fax: 831-459-4882
http://pmc.ucsc.edu/~dmk/
**********************************

The way I have implemented it is by adding an additional class
BlockingKeyMouseInput, which is quite similar to BlockingMouseInput, but
waits for both key and mouse events. A smarter person than I could
probably combine these two classes and make something that would serve
both functions. But I am basically new to python and don't feel
comfortable. Perhaps someone else could take a look and make
improvements/simplifications?

The only significantly different lines are the two lines where an
mplconnect is done to register the callback. You could abstract this in a
method and then have a base class and two sub classes for each call: the
blocking from mouse and the blocking from button.

The other thing that I have noticed with both ginput and
waitforbuttonpress is that if you use ctrl-c to break out of either,
then the callback functions remain attached to their respective events
(e.g., try ginput(n=-1,timeout=-1,verbose=True) and hit ctrl-c). This
probably isn't a huge problem, but it would be nice if there was a way
to say "if ctrl-c is pressed, cleanup nicely". Does someone know if
that is possible?

I think this is a good usecase for a try: ... finally: ... .

I don't have time to do these changes right now, as I am very busy both
with IPython and Mayavi, and will be travelling next two weeks, but you
can have a good at them, and someone else will probably commit your
patch, if you removed the code duplication.

Cheers,

Ga�l

···

On Fri, Jul 11, 2008 at 03:22:30PM +0200, David M. Kaplan wrote:

Hi,

Attached is a new patch to replace the previous one that I sent that
does what Gael suggested. It works well and seems fairly efficient to
me, but again I am new to python and I could be mistaken about what Gael
was suggesting. Basically, I created a base class that does the
blocking and collects events of any particular set of types specified by
name at initiation. I then created two subclasses that specify exactly
which events they deal with and do additional processing beyond just
collecting events, one for mouse events and one for mouse+keyboard
events. These are then called by ginput and waitforbuttonpress
respectively. I also changed my version of waitforbuttonpress so that
it precisely matches the functionality of matlab's waitforbuttonpress,
with the exception of a timeout option.

Comments welcome.

Cheers,
David

waitforbuttonpress.patch (8.91 KB)

···

On Fri, 2008-07-11 at 16:12 +0200, Gael Varoquaux wrote:

On Fri, Jul 11, 2008 at 03:22:30PM +0200, David M. Kaplan wrote:
> The way I have implemented it is by adding an additional class
> BlockingKeyMouseInput, which is quite similar to BlockingMouseInput, but
> waits for both key and mouse events. A smarter person than I could
> probably combine these two classes and make something that would serve
> both functions. But I am basically new to python and don't feel
> comfortable. Perhaps someone else could take a look and make
> improvements/simplifications?

The only significantly different lines are the two lines where an
mplconnect is done to register the callback. You could abstract this in a
method and then have a base class and two sub classes for each call: the
blocking from mouse and the blocking from button.

> The other thing that I have noticed with both ginput and
> waitforbuttonpress is that if you use ctrl-c to break out of either,
> then the callback functions remain attached to their respective events
> (e.g., try ginput(n=-1,timeout=-1,verbose=True) and hit ctrl-c). This
> probably isn't a huge problem, but it would be nice if there was a way
> to say "if ctrl-c is pressed, cleanup nicely". Does someone know if
> that is possible?

I think this is a good usecase for a try: ... finally: ... .

I don't have time to do these changes right now, as I am very busy both
with IPython and Mayavi, and will be travelling next two weeks, but you
can have a good at them, and someone else will probably commit your
patch, if you removed the code duplication.

Cheers,

Gaël

--
**********************************
David M. Kaplan
Assistant Researcher
UCSC / Institute of Marine Sciences
Ocean Sciences
1156 High St.
SC, CA 95064

Phone: 831-459-4789
Fax: 831-459-4882
http://pmc.ucsc.edu/~dmk/
**********************************