/[ascend]/trunk/pygtk/diagnose.py
ViewVC logotype

Annotation of /trunk/pygtk/diagnose.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 437 - (hide annotations) (download) (as text)
Tue Apr 4 11:08:15 2006 UTC (13 years, 4 months ago) by johnpye
Original Path: trunk/pygtk/interface/diagnose.py
File MIME type: text/x-python
File size: 10668 byte(s)
Missed a GLADE_FILE
1 johnpye 280 import gtk
2     import gtk.glade
3     import ascend
4     from itertools import groupby
5     from operator import itemgetter
6 johnpye 283 import math
7 johnpye 284 import re
8 johnpye 280
9 johnpye 351 import config
10    
11 johnpye 284 ZOOM_RE = re.compile(r"([0-9]+)\s*%?")
12     MAX_ZOOM_SIZE = 2000
13     MAX_ZOOM_RATIO = 16
14 johnpye 287 AT_BOUND_TOL = 0.0001;
15 johnpye 284
16 johnpye 280 class DiagnoseWindow:
17 johnpye 351 def __init__(self,browser,block=0):
18 johnpye 280 self.browser=browser
19 johnpye 437 _xml = gtk.glade.XML(browser.glade_file,"diagnosewin")
20 johnpye 280 _xml.signal_autoconnect(self)
21    
22     self.window = _xml.get_widget("diagnosewin")
23 johnpye 328 self.window.set_transient_for(self.browser.window)
24    
25 johnpye 284 self.imagescroll = _xml.get_widget("imagescroll")
26 johnpye 282 self.image = _xml.get_widget("image")
27 johnpye 280 self.blockentry = _xml.get_widget("blockentry")
28 johnpye 284 self.zoomentry = _xml.get_widget("zoomentry")
29 johnpye 280
30 johnpye 286 self.varname = _xml.get_widget("varname");
31     self.varval = _xml.get_widget("varval");
32     self.relname = _xml.get_widget("relname");
33     self.relresid = _xml.get_widget("relresid");
34    
35 johnpye 280 self.varview = _xml.get_widget("varview")
36     self.varbuf = gtk.TextBuffer()
37     self.varview.set_buffer(self.varbuf)
38     self.varcollapsed = _xml.get_widget("varcollapsed")
39     self.relview = _xml.get_widget("relview")
40     self.relcollapsed = _xml.get_widget("relcollapsed")
41     self.relvalues = _xml.get_widget("relvalues")
42     self.rellabels = _xml.get_widget("rellabels")
43     self.relrels = _xml.get_widget("relrels")
44     self.relresids = _xml.get_widget("relresids")
45     self.relbuf = gtk.TextBuffer()
46     self.relview.set_buffer(self.relbuf)
47    
48 johnpye 294 self.im = None
49 johnpye 285 self.block = 0
50 johnpye 294 self.apply_prefs()
51    
52 johnpye 280 self.prepare_data()
53 johnpye 285 self.fill_values(block) # block zero
54 johnpye 280
55     def run(self):
56     self.window.run()
57     self.window.hide()
58    
59 johnpye 294 def apply_prefs(self):
60 johnpye 319 vc = self.browser.prefs.getBoolPref("Diagnose","varcollapsed",True)
61    
62 johnpye 294 print "VARCOLLAPSED =",vc
63     self.varcollapsed.set_active(vc)
64 johnpye 319 self.relcollapsed.set_active(self.browser.prefs.getBoolPref("Diagnose","relcollapsed",True))
65 johnpye 294
66 johnpye 280 def prepare_data(self):
67     # convert incidence map to pylab numarray type:
68     print "PREPARING DATA"
69     self.im = self.browser.sim.getIncidenceMatrix()
70     self.data = self.im.getIncidenceData()
71     print "DATA LOADED"
72    
73 johnpye 284 self.zoom=1;
74 johnpye 280
75     def fill_values(self, block):
76 johnpye 319
77 johnpye 283 try:
78     rl,cl,rh,ch = self.im.getBlockLocation(block)
79     except IndexError:
80 johnpye 319 if block >= self.im.getNumBlocks():
81     block = self.im.getNumBlocks() - 1
82     rl,cl,rh,ch = self.im.getBlockLocation(block)
83     else:
84     print "BLOCK INDEX ERROR: block =",block
85     self.blockentry.set_text(str(self.block))
86     return
87     except RuntimeError,e:
88     print "ERROR GETTING BLOCK LOCATION:",str(e)
89 johnpye 285 self.blockentry.set_text(str(self.block))
90 johnpye 283 return
91 johnpye 285
92 johnpye 280 self.block = block
93     self.blockentry.set_text(str(block))
94 johnpye 283
95 johnpye 286 self.rl = rl
96     self.cl = cl
97     self.rh = rh
98     self.ch = ch
99    
100 johnpye 283 nr = int(rh-rl+1);
101     nc = int(ch-cl+1);
102    
103 johnpye 284 #print "STARTING IMAGE CREATION"
104 johnpye 282 # refer http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html
105     c = chr(255)
106 johnpye 283 b = nr*nc*3*[c]
107     rowstride = 3 * nc
108    
109     blackdot = [chr(0)]*3;
110     reddot = [chr(255), chr(0), chr(0)]
111     pinkdot = [chr(255), chr(127), chr(127)]
112     skydot = [chr(127), chr(127), chr(255)]
113     bluedot = [chr(0), chr(0), chr(255)]
114 johnpye 285 hotpinkdot = [chr(255), chr(47), chr(179)] # very big (+/-)
115     brightbluedot = [chr(71), chr(157), chr(255)] # very small (+/-)
116     greendot = [chr(87), chr(193), chr(70)] # close to 1
117     orangedot = [chr(255), chr(207), chr(61)] # 10-1000
118     bluegreendot = [chr(70), chr(221), chr(181)] # 0.001 - 0.1
119 johnpye 280 for i in self.data:
120 johnpye 283 if i.row < rl or i.row > rh or i.col < cl or i.col > ch:
121     continue
122     r = i.row - rl;
123     c = i.col - cl;
124     pos = rowstride*r + 3*c
125     dot = blackdot;
126     var = self.im.getVariable(i.col);
127 johnpye 287 if abs( (var.getValue()-var.getUpperBound())/ var.getNominal() ) < AT_BOUND_TOL:
128     dot = reddot
129     elif abs( var.getValue() - var.getLowerBound() ) / var.getNominal() < AT_BOUND_TOL:
130     dot = reddot
131     else:
132     rat = var.getValue() / var.getNominal()
133     if rat!=0:
134     try:
135     val = abs(rat)
136     if abs(rat) > 1000:
137     dot = hotpinkdot
138     elif abs(rat) > 10:
139     dot = orangedot
140     elif abs(rat) < 0.001:
141     dot = brightbluedot
142     elif abs(rat) < 10 and abs(rat) > 0.1:
143     dot = greendot
144     elif abs(rat) > 0.001 and abs(rat) < 0.1:
145     dot = bluegreendot
146     else:
147     dot = blackdot
148     except ValueError, e:
149     pass
150 johnpye 284 #print "DOT: ",dot
151 johnpye 283 b[pos], b[pos+1], b[pos+2] = dot
152    
153 johnpye 282 d = ''.join(b)
154 johnpye 283
155 johnpye 284 #print "DONE IMAGE CREATION"
156 johnpye 282
157 johnpye 284 self.pixbuf = gtk.gdk.pixbuf_new_from_data(d, gtk.gdk.COLORSPACE_RGB, False, 8 \
158     , nc, nr, rowstride);
159 johnpye 283
160 johnpye 284 self.nr = nr
161     self.nc = nc
162     self.zoom = -1 # to fit, up to max 16x
163     self.do_zoom()
164 johnpye 283
165 johnpye 284 #print "DONE IMAGE TRANSFER TO SERVER"
166 johnpye 283
167 johnpye 284 self.fill_var_names()
168     self.fill_rel_names()
169 johnpye 286
170     self.varname.set_text("");
171     self.varval.set_text("");
172     self.relname.set_text("");
173     self.relresid.set_text("");
174 johnpye 280
175 johnpye 286
176 johnpye 284 def do_zoom(self):
177     if self.zoom == -1:
178     w, h = self.imagescroll.size_request()
179     #print "SCALE TO FIX, w=%d, h=%d" % (w,h)
180     if self.nc/self.nr > w/h:
181     # a 'wide' image
182     self.zoom = w / self.nc
183     else:
184     self.zoom = h / self.nr
185 johnpye 283
186 johnpye 284 if self.zoom > MAX_ZOOM_RATIO:
187     self.zoom = MAX_ZOOM_RATIO
188 johnpye 280
189 johnpye 284 if self.zoom * self.nc > MAX_ZOOM_SIZE or self.zoom * self.nr > MAX_ZOOM_SIZE:
190     self.zoom = MAX_ZOOM_SIZE / max(self.nc,self.nr)
191 johnpye 283
192 johnpye 284 w = int(self.zoom * self.nc);
193     h = int(self.zoom * self.nr);
194    
195     self.zoomentry.set_text("%d %%" % (int(self.zoom*100)) )
196 johnpye 280
197 johnpye 284 if self.zoom < 2:
198     pb1 = self.pixbuf.scale_simple(w,h,gtk.gdk.INTERP_BILINEAR)
199     else:
200     pb1 = self.pixbuf.scale_simple(w,h,gtk.gdk.INTERP_NEAREST)
201    
202     self.image.set_from_pixbuf(pb1)
203    
204 johnpye 280 def fill_var_names(self):
205     names = [str(i) for i in self.im.getBlockVars(self.block)]
206     if self.varcollapsed.get_active():
207 johnpye 287 res = reduce(names)
208 johnpye 280 rows = []
209     for k in res:
210     if k=="":
211     for r in res[k]:
212     rows.append(r)
213     else:
214     rows.append( '%s:\n\t%s' % (k, "\n\t".join(res[k])) )
215     text = "\n".join(rows)
216     else:
217     text = "\n".join(names)
218     self.varbuf.set_text(text)
219    
220     def fill_rel_names(self):
221     names = [str(i) for i in self.im.getBlockRels(self.block)]
222     if self.relcollapsed.get_active():
223 johnpye 287 res = reduce(names)
224     rows = []
225     for k in res:
226     if k=="":
227     for r in res[k]:
228     rows.append(r)
229     else:
230     rows.append( '%s:\n\t%s' % (k, "\n\t".join(res[k])) )
231     text = "\n".join(rows)
232 johnpye 280 else:
233     text = "\n".join(names)
234     self.relbuf.set_text(text)
235    
236     def set_block(self, block):
237     self.fill_values(block)
238    
239 johnpye 284 def set_zoom(self,zoom):
240     self.zoom = zoom
241     self.do_zoom()
242    
243 johnpye 286 def show_cursor(self,x,y):
244     c = self.cl + int(x/self.zoom)
245     r = self.rl + int(y / self.zoom)
246     if c > self.ch or r > self.rh:
247     #print "OUT OF RANGE"
248     return
249     var = self.im.getVariable(c)
250     self.varname.set_text(var.getName())
251     self.varval.set_text(str(var.getValue()))
252     rel = self.im.getRelation(r)
253     self.relname.set_text(rel.getName())
254     self.relresid.set_text(str(rel.getResidual()))
255    
256     # GUI EVENT HOOKS-----------------------------------------------------------
257    
258 johnpye 280 def on_varcollapsed_toggled(self,*args):
259 johnpye 294 vc = self.varcollapsed.get_active()
260     self.browser.prefs.setBoolPref("Diagnose","varcollapsed",vc)
261     if self.im:
262     self.fill_var_names()
263 johnpye 280
264     def on_relcollapsed_toggled(self,*args):
265 johnpye 294 rc = self.varcollapsed.get_active()
266     self.browser.prefs.setBoolPref("Diagnose","relcollapsed",rc)
267     if self.im:
268     self.fill_rel_names()
269 johnpye 280
270     def on_nextbutton_clicked(self,*args):
271     self.set_block(self.block + 1)
272    
273     def on_prevbutton_clicked(self,*args):
274 johnpye 290 self.set_block(self.block - 1)
275 johnpye 280
276 johnpye 290 def on_prevbigbutton_clicked(self,*args):
277     b = self.block - 1
278 johnpye 291 while b >= 0:
279 johnpye 290 rl,cl,rh,ch = self.im.getBlockLocation(b)
280     if rh-rl > 0 or ch-cl>0:
281     self.set_block(b)
282     b = b - 1
283     print "NO PRECEDING 'BIG' BLOCKS"
284    
285     def on_nextbigbutton_clicked(self,*args):
286     b = self.block + 1
287     n = self.im.getNumBlocks()
288     while b < n:
289     rl,cl,rh,ch = self.im.getBlockLocation(b)
290     if rh-rl > 0 or ch-cl>0:
291     self.set_block(b)
292     b = b + 1
293     print "NO FOLLOWING 'BIG' BLOCKS"
294    
295 johnpye 283 def on_blockentry_key_press_event(self,widget,event):
296     keyname = gtk.gdk.keyval_name(event.keyval)
297     print "KEY ",keyname
298     if keyname=="Return":
299     self.set_block( int(self.blockentry.get_text()) )
300 johnpye 280
301 johnpye 284 def on_zoominbutton_clicked(self,*args):
302     z = int( math.log(self.zoom)/math.log(2) )
303     z = pow(2,z + 1);
304     self.set_zoom(z)
305    
306     def on_zoomoutbutton_clicked(self,*args):
307     z = int( math.log(self.zoom)/math.log(2) + 0.999)
308     z = pow(2,z - 1);
309     self.set_zoom(z)
310    
311     def on_zoomentry_key_press_event(self,widget,event):
312     keyname = gtk.gdk.keyval_name(event.keyval)
313     print "KEY ",keyname
314     if keyname=="Return":
315     t = self.zoomentry.get_text()
316     m = ZOOM_RE.match(t)
317     if not m:
318     self.zoomentry.set_text("%d %%" % int(self.zoom*100))
319     for mm in m:
320     print m
321     self.set_zoom( int(self.zoomentry.get_text()) )
322    
323 johnpye 286 def on_imageevent_motion_notify_event(self,widget,event):
324     self.show_cursor(event.x, event.y)
325    
326     def on_imageevent_button_press_event(self,widget,event):
327     self.show_cursor(event.x, event.y)
328    
329    
330 johnpye 280 # The following is from
331     # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
332 johnpye 284 # it's still buggy.
333 johnpye 280
334     def fold(data):
335     """ fold sorted numeric sequence data into ranged representation:
336     >>> fold([1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28])
337     '[1,4-6,10,15-18,22,25-28]'
338     """
339     folded = []
340 johnpye 287 for k, g in groupby(enumerate(sorted(data)), lambda (i,x):i-x):
341 johnpye 280 seq = map(itemgetter(1), g)
342     if len(seq) > 1:
343     x = '%s-%s' % (seq[0], seq[-1])
344     else:
345     x = str(seq[0])
346     folded.append(x)
347     return folded and '[%s]' % ','.join(folded) or ''
348    
349 johnpye 287 def reduce(names):
350     """reduce a list of items nto something more readable:
351 johnpye 280 >>> data = 'C.x C.p C.T C.delta[1] C.delta[2] C.delta[3] C.sat.x C.sat.p C.h C.delta[5]'.split()
352     >>> res = reduce(data)
353     >>> for k in sorted(res):
354     ... print '%s: %s' % (k, res[k])
355     C: T, delta[1-3,5], h, p, x
356     C.sat: p, x
357     """
358 johnpye 287 data = sorted([n.split('.') for n in sorted(names)], key=len)
359 johnpye 280 res = {}
360     for k, g in groupby(data, lambda x: len(x)):
361 johnpye 287 if k == 1:
362     indexed = {}
363     seq = set(get(indexed, item) for item in g)
364     res['[global]'] = [ i+fold(indexed.get(i, [])) for i in sorted(seq) ]
365     else:
366     for key, g1 in groupby(g, lambda x: '.'.join(x[:-1])):
367     indexed = {}
368     seq = set(get(indexed, item) for item in g1)
369     res[key] = [ i+fold(indexed.get(i, [])) for i in sorted(seq) ]
370 johnpye 280 return res
371    
372     def get(indexed, item):
373     item = item[-1]
374     if item.endswith(']'):
375     item, idx = item[:-1].split('[')
376     indexed.setdefault(item, []).append(int(idx))
377     return item

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22