Skip to content

Commit a810207

Browse files
committed
Completed first draft of numpy migration
1 parent 3f9e69c commit a810207

49 files changed

Lines changed: 1073 additions & 1176 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

stumpy/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3717,7 +3717,8 @@ def check_ignore_trivial(T_A, T_B, ignore_trivial):
37173717
import numpy as np
37183718
import warnings
37193719
3720-
T = np.random.rand(10_000)
3720+
rng = np.random.default_rng()
3721+
T = rng.random(10_000)
37213722
m = 50
37223723
with warnings.catch_warnings():
37233724
warnings.filterwarnings("ignore", message="Arrays T_A, T_B are equal")
@@ -4493,7 +4494,8 @@ def check_self_join(ignore_trivial):
44934494
import numpy as np
44944495
import warnings
44954496
4496-
T = np.random.rand(10_000)
4497+
rng = np.random.default_rng()
4498+
T = rng.random(10_000)
44974499
m = 50
44984500
with warnings.catch_warnings():
44994501
warnings.filterwarnings("ignore", message="`ignore_trivial` cannot be `False`")

stumpy/floss.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ def _iac(
8484
IAC : numpy.ndarray
8585
Idealized arc curve (IAC)
8686
"""
87-
np.random.seed(seed)
87+
local_rng = np.random.default_rng(seed)
8888

89-
I = np.random.randint(0, width, size=width, dtype=np.int64)
89+
I = local_rng.integers(0, width, size=width, dtype=np.int64)
9090
if bidirectional is False: # Idealized 1-dimensional matrix profile index
9191
I[:-1] = width
9292
for i in range(width - 1):
93-
I[i] = np.random.randint(i + 1, width, dtype=np.int64)
93+
I[i] = local_rng.integers(i + 1, width, dtype=np.int64)
9494

9595
target_AC = _nnmark(I)
9696

@@ -99,7 +99,7 @@ def _iac(
9999
hist_dist = scipy.stats.rv_histogram(
100100
(target_AC, np.append(np.arange(width), width))
101101
)
102-
data = hist_dist.rvs(size=n_samples)
102+
data = hist_dist.rvs(size=n_samples, random_state=local_rng)
103103
a, b, c, d = scipy.stats.beta.fit(data, floc=0, fscale=width)
104104

105105
params[i, 0] = a

stumpy/rng.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from contextlib import contextmanager
2+
13
import numpy as np
24

35
bit_gen = np.random.PCG64()
@@ -85,7 +87,28 @@ def _fix_state():
8587
_set_state(FIXED_STATE)
8688

8789

90+
@contextmanager
91+
def fix_state(*args):
92+
"""
93+
A context manager for setting the RNG state to a fixed, hardcoded, safe state
94+
95+
This is exceptionally rare and you probably want to use `state = get_state()`
96+
and `set_state(state)` instead.
97+
98+
Parameters
99+
----------
100+
None
101+
102+
Returns
103+
-------
104+
None
105+
"""
106+
_fix_state()
107+
try:
108+
yield
109+
finally:
110+
_reset_state()
111+
112+
88113
get_state = _get_state
89114
set_state = _set_state
90-
fix_state = _fix_state
91-
unfix_state = _reset_state

stumpy/scraamp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
from numba import njit, prange
88

9-
from . import config, core
9+
from . import config, core, rng
1010
from .aamp import _aamp
1111

1212

@@ -78,7 +78,7 @@ def _preprocess_prescraamp(T_A, m, T_B=None, s=None):
7878
else: # AB-join
7979
s = int(np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM))
8080

81-
indices = np.random.permutation(range(0, l, s)).astype(np.int64)
81+
indices = rng.RNG.permutation(range(0, l, s)).astype(np.int64)
8282

8383
return (T_A, T_B, T_A_subseq_isfinite, T_B_subseq_isfinite, indices, s, excl_zone)
8484

@@ -718,7 +718,7 @@ def __init__(
718718
core._merge_topk_PI(self._P, P, self._I, I)
719719

720720
if self._ignore_trivial:
721-
self._diags = np.random.permutation(
721+
self._diags = rng.RNG.permutation(
722722
range(self._excl_zone + 1, self._n_A - self._m + 1)
723723
).astype(np.int64)
724724
if self._diags.shape[0] == 0: # pragma: no cover
@@ -728,7 +728,7 @@ def __init__(
728728
f"Please try a value of `m <= {max_m}`"
729729
)
730730
else:
731-
self._diags = np.random.permutation(
731+
self._diags = rng.RNG.permutation(
732732
range(-(self._n_A - self._m + 1) + 1, self._n_B - self._m + 1)
733733
).astype(np.int64)
734734

stumpy/scrump.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
from numba import njit, prange
88

9-
from . import config, core, sdp
9+
from . import config, core, rng, sdp
1010
from .scraamp import prescraamp, scraamp
1111
from .stump import _stump
1212

@@ -116,7 +116,7 @@ def _preprocess_prescrump(
116116
else: # AB-join
117117
s = int(np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM))
118118

119-
indices = np.random.permutation(range(0, l, s)).astype(np.int64)
119+
indices = rng.RNG.permutation(range(0, l, s)).astype(np.int64)
120120

121121
return (
122122
T_A,
@@ -378,6 +378,7 @@ def _compute_PI(
378378
P_squared[thread_idx, j], idx, squared_distance_profile[j]
379379
)
380380
core._shift_insert_at_index(I[thread_idx, j], idx, i)
381+
# core._shift_insert_at_index(I[thread_idx, j, :idx], idx, i)
381382

382383

383384
@njit(
@@ -997,7 +998,7 @@ def __init__(
997998
core._merge_topk_PI(self._P, P, self._I, I)
998999

9991000
if self._ignore_trivial:
1000-
self._diags = np.random.permutation(
1001+
self._diags = rng.RNG.permutation(
10011002
range(self._excl_zone + 1, self._n_A - self._m + 1)
10021003
).astype(np.int64)
10031004
if self._diags.shape[0] == 0: # pragma: no cover
@@ -1007,7 +1008,7 @@ def __init__(
10071008
f"Please try a value of `m <= {max_m}`"
10081009
)
10091010
else:
1010-
self._diags = np.random.permutation(
1011+
self._diags = rng.RNG.permutation(
10111012
range(-(self._n_A - self._m + 1) + 1, self._n_B - self._m + 1)
10121013
).astype(np.int64)
10131014

tests/naive.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from scipy.spatial.distance import cdist
55
from scipy.stats import norm
66

7-
from stumpy import config, core
7+
from stumpy import config, core, rng
88

99

1010
def is_ptp_zero_1d(a, w): # `a` is 1-D
@@ -1818,7 +1818,7 @@ def prescrump(
18181818
P = np.full((l, k), np.inf, dtype=np.float64)
18191819
I = np.full((l, k), -1, dtype=np.int64)
18201820

1821-
for i in np.random.permutation(range(0, l, s)):
1821+
for i in rng.RNG.permutation(range(0, l, s)):
18221822
distance_profile = dist_matrix[i]
18231823
if exclusion_zone is not None:
18241824
apply_exclusion_zone(distance_profile, i, exclusion_zone, np.inf)
@@ -1914,11 +1914,11 @@ def scrump(
19141914
pass
19151915

19161916
if exclusion_zone is not None:
1917-
diags = np.random.permutation(range(exclusion_zone + 1, n_A - m + 1)).astype(
1917+
diags = rng.RNG.permutation(range(exclusion_zone + 1, n_A - m + 1)).astype(
19181918
np.int64
19191919
)
19201920
else:
1921-
diags = np.random.permutation(range(-(n_A - m + 1) + 1, n_B - m + 1)).astype(
1921+
diags = rng.RNG.permutation(range(-(n_A - m + 1) + 1, n_B - m + 1)).astype(
19221922
np.int64
19231923
)
19241924

@@ -1981,7 +1981,7 @@ def prescraamp(T_A, m, T_B, s, exclusion_zone=None, p=2.0, k=1):
19811981
P = np.full((l, k), np.inf, dtype=np.float64)
19821982
I = np.full((l, k), -1, dtype=np.int64)
19831983

1984-
for i in np.random.permutation(range(0, l, s)):
1984+
for i in rng.RNG.permutation(range(0, l, s)):
19851985
distance_profile = distance_matrix[i]
19861986
if exclusion_zone is not None:
19871987
apply_exclusion_zone(distance_profile, i, exclusion_zone, np.inf)
@@ -2054,11 +2054,11 @@ def scraamp(T_A, m, T_B, percentage, exclusion_zone, pre_scraamp, s, p=2.0, k=1)
20542054
l = n_A - m + 1
20552055

20562056
if exclusion_zone is not None:
2057-
diags = np.random.permutation(range(exclusion_zone + 1, n_A - m + 1)).astype(
2057+
diags = rng.RNG.permutation(range(exclusion_zone + 1, n_A - m + 1)).astype(
20582058
np.int64
20592059
)
20602060
else:
2061-
diags = np.random.permutation(range(-(n_A - m + 1) + 1, n_B - m + 1)).astype(
2061+
diags = rng.RNG.permutation(range(-(n_A - m + 1) + 1, n_B - m + 1)).astype(
20622062
np.int64
20632063
)
20642064

tests/test_aamp.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pandas as pd
55
import pytest
66

7-
from stumpy import config
7+
from stumpy import config, rng
88
from stumpy.aamp import aamp
99

1010
test_data = [
@@ -13,8 +13,8 @@
1313
np.array([584, -11, 23, 79, 1001, 0, -19], dtype=np.float64),
1414
),
1515
(
16-
np.random.uniform(-1000, 1000, [8]).astype(np.float64),
17-
np.random.uniform(-1000, 1000, [64]).astype(np.float64),
16+
rng.RNG.uniform(-1000, 1000, size=8).astype(np.float64),
17+
rng.RNG.uniform(-1000, 1000, size=64).astype(np.float64),
1818
),
1919
]
2020

@@ -72,7 +72,7 @@ def test_aamp_constant_subsequence_self_join():
7272

7373

7474
def test_aamp_one_constant_subsequence_A_B_join():
75-
T_A = np.random.rand(20)
75+
T_A = rng.RNG.random(20)
7676
T_B = np.concatenate((np.zeros(20, dtype=np.float64), np.ones(5, dtype=np.float64)))
7777
m = 3
7878
ref_mp = naive.aamp(T_A, m, T_B=T_B)
@@ -122,8 +122,8 @@ def test_aamp_two_constant_subsequences_A_B_join():
122122

123123

124124
def test_aamp_identical_subsequence_self_join():
125-
identical = np.random.rand(8)
126-
T_A = np.random.rand(20)
125+
identical = rng.RNG.random(8)
126+
T_A = rng.RNG.random(20)
127127
T_A[1 : 1 + identical.shape[0]] = identical
128128
T_A[11 : 11 + identical.shape[0]] = identical
129129
m = 3
@@ -143,9 +143,9 @@ def test_aamp_identical_subsequence_self_join():
143143

144144

145145
def test_aamp_identical_subsequence_A_B_join():
146-
identical = np.random.rand(8)
147-
T_A = np.random.rand(20)
148-
T_B = np.random.rand(20)
146+
identical = rng.RNG.random(8)
147+
T_A = rng.RNG.random(20)
148+
T_B = rng.RNG.random(20)
149149
T_A[1 : 1 + identical.shape[0]] = identical
150150
T_B[11 : 11 + identical.shape[0]] = identical
151151
m = 3

tests/test_aamp_mmotifs.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy.testing as npt
44
import pytest
55

6-
from stumpy import config
6+
from stumpy import config, rng
77
from stumpy.aamp_mmotifs import aamp_mmotifs
88

99
test_data = [
@@ -51,32 +51,30 @@
5151

5252

5353
def test_aamp_mmotifs_default_parameters():
54-
motif_distances_ref = np.array(
55-
[[0.0, 0.06315749, 0.25275899, 0.34087884, 0.3452315]]
56-
)
57-
motif_indices_ref = np.array([[19, 77, 63, 52, 71]])
58-
motif_subspaces_ref = [np.array([2])]
54+
motif_distances_ref = np.array([[0.0, 0.10660435, 0.27927693]])
55+
motif_indices_ref = np.array([[19, 41, 34]])
56+
motif_subspaces_ref = [np.array([1])]
5957
motif_mdls_ref = [
60-
np.array([411.60964047, 423.69925001, 449.11032383, 476.95855027, 506.62406252])
58+
np.array([411.60964047, 433.21928095, 429.77443751, 457.74925683, 477.80793557])
6159
]
6260

63-
np.random.seed(0)
64-
T = np.random.rand(500).reshape(5, 100)
65-
66-
m = 5
67-
excl_zone = int(np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM))
68-
P, I = naive.maamp(T, m, excl_zone)
69-
(
70-
motif_distances_cmp,
71-
motif_indices_cmp,
72-
motif_subspaces_cmp,
73-
motif_mdls_cmp,
74-
) = aamp_mmotifs(T, P, I)
75-
76-
npt.assert_array_almost_equal(motif_distances_ref, motif_distances_cmp)
77-
npt.assert_array_almost_equal(motif_indices_ref, motif_indices_cmp)
78-
npt.assert_array_almost_equal(motif_subspaces_ref, motif_subspaces_cmp)
79-
npt.assert_array_almost_equal(motif_mdls_ref, motif_mdls_cmp)
61+
with rng.fix_state():
62+
T = rng.RNG.random(size=(5, 100))
63+
64+
m = 5
65+
excl_zone = int(np.ceil(m / config.STUMPY_EXCL_ZONE_DENOM))
66+
P, I = naive.maamp(T, m, excl_zone)
67+
(
68+
motif_distances_cmp,
69+
motif_indices_cmp,
70+
motif_subspaces_cmp,
71+
motif_mdls_cmp,
72+
) = aamp_mmotifs(T, P, I)
73+
74+
npt.assert_array_almost_equal(motif_distances_ref, motif_distances_cmp)
75+
npt.assert_array_almost_equal(motif_indices_ref, motif_indices_cmp)
76+
npt.assert_array_almost_equal(motif_subspaces_ref, motif_subspaces_cmp)
77+
npt.assert_array_almost_equal(motif_mdls_ref, motif_mdls_cmp)
8078

8179

8280
@pytest.mark.parametrize("T", test_data)

0 commit comments

Comments
 (0)