Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Use NumPy random generators #62

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions tract_querier/tensor/tests/test_scalar_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from numpy.testing import assert_array_almost_equal


def test_fractional_anisotropy(N=10, random=numpy.random.RandomState(0)):
tensors = random.randn(N, 3, 3)
def test_fractional_anisotropy(N=10, random=numpy.random.default_rng(0)):
tensors = random.standard_normal((N, 3, 3))
fa = numpy.empty(N)
for i, t in enumerate(tensors):
tt = numpy.dot(t, t.T)
Expand All @@ -17,8 +17,8 @@ def test_fractional_anisotropy(N=10, random=numpy.random.RandomState(0)):
assert_array_almost_equal(fa, scalar_measures.fractional_anisotropy(tensors))


def test_volume_fraction(N=10, random=numpy.random.RandomState(0)):
tensors = random.randn(N, 3, 3)
def test_volume_fraction(N=10, random=numpy.random.default_rng(0)):
tensors = random.standard_normal((N, 3, 3))
vf = numpy.empty(N)
for i, t in enumerate(tensors):
tt = numpy.dot(t, t.T)
Expand All @@ -30,8 +30,8 @@ def test_volume_fraction(N=10, random=numpy.random.RandomState(0)):
assert_array_almost_equal(vf, scalar_measures.volume_fraction(tensors))


def test_tensor_determinant(N=10, random=numpy.random.RandomState(0)):
tensors = random.randn(N, 3, 3)
def test_tensor_determinant(N=10, random=numpy.random.default_rng(0)):
tensors = random.standard_normal((N, 3, 3))
dt = numpy.empty(N)
for i, t in enumerate(tensors):
tt = numpy.dot(t, t.T)
Expand All @@ -41,8 +41,8 @@ def test_tensor_determinant(N=10, random=numpy.random.RandomState(0)):
assert_array_almost_equal(dt, scalar_measures.tensor_det(tensors))


def test_tensor_traces(N=10, random=numpy.random.RandomState(0)):
tensors = random.randn(N, 3, 3)
def test_tensor_traces(N=10, random=numpy.random.default_rng(0)):
tensors = random.standard_normal((N, 3, 3))
res = numpy.empty(N)
for i, t in enumerate(tensors):
tt = numpy.dot(t, t.T)
Expand All @@ -52,9 +52,9 @@ def test_tensor_traces(N=10, random=numpy.random.RandomState(0)):
assert_array_almost_equal(res, scalar_measures.tensor_trace(tensors))


def test_tensor_contraction(N=10, random=numpy.random.RandomState(0)):
tensors1 = random.randn(N, 3, 3)
tensors2 = random.randn(N, 3, 3)
def test_tensor_contraction(N=10, random=numpy.random.default_rng(0)):
tensors1 = random.standard_normal((N, 3, 3))
tensors2 = random.standard_normal((N, 3, 3))

res = numpy.empty(N)
for i in range(N):
Expand Down
6 changes: 4 additions & 2 deletions tract_querier/tests/test_query_eval.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from .. import query_processor

from numpy import random
import ast
import numpy as np


# Ten tracts traversing random labels
another_set = True
while (another_set):
tracts_labels = dict([(i, set(random.randint(100, size=2))) for i in range(100)])
rng = np.random.default_rng()
tracts_labels = dict([(i, set(rng.integers(100, size=2))) for i in range(100)])
labels_tracts = query_processor.labels_for_tracts(tracts_labels)
another_set = 0 not in labels_tracts.keys() or 1 not in labels_tracts.keys()

Expand Down
5 changes: 3 additions & 2 deletions tract_querier/tract_math/tract_obb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,14 @@ def draw_box_2d(obbs, **args):


def draw_box_3d(obbs, tube_radius=1, color=None, **kwargs):
import numpy as np
from mayavi.mlab import plot3d
from numpy.random import rand
rng = np.random.default_rng(1234)
if isinstance(obbs, Box2D):
obbs = [obbs]
for obb in obbs:
if color is None:
color_ = tuple(rand(3))
color_ = tuple(rng.random(3))
else:
color_ = color
box = obb.box
Expand Down
11 changes: 6 additions & 5 deletions tract_querier/tractography/tests/test_tractography.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import copy
from itertools import chain

import numpy as np
from numpy import all, eye, ones, allclose
from numpy.random import randint, randn
from numpy.testing import assert_array_equal

dimensions = None
Expand Down Expand Up @@ -72,14 +72,15 @@ def setup_module(*args, **kwargs):
else:
test_active_data = False

dimensions = [(randint(5, max_tract_length), 3) for _ in range(n_tracts)]
tracts = [randn(*d) for d in dimensions]
rng = np.random.default_rng(1234)
dimensions = [(rng.integers(5, max_tract_length), 3) for _ in range(n_tracts)]
tracts = [rng.standard_normal(d) for d in dimensions]
tracts_data = {
'a%d' % i: [
randn(d[0], k)
rng.standard_normal((d[0], k))
for d in dimensions
]
for i, k in zip(range(4), randint(1, 3, 9))
for i, k in zip(range(4), rng.integers(1, 3, 9))
}

if test_active_data:
Expand Down
Loading