profiler.py 7.38 KB
Newer Older
1 2 3
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
4
from concentracao import *
5
import sys
6
import os
7 8

listay = list()
9
listax = list()
10 11 12
drs = []
fig = plt.figure()
ax = fig.add_subplot(111)
13 14 15 16
filename2 = sys.argv[1]+".prof" #raw_input()
filename = sys.argv[1]+".dat" #raw_input()
print "nome dos arquivos "+filename+" e "+ filename2
fig.canvas.set_window_title(filename) 
17
dxstep = 0.01
18
profundidademax = 20.
19 20
xx = arange(0,20, dxstep)         				# we'll create an x-axis from 0 to 2 pi
temp, = plot(xx,xx*0)               			# this is our initial plot, and does nothing     
21 22 23
modo = 'step' 
#modo = 'reta'
print os.getcwdu()+'/samples/'+filename
24

25 26 27
#######################################################################################################
# Add square

28
def add_square(x, y):
29
    global listay, drs, ax
30
    rect = ax.bar(x, 0.02, width=0.3) 
31 32 33 34
    for rec in rect:
        dr = DraggableRectangle(rec)
        dr.connect()
        drs.append(dr)
35
        rec.set_y(y)#-0.01)
36 37 38 39
        listay.append(dr.rect.xy[1])
        listax.append(dr.rect.xy[0])
        dr.updateplot()
    ylim([0,1])
40
    xlim([0,profundidademax])
41 42 43 44 45 46 47

def add_immovable_square(x, y):
    global listay, drs, ax
    rect = ax.bar(x, 0.02, width=0.3) 
    for rec in rect:
        dr = DraggableRectangle(rec)
        drs.append(dr)
48
        rec.set_y(y)#-0.01)
49
        listay.append(dr.rect.xy[1])
50
        listax.append(dr.rect.xy[0])
51
    ylim([0,1])
52
    xlim([0,profundidademax])
53 54 55 56 57 58 59

def remove_square():
    global listay, drs
    listay.pop()
    drs.pop()
    print listay, drs

60 61 62 63
def clear_figure():
    global fig
    fig.clf()

64 65
#######################################################################################################

66
class voidfunctions:
67 68 69 70 71 72 73 74 75
    def __init__(self, rect):
        self.rect = rect

    def connect(self):
        'connect to all the events we need'
        self.cida = self.rect.figure.canvas.mpl_connect(
            'key_release_event', self.on_a)
        self.cidr = self.rect.figure.canvas.mpl_connect(
            'key_release_event', self.on_r)
76 77
        self.cidc = self.rect.figure.canvas.mpl_connect(
            'key_release_event', self.on_c)
78 79 80 81

    def on_a(self, event):
        if event.key=='a':
            if not event.inaxes: return
82
            add_square(float(event.xdata), float(event.ydata))
83

84
    # NAO FUNCIONA
85 86 87 88 89
    def on_r(self, event):
        if event.key=='r':
            if not event.inaxes: return
            remove_square()

90 91 92 93 94
    def on_c(self, event):
        if event.key=='c':
            if not event.inaxes: return
            clear_figure()

95 96 97 98
    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cida)
        self.rect.figure.canvas.mpl_disconnect(self.cidr)
99
        self.rect.figure.canvas.mpl_disconnect(self.cidc)
100

101
#######################################################################################################
102 103 104 105 106 107 108

class DraggableRectangle:
    lock = None  # only one can be animated at a time
    def __init__(self, rect):
        self.rect = rect
        self.press = None
        self.background = None
109 110

    def updateplot(self):
111
        global listax, listay, temp, drs, dxstep, modo
112 113 114 115 116 117 118 119 120 121 122 123
        xytemp = list(drs)
        for i in range(len(drs)):
            xytemp[i] = drs[i].rect.xy          
        xytemp.sort(key=lambda s: s[0])

        for i in range(len(listay)):
            (listax[i], listay[i]) = xytemp[i]

        data = open(filename , 'w')
        for i in range(len(listay)):
            print>>data, listax[i], listay[i]
        data.close()
