Skip to content

Commit

Permalink
minor fix. code cleanup. tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fandreuz committed Jul 18, 2021
1 parent 3982a61 commit c22243d
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 94 deletions.
10 changes: 4 additions & 6 deletions bispy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from .paige_tarjan.paige_tarjan import (
paige_tarjan,
paige_tarjan_qblocks,
)
from .paige_tarjan.paige_tarjan import paige_tarjan
from .dovier_piazza_policriti.dovier_piazza_policriti import (
dovier_piazza_policriti,
dovier_piazza_policriti_partition,
)
from .saha.saha_partition import saha

Expand All @@ -13,6 +9,8 @@
decorate_nx_graph,
to_tuple_list,
)
from enum import Enum, auto
import networkx as nx


class Algorithms(Enum):
Expand All @@ -23,7 +21,7 @@ class Algorithms(Enum):
def compute_maximum_bisimulation(
graph: nx.DiGraph,
initial_partition,
algorithm=Algorithm.PaigeTarjan,
algorithm=Algorithms.PaigeTarjan,
):
"""Compute the maximum bisimulation of the given graph, possibly using
an initial partition (or labeling set). The preferred algorithm may be
Expand Down
16 changes: 9 additions & 7 deletions bispy/dovier_piazza_policriti/dovier_piazza_policriti.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ def dovier_piazza_policriti_partition(
# loop over the ranks
for partition_idx in range(len(partition)):
if len(partition[partition_idx]) == 1:
block = partition[partition_idx][0]
survivor_vertex, collapsed_vertexes = collapse(block)
if survivor_vertex is not None:
# update the collapsed nodes map
collapse_map[survivor_vertex.label] = collapsed_vertexes
# update the partition
split_upper_ranks(partition, block)
if len(partition[partition_idx][0].vertexes):
block = partition[partition_idx][0]
survivor_vertex, collapsed_vertexes = collapse(block)
if survivor_vertex is not None:
# update the collapsed nodes map
collapse_map[survivor_vertex.label] = collapsed_vertexes
# update the partition
split_upper_ranks(partition, block)
# OPTIMIZATION: if at the current rank we only have blocks of single
# vertexes, skip this step.
elif any(map(lambda block: block.size > 1, partition[partition_idx])):
Expand Down Expand Up @@ -180,6 +181,7 @@ def dovier_piazza_policriti_partition(
block_vertexes = []
for scaled_vertex in block.vertexes:
scaled_vertex.back_to_original_label()
scaled_vertex.back_to_original_graph()
block_vertexes.append(scaled_vertex)

# we can set XBlock to None because PTA won't be called again
Expand Down
8 changes: 0 additions & 8 deletions tests/api/__init__.py

This file was deleted.

11 changes: 0 additions & 11 deletions tests/api/api_test_cases.py

This file was deleted.

30 changes: 0 additions & 30 deletions tests/api/test_api.py

This file was deleted.

19 changes: 18 additions & 1 deletion tests/dovier_piazza_policriti/test_dovier_piazza_policriti.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ def test_dpp_rscp_correctness(graph, initial_partition, expected_q_partition):
checker_graphs,
)
def test_dpp_correctness2(graph):
assert to_set(dovier_piazza_policriti(graph)) == to_set(paige_tarjan(graph))
assert to_set(dovier_piazza_policriti(graph)) == to_set(
paige_tarjan(graph)
)


def test_dpp_correctness_all_scc_leaf_with_initial_partition():
Expand All @@ -131,3 +133,18 @@ def test_dpp_correctness_all_scc_leaf_with_initial_partition():
assert to_set(dovier_piazza_policriti(graph, [(0, 1), (2,)])) == set(
[frozenset([0]), frozenset([1]), frozenset([2])]
)


def test_balanced_tree_initial_partition():
graph = nx.balanced_tree(2, 3, create_using=nx.DiGraph)
initial_partition = [
(0, 1, 2),
(3, 4),
(5, 6),
(7, 8, 9, 10),
(11, 12, 13),
(14,),
]
assert to_set(
dovier_piazza_policriti(graph, initial_partition=initial_partition)
) == to_set(paige_tarjan(graph, initial_partition=initial_partition))
22 changes: 21 additions & 1 deletion tests/dovier_piazza_policriti/test_ranked_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def test_create_initial_partition(graph):
vertex.rank == rank for vertex in vertexes
].count(True)
# right rank
assert all(vertex.rank == rank for vertex in partition[idx][0].vertexes)
assert all(
vertex.rank == rank for vertex in partition[idx][0].vertexes
)


@pytest.mark.parametrize("graph", graphs)
Expand Down Expand Up @@ -101,3 +103,21 @@ def test_get_item():
vertexes, _ = decorate_nx_graph(nx.balanced_tree(2, 3))
partition = RankedPartition(vertexes)
assert partition[1] == partition._partition[1]


def test_different_blocks_initial_partition():
graph = nx.balanced_tree(2, 3, create_using=nx.DiGraph)
initial_partition = [
(0, 1, 2),
(3, 4),
(5, 6),
(7, 8, 9, 10),
(11, 12, 13),
(14,),
]
vertexes, _ = decorate_nx_graph(nx.balanced_tree(2, 3), initial_partition)

partition = RankedPartition(vertexes)

assert len(partition[1]) == 3
assert len(partition[2]) == 2
12 changes: 9 additions & 3 deletions tests/paige_tarjan/paige_tarjan_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,25 @@
graph10 = nx.DiGraph()
graph10.add_nodes_from(range(3))
graph10.add_edges_from([(0, 1), (0, 2), (1, 0), (1, 2)])
graph_partition_tuples.append((graph10, initial_partitions[len(graph10.nodes)]))
graph_partition_tuples.append(
(graph10, initial_partitions[len(graph10.nodes)])
)

# 11
graph11 = nx.DiGraph()
graph11.add_nodes_from(range(3))
graph11.add_edges_from([(0, 1), (1, 2), (2, 0), (2, 1)])
graph_partition_tuples.append((graph11, initial_partitions[len(graph11.nodes)]))
graph_partition_tuples.append(
(graph11, initial_partitions[len(graph11.nodes)])
)

# 12
graph12 = nx.DiGraph()
graph12.add_nodes_from(range(3))
graph12.add_edges_from([(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)])
graph_partition_tuples.append((graph12, initial_partitions[len(graph12.nodes)]))
graph_partition_tuples.append(
(graph12, initial_partitions[len(graph12.nodes)])
)

graph_partition_rscp_tuples = []

Expand Down
1 change: 1 addition & 0 deletions tests/paige_tarjan/rscp_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from bispy.utilities.graph_entities import _Vertex, _QBlock


# check if the given partition is stable with respect to the given block, or if
# it's stable if the block isn't given
def is_stable_vertexes_partition(partition: List[List[_Vertex]]) -> bool:
Expand Down
51 changes: 27 additions & 24 deletions tests/paige_tarjan/test_paige_tarjan.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
paige_tarjan_qblocks,
preprocess_initial_partition,
)
from bispy.utilities.graph_decorator import decorate_nx_graph
from bispy.utilities.graph_decorator import decorate_nx_graph, to_set
import tests.paige_tarjan.paige_tarjan_test_cases as test_cases


Expand Down Expand Up @@ -210,7 +210,8 @@ def test_build_block_counterimage_aux_count(graph, initial_partition):
vertex.aux_count = None


# error "dllistnode belongs to another list" triggered by split when using the result of build_block_counterimage
# error "dllistnode belongs to another list" triggered by split when using the
# result of build_block_counterimage
# error "dllistnode doesn't belong to a list"
@pytest.mark.parametrize(
"graph, initial_partition", test_cases.graph_partition_tuples
Expand All @@ -227,7 +228,8 @@ def test_vertex_taken_from_right_list(graph, initial_partition):
assert True


# error "dllistnode belongs to another list" triggered by split when using the result of build_block_counterimage
# error "dllistnode belongs to another list" triggered by split when using the
# result of build_block_counterimage
# error "dllistnode doesn't belong to a list"
@pytest.mark.parametrize(
"graph, initial_partition", test_cases.graph_partition_tuples
Expand All @@ -237,7 +239,7 @@ def test_can_remove_any_vertex_from_its_list(graph, initial_partition):

for qblock in q_partition:
vertex = qblock.vertexes.first
while vertex != None:
while vertex is not None:
qblock.vertexes.remove(vertex)
vertex = vertex.next
# check that this doesn't raise an exception
Expand All @@ -252,23 +254,27 @@ def test_split(graph, initial_partition):
xblock = q_partition[0].xblock

qblock_splitter = q_partition[0]
# qblock_splitter may be modified by split, therefore we need to keep a copy
# qblock_splitter may be modified by split, therefore we need to keep a
# copy
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]

block_counterimage = build_block_counterimage(qblock_splitter)
split(block_counterimage)

# after split the partition should be stable with respect to the block chosen for the split
# after split the partition should be stable with respect to the block
# chosen for the split
for qblock in xblock.qblocks:
assert check_vertexes_stability(
[vertex for vertex in qblock.vertexes], splitter_vertexes
)

# test if the size of the qblocks after the split is equal to the number of vertexes
# test if the size of the qblocks after the split is equal to the number of
# vertexes
for qblock in xblock.qblocks:
assert qblock.size == len(qblock.vertexes)

# check if the qblock a vertex belongs to corresponds to the value vertex.qblock for each of its vertexes
# check if the qblock a vertex belongs to corresponds to the value
# vertex.qblock for each of its vertexes
for qblock in xblock.qblocks:
for vertex in qblock.vertexes:
assert vertex.qblock == qblock
Expand All @@ -283,15 +289,18 @@ def test_split_helper_block_right_xblock(graph, initial_partition):
new_blocks, _, _ = split(vertexes[3:7])

for new_block in new_blocks:
assert any([qblock == new_block for qblock in new_block.xblock.qblocks])
assert any(
[qblock == new_block for qblock in new_block.xblock.qblocks]
)

for old_block in q_partition:
assert old_block.size == 0 or any(
[qblock == old_block for qblock in old_block.xblock.qblocks]
)


# second_splitter should be E^{-1}(B) - E^{-1}(S-B), namely there should only be vertexes in E^{-1}(B) but not in E^{-1}(S-B)
# second_splitter should be E^{-1}(B) - E^{-1}(S-B), namely there should only
# be vertexes in E^{-1}(B) but not in E^{-1}(S-B)
@pytest.mark.parametrize(
"graph, initial_partition", test_cases.graph_partition_tuples
)
Expand All @@ -301,7 +310,8 @@ def test_second_splitter_counterimage(graph, initial_partition):

qblock_splitter = q_partition[0]

# qblock_splitter may be modified by split, therefore we need to keep a copy
# qblock_splitter may be modified by split, therefore we need to keep a
# copy
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]

block_counterimage = build_block_counterimage(qblock_splitter)
Expand Down Expand Up @@ -334,7 +344,8 @@ def test_second_split(graph, initial_partition):
xblock = q_partition[0].xblock

qblock_splitter = q_partition[0]
# qblock_splitter may be modified by split, therefore we need to keep a copy
# qblock_splitter may be modified by split, therefore we need to keep a
# copy
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]

block_counterimage = build_block_counterimage(qblock_splitter)
Expand All @@ -352,7 +363,8 @@ def test_second_split(graph, initial_partition):
new_qblocks, _, _ = split(second_splitter_counterimage)
q_partition.extend(new_qblocks)

# after split the partition should be stable with respect to the block chosen for the split
# after split the partition should be stable with respect to the block
# chosen for the split
for qblock in xblock.qblocks:
assert check_vertexes_stability(
[vertex for vertex in qblock.vertexes], second_splitter_vertexes
Expand Down Expand Up @@ -390,7 +402,7 @@ def test_reset_aux_count_after_refinement(graph, initial_partition):
refine([xblock], [xblock])

for vertex in vertexes:
assert vertex.aux_count == None
assert vertex.aux_count is None


def test_count_after_refinement():
Expand Down Expand Up @@ -451,7 +463,7 @@ def test_no_negative_edge_counts(graph, initial_partition):

for vertex in vertexes:
for edge in vertex.image:
assert edge.count == None or edge.count.value > 0
assert edge.count is None or edge.count.value > 0


@pytest.mark.parametrize(
Expand Down Expand Up @@ -557,12 +569,3 @@ def test_pt_same_initial_partition(graph, initial_partition):
vertex.initial_partition_block_id
== vertex_to_initial_partition_id[vertex.label]
)


def test_simp():
graph = nx.DiGraph()
graph.add_nodes_from(range(2))
graph.add_edges_from([(0, 1)])
initial_partition = [(0, 1)]

x = paige_tarjan(graph, is_integer_graph=True)
5 changes: 3 additions & 2 deletions tests/saha/test_saha.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def test_check_new_scc(graph, new_edge, value):
add_edge(vertexes[new_edge[0]], vertexes[new_edge[1]])

assert (
check_new_scc(vertexes[new_edge[0]], vertexes[new_edge[1]], []) == value
check_new_scc(vertexes[new_edge[0]], vertexes[new_edge[1]], [])
== value
)


Expand All @@ -133,7 +134,7 @@ def test_check_new_scc_cleans(graph, new_edge, value):
check_new_scc(vertexes[new_edge[0]], vertexes[new_edge[1]], [])

for vertex in vertexes:
assert vertex.visited == False
assert not vertex.visited


@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit c22243d

Please sign in to comment.