Ticket #11880: trac_11880-first_review.patch

File trac_11880-first_review.patch, 6.7 KB (added by ncohen, 8 years ago)
  • sage/graphs/isgci.py

    # 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  
    204204  Sage's startup. The internal methods using the database should often be
    205205  preceded by a call to :meth:`_load_ISGCI_if_not_loaded
    206206  <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>`.
    209209
    210210* Once the database has been accessed once, the information is extracted from
    211211  the ``.sobj`` files in which it is stored and their information can be
     
    213213
    214214  * ``sage.graphs.isgci.classes`` (dictionary)
    215215  * ``sage.graphs.isgci.inclusions`` (list of dictionaries)
    216   * ``sage.graphs.isgci.class_digraph`` (DiGraph)
     216  * ``sage.graphs.isgci.inclusion_digraph`` (DiGraph)
    217217
    218218  Note that the digraph is only built if necessary (for instance if the user
    219219  tried to compare two classes).
    220220
    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
    222222  the ID of the graph classes. This digraph contains an edge `uv` if the graph
    223223  class represented by `u` is larger (not necessarily strictly) than the one
    224224  represented by `v`.
     
    230230  Though it represents inclusions of sets, this ``DiGraph`` is **NOT
    231231  ACYCLIC**::
    232232
    233       sage: sage.graphs.isgci.class_digraph.is_directed_acyclic()
     233      sage: sage.graphs.isgci.inclusion_digraph.is_directed_acyclic()
    234234      False
    235235
    236236  Indeed, several entries exist in the ISGCI database which represent
     
    293293-------
    294294"""
    295295
     296from sage.structure.sage_object import SageObject
     297from sage.structure.unique_representation import UniqueRepresentation
     298
    296299classes = {}
    297300inclusions = []
    298 class_digraph = None
    299 
     301inclusion_digraph = None
    300302
    301303#*****************************************************************************
    302304#      Copyright (C) 2011 Nathann Cohen <nathann.cohen@gmail.com>
     
    419421        sage: sage.graphs.isgci.update_db() # Not tested -- requires internet
    420422    """
    421423    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
    423425
    424426    try:
    425427        _download_db()
     
    446448    try:
    447449        save(classes, SAGE_ROOT+'/data/graphs/isgci_classes.sobj')
    448450        save(inclusions, SAGE_ROOT+'/data/graphs/isgci_inclusions.sobj')
    449         inclusions = class_digraph = None
     451        inclusions = inclusion_digraph = None
    450452        classes = {}
    451453        if verbose:
    452454            print "Database saved to .sobj files in "+SAGE_ROOT+'/data/graphs/'
     
    462464    try:
    463465        save(classes, SAGE_DB+"/isgci_classes.sobj")
    464466        save(inclusions, SAGE_DB+"/isgci_inclusions.sobj")
    465         classes = inclusions = class_digraph = None
     467        classes = inclusions = inclusion_digraph = None
    466468        if verbose:
    467469            print "Database saved to .sobj files in "+SAGE_DB
    468470
     
    597599    if classes == {}:
    598600        classes, inclusions = get_ISGCI()
    599601
    600 def _build_class_digraph_if_not_built():
     602def _build_inclusion_digraph_if_not_built():
    601603    r"""
    602604    Builds the class digraph
    603605    """
    604     global classes, inclusions, class_digraph
     606    global classes, inclusions, inclusion_digraph
    605607
    606     if not (class_digraph is None):
     608    if not (inclusion_digraph is None):
    607609        return
    608610
    609611    _load_ISGCI_if_not_loaded()
    610612
    611613    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())
    614616
    615617    for edge in inclusions:
    616618        if edge.get("confidence","") == "unpublished":
    617619            continue
    618620
    619         class_digraph.add_edge(edge['super'], edge['sub'])
     621        inclusion_digraph.add_edge(edge['super'], edge['sub'])
    620622
    621623    inclusions = []
    622624
    623 # TODO: maybe inherit from SageObject?
    624 class GraphClass:
     625
     626class GraphClass(SageObject, UniqueRepresentation):
    625627    r"""
    626628    An instance of this class represents a Graph Class, matching some entry in
    627629    the ISGCI database.
     
    669671        return other.__ge__(self)
    670672
    671673    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) != [])
    677676
    678677    def __eq__(self, other):
    679678        return self.__ge__(other) and other.__ge__(self)
     
    714713        global classes
    715714        _load_ISGCI_if_not_loaded()
    716715
    717         classs = classes[self._gc_id]
     716        cls = classes[self._gc_id]
    718717        print "Class of graphs : "+self._name
    719718        print "-"*(len(self._name)+18)
    720719
    721         for key, value in classs.iteritems():
     720        for key, value in cls.iteritems():
    722721            if value != "" and key != "problems":
    723722                print "{0:30} : ".format(key),
    724723                print value
    725724
    726725        print "\nProblems :"
    727726        print "-"*11
    728         for key, value in classs["problems"].iteritems():
     727        for key, value in cls["problems"].iteritems():
    729728            if value != "":
    730729                print "{0:30} : ".format(key),
    731730                print value
    732731
     732from sage.misc.cachefunc import cached_function
    733733
    734 
    735 class GraphClasses:
     734class GraphClasses(UniqueRepresentation):
    736735    def get_class_from_id(self, ID):
    737736        r"""
    738737        Returns the class corresponding to the given ID in the ISGCI database.
     
    771770        else:
    772771            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().")
    773772
     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
    774785graph_classes = GraphClasses()
    775786
    776 
    777 
    778 
    779 
    780 
    781787# Any object added to this list should also appear in the class' documentation, at the top of the file.
    782788graph_classes.BinaryTrees = GraphClass("BinaryTrees", "gc_847")
    783789graph_classes.Bipartite = GraphClass("Bipartite", "gc_69")