Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Ignore edge contraction for now (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiedxu committed Jan 14, 2021
1 parent f5f8c50 commit 93a6b96
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 291 deletions.
73 changes: 0 additions & 73 deletions tests/test_conversion.py

This file was deleted.

20 changes: 19 additions & 1 deletion tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pandas.core.frame import DataFrame
import pytest as pt
from vsec.graph import COLUMNS, Graph
from vsec.utils import join_terminal_labels

ATTR = "level"
IS_FIRST = lambda x: x == "high"
Expand All @@ -13,11 +14,13 @@
("f", "g_lv"),
("g_hv", "g_lv"),
}
# There are only two edges left, but sequences might be different.
EDGES = {("n1", "n2n3"), ("n2n3", "n1"), ("n2n3", "n4"), ("n4", "n2n3")}


@pt.mark.usefixtures("case_readme")
def test_split(case_readme: nx.DiGraph):
"""Check basic features of ``split`` method in ``GeoGraph``.
"""Check basic features after one vertex splitting.
Note:
``split`` method is not tested thoroughly here.
Expand Down Expand Up @@ -52,3 +55,18 @@ def test_split(case_readme: nx.DiGraph):
assert res.find_vertices_component("g_lv") == {"g_lv", "d", "f"}

assert res.vertices_new == {"g_hv", "g_lv"}


@pt.mark.skip(reason="not sure about edge contraction yet")
@pt.mark.usefixtures("case_simple")
def test_contract(case_simple: nx.Graph):
"""Check if edge in a graph contracted and its terminals renamed.
Args:
case_simple: a simple graph with 3 edges.
"""
g = Graph(case_simple)
res = g.contract("contraction", join_terminal_labels)
print(res.nodes)
assert type(res) is Graph
assert set(res.edges).difference(EDGES) == set()
5 changes: 2 additions & 3 deletions vsec/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
"""Main module for ``vsec``."""
from vsec.conversion import contract, split
from vsec.graph import Graph
from vsec.utils import join_terminal_labels

__version__ = "0.1.0"
__all__ = [
"contract",
"split",
"Graph",
"join_terminal_labels",
]
130 changes: 0 additions & 130 deletions vsec/conversion.py

This file was deleted.

83 changes: 0 additions & 83 deletions vsec/geometry.py

This file was deleted.

42 changes: 41 additions & 1 deletion vsec/graph.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""A class for two operations at the same time."""
"""A class for two operations at the same time.
Any modification after the initiation should be avoided, or many methods
and properties will not work as expected.
"""
from typing import Callable, Optional, Set, Tuple, Union

from loguru import logger
import networkx as nx
from networkx.algorithms.minors import contracted_edge
from networkx.relabel import relabel_nodes
import pandas as pd
from pandas.core.frame import DataFrame

Expand Down Expand Up @@ -283,3 +289,37 @@ def is_connected_graph(self) -> bool:
True if this undirected graph is connected.
"""
return nx.is_connected(self.to_undirected())

def contract(
self, attr: str, naming: Optional[Callable[[Tuple[str, str]], str]],
) -> nx.Graph:
"""Contract edges with ``attr`` being true in ``Graph``.
Args:
attr: some bool edge attribute indicating contraction if
true.
naming: a function to name the new node based on labels of
two terminals of the contracted edge.
Returns:
A graph with less edge(s).
"""
graph = nx.Graph(self)
for u, v, attributes in self.edges(data=True):
if attributes[attr]:
edge_contracted = (u, v)
graph = contracted_edge(
graph, edge_contracted, self_loops=False
)
logger.debug(f"Edge {edge_contracted} has been contracted.")

# Find which node is kept.
if u in graph.nodes:
mapping = {u: naming(edge_contracted)}
else:
mapping = {v: naming(edge_contracted)}

# Rename the kept node.
relabel_nodes(graph, mapping, copy=False)

return graph
Loading

0 comments on commit 93a6b96

Please sign in to comment.