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

Diff of /trunk/pygtk/diagnose.py

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

revision 736 by johnpye, Wed Jul 5 04:56:26 2006 UTC revision 2495 by aakash, Tue Aug 16 11:35:08 2011 UTC
# Line 1  Line 1 
1  import gtk  import gtk
 import gtk.glade  
2  import ascpy  import ascpy
3  from itertools import groupby  from itertools import groupby
4  from operator import itemgetter  from operator import itemgetter
# Line 10  import config Line 9  import config
9  from infodialog import *  from infodialog import *
10    
11  ZOOM_RE = re.compile(r"([0-9]+)\s*%?")  ZOOM_RE = re.compile(r"([0-9]+)\s*%?")
12  MAX_ZOOM_SIZE = 2000  MAX_ZOOM_SIZE = float(2000) # float
13  MAX_ZOOM_RATIO = 16  MAX_ZOOM_RATIO = float(16) # float
14  AT_BOUND_TOL = 0.0001;  AT_BOUND_TOL = 0.0001;
15    
16  class DiagnoseWindow:  class DiagnoseWindow:
17      def __init__(self,browser,block=0):      def __init__(self,browser,block=0):
18          self.browser=browser          self.browser=browser
19          _xml = gtk.glade.XML(browser.glade_file,"diagnosewin")          self.browser.builder.add_objects_from_file(self.browser.glade_file, ["diagnosewin"])
20          _xml.signal_autoconnect(self)            self.browser.builder.connect_signals(self)
21            self.window = self.browser.builder.get_object("diagnosewin")
         self.window = _xml.get_widget("diagnosewin")  
