profiler.py 9.75 KB
Newer Older
1
2
3
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
4
import sys
5
6

listay = list()
7
listax = list()
8
drs = []
9
RectControl = 0
10
11
fig = plt.figure()
ax = fig.add_subplot(111)
12
13
filename2 = sys.argv[1]+".prof"
filename = sys.argv[1]+".dat" 
14
modo = sys.argv[2] 
15
16
profundidademax = float(sys.argv[3])
dxstep = float(sys.argv[4])
17
18
print "nome dos arquivos "+filename+" e "+ filename2
fig.canvas.set_window_title(filename) 
Matheus Müller's avatar
Matheus Müller committed
19
xx = arange(0, profundidademax, dxstep)       
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
20
temp, = plot(xx,xx*0)               			      
21

22
#######################################################################################################
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#######################################################################################################
# Funcoes que corrigem erros de arredondamento

def fix1(y, x):
    for k in arange(1, (len(y)-1)):
         if y[k] == 0:
            if y[k-1] and y[k+1] != 0:
                    y[k] = y[k-1]

def fix2(y, x):
    for k in arange(1, (len(y)-1)):
         if y[k] == 0:
            if y[k-1] and y[k+1] != 0:
                if y[k-1] == y[k+1]:
                    y[k] = y[k-1]
                else:
                    y[k] = (y[k+1]+y[k-1])/2

#####################################################################################################
# Funcao que retorna o perfil por pontos

def montaperfil( xy , step, modo):
    u = 0.
    yu = 0.

    profundidade = arange(0, max(xy[:,0]), step)
    concent = profundidade*0

    if modo == 'step':
        for i in xy:
            for j in arange (u, i[0], step):
                if j>max(xy[:,0])/2 and j<=len(xy) :
                    concent[int(j/step)] = i[1]
                else :
                    concent[math.floor(j/step)] = i[1]
            u = i[0]
        fix1(concent, profundidade)

    if modo == 'reta':
        for i in xy:
            for j in arange (u, round(i[0],3), step):
                concent[round(j/step)] = yu + (j-u)*(i[1] - yu)/(i[0] - u)
            u = round(i[0],2) 
            yu = i[1]   
        fix2(concent, profundidade)    

    return concent

#######################################################################################################
#######################################################################################################
# Add/Remove square
74

75
def add_square(x, y):
76
    global listay, listax, drs, ax, RectControl
77
    rect = ax.bar(x, 0.02, width=profundidademax*0.3/20) 
78
79
80
81
    for rec in rect:
        dr = DraggableRectangle(rec)
        dr.connect()
        drs.append(dr)
82
        rec.set_y(y)
83
84
85
86
        listay.append(dr.rect.xy[1])
        listax.append(dr.rect.xy[0])
        dr.updateplot()
    ylim([0,1])
87
    xlim([0,profundidademax])
88
89

def add_immovable_square(x, y):
90
91
    global listay, listax, drs, ax
    rect = ax.bar(x, 0.001, width=0.01, color='white') 
92
93
94
    for rec in rect:
        dr = DraggableRectangle(rec)
        drs.append(dr)
95
        rec.set_y(y)
96
        listay.append(dr.rect.xy[1])
97
        listax.append(dr.rect.xy[0])
98
    ylim([0,1])
99
    xlim([0,profundidademax])
100
101

def remove_square():
102
    global listay, listax, drs
103
    listay.pop()
104
105
    listax.pop()
    drs[len(drs)-1].on_removed()
106
    drs.pop()
107
    drs[0].updateplot()
108

109
#######################################################################################################
110
#######################################################################################################
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
111
#######################################################################################################
112

113
class voidfunctions:
114
115
116
117
118
119
120
121
122
123
124
125
    def __init__(self, rect):
        self.rect = rect

    def connect(self):
        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)

    def on_a(self, event):
        if event.key=='a':
            if not event.inaxes: return
126
            add_square(float(event.xdata), float(event.ydata))
127
128
129
130
131
132

    def on_r(self, event):
        if event.key=='r':
            if not event.inaxes: return
            remove_square()

133
134
135
136
    def disconnect(self):
        self.rect.figure.canvas.mpl_disconnect(self.cida)
        self.rect.figure.canvas.mpl_disconnect(self.cidr)

137
#######################################################################################################
138
#######################################################################################################
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
139
#######################################################################################################
140
141
142
143
144
145
146

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
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
    def on_e(self, event):
        global listay, listax, drs
        if event.key=='e':
            if not event.inaxes: return
            if event.inaxes != self.rect.axes: return 
            if DraggableRectangle.lock is not None: return
            contains, attrd = self.rect.contains(event)
            if not contains: return
            listax.remove(self.rect.xy[0])
            listay.remove(self.rect.xy[1])
            drs.remove(self)
            self.on_removed()
            drs[0].updateplot()

162
    def updateplot(self):
163
        global listax, listay, temp, drs, dxstep, modo
164
        xytemp = list(drs)
165

166
167
168
169
170
171
172
173
174
175
        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()
        xydata = np.loadtxt(filename)
176
177
        xy = montaperfil( xydata, dxstep, modo)
        data2 = open(filename2 , 'w')
178

179
180
181
182
        for i in range(len(xy)):
            print>>data2, xy[i]
        data2.close()

183
        temp.set_ydata(montaperfil( xydata, dxstep, modo))           # update the plot data           
184
185
        draw()     						     # redraw the canvas
        ylim([0,1.01])
186
        xlim([0,profundidademax])  
187
188

    def connect(self):
189
190
191
192
        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)
        self.cide = self.rect.figure.canvas.mpl_connect('key_release_event', self.on_e)
193

194
195
196
197
198
199
    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
200
         
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
        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)
233

234
235
    def on_release(self, event):
        'on release we reset the press data'
236
        if DraggableRectangle.lock is not self: return
237
238
239
240
241
242
243
        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()
244
245
246
247
248
249
250
251
        self.updateplot()  

    def on_removed(self): 
        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)            				
252
253
254
255
256

    def disconnect(self):
        self.rect.figure.canvas.mpl_disconnect(self.cidpress)
        self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
        self.rect.figure.canvas.mpl_disconnect(self.cidmotion)
257
        self.rect.figure.canvas.mpl_disconnect(self.cide)
258

259
#######################################################################################################
260
#######################################################################################################
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
261
#######################################################################################################
262

263
264
265
266
voidrect = ax.bar(0, 0, width=0)
for void in voidrect:
    vf = voidfunctions(void)
    vf.connect()
267
add_immovable_square(0,0)
268
add_immovable_square(profundidademax,0)
269

270
271
272
273
274
275
try:
    f = np.loadtxt(filename)
except IOError: 
    pass
else:
    for initial in range( len(f) ):
276
        if f[initial][0] < profundidademax and f[initial][0] > 0:
277
            add_square(f[initial][0],f[initial][1])
278

279
#######################################################################################################
280
281
282

plt.show()

283
#######################################################################################################