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

Commit

Permalink
Gather split vertices directly (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiedxu committed Jan 11, 2021
1 parent 2057057 commit d4da773
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "vsec"
version = "0.1.2"
version = "0.1.3"
description = "Data structure when some edges in a graph are planar points in essence"
authors = ["edxu96 <edxu96@outlook.com>"]

Expand Down
22 changes: 21 additions & 1 deletion tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import networkx as nx
from pandas.core.frame import DataFrame
import pytest as pt
from vsec.conversion import contract, split
from vsec.conversion import contract, split, split_vertex_df
from vsec.geometry import GeoGraph

# There are only two edges left, but sequences might be different.
Expand Down Expand Up @@ -43,11 +43,31 @@ def test_split(case_grid: GeoGraph, vertices_grid: DataFrame):
vertices_grid: vertices in ``case_grid`` and their **type**
attributes.
"""
# Only split 10-0.4 kV transformers.
vertices_split = vertices_grid.index[vertices_grid["type"] == "STAT1004"]

graph, vertex_df = split(case_grid, vertices_split, NAMING, ATTR, IS_FIRST)

assert isinstance(nx.to_pandas_edgelist(graph), DataFrame)
assert nx.is_connected(graph)
assert isinstance(vertex_df, DataFrame)
assert vertex_df.index.name == "original"
assert set(vertex_df.columns) == COLUMNS
assert vertex_df.shape == (34, 2)


@pt.mark.usefixtures("vertices_grid")
def test_split_vertex_df(vertices_grid: DataFrame):
"""Check if can gather split vertices and resulted new vertices.
Args:
vertices_grid: vertices in ``case_grid`` and their **type**
attributes.
"""
# Only split 10-0.4 kV transformers.
vertices_split = vertices_grid.index[vertices_grid["type"] == "STAT1004"]

res = split_vertex_df(vertices_split, NAMING)
assert set(res.columns) == COLUMNS
assert res.shape == (34, 2)
assert res.index.name == "original"
25 changes: 25 additions & 0 deletions vsec/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,28 @@ def split(
vertex_df.index.name = "original"

return graph, vertex_df


def split_vertex_df(
vertices: Set[str], naming: Callable[[str], Tuple[str, str]],
) -> DataFrame:
"""Gather split vertices and resulted new vertices.
Warning:
This should be used when vertex splitting is known to be
correct, because there is no validation.
Args:
vertices: vertices ought to be modelled as edges.
naming: how two resulted vertices should be named.
Returns:
Correspondence between split vertices & resulted new vertices.
"""
vertex_dict = {}
for vertex in vertices:
vertex_dict[vertex] = naming(vertex)

res = pd.DataFrame.from_dict(vertex_dict, orient="index", columns=COLUMNS,)
res.index.name = "original"
return res

0 comments on commit d4da773

Please sign in to comment.