From d1ce83d32a75487b5f30320f648fadd8c797dd00 Mon Sep 17 00:00:00 2001 From: Frank Loesche Date: Mon, 16 Dec 2024 11:11:53 -0500 Subject: [PATCH] change numpy representations just within the test instead of setting an environment-specific number representation within numpy, this commit sets the number representation per test case and with a suggestion on how to change it once numpy>=2.0 is the minimal requirement. --- navis/core/base.py | 5 +++++ navis/core/skeleton.py | 5 +++++ navis/graph/graph_utils.py | 5 +++++ navis/intersection/intersect.py | 7 ++++++ navis/morpho/mmetrics.py | 38 ++++++++++++++++++++++++++++++++- navis/nbl/utils.py | 5 +++++ 6 files changed, 64 insertions(+), 1 deletion(-) diff --git a/navis/core/base.py b/navis/core/base.py index 73bf2bc4..5ea9e131 100644 --- a/navis/core/base.py +++ b/navis/core/base.py @@ -608,7 +608,12 @@ def convert_units( Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.float32(266476.88)` instead of `266476.8`) >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> n.units diff --git a/navis/core/skeleton.py b/navis/core/skeleton.py index 5bcab701..57478416 100644 --- a/navis/core/skeleton.py +++ b/navis/core/skeleton.py @@ -1422,7 +1422,12 @@ def snap(self, locs, to='nodes'): Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int32(1124)` instead of `1124`) >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> id, dist = n.snap([0, 0, 0]) >>> id diff --git a/navis/graph/graph_utils.py b/navis/graph/graph_utils.py index 958a095b..3518a20b 100644 --- a/navis/graph/graph_utils.py +++ b/navis/graph/graph_utils.py @@ -1078,7 +1078,12 @@ def find_main_branchpoint( Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int32(110)` instead of `110`) >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> navis.find_main_branchpoint(n, reroot_soma=True) 110 diff --git a/navis/intersection/intersect.py b/navis/intersection/intersect.py index 7462de8c..c03d66d5 100644 --- a/navis/intersection/intersect.py +++ b/navis/intersection/intersect.py @@ -198,7 +198,14 @@ def in_volume(x: Union['core.NeuronObject', Sequence, pd.DataFrame], -------- Prune neuron to volume + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `array([False, False, False, ..., False, False, False], shape=(4465,))` + instead of `array([False, False, False, ..., False, False, False])`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> lh = navis.example_volume('LH') >>> n_lh = navis.in_volume(n, lh, inplace=False) diff --git a/navis/morpho/mmetrics.py b/navis/morpho/mmetrics.py index 93a4feca..cd4721b1 100644 --- a/navis/morpho/mmetrics.py +++ b/navis/morpho/mmetrics.py @@ -147,7 +147,13 @@ def strahler_index( Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int16(6)` instead of `91234`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(2, kind='skeleton') >>> n.reroot(n.soma, inplace=True) >>> _ = navis.strahler_index(n) @@ -550,7 +556,13 @@ def arbor_segregation_index(x: "core.NeuronObject") -> "core.NeuronObject": Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.float64(0.277)` instead of `0.277`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> n.reroot(n.soma, inplace=True) >>> _ = navis.arbor_segregation_index(n) @@ -699,7 +711,13 @@ def bending_flow(x: "core.NeuronObject") -> "core.NeuronObject": Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int64(785645)` instead of `785645`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> n.reroot(n.soma, inplace=True) >>> _ = navis.bending_flow(n) @@ -969,7 +987,13 @@ def synapse_flow_centrality( Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int64(786969)` instead of `786969`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(2) >>> n.reroot(n.soma, inplace=True) >>> _ = navis.synapse_flow_centrality(n) @@ -1174,7 +1198,13 @@ def flow_centrality(x: "core.NeuronObject") -> "core.NeuronObject": Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int64(91234)` instead of `91234`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(2) >>> n.reroot(n.soma, inplace=True) >>> _ = navis.flow_centrality(n) @@ -1326,7 +1356,7 @@ def tortuosity( should become `np.float64(1.074)` instead of `1.074`) >>> import navis >>> if int(np.version.version.split('.')[0])>=2: - np.set_printoptions(legacy="1.25") + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(1) >>> # Calculate tortuosity as-is >>> T = navis.tortuosity(n) @@ -1635,7 +1665,13 @@ def betweeness_centrality( Examples -------- + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.int64(436866)` instead of `436866`) + >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> n = navis.example_neurons(2, kind='skeleton') >>> n.reroot(n.soma, inplace=True) >>> _ = navis.betweeness_centrality(n) diff --git a/navis/nbl/utils.py b/navis/nbl/utils.py index 73cba417..8b54fcae 100644 --- a/navis/nbl/utils.py +++ b/navis/nbl/utils.py @@ -199,7 +199,12 @@ def update_scores(queries, targets, scores_ex, nblast_func, **kwargs): Mostly for testing but also illustrates the principle: + (Set up numpy number representation within the test, see NEP51. + Once numpy<=2 is dropped from requirements, the doctest comparissons + should become `np.True_` instead of `True`) >>> import navis + >>> if int(np.version.version.split('.')[0])>=2: + np.set_printoptions(legacy="1.25") >>> import numpy as np >>> nl = navis.example_neurons(n=5) >>> dp = navis.make_dotprops(nl, k=5) / 125