From d209e3094e69de36d9c730101a7628038a6ddf97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlson=20B=C3=BCth?= Date: Sun, 31 Mar 2024 17:25:24 +0200 Subject: [PATCH] Fix tests - int32 sparse graph Format black --- superblockify/__init__.py | 1 + tests/metrics/test_measures.py | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/superblockify/__init__.py b/superblockify/__init__.py index 2101c02..7f22cab 100644 --- a/superblockify/__init__.py +++ b/superblockify/__init__.py @@ -1,4 +1,5 @@ """superblockify init.""" + from os import environ environ["USE_PYGEOS"] = "0" # pylint: disable=wrong-import-position diff --git a/tests/metrics/test_measures.py b/tests/metrics/test_measures.py index 7ed5787..720b202 100644 --- a/tests/metrics/test_measures.py +++ b/tests/metrics/test_measures.py @@ -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( @@ -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", [ @@ -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( @@ -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), @@ -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(