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

Contents of /trunk/pygtk/diagnose.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 282 - (show annotations) (download) (as text)
Wed Feb 8 13:51:04 2006 UTC (13 years, 7 months ago) by johnpye
Original Path: trunk/pygtk/interface/diagnose.py
File MIME type: text/x-python
File size: 4941 byte(s)
This version uses direct creation of the block incidence matrix image data
using the gtk.gdk.pixbuf_new_from_data function.
1 import gtk
2 import gtk.glade
3 import ascend
4 import pylab;
5 import matplotlib
6 matplotlib.use('GTK')
7 from matplotlib.figure import Figure
8 from matplotlib.axes import Subplot
9 from matplotlib.colors import LinearSegmentedColormap
10 from matplotlib.backends.backend_gtk import FigureCanvasGTK, NavigationToolbar
11 from itertools import groupby
12 from operator import itemgetter
13
14 class DiagnoseWindow:
15 def __init__(self,GLADE_FILE,browser):
16 self.browser=browser
17 _xml = gtk.glade.XML(GLADE_FILE,"diagnosewin")
18 _xml.signal_autoconnect(self)
19
20 self.window = _xml.get_widget("diagnosewin")
21 self.image = _xml.get_widget("image")
22 self.blockentry = _xml.get_widget("blockentry")
23
24 self.varview = _xml.get_widget("varview")
25 self.varbuf = gtk.TextBuffer()
26 self.varview.set_buffer(self.varbuf)
27 self.varcollapsed = _xml.get_widget("varcollapsed")
28 self.relview = _xml.get_widget("relview")
29 self.relcollapsed = _xml.get_widget("relcollapsed")
30 self.relvalues = _xml.get_widget("relvalues")
31 self.rellabels = _xml.get_widget("rellabels")
32 self.relrels = _xml.get_widget("relrels")
33 self.relresids = _xml.get_widget("relresids")
34 self.relbuf = gtk.TextBuffer()
35 self.relview.set_buffer(self.relbuf)
36
37 self.prepare_data()
38 self.fill_values(0) # block zero
39
40 def run(self):
41 self.window.run()
42 self.window.hide()
43
44 def prepare_data(self):
45 # convert incidence map to pylab numarray type:
46 print "PREPARING DATA"
47 self.im = self.browser.sim.getIncidenceMatrix()
48 self.data = self.im.getIncidenceData()
49 print "DATA LOADED"
50
51 self.canvas = None
52
53 def fill_values(self, block):
54 print "FILL VALUES %d" % block
55 self.block = block
56 self.blockentry.set_text(str(block))
57
58 # refer http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html
59 c = chr(255)
60 b = self.im.getNumRows()*self.im.getNumCols()*3*[c]
61 rowstride = 3 * self.im.getNumCols()
62 for i in self.data:
63 pos = rowstride*i.row + 3*i.col
64 b[pos], b[pos+1], b[pos+2] = [chr(0)]*3
65
66 d = ''.join(b)
67 pb = gtk.gdk.pixbuf_new_from_data(d, gtk.gdk.COLORSPACE_RGB, False, 8 \
68 , self.im.getNumCols(), self.im.getNumRows(), rowstride)
69
70 pb1 = pb.scale_simple(400,400,gtk.gdk.INTERP_BILINEAR)
71 del pb;
72
73 self.image.set_from_pixbuf(pb1)
74
75 self.fill_var_names()
76 self.fill_rel_names()
77
78 def fill_var_names(self):
79 names = [str(i) for i in self.im.getBlockVars(self.block)]
80 if self.varcollapsed.get_active():
81 res = collapse(names)
82 rows = []
83 for k in res:
84 if k=="":
85 for r in res[k]:
86 rows.append(r)
87 else:
88 rows.append( '%s:\n\t%s' % (k, "\n\t".join(res[k])) )
89 text = "\n".join(rows)
90 else:
91 text = "\n".join(names)
92 self.varbuf.set_text(text)
93
94 def fill_rel_names(self):
95 names = [str(i) for i in self.im.getBlockRels(self.block)]
96 if self.relcollapsed.get_active():
97 text = "\n".join(collapse(names))
98 else:
99 text = "\n".join(names)
100 self.relbuf.set_text(text)
101
102 def set_block(self, block):
103 self.block = block;
104 self.fill_values(block)
105
106 def on_varcollapsed_toggled(self,*args):
107 print "COLLAPSED-TOGGLED"
108 self.fill_var_names()
109
110 def on_relcollapsed_toggled(self,*args):
111 print "COLLAPSED-TOGGLED"
112 self.fill_rel_names()
113
114 def on_nextbutton_clicked(self,*args):
115 self.set_block(self.block + 1)
116
117 def on_prevbutton_clicked(self,*args):
118 self.set_block(self.block - 1)
119
120 def on_blockentry_changed(self,*args):
121 self.set_block( int(self.blockentry.get_text()) )
122
123 # The following is from
124 # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
125
126 def fold(data):
127 """ fold sorted numeric sequence data into ranged representation:
128 >>> fold([1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28])
129 '[1,4-6,10,15-18,22,25-28]'
130 """
131 folded = []
132 for k, g in groupby(enumerate(data), lambda (i,x):i-x):
133 seq = map(itemgetter(1), g)
134 if len(seq) > 1:
135 x = '%s-%s' % (seq[0], seq[-1])
136 else:
137 x = str(seq[0])
138 folded.append(x)
139 return folded and '[%s]' % ','.join(folded) or ''
140
141 def collapse(names):
142 """reduce a list of items into something more readable:
143 >>> 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()
144 >>> res = reduce(data)
145 >>> for k in sorted(res):
146 ... print '%s: %s' % (k, res[k])
147 C: T, delta[1-3,5], h, p, x
148 C.sat: p, x
149 """
150 data = sorted([n.split('.') for n in names], key=len)
151 res = {}
152 for k, g in groupby(data, lambda x: len(x)):
153 item = g.next()
154 assert len(item) == k
155 key = '.'.join(item[:-1])
156 indexed = {}
157 seq = set(get(indexed, item))
158 for item in g:
159 seq.add(get(indexed, item))
160 res[key] = [i+fold(indexed.get(i, [])) for i in sorted(seq)]
161 return res
162
163 def get(indexed, item):
164 item = item[-1]
165 if item.endswith(']'):
166 item, idx = item[:-1].split('[')
167 indexed.setdefault(item, []).append(int(idx))
168 return item

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