Skip to content

Commit f585942

Browse files
authored
Merge pull request scipy#21880 from j-bowhay/cwtspec7
2 parents 62e7590 + 72fea07 commit f585942

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

scipy/linalg/_sketches.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
import numpy as np
77

8-
from scipy._lib._util import check_random_state, rng_integers
8+
from scipy._lib._util import (check_random_state, rng_integers,
9+
_transition_to_rng)
910
from scipy.sparse import csc_matrix
1011

1112
__all__ = ['clarkson_woodruff_transform']
1213

1314

14-
def cwt_matrix(n_rows, n_columns, seed=None):
15+
def cwt_matrix(n_rows, n_columns, rng=None):
1516
r"""
1617
Generate a matrix S which represents a Clarkson-Woodruff transform.
1718
@@ -26,13 +27,12 @@ def cwt_matrix(n_rows, n_columns, seed=None):
2627
Number of rows of S
2728
n_columns : int
2829
Number of columns of S
29-
seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
30-
If `seed` is None (or `np.random`), the `numpy.random.RandomState`
31-
singleton is used.
32-
If `seed` is an int, a new ``RandomState`` instance is used,
33-
seeded with `seed`.
34-
If `seed` is already a ``Generator`` or ``RandomState`` instance then
35-
that instance is used.
30+
rng : `numpy.random.Generator`, optional
31+
Pseudorandom number generator state. When `rng` is None, a new
32+
`numpy.random.Generator` is created using entropy from the
33+
operating system. Types other than `numpy.random.Generator` are
34+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
35+
3636
3737
Returns
3838
-------
@@ -45,15 +45,16 @@ def cwt_matrix(n_rows, n_columns, seed=None):
4545
.. math:: \|SA\| = (1 \pm \epsilon)\|A\|
4646
Where the error epsilon is related to the size of S.
4747
"""
48-
rng = check_random_state(seed)
48+
rng = check_random_state(rng)
4949
rows = rng_integers(rng, 0, n_rows, n_columns)
5050
cols = np.arange(n_columns+1)
5151
signs = rng.choice([1, -1], n_columns)
52-
S = csc_matrix((signs, rows, cols),shape=(n_rows, n_columns))
52+
S = csc_matrix((signs, rows, cols), shape=(n_rows, n_columns))
5353
return S
5454

5555

56-
def clarkson_woodruff_transform(input_matrix, sketch_size, seed=None):
56+
@_transition_to_rng("seed", position_num=2)
57+
def clarkson_woodruff_transform(input_matrix, sketch_size, rng=None):
5758
r"""
5859
Applies a Clarkson-Woodruff Transform/sketch to the input matrix.
5960
@@ -71,13 +72,11 @@ def clarkson_woodruff_transform(input_matrix, sketch_size, seed=None):
7172
Input matrix, of shape ``(n, d)``.
7273
sketch_size : int
7374
Number of rows for the sketch.
74-
seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
75-
If `seed` is None (or `np.random`), the `numpy.random.RandomState`
76-
singleton is used.
77-
If `seed` is an int, a new ``RandomState`` instance is used,
78-
seeded with `seed`.
79-
If `seed` is already a ``Generator`` or ``RandomState`` instance then
80-
that instance is used.
75+
rng : `numpy.random.Generator`, optional
76+
Pseudorandom number generator state. When `rng` is None, a new
77+
`numpy.random.Generator` is created using entropy from the
78+
operating system. Types other than `numpy.random.Generator` are
79+
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
8180
8281
Returns
8382
-------
@@ -175,5 +174,5 @@ def clarkson_woodruff_transform(input_matrix, sketch_size, seed=None):
175174
166.58473879945151
176175
177176
"""
178-
S = cwt_matrix(sketch_size, input_matrix.shape[0], seed)
177+
S = cwt_matrix(sketch_size, input_matrix.shape[0], rng=rng)
179178
return S.dot(input_matrix)

scipy/linalg/tests/test_sketches.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class TestClarksonWoodruffTransform:
1313
Testing the Clarkson Woodruff Transform
1414
"""
1515
# set seed for generating test matrices
16-
rng = np.random.RandomState(seed=1179103485)
16+
rng = np.random.default_rng(1179103485)
1717

1818
# Test matrix parameters
1919
n_rows = 2000
@@ -27,7 +27,7 @@ class TestClarksonWoodruffTransform:
2727
seeds = [1755490010, 934377150, 1391612830, 1752708722, 2008891431,
2828
1302443994, 1521083269, 1501189312, 1126232505, 1533465685]
2929

30-
A_dense = rng.randn(n_rows, n_cols)
30+
A_dense = rng.random((n_rows, n_cols))
3131
A_csc = rand(
3232
n_rows, n_cols, density=density, format='csc', random_state=rng,
3333
)
@@ -44,11 +44,12 @@ class TestClarksonWoodruffTransform:
4444
]
4545

4646
# Test vector with norm ~1
47-
x = rng.randn(n_rows, 1) / np.sqrt(n_rows)
47+
x = rng.random((n_rows, 1)) / np.sqrt(n_rows)
4848

4949
def test_sketch_dimensions(self):
5050
for A in self.test_matrices:
5151
for seed in self.seeds:
52+
# seed to ensure backwards compatibility post SPEC7
5253
sketch = clarkson_woodruff_transform(
5354
A, self.n_sketch_rows, seed=seed
5455
)
@@ -57,21 +58,21 @@ def test_sketch_dimensions(self):
5758
def test_seed_returns_identical_transform_matrix(self):
5859
for seed in self.seeds:
5960
S1 = cwt_matrix(
60-
self.n_sketch_rows, self.n_rows, seed=seed
61+
self.n_sketch_rows, self.n_rows, rng=seed
6162
).toarray()
6263
S2 = cwt_matrix(
63-
self.n_sketch_rows, self.n_rows, seed=seed
64+
self.n_sketch_rows, self.n_rows, rng=seed
6465
).toarray()
6566
assert_equal(S1, S2)
6667

6768
def test_seed_returns_identically(self):
6869
for A in self.test_matrices:
6970
for seed in self.seeds:
7071
sketch1 = clarkson_woodruff_transform(
71-
A, self.n_sketch_rows, seed=seed
72+
A, self.n_sketch_rows, rng=seed
7273
)
7374
sketch2 = clarkson_woodruff_transform(
74-
A, self.n_sketch_rows, seed=seed
75+
A, self.n_sketch_rows, rng=seed
7576
)
7677
if issparse(sketch1):
7778
sketch1 = sketch1.toarray()
@@ -91,7 +92,7 @@ def test_sketch_preserves_frobenius_norm(self):
9192
true_norm = np.linalg.norm(A)
9293
for seed in self.seeds:
9394
sketch = clarkson_woodruff_transform(
94-
A, self.n_sketch_rows, seed=seed,
95+
A, self.n_sketch_rows, rng=seed,
9596
)
9697
if issparse(sketch):
9798
sketch_norm = norm(sketch)
@@ -108,7 +109,7 @@ def test_sketch_preserves_vector_norm(self):
108109
true_norm = np.linalg.norm(self.x)
109110
for seed in self.seeds:
110111
sketch = clarkson_woodruff_transform(
111-
self.x, n_sketch_rows, seed=seed,
112+
self.x, n_sketch_rows, rng=seed,
112113
)
113114
sketch_norm = np.linalg.norm(sketch)
114115

0 commit comments

Comments
 (0)