124 125 126

        #path = os.getcwdu()+'/samples/'+filename #os.path.abspath('')+'/samples/'+filename
        #path.decode("utf-8") 
127 128
        xydata = np.loadtxt(filename)

129 130
        xy = montaperfil( xydata, dxstep, modo)

131 132
        #path2 = os.getcwdu()+'/samples/'+filename2 #os.path.abspath('')+'/samples/'+filename2
        #path2.decode("utf-8") 
133 134 135 136 137
        data2 = open(filename2 , 'w')
        for i in range(len(xy)):
            print>>data2, xy[i]
        data2.close()

138 139 140
        temp.set_ydata(montaperfil( xydata, dxstep, modo))           # update the plot data           
        draw()     						# redraw the canvas
        ylim([0,1])
141
        xlim([0,profundidademax])  
142 143 144 145 146 147 148 149 150 151

    def connect(self):
        'connect to all the events we need'
        self.cidpress = self.rect.figure.canvas.mpl_connect(
            'button_press_event', self.on_press)
        self.cidrelease = self.rect.figure.canvas.mpl_connect(
            'button_release_event', self.on_release)
        self.cidmotion = self.rect.figure.canvas.mpl_connect(
            'motion_notify_event', self.on_motion)

152 153 154 155 156 157
    def on_press(self, event):
        'on button press we will see if the mouse is over us and store some data'
        if event.inaxes != self.rect.axes: return 
        if DraggableRectangle.lock is not None: return
        contains, attrd = self.rect.contains(event)
        if not contains: return
158
         
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        #print 'event contains', self.rect.xy
        x0, y0 = self.rect.xy
        self.press = x0, y0, event.xdata, event.ydata
        DraggableRectangle.lock = self

        # draw everything but the selected rectangle and store the pixel buffer
        canvas = self.rect.figure.canvas
        axes = self.rect.axes
        self.rect.set_animated(True)
        canvas.draw()
        self.background = canvas.copy_from_bbox(self.rect.axes.bbox)

        # now redraw just the rectangle
        axes.draw_artist(self.rect)
        # and blit just the redrawn area
        canvas.blit(axes.bbox)

    def on_motion(self, event):
        'on motion we will move the rect if the mouse is over us'
        if DraggableRectangle.lock is not self: return
        if event.inaxes != self.rect.axes: return
        x0, y0, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.rect.set_x(x0+dx)
        self.rect.set_y(y0+dy)

        canvas = self.rect.figure.canvas
        axes = self.rect.axes
        # restore the background region
        canvas.restore_region(self.background)

        # redraw just the current rectangle
        axes.draw_artist(self.rect)
        # blit just the redrawn area
        canvas.blit(axes.bbox)

196 197
        #self.updateplot()

198 199
    def on_release(self, event):
        'on release we reset the press data'
200
        if DraggableRectangle.lock is not self: return
201 202 203 204 205 206 207 208 209 210

        self.press = None
        DraggableRectangle.lock = None

        # turn off the rect animation property and reset the background
        self.rect.set_animated(False)
        self.background = None

        # redraw the full figure
        self.rect.figure.canvas.draw()
211
        self.updateplot()               				
212 213 214 215 216 217 218

    def disconnect(self):
        'disconnect all the stored connection ids'
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

219 220
#######################################################################################################

221 222 223 224
voidrect = ax.bar(0, 0, width=0)
for void in voidrect:
    vf = voidfunctions(void)
    vf.connect()
225 226 227
add_immovable_square(0,0)
add_immovable_square(20,0)

228 229 230 231 232 233 234 235
try:
    f = np.loadtxt(filename)
except IOError: 
    pass
else:
    for initial in range( len(f) ):
        add_square(f[initial][0],f[initial][1])

236
#######################################################################################################
237 238 239 240 241

plt.show()