Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 5ac6de7

Browse files
Fix tests for numpy>=2 (#267)
* Fix tests for numpy>=2 * only set legacy print mode if numpy >= 2 * Change how np.printoptions is used * add changelog entry * change deprecated 'np.in1d' to 'np.isin'
1 parent d7ca855 commit 5ac6de7

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Version v3.1.0
66

77
Improvements
88
~~~~~~~~~~~~
9+
- Support for ``numpy>=2``
910
- Made ``Edges`` and ``EdgePopulation`` ``get`` functions more consistent
1011

1112
- Both now return ``self.ids(query)`` if ``properties=None``

bluepysnap/circuit_validation.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import logging
7+
from contextlib import contextmanager
78
from pathlib import Path
89

910
import h5py
@@ -21,6 +22,13 @@
2122
MAX_MISSING_FILES_DISPLAY = 10
2223

2324

25+
@contextmanager
26+
def _np_printoptions():
27+
"""Get numpy print options."""
28+
with np.printoptions(legacy=("1.25" if np.version.version >= "2.0.0" else None)):
29+
yield
30+
31+
2432
def _check_partial_circuit_config(config):
2533
return config.get("metadata", {}).get("status") == "partial"
2634

@@ -347,11 +355,12 @@ def _check_edges_node_ids(nodes_ds, nodes):
347355
if node_ids.size > 0:
348356
missing_ids = sorted(set(nodes_ds[:]) - set(node_ids))
349357
if missing_ids:
350-
errors.append(
351-
BluepySnapValidationError.fatal(
352-
f"{nodes_ds.name} misses node ids in its node population: {missing_ids}"
358+
with _np_printoptions():
359+
errors.append(
360+
BluepySnapValidationError.fatal(
361+
f"{nodes_ds.name} misses node ids in its node population: {missing_ids}"
362+
)
353363
)
354-
)
355364
elif f"nodes/{node_population_name}" in h5f:
356365
errors.append(
357366
BluepySnapValidationError.fatal(
@@ -386,12 +395,13 @@ def _check(indices, nodes_ds):
386395
edges_range = node_to_edges_ranges[nodes_range[0] : nodes_range[1]][0]
387396
edge_node_ids = list(set(nodes_ds[edges_range[0] : edges_range[1]]))
388397
if len(edge_node_ids) > 1 or edge_node_ids[0] != node_id:
389-
errors.append(
390-
BluepySnapValidationError.fatal(
391-
f"Population {population.file.filename} edges {edge_node_ids} have "
392-
f"node ids {edges_range} instead of single id {node_id}"
398+
with _np_printoptions():
399+
errors.append(
400+
BluepySnapValidationError.fatal(
401+
f"Population {population.file.filename} edges {edge_node_ids} have "
402+
f"node ids {edges_range} instead of single id {node_id}"
403+
)
393404
)
394-
)
395405

396406
errors = []
397407
indices = population["indices"]

bluepysnap/edges/edge_population.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def _optimal_direction():
506506
# np.stack(uint64, int64) -> float64
507507
connected_node_ids_with_count = connected_node_ids_with_count.astype(np.uint32)
508508
if secondary_node_ids is not None:
509-
mask = np.in1d(
509+
mask = np.isin(
510510
connected_node_ids_with_count[:, 0], secondary_node_ids, assume_unique=True
511511
)
512512
connected_node_ids_with_count = connected_node_ids_with_count[mask]

tests/test_edges.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ def test_get(self):
233233
tested = self.test_obj.get(ids, properties=["other2", "other1", "@source_node"])
234234
expected = pd.DataFrame(
235235
{
236-
"other2": np.array([np.NaN, np.NaN, np.NaN, np.NaN, 10, 11, 12, 13], dtype=float),
236+
"other2": np.array([np.nan, np.nan, np.nan, np.nan, 10, 11, 12, 13], dtype=float),
237237
"other1": np.array(
238-
[np.NaN, np.NaN, np.NaN, np.NaN, "A", "B", "C", "D"], dtype=object
238+
[np.nan, np.nan, np.nan, np.nan, "A", "B", "C", "D"], dtype=object
239239
),
240240
"@source_node": np.array([2, 0, 0, 2, 2, 0, 0, 2], dtype=int),
241241
},
@@ -329,7 +329,7 @@ def test_get(self):
329329
tested = self.test_obj.get(ids, properties="other2")
330330
expected = pd.DataFrame(
331331
{
332-
"other2": np.array([np.NaN, np.NaN, np.NaN, np.NaN, 10, 11, 12, 13], dtype=float),
332+
"other2": np.array([np.nan, np.nan, np.nan, np.nan, 10, 11, 12, 13], dtype=float),
333333
},
334334
index=pd.MultiIndex.from_tuples(
335335
[

tests/test_nodes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,8 @@ def test_get(self):
286286
tested = pd.concat(df for _, df in tested)
287287
expected = pd.DataFrame(
288288
{
289-
"other2": np.array([np.NaN, np.NaN, np.NaN, 10, 11, 12, 13], dtype=float),
290-
"other1": np.array([np.NaN, np.NaN, np.NaN, "A", "B", "C", "D"], dtype=object),
289+
"other2": np.array([np.nan, np.nan, np.nan, 10, 11, 12, 13], dtype=float),
290+
"other1": np.array([np.nan, np.nan, np.nan, "A", "B", "C", "D"], dtype=object),
291291
"layer": np.array([2, 6, 6, 7, 8, 8, 2], dtype=int),
292292
},
293293
index=pd.MultiIndex.from_tuples(
@@ -373,7 +373,7 @@ def test_get(self):
373373
tested = self.test_obj.get(properties="other2")
374374
expected = pd.DataFrame(
375375
{
376-
"other2": np.array([np.NaN, np.NaN, np.NaN, 10, 11, 12, 13], dtype=float),
376+
"other2": np.array([np.nan, np.nan, np.nan, 10, 11, 12, 13], dtype=float),
377377
},
378378
index=pd.MultiIndex.from_tuples(
379379
[

0 commit comments

Comments
 (0)