0

I'm running a sequence of functions (that I defined myself) in a script:

generate(file)
cut(file)
norm(file)

All these functions are manipulating the data from file. For instance:

import numpy as np
import matplotlib.pyplot as plt


def generate(file):
    data=np.genfromtxt(file+'.txt')
    np.save(file+'_coord',data)
    print len(data)


class cutGUI(object):
    def __init__(self,ax,data,file):
        self.file=file
        self.data=data
        self.ax = ax
        self.x0 = 0
        self.y0 = 0
        self.r = 0
        self.circ=plt.Circle((self.x0,self.y0),radius=self.r,color='red',
                         fill=False)
        self.ax.add_patch(self.circ)
        self.ax.figure.canvas.mpl_connect('button_press_event',self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event',self.on_release)

    def on_press(self,event):
        if event.button==1:
            self.x0 = event.xdata
            self.y0 = event.ydata

    def on_release(self,event):
        if event.button==1:
            self.r = np.sqrt((self.x0-event.xdata)**2+(self.y0-event.ydata)**2)
            self.circ.center=self.x0,self.y0
            self.circ.set_radius(self.r)
            self.ax.figure.canvas.draw()
        elif event.button==3:
            indizes=np.where((self.data[::1,0]-self.x0)**2+(self.data[::1,1]
                          -self.y0)**2>self.r**2)
            self.data=np.delete(self.data,indizes,axis=0)
            np.save(self.file+'_coord',self.data)
            plt.close('all')


def cut(file):
    data=np.load(file+'_coord.npy')
    x,y,z=data[::1,0],data[::1,1],data[::1,2]
    fig=plt.figure(figsize=(10,10))
    ax=fig.add_subplot(111,aspect='equal')
    ax.scatter(x,y,s=0.5,c=z,cmap='gray',marker='.')
    GUI=cutGUI(ax,data,file)
    plt.show()

The point is that the cut function opens a GUI in which the data in File is manipulated graphically by hand. So the script must wait for the user to finish by pressing the right mouse button. Currently, the script is not waiting. How can I implement this?

phlegmax
  • 419
  • 1
  • 4
  • 11
  • 3
    Show us the code of `Generate`. Pro tip: function names don't start with capital letters in Python. –  Apr 10 '15 at 10:14
  • can the time be set beforehand or does it alter? If it takes a few seconds you can use time.sleep(seconds here) in your code. If not, it sounds like a while-loop stuff. – jester112358 Apr 10 '15 at 10:17
  • 4
    Can we see the code of `Cut`, as that's the one that's not waiting... (another pro-tip, variables don't start with capital letters either ;-)) – Ben Apr 10 '15 at 10:19
  • Well, I would like to allow the user to take his time when manipulating the data graphically. So setting a fixed time for sleep is not so convenient... – phlegmax Apr 10 '15 at 10:21
  • OK, we need a volunteer to easily describe coroutines :-) – Kos Apr 10 '15 at 10:23
  • So, are these operations done in different processes? – pnv Apr 10 '15 at 10:24
  • Can we have a look at source code? – pnv Apr 10 '15 at 10:25
  • I don't think that these are done in different processes. The three functions are simply executed sequentially by the script. – phlegmax Apr 10 '15 at 10:37
  • What is `plt.show()`? Does it return anything? –  Apr 10 '15 at 10:39
  • Oh, plt is just pyplot. Sorry. See the edited code. (I thought this was a standard abbreviation.) – phlegmax Apr 10 '15 at 10:49
  • May be this, http://stackoverflow.com/questions/489861/locking-a-file-in-python – pnv Apr 10 '15 at 11:40
  • 1
    Indeed, http://stackoverflow.com/questions/9280171/matplotlib-python-show-returns-immediately solved my problem. I simply had to add the line plt.ioff() before plt.show(). Thank you a lot! – phlegmax Apr 10 '15 at 12:06

0 Answers0