Generator functions and user interfaces

Discussion in 'Python' started by Bruno Desthuilliers, Sep 10, 2008.

  1. 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 ?
     
    Bruno Desthuilliers, Sep 10, 2008
    #1
    1. Advertising

  2. Bruno Desthuilliers

    Guest

    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
     
    , Sep 10, 2008
    #2
    1. Advertising

  3. On Sep 10, 10:35 am, Bruno Desthuilliers
    <> wrote:
    > 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 ?


    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.
     
    Aaron \Castironpi\ Brady, Sep 10, 2008
    #3
  4. Bruno Desthuilliers

    Terry Reedy Guest

    wrote:
    > 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
     
    Terry Reedy, Sep 11, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Maurer
    Replies:
    3
    Views:
    4,877
    Peter
    Apr 19, 2006
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,614
    Victor Bazarov
    Jun 21, 2004
  3. =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=

    Interfaces and functions...

    =?ISO-8859-1?Q?Mattias_Br=E4ndstr=F6m?=, Nov 1, 2004, in forum: C++
    Replies:
    4
    Views:
    287
    Jesper Madsen
    Nov 1, 2004
  4. Steven T. Hatton
    Replies:
    7
    Views:
    389
    Kai-Uwe Bux
    Nov 30, 2006
  5. Terry Reedy

    Generator functions subclass generator?

    Terry Reedy, Jun 18, 2009, in forum: Python
    Replies:
    0
    Views:
    471
    Terry Reedy
    Jun 18, 2009
Loading...

Share This Page