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

Contents of /trunk/pygtk/interface/diagnose.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download) (as text)
Wed Feb 8 08:31:56 2006 UTC (13 years, 9 months ago) by johnpye
File MIME type: text/x-python
File size: 5459 byte(s)
Some work on adding the 'block diagnosis' window
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.view = _xml.get_widget("canvasvbox")
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 if self.canvas:
59 self.view.remove(self.canvas)
60
61 # This is not going to be very efficient at this stage:
62 mtx = pylab.zeros((self.im.getNumRows(), self.im.getNumCols(), ))*0.
63 for i in self.data:
64 mtx[i.row, i.col] = int(i.type)
65
66 # prepare colour map
67 cmapdata = {
68 # type = 0 type = 1 type = 2
69 # norelation active fixed active free
70 'red' : ((0., 1., 1.), (0.5, 0., 0.), (1., 0., 0.)),
71 'green': ((0., 1., 1.), (0.5, 1., 1.), (1., 0., 0.)),
72 'blue' : ((0., 1., 1.), (0.5, 0., 0.), (1., 0.3, 0.3))
73 }
74
75 _im_cmap = LinearSegmentedColormap('im_cmap', cmapdata, 4)
76
77 self.figure = pylab.Figure(figsize=(6,4), dpi=72)
78 _axes = self.figure.add_subplot(111)
79 _axes.set_xlabel('Variables')
80 _axes.set_ylabel('Relations')
81 _axes.set_title('Block Incidence Matrix')
82 _axes.grid(True)
83 _axes.imshow(mtx, cmap=_im_cmap, interpolation='nearest')
84
85 self.canvas = FigureCanvasGTK(self.figure) # a gtk.DrawingArea
86 self.canvas.show()
87
88 self.view.pack_start(self.canvas, True, True)
89
90 self.fill_var_names()
91 self.fill_rel_names()
92
93 def fill_var_names(self):
94 names = [str(i) for i in self.im.getBlockVars(self.block)]
95 if self.varcollapsed.get_active():
96 res = collapse(names)
97 rows = []
98 for k in res:
99 if k=="":
100 for r in res[k]:
101 rows.append(r)
102 else:
103 rows.append( '%s:\n\t%s' % (k, "\n\t".join(res[k])) )
104 text = "\n".join(rows)
105 else:
106 text = "\n".join(names)
107 self.varbuf.set_text(text)
108
109 def fill_rel_names(self):
110 names = [str(i) for i in self.im.getBlockRels(self.block)]
111 if self.relcollapsed.get_active():
112 text = "\n".join(collapse(names))
113 else:
114 text = "\n".join(names)
115 self.relbuf.set_text(text)
116
117 def set_block(self, block):
118 self.block = block;
119 self.fill_values(block)
120
121 def on_varcollapsed_toggled(self,*args):
122 print "COLLAPSED-TOGGLED"
123 self.fill_var_names()
124
125 def on_relcollapsed_toggled(self,*args):
126 print "COLLAPSED-TOGGLED"
127 self.fill_rel_names()
128
129 def on_nextbutton_clicked(self,*args):
130 self.set_block(self.block + 1)
131
132 def on_prevbutton_clicked(self,*args):
133 self.set_block(self.block - 1)
134
135 def on_blockentry_changed(self,*args):
136 self.set_block( int(self.blockentry.get_text()) )
137
138 # The following is from
139 # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
140
141 def fold(data):
142 """ fold sorted numeric sequence data into ranged representation:
143 >>> fold([1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28])
144 '[1,4-6,10,15-18,22,25-28]'
145 """
146 folded = []
147 for k, g in groupby(enumerate(data), lambda (i,x):i-x):
148 seq = map(itemgetter(1), g)
149 if len(seq) > 1:
150 x = '%s-%s' % (seq[0], seq[-1])
151 else:
152 x = str(seq[0])
153 folded.append(x)
154 return folded and '[%s]' % ','.join(folded) or ''
155
156 def collapse(names):
157 """reduce a list of items into something more readable:
158 >>> 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()
159 >>> res = reduce(data)
160 >>> for k in sorted(res):
161 ... print '%s: %s' % (k, res[k])
162 C: T, delta[1-3,5], h, p, x
163 C.sat: p, x
164 """
165 data = sorted([n.split('.') for n in names], key=len)
166 res = {}
167 for k, g in groupby(data, lambda x: len(x)):
168 item = g.next()
169 assert len(item) == k
170 key = '.'.join(item[:-1])
171 indexed = {}
172 seq = set(get(indexed, item))
173 for item in g:
174 seq.add(get(indexed, item))
175 res[key] = [i+fold(indexed.get(i, [])) for i in sorted(seq)]
176 return res
177
178 def get(indexed, item):
179 item = item[-1]
180 if item.endswith(']'):
181 item, idx = item[:-1].split('[')
182 indexed.setdefault(item, []).append(int(idx))
183 return item

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