Generator functions and user interfaces

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
I'm trying to implement an interactive graph visualisation tool using
matplotlib.

I want to use a spring layout, where nodes repulse each other and
edges act as springs to pull connected nodes together. Usually, this
algorithm runs through a number of iterations of attraction/repulsion
to allow the nodes to converge to suitable positions. However, rather
than running all these iterations to lay out the graph and then
rendering it, I want to animate the graph as it is "springing" into
place, and then allow the user to drag nodes around and have the graph
redraw on the fly.

My idea for doing this was to use a generator function, where I yield
the position of the nodes after each iteration and then call draw() on
the position yielded. Does this seem like a sensible approach?

I don't have much experience with this kind of algorithms, but AFAICT,
it seems sensible to me, yes. But don't take me on words...
The
problem is that the node positions that are being operated on by the
generator function may be altered by user input - dragging the nodes -
and I'm not sure if this will break the way that the new positions are
yielded. How do I use a generator function that might stop (when the
nodes stop moving) but then need to restart again (once the user moves
the nodes)?

Starting with Python 2.5, there's a way to pass values back to generators:
http://docs.python.org/whatsnew/pep-342.html

Now, not having played much with these new features so far, I'm afraid I
can't help more, nor even be strictly positive about this being what you
need.


Any generator guru around ?
 
P

psaffrey

I'm trying to implement an interactive graph visualisation tool using
matplotlib.

I want to use a spring layout, where nodes repulse each other and
edges act as springs to pull connected nodes together. Usually, this
algorithm runs through a number of iterations of attraction/repulsion
to allow the nodes to converge to suitable positions. However, rather
than running all these iterations to lay out the graph and then
rendering it, I want to animate the graph as it is "springing" into
place, and then allow the user to drag nodes around and have the graph
redraw on the fly.

My idea for doing this was to use a generator function, where I yield
the position of the nodes after each iteration and then call draw() on
the position yielded. Does this seem like a sensible approach? The
problem is that the node positions that are being operated on by the
generator function may be altered by user input - dragging the nodes -
and I'm not sure if this will break the way that the new positions are
yielded. How do I use a generator function that might stop (when the
nodes stop moving) but then need to restart again (once the user moves
the nodes)?

I'm quite an experienced Python programmer but I've never taken the
trouble to get my head around generator functions, so any guidance
welcome!

Peter
 
A

Aaron \Castironpi\ Brady

(e-mail address removed) a écrit :




I don't have much experience with this kind of algorithms, but AFAICT,
it seems sensible to me, yes. But don't take me on words...


Starting with Python 2.5, there's a way to pass values back to generators:http://docs.python.org/whatsnew/pep-342.html

Now, not having played much with these new features so far, I'm afraid I
can't help more, nor even be strictly positive about this being what you
need.

Any generator guru around ?

Yield can return values. The syntax you're looking for is:

def generator_fun( ):
a= []
while 1:
b= yield( a )
a.append( b )

g= generator_fun( )
g.next( )
g.send( 3 )
g.send( 4 )
g.send( 5 )

/Output:
g.next( ) []
g.send( 3 ) [3]
g.send( 4 ) [3, 4]
g.send( 5 )
[3, 4, 5]

'g' is inactive in between 'send' calls.
 
T

Terry Reedy

I'm trying to implement an interactive graph visualisation tool using
matplotlib.

I want to use a spring layout, where nodes repulse each other and
edges act as springs to pull connected nodes together. Usually, this
algorithm runs through a number of iterations of attraction/repulsion
to allow the nodes to converge to suitable positions. However, rather
than running all these iterations to lay out the graph and then
rendering it, I want to animate the graph as it is "springing" into
place, and then allow the user to drag nodes around and have the graph
redraw on the fly.

My idea for doing this was to use a generator function, where I yield
the position of the nodes after each iteration and then call draw() on
the position yielded. Does this seem like a sensible approach?

To me, no. The reasons for using one do not apply here. What you
describe is much like a game with auto updates plus user intervention.
I would write an update function and add it to the gui mainloop (if
running: update()) along with the draw function to be tied into the gui
redraw (those parts I know little about). I would allow use of the
spacebar instead of or in addition to a button to toggle 'running' on
and off. That is standard in games that allow users to stop the action.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top