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

Diff of /trunk/pygtk/diagnose.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/pygtk/interface/diagnose.py revision 291 by johnpye, Fri Feb 10 03:32:21 2006 UTC trunk/pygtk/diagnose.py revision 895 by johnpye, Tue Oct 24 00:12:34 2006 UTC
# Line 1  Line 1 
1  import gtk  import gtk
2  import gtk.glade  import gtk.glade
3  import ascend  import ascpy
4  from itertools import groupby  from itertools import groupby
5  from operator import itemgetter  from operator import itemgetter
6  import math  import math
7  import re  import re
8    
9    import config
10    from infodialog import *
11    
12  ZOOM_RE = re.compile(r"([0-9]+)\s*%?")  ZOOM_RE = re.compile(r"([0-9]+)\s*%?")
13  MAX_ZOOM_SIZE = 2000  MAX_ZOOM_SIZE = float(2000) # float
14  MAX_ZOOM_RATIO = 16  MAX_ZOOM_RATIO = float(16) # float
15  AT_BOUND_TOL = 0.0001;  AT_BOUND_TOL = 0.0001;
16    
17  class DiagnoseWindow:  class DiagnoseWindow:
18      def __init__(self,GLADE_FILE,browser,block=0):      def __init__(self,browser,block=0):
19          self.browser=browser          self.browser=browser
20          _xml = gtk.glade.XML(GLADE_FILE,"diagnosewin")          _xml = gtk.glade.XML(browser.glade_file,"diagnosewin")
21          _xml.signal_autoconnect(self)            _xml.signal_autoconnect(self)  
22    
23          self.window = _xml.get_widget("diagnosewin")          self.window = _xml.get_widget("diagnosewin")
24            self.window.set_transient_for(self.browser.window)
25    
26            try:
27                _icon = gtk.Image()
28                _iconpath = browser.assets_dir+'diagnose'+config.ICON_EXTENSION
29                print "ICON PATH =",_iconpath
30                _icon.set_from_file(_iconpath)
31                print "ICON = ",_icon
32                self.window.set_icon(_icon)
33            except:
34                pass
35            
36          self.imagescroll = _xml.get_widget("imagescroll")          self.imagescroll = _xml.get_widget("imagescroll")
37          self.image = _xml.get_widget("image")          self.image = _xml.get_widget("image")
38          self.blockentry = _xml.get_widget("blockentry")          self.blockentry = _xml.get_widget("blockentry")
39          self.zoomentry = _xml.get_widget("zoomentry")          self.zoomentry = _xml.get_widget("zoomentry")
40    
41          self.varname = _xml.get_widget("varname");          self.var = None; self.rel = None
42          self.varval = _xml.get_widget("varval");          self.varname = _xml.get_widget("varname")
43          self.relname = _xml.get_widget("relname");          self.varval = _xml.get_widget("varval")
44          self.relresid = _xml.get_widget("relresid");          self.varinfobutton = _xml.get_widget("varinfobutton")
45            self.relname = _xml.get_widget("relname")
46            self.relresid = _xml.get_widget("relresid")
47            self.relinfobutton = _xml.get_widget("relinfobutton")
48    
49          self.varview = _xml.get_widget("varview")          self.varview = _xml.get_widget("varview")
50          self.varbuf = gtk.TextBuffer()          self.varbuf = gtk.TextBuffer()
# Line 41  class DiagnoseWindow: Line 59  class DiagnoseWindow:
59          self.relbuf = gtk.TextBuffer()          self.relbuf = gtk.TextBuffer()
60          self.relview.set_buffer(self.relbuf)          self.relview.set_buffer(self.relbuf)
61    
62            self.im = None
63          self.block = 0          self.block = 0
64            self.apply_prefs()
65    
66          self.prepare_data()          self.prepare_data()
67          self.fill_values(block) # block zero          self.fill_values(block) # block zero
68    
# Line 49  class DiagnoseWindow: Line 70  class DiagnoseWindow:
70          self.window.run()          self.window.run()
71          self.window.hide()          self.window.hide()
72    
73        def apply_prefs(self):
74            vc = self.browser.prefs.getBoolPref("Diagnose","varcollapsed",True)
75    
76            print "VARCOLLAPSED =",vc
77            self.varcollapsed.set_active(vc)
78            self.relcollapsed.set_active(self.browser.prefs.getBoolPref("Diagnose","relcollapsed",True))
79    
80      def prepare_data(self):      def prepare_data(self):
81          # convert incidence map to pylab numarray type:          # convert incidence map to pylab numarray type:
82          print "PREPARING DATA"          print "PREPARING DATA"
# Line 59  class DiagnoseWindow: Line 87  class DiagnoseWindow:
87          self.zoom=1;          self.zoom=1;
88            
89      def fill_values(self, block):      def fill_values(self, block):
90            
91          try:          try:
92                if self.im.getNumBlocks()==0:
93                    print "NO BLOCKS!"
94                    self.image.set_from_stock(gtk.STOCK_DIALOG_ERROR
95                        ,gtk.ICON_SIZE_DIALOG
96                    )
97                    self.browser.reporter.reportError(
98                        "Can't 'Diagnose blocks' until solver has been used."
99                    )
100                    return;
101              rl,cl,rh,ch = self.im.getBlockLocation(block)              rl,cl,rh,ch = self.im.getBlockLocation(block)
102          except IndexError:          except IndexError:
103              self.blockentry.set_text(str(self.block))              if block >= self.im.getNumBlocks():
104              return                  block = self.im.getNumBlocks() - 1
105          except RuntimeError:                  rl,cl,rh,ch = self.im.getBlockLocation(block)
106                else:              
107                    print "BLOCK INDEX ERROR: block =",block
108                    self.blockentry.set_text(str(self.block))
109                    return
110            except RuntimeError,e:
111                print "ERROR GETTING BLOCK LOCATION:",str(e)
112              self.blockentry.set_text(str(self.block))              self.blockentry.set_text(str(self.block))
113              return              return
114    
# Line 79  class DiagnoseWindow: Line 123  class DiagnoseWindow:
123          nr = int(rh-rl+1);          nr = int(rh-rl+1);
124          nc = int(ch-cl+1);          nc = int(ch-cl+1);
125    
126          #print "STARTING IMAGE CREATION"          print "STARTING IMAGE CREATION"
127          # refer http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html          # refer http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html
128          c = chr(255)          c = chr(255)
129          b = nr*nc*3*[c]          b = nr*nc*3*[c]
# Line 131  class DiagnoseWindow: Line 175  class DiagnoseWindow:
175    
176          d = ''.join(b)          d = ''.join(b)
177    
178          #print "DONE IMAGE CREATION"          print "DONE IMAGE CREATION"
179            
180          self.pixbuf = gtk.gdk.pixbuf_new_from_data(d, gtk.gdk.COLORSPACE_RGB, False, 8 \          self.pixbuf = gtk.gdk.pixbuf_new_from_data(d, gtk.gdk.COLORSPACE_RGB, False, 8 \
181                  , nc, nr, rowstride);                  , nc, nr, rowstride);
# Line 141  class DiagnoseWindow: Line 185  class DiagnoseWindow:
185          self.zoom = -1 # to fit, up to max 16x          self.zoom = -1 # to fit, up to max 16x
186          self.do_zoom()          self.do_zoom()
187    
188          #print "DONE IMAGE TRANSFER TO SERVER"          print "DONE IMAGE TRANSFER TO SERVER"
189    
190          self.fill_var_names()          self.fill_var_names()
191          self.fill_rel_names()          self.fill_rel_names()
192            
193          self.varname.set_text("");          self.fill_selection_info()
         self.varval.set_text("");  
         self.relname.set_text("");  
         self.relresid.set_text("");  
