-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleUI.py
295 lines (244 loc) · 8.7 KB
/
SampleUI.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
import Tkinter as tk
from circle import *
from Population import Population
from options import O
from SampleFrame import ClusterFrame,Cluster
class Sample(Population):
"""
the sample class, which represents the data present in a sample
"""
def __init__(self, data, *args, **kwargs):
Population.__init__(self, *args, **kwargs)
self.data = data
self.stats = dict()
def add_stat(self, stat):
pass
class SampleUI(tk.Frame):
sortStat = 'name'
"""this class represens a single sample with use checkbox, label, x and y coords"""
def __init__(self, master=None, config=None, data=None, cluster=None,text="Sample",x=0,y=0, **kwargs):
tk.Frame.__init__(self,master, bd=2, **kwargs)
self.cluster = cluster
self.c = config
self.d = data
self.color = "red"
self.cstr = "red"
#coordinates in integers
self.x = float(x)
self.y = float(y)
self.name = text
self.stats = {'name': self.name}
#coordinates as strings, bound to xbox, ybox
self.tX, self.tY = tk.StringVar(), tk.StringVar()
#is visible?
self.active = tk.IntVar()
#setting up the widgets
self.checkbox = tk.Checkbutton(self,bg="red", command=self.showhide, var=self.active,)
self.checkbox.select()
self.label = tk.Label(self,text=text)
self.xbox = tk.Entry(self,width=4, textvariable = self.tX)
self.tX.set(x)
self.ybox = tk.Entry(self,width=4, textvariable = self.tY)
self.tY.set(y)
#the corresponding plot elements:
# - circH is the circle that is drawn on hyperbola plot
# - circP is the circle that is drawn on PWPsi plot
# - hyperbolas are the hyperbolas associated with this
self.circH = Point(self,(x,y))
self.circP = Point(self,(x,y))
self.circH.sample = self
self.circP.sample = self
#registering events and layout
self.tX.trace("w",lambda a,b,c,n=self.xbox:self.moved(a,n))
self.tY.trace("w",lambda a,b,c,n=self.ybox:self.moved(a,n))
self.checkbox.grid(column=0,row=0)
self.label.grid(column=1,row=0,sticky="nsew")
self.xbox.grid(column=2,row=0)
self.ybox.grid(column=3,row=0)
tk.Grid.columnconfigure(self,1,weight=1)
if cluster != None:
cluster.add_pop(self)
self.cluster_frame = ClusterFrame(parent=self,
data=self.d,
config=self.c)
self.cluster_frame.grid(in_=self,column=4,row=0)
self.hyperbolas = []
self.psi_lines = []
def set_color(self, color):
self.color = color
cint = [min(255,c *256) for c in color]
cstr = '#%02x%02x%02x'%tuple(cint[:3])
self.cstr = cstr
self.config(bg=cstr)
self.checkbox.config(bg=cstr)
self.label.config(bg=cstr)
self.circH.set_color(color)
self.circP.set_color(color)
def set_cluster(self, cluster):
self.cluster = cluster
if hasattr(self, 'cluster_frame'):
self.cluster_frame.set_mincol(cluster.col.mincol)
self.cluster_frame.set_maxcol(cluster.col.maxcol)
def is_active(self):
return self.active.get()
#self[0] and self[1] return x and y
def __getitem__(self,x):
if x == 0:
return self.get_x()
if x == 1:
return self.get_y()
def get_x(self):
return float(self.x)
def get_y(self):
return float(self.y)
def set_x(self,x):
self.x = x
def set_y(self,y):
self.y = y
def get_name(self):
return self.name
#function that is run when the coords of the dot are updated
def moved(self,ele,val):
if Point.lock == self.circH or Point.lock == self.circP:
return
print "MOVE FIRED"
#see if the value is actually a float, if not, return
try:
float(val.get())
except:
print "val", val
return
print ele, val
#if x changed...
if ele == str(self.tX):
x = float(val.get())
self.set_x( x )
self.circH.set_xdata([x])
self.circP.set_xdata([x])
#if y changed
elif ele == str(self.tY):
y = float(val.get())
self.set_y( y )
self.circH.set_ydata([y])
self.circP.set_ydata([y])
else: print "NO"
#update, whole thing, there might be a more efficient way to do this
self.updateHyperbolas()
self.updatePsiLines()
self.circH._update()
self.circP._update()
self.circH.figure.canvas.draw()
self.circP.figure.canvas.draw()
#self.drawCoords()
#self.circ.center = (float(x)
def showhide(self):
"""funcion that shows and hides this plot. It should do the following things:
onhide:
- 1. hide circle
- 2. hide all hyperbolas
onshow:
- 1. show circle again
- 2. show hyperbolas as long as the other focus is also active
#maybe: update estimated origin
"""
#todo
if self.active.get():
print "check, nHyp", len(self.hyperbolas)
self.show()
else:
print "uncheck, nHyp", len(self.hyperbolas)
self.hide()
def hide(self):
canvasH = self.circH.figure.canvas
canvasP = self.circP.figure.canvas
print "CANVVAS", canvasH, canvasP
self.circH.hide()
self.circP.hide()
for h in self.hyperbolas:
h.set_visible(False)
for l in self.psi_lines:
l.set_visible(False)
canvasH.draw()
canvasP.draw()
def show(self):
canvasH = self.circH.figure.canvas
canvasP = self.circP.figure.canvas
self.circH.show()
self.circP.show()
for h in self.hyperbolas:
h.show()
for l in self.psi_lines:
l.show()
canvasH.draw()
canvasP.draw()
def updateHyperbolas(self):
for h in self.hyperbolas:
h.update_()
def updatePsiLines(self):
for l in self.psi_lines:
l.update_()
def updateCircles(self):
self.circH._update()
self.circP._update()
def update_(self):
self.updateHyperbolas()
self.updatePsiLines()
self.circH._update()
self.circP._update()
self.circH.figure.canvas.draw()
self.circP.figure.canvas.draw()
def add_line(self,l):
self.psi_lines.append(l)
self.circP.add_line(l)
def add_hyperbola(self,h):
self.hyperbolas.append(h)
self.circH.add_line(h)
####################################################
# sorting stuff and 1 sample statistics
###################################################
def __lt__(self, other):
if self.cluster == other.cluster:
return self.d.get_default_sort_stat(self.pop) < \
self.d.get_default_sort_stat(other.pop)
else:
return self.cluster < other.cluster
class SampleUIWPop(SampleUI):
"""
simple subclass of SampleUI where x,y, name are replaced by
a Population object that can also be used for the class
"""
def __init__(self, pop, master=None, data=None, config=None, cluster=None):
x,y = pop.location[:2] # just use first two locs
text = pop.name
self.pop = pop
SampleUI.__init__(self,master, data=data, config=config,
cluster=cluster, text=text, x=x, y=y)
def get_x(self):
return float(self.pop.location[0])
def get_y(self):
return float(self.pop.location[1])
def set_x(self,x):
self.pop.location[0] = x
def set_y(self,y):
self.pop.location[1] = y
def get_name(self):
return self.pop.name
def destroy(self):
tk.Frame.destroy(self)
def __hash__(self):
return self.pop.__hash__()
def __eq__(self,other):
return self.pop.__eq__(other)
class InferredOrigin(SampleUI):
origin_counter = 0
def __init__(self, master=None, main=None, cluster=None, text="Origin", x=0,y=0, color="red", **kwargs):
SampleUI.__init__(self,master, main=main, cluster=cluster, text=text, x=x, y=y, **kwargs)
self.circH.set_color("red")
self.circP.set_color("red")
self.circH.set_marker("+")
self.circP.set_marker("+")
self.circH.set_markeredgecolor("red")
self.circP.set_markeredgecolor("red")
"""
class that represents an inferred origin of a group
"""