Skip to content

Commit

Permalink
Fix tests - int32 sparse graph
Browse files Browse the repository at this point in the history
Format black
  • Loading branch information
cbueth committed Apr 8, 2024
1 parent f31fa19 commit d209e30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions superblockify/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""superblockify init."""

from os import environ

environ["USE_PYGEOS"] = "0" # pylint: disable=wrong-import-position
Expand Down
25 changes: 23 additions & 2 deletions tests/metrics/test_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
__calculate_high_bc_anisotropy,
add_relative_changes,
)
from superblockify.utils import __edges_to_1d, percentual_increase
from superblockify.utils import __edges_to_1d, percentual_increase, logger


@pytest.mark.parametrize(
Expand Down Expand Up @@ -292,6 +292,25 @@ def test_betweenness_centrality_options(
)


def _downcast_(sparse_graph):
# Try to downcast indices to int32
if sparse_graph.indices.dtype != int32:
logger.debug("Downcasting indices to int32.")
downcasted_indices = sparse_graph.indices.astype(int32)
if array_equal(downcasted_indices, sparse_graph.indices):
sparse_graph.indices = downcasted_indices
else:
logger.warning("Downcasting indices to int32 failed.")
# Try to downcast indptr to int32
if sparse_graph.indptr.dtype != int32:
logger.debug("Downcasting indptr to int32.")
downcasted_indptr = sparse_graph.indptr.astype(int32)
if array_equal(downcasted_indptr, sparse_graph.indptr):
sparse_graph.indptr = downcasted_indptr
else:
logger.warning("Downcasting indptr to int32 failed.")


@pytest.mark.parametrize(
"graph,expected",
[
Expand Down Expand Up @@ -420,6 +439,7 @@ def test_calculate_betweenness_scales(graph, expected):
"""Test calculation of edge betweenness with scaled results of toy graphs."""
set_edge_attributes(graph, 1, "weight")
sparse_graph = to_scipy_sparse_array(graph, nodelist=sorted(graph))
_downcast_(sparse_graph)
dist, pred = dijkstra(sparse_graph, return_predecessors=True, directed=True)
betweenness_centrality(graph, list(sorted(graph)), dist, pred, weight="weight")
assert array_equal(
Expand Down Expand Up @@ -542,6 +562,7 @@ def test_calculate_betweenness_scales(graph, expected):
def test__calculate_betweenness_unscaled(graph, expected):
"""Test calculation of betweenness centrality graph to dict, unscaled."""
sparse_graph = to_scipy_sparse_array(graph, nodelist=sorted(graph))
_downcast_(sparse_graph)
padding = len(str(len(graph)))
edges = __edges_to_1d(
array([u for u, _ in graph.edges(keys=False)], dtype=int32),
Expand Down Expand Up @@ -708,7 +729,7 @@ def test___calculate_high_bc_anisotropy(
anisotropy = __calculate_high_bc_anisotropy(coord_high_bc)
assert 1.0 <= anisotropy
# check invariance to x and y coordinate swap
assert __calculate_high_bc_anisotropy(coord_high_bc[:, ::-1]) == anisotropy
assert isclose(__calculate_high_bc_anisotropy(coord_high_bc[:, ::-1]), anisotropy)


@pytest.mark.parametrize(
Expand Down

0 comments on commit d209e30

Please sign in to comment.