Skip to content

Commit c22243d

Browse files
committed
minor fix. code cleanup. tests
1 parent 3982a61 commit c22243d

14 files changed

Lines changed: 133 additions & 94 deletions

bispy/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
from .paige_tarjan.paige_tarjan import (
2-
paige_tarjan,
3-
paige_tarjan_qblocks,
4-
)
1+
from .paige_tarjan.paige_tarjan import paige_tarjan
52
from .dovier_piazza_policriti.dovier_piazza_policriti import (
63
dovier_piazza_policriti,
7-
dovier_piazza_policriti_partition,
84
)
95
from .saha.saha_partition import saha
106

@@ -13,6 +9,8 @@
139
decorate_nx_graph,
1410
to_tuple_list,
1511
)
12+
from enum import Enum, auto
13+
import networkx as nx
1614

1715

1816
class Algorithms(Enum):
@@ -23,7 +21,7 @@ class Algorithms(Enum):
2321
def compute_maximum_bisimulation(
2422
graph: nx.DiGraph,
2523
initial_partition,
26-
algorithm=Algorithm.PaigeTarjan,
24+
algorithm=Algorithms.PaigeTarjan,
2725
):
2826
"""Compute the maximum bisimulation of the given graph, possibly using
2927
an initial partition (or labeling set). The preferred algorithm may be

bispy/dovier_piazza_policriti/dovier_piazza_policriti.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,14 @@ def dovier_piazza_policriti_partition(
140140
# loop over the ranks
141141
for partition_idx in range(len(partition)):
142142
if len(partition[partition_idx]) == 1:
143-
block = partition[partition_idx][0]
144-
survivor_vertex, collapsed_vertexes = collapse(block)
145-
if survivor_vertex is not None:
146-
# update the collapsed nodes map
147-
collapse_map[survivor_vertex.label] = collapsed_vertexes
148-
# update the partition
149-
split_upper_ranks(partition, block)
143+
if len(partition[partition_idx][0].vertexes):
144+
block = partition[partition_idx][0]
145+
survivor_vertex, collapsed_vertexes = collapse(block)
146+
if survivor_vertex is not None:
147+
# update the collapsed nodes map
148+
collapse_map[survivor_vertex.label] = collapsed_vertexes
149+
# update the partition
150+
split_upper_ranks(partition, block)
150151
# OPTIMIZATION: if at the current rank we only have blocks of single
151152
# vertexes, skip this step.
152153
elif any(map(lambda block: block.size > 1, partition[partition_idx])):
@@ -180,6 +181,7 @@ def dovier_piazza_policriti_partition(
180181
block_vertexes = []
181182
for scaled_vertex in block.vertexes:
182183
scaled_vertex.back_to_original_label()
184+
scaled_vertex.back_to_original_graph()
183185
block_vertexes.append(scaled_vertex)
184186

185187
# we can set XBlock to None because PTA won't be called again

tests/api/__init__.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/api/api_test_cases.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/api/test_api.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/dovier_piazza_policriti/test_dovier_piazza_policriti.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ def test_dpp_rscp_correctness(graph, initial_partition, expected_q_partition):
121121
checker_graphs,
122122
)
123123
def test_dpp_correctness2(graph):
124-
assert to_set(dovier_piazza_policriti(graph)) == to_set(paige_tarjan(graph))
124+
assert to_set(dovier_piazza_policriti(graph)) == to_set(
125+
paige_tarjan(graph)
126+
)
125127

126128

127129
def test_dpp_correctness_all_scc_leaf_with_initial_partition():
@@ -131,3 +133,18 @@ def test_dpp_correctness_all_scc_leaf_with_initial_partition():
131133
assert to_set(dovier_piazza_policriti(graph, [(0, 1), (2,)])) == set(
132134
[frozenset([0]), frozenset([1]), frozenset([2])]
133135
)
136+
137+
138+
def test_balanced_tree_initial_partition():
139+
graph = nx.balanced_tree(2, 3, create_using=nx.DiGraph)
140+
initial_partition = [
141+
(0, 1, 2),
142+
(3, 4),
143+
(5, 6),
144+
(7, 8, 9, 10),
145+
(11, 12, 13),
146+
(14,),
147+
]
148+
assert to_set(
149+
dovier_piazza_policriti(graph, initial_partition=initial_partition)
150+
) == to_set(paige_tarjan(graph, initial_partition=initial_partition))

tests/dovier_piazza_policriti/test_ranked_partition.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def test_create_initial_partition(graph):
3030
vertex.rank == rank for vertex in vertexes
3131
].count(True)
3232
# right rank
33-
assert all(vertex.rank == rank for vertex in partition[idx][0].vertexes)
33+
assert all(
34+
vertex.rank == rank for vertex in partition[idx][0].vertexes
35+
)
3436

3537

3638
@pytest.mark.parametrize("graph", graphs)
@@ -101,3 +103,21 @@ def test_get_item():
101103
vertexes, _ = decorate_nx_graph(nx.balanced_tree(2, 3))
102104
partition = RankedPartition(vertexes)
103105
assert partition[1] == partition._partition[1]
106+
107+
108+
def test_different_blocks_initial_partition():
109+
graph = nx.balanced_tree(2, 3, create_using=nx.DiGraph)
110+
initial_partition = [
111+
(0, 1, 2),
112+
(3, 4),
113+
(5, 6),
114+
(7, 8, 9, 10),
115+
(11, 12, 13),
116+
(14,),
117+
]
118+
vertexes, _ = decorate_nx_graph(nx.balanced_tree(2, 3), initial_partition)
119+
120+
partition = RankedPartition(vertexes)
121+
122+
assert len(partition[1]) == 3
123+
assert len(partition[2]) == 2

tests/paige_tarjan/paige_tarjan_test_cases.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,25 @@
176176
graph10 = nx.DiGraph()
177177
graph10.add_nodes_from(range(3))
178178
graph10.add_edges_from([(0, 1), (0, 2), (1, 0), (1, 2)])
179-
graph_partition_tuples.append((graph10, initial_partitions[len(graph10.nodes)]))
179+
graph_partition_tuples.append(
180+
(graph10, initial_partitions[len(graph10.nodes)])
181+
)
180182

181183
# 11
182184
graph11 = nx.DiGraph()
183185
graph11.add_nodes_from(range(3))
184186
graph11.add_edges_from([(0, 1), (1, 2), (2, 0), (2, 1)])
185-
graph_partition_tuples.append((graph11, initial_partitions[len(graph11.nodes)]))
187+
graph_partition_tuples.append(
188+
(graph11, initial_partitions[len(graph11.nodes)])
189+
)
186190

187191
# 12
188192
graph12 = nx.DiGraph()
189193
graph12.add_nodes_from(range(3))
190194
graph12.add_edges_from([(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)])
191-
graph_partition_tuples.append((graph12, initial_partitions[len(graph12.nodes)]))
195+
graph_partition_tuples.append(
196+
(graph12, initial_partitions[len(graph12.nodes)])
197+
)
192198

193199
graph_partition_rscp_tuples = []
194200

tests/paige_tarjan/rscp_utilities.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from bispy.utilities.graph_entities import _Vertex, _QBlock
55

6+
67
# check if the given partition is stable with respect to the given block, or if
78
# it's stable if the block isn't given
89
def is_stable_vertexes_partition(partition: List[List[_Vertex]]) -> bool:

tests/paige_tarjan/test_paige_tarjan.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
paige_tarjan_qblocks,
3131
preprocess_initial_partition,
3232
)
33-
from bispy.utilities.graph_decorator import decorate_nx_graph
33+
from bispy.utilities.graph_decorator import decorate_nx_graph, to_set
3434
import tests.paige_tarjan.paige_tarjan_test_cases as test_cases
3535

3636

@@ -210,7 +210,8 @@ def test_build_block_counterimage_aux_count(graph, initial_partition):
210210
vertex.aux_count = None
211211

212212

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

229230

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

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

254256
qblock_splitter = q_partition[0]
255-
# qblock_splitter may be modified by split, therefore we need to keep a copy
257+
# qblock_splitter may be modified by split, therefore we need to keep a
258+
# copy
256259
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]
257260

258261
block_counterimage = build_block_counterimage(qblock_splitter)
259262
split(block_counterimage)
260263

261-
# after split the partition should be stable with respect to the block chosen for the split
264+
# after split the partition should be stable with respect to the block
265+
# chosen for the split
262266
for qblock in xblock.qblocks:
263267
assert check_vertexes_stability(
264268
[vertex for vertex in qblock.vertexes], splitter_vertexes
265269
)
266270

267-
# test if the size of the qblocks after the split is equal to the number of vertexes
271+
# test if the size of the qblocks after the split is equal to the number of
272+
# vertexes
268273
for qblock in xblock.qblocks:
269274
assert qblock.size == len(qblock.vertexes)
270275

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

285291
for new_block in new_blocks:
286-
assert any([qblock == new_block for qblock in new_block.xblock.qblocks])
292+
assert any(
293+
[qblock == new_block for qblock in new_block.xblock.qblocks]
294+
)
287295

288296
for old_block in q_partition:
289297
assert old_block.size == 0 or any(
290298
[qblock == old_block for qblock in old_block.xblock.qblocks]
291299
)
292300

293301

294-
# 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)
302+
# second_splitter should be E^{-1}(B) - E^{-1}(S-B), namely there should only
303+
# be vertexes in E^{-1}(B) but not in E^{-1}(S-B)
295304
@pytest.mark.parametrize(
296305
"graph, initial_partition", test_cases.graph_partition_tuples
297306
)
@@ -301,7 +310,8 @@ def test_second_splitter_counterimage(graph, initial_partition):
301310

302311
qblock_splitter = q_partition[0]
303312

304-
# qblock_splitter may be modified by split, therefore we need to keep a copy
313+
# qblock_splitter may be modified by split, therefore we need to keep a
314+
# copy
305315
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]
306316

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

336346
qblock_splitter = q_partition[0]
337-
# qblock_splitter may be modified by split, therefore we need to keep a copy
347+
# qblock_splitter may be modified by split, therefore we need to keep a
348+
# copy
338349
splitter_vertexes = [vertex for vertex in qblock_splitter.vertexes]
339350

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

355-
# after split the partition should be stable with respect to the block chosen for the split
366+
# after split the partition should be stable with respect to the block
367+
# chosen for the split
356368
for qblock in xblock.qblocks:
357369
assert check_vertexes_stability(
358370
[vertex for vertex in qblock.vertexes], second_splitter_vertexes
@@ -390,7 +402,7 @@ def test_reset_aux_count_after_refinement(graph, initial_partition):
390402
refine([xblock], [xblock])
391403

392404
for vertex in vertexes:
393-
assert vertex.aux_count == None
405+
assert vertex.aux_count is None
394406

395407

396408
def test_count_after_refinement():
@@ -451,7 +463,7 @@ def test_no_negative_edge_counts(graph, initial_partition):
451463

452464
for vertex in vertexes:
453465
for edge in vertex.image:
454-
assert edge.count == None or edge.count.value > 0
466+
assert edge.count is None or edge.count.value > 0
455467

456468

457469
@pytest.mark.parametrize(
@@ -557,12 +569,3 @@ def test_pt_same_initial_partition(graph, initial_partition):
557569
vertex.initial_partition_block_id
558570
== vertex_to_initial_partition_id[vertex.label]
559571
)
560-
561-
562-
def test_simp():
563-
graph = nx.DiGraph()
564-
graph.add_nodes_from(range(2))
565-
graph.add_edges_from([(0, 1)])
566-
initial_partition = [(0, 1)]
567-
568-
x = paige_tarjan(graph, is_integer_graph=True)

0 commit comments

Comments
 (0)