diff --git a/project/task01.py b/project/task01.py new file mode 100644 index 000000000..b5fe8b39a --- /dev/null +++ b/project/task01.py @@ -0,0 +1,29 @@ +from dataclasses import dataclass +from pathlib import Path +from typing import Any +import cfpq_data +import networkx as nx +import pydot + + +@dataclass +class GraphData: + vertices: int + edges: int + labels: Any + + +def load_graph_data(graph_name: str) -> GraphData | None: + try: + graph_path = cfpq_data.download(graph_name) + graph = cfpq_data.graph_from_csv(graph_path) + return GraphData(graph.number_of_nodes(), graph.number_of_edges(), set(cfpq_data.get_sorted_labels(graph))) + except FileNotFoundError: + return None + + +def dump_two_cycles_graph(first_cycle_size: int, second_cycle_size: int, labels: tuple[str, str], output: Path) -> None: + g = cfpq_data.labeled_two_cycles_graph(first_cycle_size, second_cycle_size, labels=labels) + output.parent.mkdir(parents=True, exist_ok=True) + pydot_graph = nx.nx_pydot.to_pydot(g) + pydot_graph.write(output) diff --git a/tests/test_dump_two_cycles_graph.py b/tests/test_dump_two_cycles_graph.py new file mode 100644 index 000000000..479cd0d78 --- /dev/null +++ b/tests/test_dump_two_cycles_graph.py @@ -0,0 +1,42 @@ +import project.task01 as tsk01 +import networkx as nx +from pathlib import Path + + +class TestIsomorphismAfterDump: + def setup_method(self): + tsk01.dump_two_cycles_graph(1, 2, ("x", "y"), Path("./tmp/1x2.dot")) + tsk01.dump_two_cycles_graph(10, 100, ("first label", "second label"), Path("./tmp/10x100.dot")) + + + def reference_graph(self, first_cycle_size: int, second_cycle_size: int, labels: tuple[str, str]) -> nx.Graph: + c1 = nx.cycle_graph(first_cycle_size + 1) + c2 = nx.cycle_graph([i + first_cycle_size if i != 0 else 0 for i in range(second_cycle_size + 1)]) + nx.set_edge_attributes(c1, name="label", values=labels[0]) + nx.set_edge_attributes(c2, name="label", values=labels[1]) + g = nx.compose(c1, c2) + return g + + + def test_isomorphism_of_small_graph(self): + g = nx.Graph(nx.nx_pydot.read_dot("./tmp/1x2.dot")) + ref = self.reference_graph(1, 2, ("x", "y")) + assert nx.is_isomorphic(g, ref) + + + def test_isomorphism_of_bigger_graph(self): + g = nx.Graph(nx.nx_pydot.read_dot("./tmp/10x100.dot")) + ref = self.reference_graph(10, 100, ("first label", "second label")) + assert nx.is_isomorphic(g, ref) + + + def teardown_method(self): + dir = Path("./tmp") + if dir.exists() and dir.is_dir(): + for item in sorted(dir.rglob("*"), reverse=True): + if item.is_file() or item.is_symlink(): + item.unlink() + elif item.is_dir(): + item.rmdir() + + dir.rmdir() diff --git a/tests/test_load_graph_data.py b/tests/test_load_graph_data.py new file mode 100644 index 000000000..bdb39cf9a --- /dev/null +++ b/tests/test_load_graph_data.py @@ -0,0 +1,77 @@ +from dataclasses import dataclass +import project.task01 as tsk01 + + +@dataclass +class wc: + vertices = 332 + edges = 269 + labels = set(["a", "d"]) + + +@dataclass +class univ: + vertices = 179 + edges = 293 + labels = set( + [ + "type", + "label", + "subClassOf", + "domain", + "range", + "first", + "rest", + "someValuesFrom", + "onProperty", + "intersectionOf", + "subPropertyOf", + "inverseOf", + "versionInfo", + "comment" + ] + ) + + +@dataclass +class gzip: + vertices = 2687 + edges = 2293 + labels = set(["a", "d"]) + + +class TestLoadNonExistingGraph: + def setup_method(self): + self.failed_graph = tsk01.load_graph_data("graph_monte_cristo") + + + def test_graphs_not_found(self): + assert self.failed_graph is None + + +class TestLoadGraph: + def setup_method(self): + self.wc = tsk01.load_graph_data("wc") + self.univ = tsk01.load_graph_data("univ") + self.gzip = tsk01.load_graph_data("gzip") + + + def test_wc(self): + assert not self.wc is None + assert self.wc.vertices == wc.vertices + assert self.wc.edges == wc.edges + assert self.wc.labels == wc.labels + + + def test_univ(self): + assert not self.wc is None + assert self.wc.vertices == wc.vertices + assert self.wc.edges == wc.edges + assert self.wc.labels == wc.labels + + + def test_gzip(self): + assert not self.wc is None + assert self.wc.vertices == wc.vertices + assert self.wc.edges == wc.edges + assert self.wc.labels == wc.labels