22          self.window.set_transient_for(self.browser.window)          self.window.set_transient_for(self.browser.window)
23    
24          try:          try:
# Line 33  class DiagnoseWindow: Line 31  class DiagnoseWindow:
31          except:          except:
32              pass              pass
33                    
34          self.imagescroll = _xml.get_widget("imagescroll")          self.blockstatus = self.browser.builder.get_object("blockstatustext")
35          self.image = _xml.get_widget("image")  
36          self.blockentry = _xml.get_widget("blockentry")          self.imagescroll = self.browser.builder.get_object("imagescroll")
37          self.zoomentry = _xml.get_widget("zoomentry")          self.image = self.browser.builder.get_object("image")
38            self.blockentry = self.browser.builder.get_object("blockentry")
39            self.zoomentry = self.browser.builder.get_object("zoomentry")
40    
41          self.var = None; self.rel = None          self.var = None; self.rel = None
42          self.varname = _xml.get_widget("varname")          self.varname = self.browser.builder.get_object("varname1")
43          self.varval = _xml.get_widget("varval")          self.varval = self.browser.builder.get_object("varval")
44          self.varinfobutton = _xml.get_widget("varinfobutton")          self.varinfobutton = self.browser.builder.get_object("varinfobutton")
45          self.relname = _xml.get_widget("relname")          self.relname = self.browser.builder.get_object("relname1")
46          self.relresid = _xml.get_widget("relresid")          self.relresid = self.browser.builder.get_object("relresid")
47          self.relinfobutton = _xml.get_widget("relinfobutton")          self.relinfobutton = self.browser.builder.get_object("relinfobutton")
48    
49          self.varview = _xml.get_widget("varview")          self.varview = self.browser.builder.get_object("varview")
50          self.varbuf = gtk.TextBuffer()          self.varbuf = gtk.TextBuffer()
51          self.varview.set_buffer(self.varbuf)          self.varview.set_buffer(self.varbuf)
52          self.varcollapsed = _xml.get_widget("varcollapsed")          self.varcollapsed = self.browser.builder.get_object("varcollapsed")
53          self.relview = _xml.get_widget("relview")            self.relview = self.browser.builder.get_object("relview")  
54          self.relcollapsed = _xml.get_widget("relcollapsed")          self.relcollapsed = self.browser.builder.get_object("relcollapsed")
55          self.relvalues = _xml.get_widget("relvalues")          self.relvalues = self.browser.builder.get_object("relvalues")
56          self.rellabels = _xml.get_widget("rellabels")          self.rellabels = self.browser.builder.get_object("rellabels")
57          self.relrels = _xml.get_widget("relrels")          self.relrels = self.browser.builder.get_object("relrels")
58          self.relresids = _xml.get_widget("relresids")          self.relresids = self.browser.builder.get_object("relresids")
59          self.relbuf = gtk.TextBuffer()          self.relbuf = gtk.TextBuffer()
60          self.relview.set_buffer(self.relbuf)          self.relview.set_buffer(self.relbuf)
61    
# Line 79  class DiagnoseWindow: Line 79  class DiagnoseWindow:
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 to be loaded"
83          self.im = self.browser.sim.getIncidenceMatrix()          self.im = self.browser.sim.getIncidenceMatrix()
84          self.data = self.im.getIncidenceData()          self.data = self.im.getIncidenceData()
85          print "DATA LOADED"          print "DATA LOADED"
# Line 189  class DiagnoseWindow: Line 189  class DiagnoseWindow:
189    
190          self.fill_var_names()          self.fill_var_names()
191          self.fill_rel_names()          self.fill_rel_names()
192            self.fill_block_status()
193            
194          self.fill_selection_info()          self.fill_selection_info()
195    
# Line 219  class DiagnoseWindow: Line 220  class DiagnoseWindow:
220              #print "SCALE TO FIX, w=%d, h=%d" % (w,h)              #print "SCALE TO FIX, w=%d, h=%d" % (w,h)
221              if self.nc/self.nr > w/h:              if self.nc/self.nr > w/h:
222                  # a 'wide' image                      # a 'wide' image    
223                  self.zoom = w / self.nc                  self.zoom = float(w) / self.nc
224              else:              else:
225                  self.zoom = h / self.nr                  self.zoom = float(h) / self.nr
226    
227            #self.browser.reporter.reportNote("Diagnose window: preliminary calculated zoom = %f (nr = %d, nc = %d)" % (self.zoom, self.nr, self.nc))
228            
229    
230          if self.zoom > MAX_ZOOM_RATIO:          if self.zoom > MAX_ZOOM_RATIO:
231              self.zoom = MAX_ZOOM_RATIO              self.zoom = MAX_ZOOM_RATIO
232    
233          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:
234                self.browser.reporter.reportNote("image is too big, reducing to MAX_ZOOM_SIZE = %f" % MAX_ZOOM_SIZE);
235              self.zoom = MAX_ZOOM_SIZE / max(self.nc,self.nr)              self.zoom = MAX_ZOOM_SIZE / max(self.nc,self.nr)
236    
237            #self.browser.reporter.reportNote("Diagnose window: matrix zoom = %f" % self.zoom)
238          w = int(self.zoom * self.nc);          w = int(self.zoom * self.nc);
239          h = int(self.zoom * self.nr);          h = int(self.zoom * self.nr);
240                            
# Line 241  class DiagnoseWindow: Line 247  class DiagnoseWindow:
247                    
248          self.image.set_from_pixbuf(pb1)          self.image.set_from_pixbuf(pb1)
249    
250        def fill_block_status(self):
251            print "FILL BLOCK STATUS"
252            s = self.im.getBlockStatus(self.block)
253            ss = "Failed"
254            if s == ascpy.IM_CONVERGED:
255                ss = "Converged"
256            elif s == ascpy.IM_NOT_YET_ATTEMPTED:
257                ss = "Not attempted yet"
258            elif s == ascpy.IM_OVER_TIME:
259                ss += " (time limit)"
260            elif s == ascpy.IM_OVER_ITER:
261                ss += " (iter limit)"
262            self.blockstatus.set_text(ss);
263            
264    
265      def fill_var_names(self):      def fill_var_names(self):
266          print "FILL VAR NAMES"          print "FILL VAR NAMES"
267    
268          names = [str(i) for i in self.im.getBlockVars(self.block)]          names = [str(i) for i in self.im.getBlockVars(self.block)]
269    
270          print "NAMES:",names          #print "NAMES:",names
271    
272          if self.varcollapsed.get_active():          if self.varcollapsed.get_active():
273              res = reduce(names)              res = reduce(names)
# Line 273  class DiagnoseWindow: Line 294  class DiagnoseWindow:
294    
295          names = [str(i) for i in rels]          names = [str(i) for i in rels]
296    
297          print "NAMES =",names          #print "NAMES =",names
298    
299          if self.relcollapsed.get_active():          if self.relcollapsed.get_active():
300              res = reduce(names)              res = reduce(names)
# Line 340  class DiagnoseWindow: Line 361  class DiagnoseWindow:
361              ,"Upper bound": self.var.getUpperBound()              ,"Upper bound": self.var.getUpperBound()
362          }          }
363          for k,v in _rows.iteritems():          for k,v in _rows.iteritems():
364              if v!=0:              text += "\n  %s\t%s" % (k,value_human(v))
                 l = math.log10(abs(v))  
             else:  
                 l = 0;  
             if l > 8 or l < -8:  
                 text += "\n  %-30s%15e" % (k,v)  
             else:  
                 text += "\n  %-30s%15f" % (k,v)  