194    
195            print "DONE FILL VALUES"
196    
197        def fill_selection_info(self):
198            if self.var:
199                self.varname.set_text(self.var.getName())
200                self.varval.set_text(str(self.var.getValue()))
201                self.varinfobutton.set_sensitive(True)
202            else:
203                self.varname.set_text("")
204                self.varval.set_text("")
205                self.varinfobutton.set_sensitive(False)
206    
207            if self.rel:
208                self.relname.set_text(self.rel.getName())
209                self.relresid.set_text(str(self.rel.getResidual()))
210                self.relinfobutton.set_sensitive(True)
211            else:
212                self.relname.set_text("")
213                self.relresid.set_text("")
214                self.relinfobutton.set_sensitive(False)
215    
216      def do_zoom(self):      def do_zoom(self):
217          if self.zoom == -1:          if self.zoom == -1:
# Line 158  class DiagnoseWindow: Line 219  class DiagnoseWindow:
219              #print "SCALE TO FIX, w=%d, h=%d" % (w,h)              #print "SCALE TO FIX, w=%d, h=%d" % (w,h)
220              if self.nc/self.nr > w/h:              if self.nc/self.nr > w/h:
221                  # a 'wide' image                      # a 'wide' image    
222                  self.zoom = w / self.nc                  self.zoom = float(w) / self.nc
223              else:              else:
224                  self.zoom = h / self.nr                  self.zoom = float(h) / self.nr
225    
226            self.browser.reporter.reportNote("Diagnose window: preliminary calculated zoom = %f (nr = %d, nc = %d)" % (self.zoom, self.nr, self.nc))
227            
228    
229          if self.zoom > MAX_ZOOM_RATIO:          if self.zoom > MAX_ZOOM_RATIO:
230              self.zoom = MAX_ZOOM_RATIO              self.zoom = MAX_ZOOM_RATIO
231    
232          if self.zoom * self.nc > MAX_ZOOM_SIZE or self.zoom * self.nr > MAX_ZOOM_SIZE:          if self.zoom * self.nc > MAX_ZOOM_SIZE or self.zoom * self.nr > MAX_ZOOM_SIZE:
233                self.browser.reporter.reportNode("image is too big, reducing to MAX_ZOOM_SIZE = %f" % MAX_ZOOM_SIZE);
234              self.zoom = MAX_ZOOM_SIZE / max(self.nc,self.nr)              self.zoom = MAX_ZOOM_SIZE / max(self.nc,self.nr)
235    
236            self.browser.reporter.reportNote("Diagnose window: matrix zoom = %f" % self.zoom)
237          w = int(self.zoom * self.nc);          w = int(self.zoom * self.nc);
238          h = int(self.zoom * self.nr);          h = int(self.zoom * self.nr);
239                            
# Line 181  class DiagnoseWindow: Line 247  class DiagnoseWindow:
247          self.image.set_from_pixbuf(pb1)          self.image.set_from_pixbuf(pb1)
248    
249      def fill_var_names(self):      def fill_var_names(self):
250            print "FILL VAR NAMES"
251    
252          names = [str(i) for i in self.im.getBlockVars(self.block)]          names = [str(i) for i in self.im.getBlockVars(self.block)]
253    
254            #print "NAMES:",names
255    
256          if self.varcollapsed.get_active():          if self.varcollapsed.get_active():
257              res = reduce(names)              res = reduce(names)
258              rows = []              rows = []
# Line 196  class DiagnoseWindow: Line 267  class DiagnoseWindow:
267              text = "\n".join(names)              text = "\n".join(names)
268          self.varbuf.set_text(text)          self.varbuf.set_text(text)
269    
270            print "DONE VAR NAMES"
271    
272      def fill_rel_names(self):      def fill_rel_names(self):
273          names = [str(i) for i in self.im.getBlockRels(self.block)]          print "REL NAMES"
274    
275            rels = self.im.getBlockRels(self.block)
276    
277            print "GOT RELS, NOW GETTING NAMES"
278    
279            names = [str(i) for i in rels]
280    
281            #print "NAMES =",names
282    
283          if self.relcollapsed.get_active():          if self.relcollapsed.get_active():
284              res = reduce(names)              res = reduce(names)
285              rows = []              rows = []
# Line 212  class DiagnoseWindow: Line 294  class DiagnoseWindow:
294              text = "\n".join(names)              text = "\n".join(names)
295          self.relbuf.set_text(text)          self.relbuf.set_text(text)
296    
297            print "DONE REL NAMES"
298    
299      def set_block(self, block):      def set_block(self, block):
300          self.fill_values(block)          self.fill_values(block)
301    
# Line 225  class DiagnoseWindow: Line 309  class DiagnoseWindow:
309          if c > self.ch or r > self.rh:          if c > self.ch or r > self.rh:
310              #print "OUT OF RANGE"              #print "OUT OF RANGE"
311              return              return
312          var = self.im.getVariable(c)          self.var = self.im.getVariable(c)
313          self.varname.set_text(var.getName())          self.rel = self.im.getRelation(r)
314          self.varval.set_text(str(var.getValue()))          self.fill_selection_info()
         rel = self.im.getRelation(r)  
         self.relname.set_text(rel.getName())  
         self.relresid.set_text(str(rel.getResidual()))  
