This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,22 +396,24 @@ class is defined by the exclusion of subgraphs, one can write a generic | |
from sage.structure.unique_representation import CachedRepresentation, UniqueRepresentation | ||
from sage.misc.unknown import Unknown | ||
from sage.env import GRAPHS_DATA_DIR | ||
from sage.misc.cachefunc import cached_method | ||
|
||
import os | ||
import zipfile | ||
from urllib.request import urlopen | ||
from ssl import create_default_context as default_context | ||
|
||
#***************************************************************************** | ||
# **************************************************************************** | ||
# Copyright (C) 2011 Nathann Cohen <[email protected]> | ||
# | ||
# Distributed under the terms of the GNU General Public License (GPL) | ||
# http://www.gnu.org/licenses/ | ||
#***************************************************************************** | ||
# https://www.gnu.org/licenses/ | ||
# **************************************************************************** | ||
|
||
_XML_FILE = "isgci_sage.xml" | ||
_SMALLGRAPHS_FILE = "smallgraphs.txt" | ||
|
||
|
||
class GraphClass(SageObject, CachedRepresentation): | ||
r""" | ||
An instance of this class represents a Graph Class, matching some entry in | ||
|
@@ -437,7 +439,7 @@ class GraphClass(SageObject, CachedRepresentation): | |
""" | ||
def __init__(self, name, gc_id, recognition_function=None): | ||
r""" | ||
Class constructor | ||
Class constructor. | ||
INPUT: | ||
|
@@ -460,7 +462,7 @@ class represented by ``gc_id`` ?* | |
|
||
def _repr_(self): | ||
r""" | ||
Returns a short description of the class | ||
Return a short description of the class. | ||
EXAMPLES:: | ||
|
@@ -471,7 +473,7 @@ def _repr_(self): | |
|
||
def __hash__(self): | ||
r""" | ||
Returns the class' ID hash | ||
Return the class' ID hash. | ||
EXAMPLES:: | ||
|
@@ -482,7 +484,7 @@ def __hash__(self): | |
|
||
def __le__(self, other): | ||
r""" | ||
<= operator | ||
<= operator. | ||
EXAMPLES:: | ||
|
@@ -493,22 +495,22 @@ def __le__(self, other): | |
|
||
def __ge__(self, other): | ||
r""" | ||
>= operator | ||
>= operator. | ||
EXAMPLES:: | ||
sage: graph_classes.Chordal >= graph_classes.Tree | ||
True | ||
""" | ||
inclusion_digraph = GraphClasses().inclusion_digraph() | ||
if inclusion_digraph.shortest_path(self._gc_id,other._gc_id): | ||
if inclusion_digraph.shortest_path(self._gc_id, other._gc_id): | ||
return True | ||
else: | ||
return Unknown | ||
|
||
def __eq__(self, other): | ||
r""" | ||
== operator | ||
== operator. | ||
EXAMPLES:: | ||
|
@@ -519,7 +521,7 @@ def __eq__(self, other): | |
|
||
def __lt__(self, other): | ||
r""" | ||
>, !=, and < operators | ||
>, !=, and < operators. | ||
EXAMPLES:: | ||
|
@@ -542,7 +544,7 @@ def __lt__(self, other): | |
|
||
def forbidden_subgraphs(self): | ||
r""" | ||
Returns the list of forbidden induced subgraphs defining the class. | ||
Return the list of forbidden induced subgraphs defining the class. | ||
If the graph class is not defined by a *finite* list of forbidden | ||
induced subgraphs, ``None`` is returned instead. | ||
|
@@ -561,15 +563,15 @@ def forbidden_subgraphs(self): | |
classes = GraphClasses().classes() | ||
gc = classes[self._gc_id] | ||
|
||
if gc.get("type",None) != "forbidden": | ||
if gc.get("type", None) != "forbidden": | ||
return None | ||
|
||
excluded = gc.get("smallgraph", None) | ||
|
||
if not excluded: | ||
return None | ||
|
||
if not isinstance(excluded,list): | ||
if not isinstance(excluded, list): | ||
excluded = [excluded] | ||
|
||
smallgraphs = GraphClasses().smallgraphs() | ||
|
@@ -581,7 +583,7 @@ def forbidden_subgraphs(self): | |
|
||
def __contains__(self, g): | ||
r""" | ||
Tests if ``g`` belongs to the graph class represented by ``self``. | ||
Check if ``g`` belongs to the graph class represented by ``self``. | ||
EXAMPLES:: | ||
|
@@ -615,7 +617,7 @@ def __contains__(self, g): | |
excluded = self.forbidden_subgraphs() | ||
|
||
if excluded is None: | ||
raise NotImplementedError("No recognition algorithm is available "+ | ||
raise NotImplementedError("No recognition algorithm is available " | ||
"for this class.") | ||
|
||
for gg in excluded: | ||
|
@@ -626,7 +628,7 @@ def __contains__(self, g): | |
|
||
def description(self): | ||
r""" | ||
Prints the information of ISGCI about the current class. | ||
Print the information of ISGCI about the current class. | ||
EXAMPLES:: | ||
|
@@ -673,16 +675,15 @@ def description(self): | |
print("\nProblems :") | ||
print("-" * 11) | ||
|
||
for pbname,data in sorted(cls["problem"].items()): | ||
for pbname, data in sorted(cls["problem"].items()): | ||
if "complexity" in data: | ||
print("{:30} : {}".format(pbname, data["complexity"])) | ||
|
||
from sage.misc.cachefunc import cached_method | ||
|
||
class GraphClasses(UniqueRepresentation): | ||
def get_class(self, id): | ||
r""" | ||
Returns the class corresponding to the given id in the ISGCI database. | ||
Return the class corresponding to the given id in the ISGCI database. | ||
INPUT: | ||
|
@@ -718,12 +719,14 @@ def get_class(self, id): | |
|
||
return GraphClass(name, id) | ||
else: | ||
raise ValueError("The given class id does not exist in the ISGCI database. Is the db too old ? You can update it with graph_classes.update_db().") | ||
raise ValueError("The given class id does not exist in the ISGCI " | ||
"database. Is the db too old ? You can update it " | ||
"with graph_classes.update_db().") | ||
|
||
@cached_method | ||
def classes(self): | ||
r""" | ||
Returns the graph classes, as a dictionary. | ||
Return the graph classes, as a dictionary. | ||
Upon the first call, this loads the database from the local XML | ||
file. Subsequent calls are cached. | ||
|
@@ -746,7 +749,7 @@ def classes(self): | |
@cached_method | ||
def inclusions(self): | ||
r""" | ||
Returns the graph class inclusions | ||
Return the graph class inclusions. | ||
OUTPUT: | ||
|
@@ -769,7 +772,7 @@ def inclusions(self): | |
@cached_method | ||
def smallgraphs(self): | ||
r""" | ||
Returns a dictionary associating a graph to a graph description string. | ||
Return a dictionary associating a graph to a graph description string. | ||
Upon the first call, this loads the database from the local XML files. | ||
Subsequent calls are cached. | ||
|
@@ -794,7 +797,7 @@ def smallgraphs(self): | |
@cached_method | ||
def inclusion_digraph(self): | ||
r""" | ||
Returns the class inclusion digraph | ||
Return the class inclusion digraph. | ||
Upon the first call, this loads the database from the local XML file. | ||
Subsequent calls are cached. | ||
|
@@ -804,23 +807,23 @@ def inclusion_digraph(self): | |
sage: g = graph_classes.inclusion_digraph(); g | ||
Digraph on ... vertices | ||
""" | ||
classes = self.classes() | ||
classes = self.classes() | ||
inclusions = self.inclusions() | ||
|
||
from sage.graphs.digraph import DiGraph | ||
inclusion_digraph = DiGraph() | ||
inclusion_digraph.add_vertices(classes.keys()) | ||
|
||
for edge in inclusions: | ||
if edge.get("confidence","") == "unpublished": | ||
if edge.get("confidence", "") == "unpublished": | ||
continue | ||
inclusion_digraph.add_edge(edge['super'], edge['sub']) | ||
|
||
return inclusion_digraph | ||
|
||
def _download_db(self): | ||
r""" | ||
Downloads the current version of the ISGCI db | ||
Download the current version of the ISGCI db. | ||
EXAMPLES:: | ||
|
@@ -842,7 +845,7 @@ def _download_db(self): | |
|
||
def _parse_db(self, directory): | ||
r""" | ||
Parses the ISGCI database and stores its content in ``self``. | ||
Parse the ISGCI database and stores its content in ``self``. | ||
INPUT: | ||
|
@@ -873,8 +876,8 @@ def _parse_db(self, directory): | |
smallgraph_file = open(os.path.join(GRAPHS_DATA_DIR, _SMALLGRAPHS_FILE), 'r') | ||
smallgraphs = {} | ||
|
||
for l in smallgraph_file.readlines(): | ||
key, string = l.split("\t") | ||
for line in smallgraph_file.readlines(): | ||
key, string = line.split("\t") | ||
smallgraphs[key] = Graph(string) | ||
|
||
smallgraph_file.close() | ||
|
@@ -941,7 +944,7 @@ def _get_ISGCI(self): | |
|
||
# Which copy is the most recent on the disk ? | ||
if (os.path.getmtime(os.path.join(SAGE_DB, _XML_FILE)) > | ||
os.path.getmtime(os.path.join(GRAPHS_DATA_DIR, _XML_FILE))): | ||
os.path.getmtime(os.path.join(GRAPHS_DATA_DIR, _XML_FILE))): | ||
|
||
directory = os.path.join(SAGE_DB, _XML_FILE) | ||
|
||
|
@@ -976,8 +979,8 @@ def show_all(self): | |
# We want to print the different fields, and this dictionary stores the | ||
# maximal number of characters of each field. | ||
MAX = { | ||
"id" : 0, | ||
"type" : 0, | ||
"id": 0, | ||
"type": 0, | ||
"smallgraph": 0, | ||
"name": 0 | ||
} | ||
|
@@ -1002,20 +1005,29 @@ def sort_key(x): | |
MAX[key] = min(length, MAX_LEN) | ||
|
||
# Head of the table | ||
print(("{0:"+str(MAX["id"])+"} | {1:"+str(MAX["name"])+"} | {2:"+str(MAX["type"])+"} | {3:"+str(MAX["smallgraph"])+"}").format("id", "name", "type", "smallgraph")) | ||
st = ("{:" + str(MAX["id"]) + "}").format("id") | ||
st += (" | {:" + str(MAX["name"]) + "}").format("name") | ||
st += (" | {:" + str(MAX["type"]) + "}").format("type") | ||
st += (" | {:" + str(MAX["smallgraph"]) + "}").format("smallgraph") | ||
print(st) | ||
print("-" * (sum(MAX.values())+9)) | ||
|
||
# Entries | ||
for entry in classes_list: | ||
ID = entry.get("id", "") | ||
name = entry.get("name", "") | ||
type = entry.get("type", "") | ||
typ = entry.get("type", "") | ||
smallgraph = entry.get("smallgraph", "") | ||
print(("{0:"+str(MAX["id"])+"} | {1:"+str(MAX["name"])+"} | {2:"+str(MAX["type"])+"} | ").format(ID, name[:MAX_LEN], type[:MAX_LEN])+str(smallgraph)[:MAX_LEN]) | ||
st = ("{:" + str(MAX["id"]) + "}").format(ID) | ||
st += (" | {:" + str(MAX["name"]) + "}").format(name[:MAX_LEN]) | ||
st += (" | {:" + str(MAX["type"]) + "}").format(typ[:MAX_LEN]) | ||
st += " | " + str(smallgraph)[:MAX_LEN] | ||
print(st) | ||
|
||
|
||
def _XML_to_dict(root): | ||
r""" | ||
Returns the XML data as a dictionary | ||
Return the XML data as a dictionary. | ||
INPUT: | ||
|
@@ -1050,6 +1062,7 @@ def _XML_to_dict(root): | |
return root.text | ||
return ans | ||
|
||
|
||
graph_classes = GraphClasses() | ||
|
||
# Any object added to this list should also appear in the class' documentation, at the top of the file. | ||
|
Oops, something went wrong.