-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicker.py
328 lines (274 loc) · 9.89 KB
/
picker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
196
197
198
199
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
"""
You can enable picking by setting the "picker" property of an artist
(for example, a matplotlib Line2D, Text, Patch, Polygon, AxesImage,
etc...)
There are a variety of meanings of the picker property
None - picking is disabled for this artist (default)
boolean - if True then picking will be enabled and the
artist will fire a pick event if the mouse event is over
the artist
float - if picker is a number it is interpreted as an
epsilon tolerance in points and the artist will fire
off an event if it's data is within epsilon of the mouse
event. For some artists like lines and patch collections,
the artist may provide additional data to the pick event
that is generated, for example, the indices of the data within
epsilon of the pick event
function - if picker is callable, it is a user supplied
function which determines whether the artist is hit by the
mouse event.
hit, props = picker(artist, mouseevent)
to determine the hit test. If the mouse event is over the
artist, return hit=True and props is a dictionary of properties
you want added to the PickEvent attributes
After you have enabled an artist for picking by setting the "picker"
property, you need to connect to the figure canvas pick_event to get
pick callbacks on mouse press events. For example,
def pick_handler(event):
mouseevent = event.mouseevent
artist = event.artist
# now do something with this...
The pick event (matplotlib.backend_bases.PickEvent) which is passed to
your callback is always fired with two attributes:
mouseevent - the mouse event that generate the pick event. The
mouse event in turn has attributes like x and y (the coordinates in
display space, such as pixels from left, bottom) and xdata, ydata (the
coords in data space). Additionally, you can get information about
which buttons were pressed, which keys were pressed, which Axes
the mouse is over, etc. See matplotlib.backend_bases.MouseEvent
for details.
artist - the matplotlib.artist that generated the pick event.
Additionally, certain artists like Line2D and PatchCollection may
attach additional meta data like the indices into the data that meet
the picker criteria (for example, all the points in the line that are within
the specified epsilon tolerance)
The examples below illustrate each of these methods.
"""
from matplotlib.pyplot import figure, show,text,draw,plot,cla,gca,get_current_fig_manager,axis
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle,Circle
from matplotlib.text import Text
from matplotlib.image import AxesImage
import numpy as np
from numpy.random import rand
import string
prefix="D:/diagonalization/et_sa_abstracts/"
prefix="D:/diagonalization/whatif/"
prefix="D:/diagonalization/fact_aesop/"
prefix="D:/diagonalization/ideal_toy/"
selected_inds=set([])
#fig = figure()
#ax1 = fig.add_subplot(211)
#ax1.set_title('click on points, rectangles or text', picker=True)
#ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red'))
#line, = ax1.plot(rand(438341), 'o', picker=5) # 5 points tolerance
# pick the rectangle
#ax2 = fig.add_subplot(212)
#bars = ax2.bar(range(10), rand(10), picker=True)
#for label in ax2.get_xticklabels(): # make the xtick labels pickable
# label.set_picker(True)
xs=[]
ys=[]
ex_classes=[]
words=[]
colors=[]
documents=[]
doc_outliers=[]
domains=[]
bterms=[]
crossbee=[]
fig = figure(figsize=(6*3.13,4*3.13))
#ax2 = fig.add_subplot(111)
ax1 = fig.add_subplot(111)#ax2.twinx()#fig.add_subplot(111)
#ax3 = ax2.twinx()#fig.add_subplot(111)
axis([0.0,0,800,800])
ax1 =gca()
ax1.set_autoscale_on(False)
ex_properties=""
def redraw(hide=False):
global fig,ax1,xs,ys,colors,selected_inds,ex_properties,crossbee
#ax2.cla()
ax1.cla();
#ax3.cla()
max_x=max(xs)
max_y=max(ys)
print len(bterms)
# for j in bterms:
# p = Rectangle([j+2,0], 3,max_y+3, facecolor="green", edgecolor="green")
# ax1.add_patch(p)
for j,alpha in crossbee:
p = Rectangle([j+2,0], 3,max_y+3, facecolor="yellow", edgecolor="yellow",alpha=alpha)
ax1.add_patch(p);
for i in doc_outliers:
p = Rectangle([0,i-5], max_x+3, 3, facecolor="orange", edgecolor="orange",alpha=0.8)
ax1.add_patch(p);
for i,x in enumerate(xs):
if i%1==0:
p = Circle([x,ys[i]],3, color=colors[i] )
ax1.add_patch(p)
for i in selected_inds:
p = Circle([xs[i],ys[i]],5, color="black" )
ax1.add_patch(p)
p = Circle([xs[i],ys[i]],3, color=colors[i] )
ax1.add_patch(p);
col = ax1.scatter([xs[i] for i in selected_inds]+[-30],[ys[i] for i in selected_inds]+[-30],s=70,c='black', picker=1)
#col2 = ax1.scatter(xs,ys,c=colors, picker=1)
if not hide:
print "Risem vse ..."
#print ax1.points
#fig.savefig('pscoll.eps')
print "redrawing done."
#draw()
#fig.canvas.clear()
bbox_props = dict(boxstyle="round", fc="w", ec="0.5", alpha=0.9)
aa=ax1.text(-100, -100, ex_properties, size=12, bbox=bbox_props)
aa.set_family("monospace")
fig.canvas.draw();
if not hide:
fig.canvas.mpl_connect('button_press_event', onpick3);
print ex_properties
# plot()
#show()
print "new"
points=[]#np.array([])
#fig.canvas.mpl_connect('pick_event', onpick1)
mig_mag_file=open(prefix+"redpin/points.txt", 'r')
#hevristic_scores="all/HevristicsScores.txt"
#every=5
line = mig_mag_file.readline() # Invokes readline() method on file
while line:
if line!="\n":
spl=line.split("\n")[0].split("\t")
#print spl
if spl[0]=="P":
#print "aaa"
_,x,y,word,greens_per_word,blues_per_word,domain,document,ex_class=spl
xs.append(float(x))
ys.append(float(y))
words.append(word)
ex_classes.append(ex_class)
documents.append(document)
colors.append('b' if ex_class=="1" else 'r')
domains.append(domain+" |%2s %2s|" % (greens_per_word,blues_per_word))
elif spl[0]=="CS":
_,j,color=spl
crossbee.append([int(j),float(color)])
elif spl[0]=="DOCOUT":
_,i=spl
doc_outliers.append(int(i))
else: #BTERM
print spl
_,j,b_term=spl
bterms.append(int(j))
#print i,spl[0]
# count+=1
#i+=1
line = mig_mag_file.readline()
#print "row_perm:",row_perm_rev
mig_mag_file.close()
max_y=max(ys)
ys=[max_y-y for y in ys]
#points=np.array(points)
#x, y = rand(2, 300)
def onpick3(event):
global ex_properties
print event.xdata,event.ydata
inds = [i for i,x in enumerate(xs) if event.xdata+3>x > event.xdata-3 and event.ydata + 3 > ys[i] > event.ydata-3]#set(event.ind)
print inds
for ind in inds:
if ind in selected_inds:
selected_inds.remove(ind)
print "removed:",ind
else:
selected_inds.add(ind)
print "added:",ind
ex_properties=string.join([string.join([domains[i],words[i]]," ") for i in selected_inds],"\n")
#selected_inds.extend(inds)
#for ind in selected_inds:
# print ind, words[ind], documents[ind], domains[ind]
#cla()
#global fig
#fig.canvas.draw()
redraw()
return True
#
#fig = figure()
#ax1 = fig.add_subplot(111)
redraw();
show()
#
#if 1: # picking with a custom hit test function
# # you can define custom pickers by setting picker to a callable
# # function. The function has the signature
# #
# # hit, props = func(artist, mouseevent)
# #
# # to determine the hit test. if the mouse event is over the artist,
# # return hit=True and props is a dictionary of
# # properties you want added to the PickEvent attributes
#
# def line_picker(line, mouseevent):
# """
# find the points within a certain distance from the mouseclick in
# data coords and attach some extra attributes, pickx and picky
# which are the data points that were picked
# """
# if mouseevent.xdata is None: return False, dict()
# xdata = line.get_xdata()
# ydata = line.get_ydata()
# maxd = 0.05
# d = np.sqrt((xdata-mouseevent.xdata)**2. + (ydata-mouseevent.ydata)**2.)
#
# ind = np.nonzero(np.less_equal(d, maxd))
# if len(ind):
# pickx = np.take(xdata, ind)
# picky = np.take(ydata, ind)
# props = dict(ind=ind, pickx=pickx, picky=picky)
# return True, props
# else:
# return False, dict()
#
# def onpick2(event):
# print('onpick2 line:', event.pickx, event.picky)
#
# fig = figure()
# ax1 = fig.add_subplot(111)
# ax1.set_title('custom picker for line data')
# line, = ax1.plot(rand(100), rand(100), 'o', picker=line_picker)
# fig.canvas.mpl_connect('pick_event', onpick2)
#
#
#if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)
#
# x, y, c, s = rand(4, 100)
# def onpick3(event):
# ind = event.ind
# print('onpick3 scatter:', ind, np.take(x, ind), np.take(y, ind))
#
# fig = figure()
# ax1 = fig.add_subplot(111)
# col = ax1.scatter(x, y, 100*s, c, picker=True)
# #fig.savefig('pscoll.eps')
# fig.canvas.mpl_connect('pick_event', onpick3)
#
#if 1: # picking images (matplotlib.image.AxesImage)
# fig = figure()
# ax1 = fig.add_subplot(111)
# im1 = ax1.imshow(rand(10,5), extent=(1,2,1,2), picker=True)
# im2 = ax1.imshow(rand(5,10), extent=(3,4,1,2), picker=True)
# im3 = ax1.imshow(rand(20,25), extent=(1,2,3,4), picker=True)
# im4 = ax1.imshow(rand(30,12), extent=(3,4,3,4), picker=True)
# ax1.axis([0,5,0,5])
#
# def onpick4(event):
# artist = event.artist
# if isinstance(artist, AxesImage):
# im = artist
# A = im.get_array()
# print('onpick4 image', A.shape)
#
# fig.canvas.mpl_connect('pick_event', onpick4)
#
#
#show()