315    
316      # GUI EVENT HOOKS-----------------------------------------------------------      # GUI EVENT HOOKS-----------------------------------------------------------
317    
318        def on_diagnosewin_close(self,*args):
319            self.window.response(gtk.RESPONSE_CLOSE);
320    
321        # incidence data view
322    
323      def on_varcollapsed_toggled(self,*args):      def on_varcollapsed_toggled(self,*args):
324          print "COLLAPSED-TOGGLED"          vc = self.varcollapsed.get_active()
325          self.fill_var_names()          self.browser.prefs.setBoolPref("Diagnose","varcollapsed",vc)    
326            if self.im:
327                self.fill_var_names()
328    
329      def on_relcollapsed_toggled(self,*args):      def on_relcollapsed_toggled(self,*args):
330          print "COLLAPSED-TOGGLED"          rc = self.varcollapsed.get_active()
331          self.fill_rel_names()          self.browser.prefs.setBoolPref("Diagnose","relcollapsed",rc)    
332            if self.im:
333                self.fill_rel_names()
334    
335        # detailed information about vars and rels (solver-side information!)
336    
337        def on_varinfobutton_clicked(self,*args):
338            title = "Variable '%s'" % self.var
339            text = "%s\n%s\n" % (title,"(from the solver's view)")
340    
341            _rows = {
342                "Value": self.var.getValue()
343                ,"Nominal": self.var.getNominal()
344                ,"Lower bound": self.var.getLowerBound()
345                ,"Upper bound": self.var.getUpperBound()
346            }
347            for k,v in _rows.iteritems():
348                text += "\n  %s\t%s" % (k,value_human(v))
349            
350            text += "\n\nIncident with %d relations:" % self.var.getNumIncidentRelations()
351            for r in self.var.getIncidentRelations():
352                text += "\n  %s" % r.getName()
353    
354            _dialog = InfoDialog(self.browser,self.window,text,title,tabs=(150,300))
355            _dialog.run()
356    
357        def on_relinfobutton_clicked(self,*args):
358            title = "Relation '%s'" % self.rel
359            text = "%s\n%s\n" % (title,"(from the solver's view)")
360            text += "\n  %s\t%15f" % ("Residual", self.rel.getResidual())
361    
362            text += "\n\nRelation expression:\n"
363            text += self.rel.getRelationAsString()
364    
365            text += "\n\nIncident with %d variables:" % self.rel.getNumIncidentVariables()
366            for v in self.rel.getIncidentVariables():
367                text += "\n  %s\t= %s" % ( v.getName(),value_human(v.getValue()) )
368    
369            _dialog = InfoDialog(self.browser,self.window,text,title,tabs=(150,300))
370            _dialog.run()
371            
372    
373        # block navigation
374    
375      def on_nextbutton_clicked(self,*args):      def on_nextbutton_clicked(self,*args):
376          self.set_block(self.block + 1)          self.set_block(self.block + 1)
# Line 273  class DiagnoseWindow: Line 403  class DiagnoseWindow:
403          if keyname=="Return":          if keyname=="Return":
404              self.set_block( int(self.blockentry.get_text()) )              self.set_block( int(self.blockentry.get_text()) )
405    
406        # zoom in and out
407    
408      def on_zoominbutton_clicked(self,*args):      def on_zoominbutton_clicked(self,*args):
409          z = int( math.log(self.zoom)/math.log(2) )          z = int( math.log(self.zoom)/math.log(2) )
410          z = pow(2,z + 1);          z = pow(2,z + 1);
# Line 295  class DiagnoseWindow: Line 427  class DiagnoseWindow:
427                  print m                  print m
428              self.set_zoom( int(self.zoomentry.get_text()) )              self.set_zoom( int(self.zoomentry.get_text()) )
429    
430        # clicking in incidence matrix to get updated information at RHS
431    
432      def on_imageevent_motion_notify_event(self,widget,event):      def on_imageevent_motion_notify_event(self,widget,event):
433          self.show_cursor(event.x, event.y)          self.show_cursor(event.x, event.y)
434    
# Line 302  class DiagnoseWindow: Line 436  class DiagnoseWindow:
436          self.show_cursor(event.x, event.y)          self.show_cursor(event.x, event.y)
437    
438    
439  # The following is from  def value_human(v):
440        if v==0 or abs( math.log10(abs(v)) )<8:
441            return "%f" % v
442        return "%e" % v
443    
444    #---------------------------------------
445    # Procedures to 'fold' a list of items from a hierarchy
446  # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html  # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
447  # it's still buggy.  # It's still buggy, I think
448    
449  def fold(data):  def fold(data):
450      """ fold sorted numeric sequence data into ranged representation:      """ fold sorted numeric sequence data into ranged representation:
# Line 350  def get(indexed, item): Line 490  def get(indexed, item):
490          item, idx = item[:-1].split('[')          item, idx = item[:-1].split('[')
491          indexed.setdefault(item, []).append(int(idx))          indexed.setdefault(item, []).append(int(idx))
492      return item      return item
493    

Legend:
Removed from v.291  
changed lines
  Added in v.895

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