# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1329654020 3600
# Node ID d141e5f37a82e4c76aa75ef6510d5b838ccb2cc9
# Parent 3382c0f5e2a8a5ff345ccf94d72f14a216262f9a
GraphClasses in Sage  Implementing the reviewer's remarks
diff git a/sage/graphs/isgci.py b/sage/graphs/isgci.py
a

b


204  204  Sage's startup. The internal methods using the database should often be 
205  205  preceded by a call to :meth:`_load_ISGCI_if_not_loaded 
206  206  <sage.graphs.isgci._load_ISGCI_if_not_loaded>` or to 
207   :meth:`_build_class_digraph_if_not_built 
208   <sage.graphs.isgci._build_class_digraph_if_not_built>`. 
 207  :meth:`_build_inclusion_digraph_if_not_built 
 208  <sage.graphs.isgci._build_inclusion_digraph_if_not_built>`. 
209  209  
210  210  * Once the database has been accessed once, the information is extracted from 
211  211  the ``.sobj`` files in which it is stored and their information can be 
… 
… 

213  213  
214  214  * ``sage.graphs.isgci.classes`` (dictionary) 
215  215  * ``sage.graphs.isgci.inclusions`` (list of dictionaries) 
216   * ``sage.graphs.isgci.class_digraph`` (DiGraph) 
 216  * ``sage.graphs.isgci.inclusion_digraph`` (DiGraph) 
217  217  
218  218  Note that the digraph is only built if necessary (for instance if the user 
219  219  tried to compare two classes). 
220  220  
221   * The ``DiGraph`` object ``sage.graphs.isgci.class_digraph`` has for vertex set 
 221  * The ``DiGraph`` object ``sage.graphs.isgci.inclusion_digraph`` has for vertex set 
222  222  the ID of the graph classes. This digraph contains an edge `uv` if the graph 
223  223  class represented by `u` is larger (not necessarily strictly) than the one 
224  224  represented by `v`. 
… 
… 

230  230  Though it represents inclusions of sets, this ``DiGraph`` is **NOT 
231  231  ACYCLIC**:: 
232  232  
233   sage: sage.graphs.isgci.class_digraph.is_directed_acyclic() 
 233  sage: sage.graphs.isgci.inclusion_digraph.is_directed_acyclic() 
234  234  False 
235  235  
236  236  Indeed, several entries exist in the ISGCI database which represent 
… 
… 

293  293   
294  294  """ 
295  295  
 296  from sage.structure.sage_object import SageObject 
 297  from sage.structure.unique_representation import UniqueRepresentation 
 298  
296  299  classes = {} 
297  300  inclusions = [] 
298   class_digraph = None 
299   
 301  inclusion_digraph = None 
300  302  
301  303  #***************************************************************************** 
302  304  # Copyright (C) 2011 Nathann Cohen <nathann.cohen@gmail.com> 
… 
… 

419  421  sage: sage.graphs.isgci.update_db() # Not tested  requires internet 
420  422  """ 
421  423  from sage.misc.misc import SAGE_TMP, SAGE_ROOT, SAGE_LOCAL, SAGE_DB 
422   global classes, inclusions, class_digraph 
 424  global classes, inclusions, inclusion_digraph 
423  425  
424  426  try: 
425  427  _download_db() 
… 
… 

446  448  try: 
447  449  save(classes, SAGE_ROOT+'/data/graphs/isgci_classes.sobj') 
448  450  save(inclusions, SAGE_ROOT+'/data/graphs/isgci_inclusions.sobj') 
449   inclusions = class_digraph = None 
 451  inclusions = inclusion_digraph = None 
450  452  classes = {} 
451  453  if verbose: 
452  454  print "Database saved to .sobj files in "+SAGE_ROOT+'/data/graphs/' 
… 
… 

462  464  try: 
463  465  save(classes, SAGE_DB+"/isgci_classes.sobj") 
464  466  save(inclusions, SAGE_DB+"/isgci_inclusions.sobj") 
465   classes = inclusions = class_digraph = None 
 467  classes = inclusions = inclusion_digraph = None 
466  468  if verbose: 
467  469  print "Database saved to .sobj files in "+SAGE_DB 
468  470  
… 
… 