365                    
366          text += "\n\nIncidence with %d relations:" % self.var.getNumIncidentRelations()          text += "\n\nIncident with %d relations:" % self.var.getNumIncidentRelations()
367          for r in self.var.getIncidentRelations():          for r in self.var.getIncidentRelations():
368              text += "\n  %s" % r.getName()              text += "\n  %s" % r.getName()
369    
370          _dialog = InfoDialog(self.browser,self.window,text,title)          _dialog = InfoDialog(self.browser,self.window,text,title,tabs=(150,300))
371          _dialog.run()          _dialog.run()
372    
373      def on_relinfobutton_clicked(self,*args):      def on_relinfobutton_clicked(self,*args):
374          title = "Relation '%s'" % self.rel          title = "Relation '%s'" % self.rel
375          text = "%s\n%s\n" % (title,"(from the solver's view)")          text = "%s\n%s\n" % (title,"(from the solver's view)")
376          text += "\n  %-30s%15f" % ("Residual", self.rel.getResidual())          text += "\n  %s\t%15f" % ("Residual", self.rel.getResidual())
377    
378          text += "\n\nRelation expression:\n"          text += "\n\nRelation expression:\n"
379          text += self.rel.getRelationAsString()          text += self.rel.getRelationAsString()
380    
381          _dialog = InfoDialog(self.browser,self.window,text,title)          text += "\n\nIncident with %d variables:" % self.rel.getNumIncidentVariables()
382            for v in self.rel.getIncidentVariables():
383                text += "\n  %s\t= %s" % ( v.getName(),value_human(v.getValue()) )
384    
385            _dialog = InfoDialog(self.browser,self.window,text,title,tabs=(150,300))
386          _dialog.run()          _dialog.run()
387                    
388    
# Line 382  class DiagnoseWindow: Line 400  class DiagnoseWindow:
400              rl,cl,rh,ch = self.im.getBlockLocation(b)              rl,cl,rh,ch = self.im.getBlockLocation(b)
401              if rh-rl > 0 or ch-cl>0:              if rh-rl > 0 or ch-cl>0:
402                  self.set_block(b)                  self.set_block(b)
403                    return
404              b = b - 1              b = b - 1
405          print "NO PRECEDING 'BIG' BLOCKS"          print "NO PRECEDING 'BIG' BLOCKS"
406                    
# Line 392  class DiagnoseWindow: Line 411  class DiagnoseWindow:
411              rl,cl,rh,ch = self.im.getBlockLocation(b)              rl,cl,rh,ch = self.im.getBlockLocation(b)
412              if rh-rl > 0 or ch-cl>0:              if rh-rl > 0 or ch-cl>0:
413                  self.set_block(b)                  self.set_block(b)
414                    return
415              b = b + 1              b = b + 1
416          print "NO FOLLOWING 'BIG' BLOCKS"          print "NO FOLLOWING 'BIG' BLOCKS"
417            
# Line 434  class DiagnoseWindow: Line 454  class DiagnoseWindow:
454          self.show_cursor(event.x, event.y)          self.show_cursor(event.x, event.y)
455    
456    
457  # The following is from  def value_human(v):
458        if v==0 or abs( math.log10(abs(v)) )<8:
459            return "%f" % v
460        return "%e" % v
461    
462    #---------------------------------------
463    # Procedures to 'fold' a list of items from a hierarchy
464  # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html  # http://www.experts-exchange.com/Programming/Programming_Languages/Python/Q_21719649.html
465  # it's still buggy.  # It's still buggy, I think
466    
467  def fold(data):  def fold(data):
468      """ fold sorted numeric sequence data into ranged representation:      """ fold sorted numeric sequence data into ranged representation:
# Line 467  def reduce(names): Line 493  def reduce(names):
493      for k, g in groupby(data, lambda x: len(x)):      for k, g in groupby(data, lambda x: len(x)):
494          if k == 1:          if k == 1:
495              indexed = {}              indexed = {}
496              seq = set(get(indexed, item) for item in g)              seq = set([get(indexed, item) for item in g])
497              res['[global]'] = [ i+fold(indexed.get(i, [])) for i in sorted(seq) ]              res['[global]'] = [ i+fold(indexed.get(i, [])) for i in sorted(seq) ]
498          else:          else:
499              for key, g1 in groupby(g, lambda x: '.'.join(x[:-1])):              for key, g1 in groupby(g, lambda x: '.'.join(x[:-1])):
# Line 482  def get(indexed, item): Line 508  def get(indexed, item):
508          item, idx = item[:-1].split('[')          item, idx = item[:-1].split('[')
509          indexed.setdefault(item, []).append(int(idx))          indexed.setdefault(item, []).append(int(idx))
510      return item      return item
511    

Legend:
Removed from v.736  
changed lines
  Added in v.2495

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