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

Contents of /trunk/pygtk/diagnose.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 283 - (show annotations) (download) (as text)
Wed Feb 8 15:30:50 2006 UTC (12 years, 8 months ago) by johnpye
Original Path: trunk/pygtk/interface/diagnose.py
File MIME type: text/x-python
File size: 5916 byte(s)
This is the initial working 'block diagnose' functionality.
1 import gtk
2 import gtk.glade
3 import ascend
4 from itertools import groupby
5 from operator import itemgetter
6 import math
7
8 class DiagnoseWindow:
9 def __init__(self,GLADE_FILE,browser):
10 self.browser=browser
11 _xml = gtk.glade.XML(GLADE_FILE,"diagnosewin")
12 _xml.signal_autoconnect(self)
13
14 self.window = _xml.get_widget("diagnosewin")
15 self.image = _xml.get_widget("image")
16 self.blockentry = _xml.get_widget("blockentry")
17
18 self.varview = _xml.get_widget("varview")
19 self.varbuf = gtk.TextBuffer()
20 self.varview.set_buffer(self.varbuf)
21 self.varcollapsed = _xml.get_widget("varcollapsed")
22 self.relview = _xml.get_widget("relview")
23 self.relcollapsed = _xml.get_widget("relcollapsed")
24 self.relvalues = _xml.get_widget("relvalues")
25 self.rellabels = _xml.get_widget("rellabels")
26 self.relrels = _xml.get_widget("relrels")
27 self.relresids = _xml.get_widget("relresids")
28 self.relbuf = gtk.TextBuffer()
29 self.relview.set_buffer(self.relbuf)
30
31 self.prepare_data()
32 self.fill_values(0) # block zero
33
34 def run(self):
35 self.window.run()
36 self.window.hide()
37
38 def prepare_data(self):
39 # convert incidence map to pylab numarray type:
40 print "PREPARING DATA"
41 self.im = self.browser.sim.getIncidenceMatrix()
42 self.data = self.im.getIncidenceData()
43 print "DATA LOADED"
44
45 self.canvas = None
46
47 def fill_values(self, block):
48 print "FILL VALUES %d" % block
49 try:
50 rl,cl,rh,ch = self.im.getBlockLocation(block)
51 except IndexError:
52 print "invalid block"
53 return
54 self.block = block
55 self.blockentry.set_text(str(block))
56
57 nr = int(rh-rl+1);
58 nc = int(ch-cl+1);
59
60 print "STARTING IMAGE CREATION"
61 # refer http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html
62 c = chr(255)
63 b = nr*nc*3*[c]
64 rowstride = 3 * nc
65
66 blackdot = [chr(0)]*3;
67 reddot = [chr(255), chr(0), chr(0)]
68 pinkdot = [chr(255), chr(127), chr(127)]
69 skydot = [chr(127), chr(127), chr(255)]
70 bluedot = [chr(0), chr(0), chr(255)]
71
72 for i in self.data:
73 if i.row < rl or i.row > rh or i.col < cl or i.col > ch:
74 continue
75 r = i.row - rl;
76 c = i.col - cl;
77 pos = rowstride*r + 3*c
78 dot = blackdot;
79 var = self.im.getVariable(i.col);
80 try:
81 rat = var.getValue() / var.getNominal()
82 print "SCALE i.col =",rat
83 val = math.log(rat);
84 print "LOG i.col =",val
85 if val > 1:
86 dot = reddot;
87 elif var < -1:
88 dot = bluedot;
89 elif var > 0:
90 dot = pinkdot;
91 elif var < 0:
92 dot = skydot;
93 except ValueError, e:
94 pass
95 print "DOT: ",dot
96 b[pos], b[pos+1], b[pos+2] = dot
97
98 d = ''.join(b)
99
100 print "DONE IMAGE CREATION"
101
102 pb = gtk.gdk.pixbuf_new_from_data(d, gtk.gdk.COLORSPACE_RGB, False, 8 \
103 , nc, nr, rowstride)
104
105
106 if nc > nr:
107 w = 400
108 h = 400 * nr / nc;
109 else:
110 h = 400
111 w = 400 * nr / nc;
112
113 if h/nr > 16 or w/nc > 16:
114 h= nr*16
115 w= nc*16
116
117 if nc < 200 and nr < 200:
118 pb1 = pb.scale_simple(w,h,gtk.gdk.INTERP_NEAREST)
119 else:
120 pb1 = pb.scale_simple(w,h,gtk.gdk.INTERP_BILINEAR)
121
122 del pb;
123
124 print "DONE IMAGE PREPARATION"
125
126 self.image.set_from_pixbuf(pb1)
127
128 print "DONE IMAGE TRANSFER TO SERVER"
129
130 self.fill_var_names()
131 self.fill_rel_names()
132
133 def fill_var_names(self):
134 names = [str(i) for i in self.im.getBlockVars(self.block)]
135 if self.varcollapsed.get_active():
136 res = collapse(names)
137 rows = []
138 for k in res:
139 if k=="":
140 for r in res[k]:
141 rows.append(r)
142 else:
143 rows.append( '%s:\n\t%s' % (k, "\n\t".join(res[k])) )
144 text = "\n".join(rows)
145 else:
146 text = "\n".join(names)
147 self.varbuf.set_text(text)
148
149 def fill_rel_names(self):
150 names = [str(i) for i in self.im.getBlockRels(self.block)]
151 if self.relcollapsed.get_active():
152 text = "\n".join(collapse(names))
153 else:
154 text = "\n".join(names)
155 self.relbuf.set_text(text)
156
157 def set_block(self, block):
158 self.block = block;
159 self.fill_values(block)
160
161 def on_varcollapsed_toggled(self,*args):
162 print "COLLAPSED-TOGGLED"
163 self.fill_var_names()
164
165 def on_relcollapsed_toggled(self,*args):
166 print "COLLAPSED-TOGGLED"
167 self.fill_rel_names()
168
169 def on_nextbutton_clicked(self,*args):
170 self.set_block(self.block + 1)
171
172 def on_prevbutton_clicked(self,*args):
173 self.set_block(self.block - 1)
174
175 def on_blockentry_key_press_event(self,widget,event):
176 keyname = gtk.gdk.keyval_name(event.keyval)
177 print "KEY ",keyname
178 if keyname=="Return":
179 self.set_block( int(self.blockentry.get_text()) )
180
181 # The following is from
182 # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
183
184 def fold(data):
185 """ fold sorted numeric sequence data into ranged representation:
186 >>> fold([1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28])
187 '[1,4-6,10,15-18,22,25-28]'
188 """
189 folded = []
190 for k, g in groupby(enumerate(data), lambda (i,x):i-x):
191 seq = map(itemgetter(1), g)
192 if len(seq) > 1:
193 x = '%s-%s' % (seq[0], seq[-1])
194 else:
195 x = str(seq[0])
196 folded.append(x)
197 return folded and '[%s]' % ','.join(folded) or ''
198
199 def collapse(names):
200 """reduce a list of items into something more readable:
201 >>> 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()
202 >>> res = reduce(data)
203 >>> for k in sorted(res):
204 ... print '%s: %s' % (k, res[k])
205 C: T, delta[1-3,5], h, p, x
206 C.sat: p, x
207 """
208 data = sorted([n.split('.') for n in names], key=len)
209 res = {}
210 for k, g in groupby(data, lambda x: len(x)):
211 item = g.next()
212 assert len(item) == k
213 key = '.'.join(item[:-1])
214 indexed = {}
215 seq = set(get(indexed, item))
216 for item in g:
217 seq.add(get(indexed, item))
218 res[key] = [i+fold(indexed.get(i, [])) for i in sorted(seq)]
219 return res
220
221 def get(indexed, item):
222 item = item[-1]
223 if item.endswith(']'):
224 item, idx = item[:-1].split('[')
225 indexed.setdefault(item, []).append(int(idx))
226 return item

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