597  599  if classes == {}: 
598  600  classes, inclusions = get_ISGCI() 
599  601  
600   def _build_class_digraph_if_not_built(): 
 602  def _build_inclusion_digraph_if_not_built(): 
601  603  r""" 
602  604  Builds the class digraph 
603  605  """ 
604   global classes, inclusions, class_digraph 
 606  global classes, inclusions, inclusion_digraph 
605  607  
606   if not (class_digraph is None): 
 608  if not (inclusion_digraph is None): 
607  609  return 
608  610  
609  611  _load_ISGCI_if_not_loaded() 
610  612  
611  613  from sage.graphs.digraph import DiGraph 
612   class_digraph = DiGraph() 
613   class_digraph.add_vertices(classes.keys()) 
 614  inclusion_digraph = DiGraph() 
 615  inclusion_digraph.add_vertices(classes.keys()) 
614  616  
615  617  for edge in inclusions: 
616  618  if edge.get("confidence","") == "unpublished": 
617  619  continue 
618  620  
619   class_digraph.add_edge(edge['super'], edge['sub']) 
 621  inclusion_digraph.add_edge(edge['super'], edge['sub']) 
620  622  
621  623  inclusions = [] 
622  624  
623   # TODO: maybe inherit from SageObject? 
624   class GraphClass: 
 625  
 626  class GraphClass(SageObject, UniqueRepresentation): 
625  627  r""" 
626  628  An instance of this class represents a Graph Class, matching some entry in 
627  629  the ISGCI database. 
… 
… 

669  671  return other.__ge__(self) 
670  672  
671  673  def __ge__(self, other): 
672   global class_digraph, classes 
673   
674   _build_class_digraph_if_not_built() 
675   
676   return (class_digraph.shortest_path(self._gc_id,other._gc_id) != []) 
 674  inclusion_digraph = GraphClasses().inclusion_digraph() 
 675  return (inclusion_digraph.shortest_path(self._gc_id,other._gc_id) != []) 
677  676  
678  677  def __eq__(self, other): 
679  678  return self.__ge__(other) and other.__ge__(self) 
… 
… 

714  713  global classes 
715  714  _load_ISGCI_if_not_loaded() 
716  715  
717   classs = classes[self._gc_id] 
 716  cls = classes[self._gc_id] 
718  717  print "Class of graphs : "+self._name 
719  718  print ""*(len(self._name)+18) 
720  719  
721   for key, value in classs.iteritems(): 
 720  for key, value in cls.iteritems(): 
722  721  if value != "" and key != "problems": 
723  722  print "{0:30} : ".format(key), 
724  723  print value 
725  724  
726  725  print "\nProblems :" 
727  726  print ""*11 
728   for key, value in classs["problems"].iteritems(): 
 727  for key, value in cls["problems"].iteritems(): 
729  728  if value != "": 
730  729  print "{0:30} : ".format(key), 
731  730  print value 
732  731  
 732  from sage.misc.cachefunc import cached_function 
733  733  
734   
735   class GraphClasses: 
 734  class GraphClasses(UniqueRepresentation): 
736  735  def get_class_from_id(self, ID): 
737  736  r""" 
738  737  Returns the class corresponding to the given ID in the ISGCI database. 
… 
… 

771  770  else: 
772  771  raise ValueError("The given class ID does not exist in the ISGCI database. Is the db too old ? You can update it with sage.graphs.isgci.update_db().") 
773  772  
 773  @cached_function 
 774  def classes(): 
 775  global classes 
 776  _load_ISGCI_if_not_loaded() 
 777  return classes 
 778  
 779  @cached_function 
 780  def inclusion_digraph(): 
 781  global inclusion_digraph, classes 
 782  _build_inclusion_digraph_if_not_built() 
 783  return inclusion_digraph 
 784  
774  785  graph_classes = GraphClasses() 
775  786  
776   
777   
778   
779   
780   
781  787  # Any object added to this list should also appear in the class' documentation, at the top of the file. 
782  788  graph_classes.BinaryTrees = GraphClass("BinaryTrees", "gc_847") 
783  789  graph_classes.Bipartite = GraphClass("Bipartite", "gc_69") 