From 1a6a5fb8e10512c855041ac13e34a76c31ef9ca7 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Wed, 24 Aug 2022 17:39:11 +0200 Subject: [PATCH 1/8] Adding random.permutation --- cunumeric/random/random.py | 14 ++++++++ tests/integration/test_random_permsort.py | 43 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 tests/integration/test_random_permsort.py diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index 278530435..a16a4e3f7 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -20,6 +20,7 @@ from cunumeric.array import ndarray from cunumeric.runtime import runtime +import cunumeric.sort from cunumeric.random import generator if TYPE_CHECKING: @@ -924,6 +925,19 @@ def pareto( return generator.get_static_generator().pareto(a, size, dtype) +def permuation(x: Union[int, ndarray]) -> ndarray: + if x is int: + count = x + else: + count = len(x) + key = uniform(0.0, 1.0, count) + indices = cunumeric.argsort(key) + if x is int: + return indices + else: + return x[indices] + + def poisson( lam: float = 1.0, size: Union[NdShapeLike, None] = None ) -> ndarray: diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py new file mode 100644 index 000000000..2c22c8184 --- /dev/null +++ b/tests/integration/test_random_permsort.py @@ -0,0 +1,43 @@ +# Copyright 2022 NVIDIA Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import sys + +import numpy as np +import pytest +from utils.random import ModuleGenerator, assert_distribution + +import cunumeric as num + + +def test_permutation_int(): + count = 1024 + p = num.random.permutation(count) + p.sort() + assert(num.linalg.norm(p - np.arange(count)) == 0.0) + + +def test_permutation_array(): + count = 1024 + x = num.arange(count) + p = num.random.permutation(x) + assert(num.linalg.norm(x-p) != 0.0) + p.sort() + assert(num.linalg.norm(x-p) == 0.0) + + +if __name__ == "__main__": + import sys + + sys.exit(pytest.main(sys.argv)) From e057a8d85adc1afd88727b0908db052ca75b7c63 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Wed, 24 Aug 2022 18:23:56 +0200 Subject: [PATCH 2/8] Adding random.shuffle --- cunumeric/random/random.py | 5 +++++ tests/integration/test_random_permsort.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index a16a4e3f7..7ecba4043 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -1291,6 +1291,11 @@ def rayleigh( sample = random_sample +def shuffle(a : ndarray) -> None: + b = cunumeric.random.permutation(a) + a[:] = b + + def standard_cauchy( size: Union[NdShapeLike, None] = None, dtype: npt.DTypeLike = np.float64, diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index 2c22c8184..a21867d2b 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -37,6 +37,16 @@ def test_permutation_array(): assert(num.linalg.norm(x-p) == 0.0) +def test_shuffle(): + count = 16 + p = num.arange(count) + x = num.arange(count) + num.random.shuffle(x) + assert(num.linalg.norm(x-p) != 0.0) + x.sort() + assert(num.linalg.norm(x-p) == 0.0) + + if __name__ == "__main__": import sys From 4ab65e6786360950f35e42923b08745e72eb3e20 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Wed, 24 Aug 2022 19:44:54 +0200 Subject: [PATCH 3/8] Adding random.choice --- cunumeric/random/random.py | 134 +++++++++++++++++++++- tests/integration/test_random_permsort.py | 107 +++++++++++++---- 2 files changed, 217 insertions(+), 24 deletions(-) diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index 7ecba4043..6ca159ef0 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -217,6 +217,96 @@ def chisquare( return generator.get_static_generator().chisquare(df, size, dtype) +def choice( + a : Union[int, ndarray], + size: Union[NdShapeLike, None]=None, + replace: bool=True, + p: Union[None, ndarray]=None +) -> Union[int, ndarray, npt.NDArray[Any]]: + """ + Returns an array of random values from a given 1-D array. + Each element of the returned array is independently sampled + from ``a`` according to ``p`` or uniformly. + + Note + ---- + Currently ``p`` is not supported when ``replace=False``. + + Parameters + ---------- + a : ndarray or int : If an array-like, + a random sample is generated from its elements. + If an int, the random sample is generated as if ``a`` was + ``np.arange(n)`` + size : int or tuple of ints : The shape of the array. + replace (boolean): Whether the sample is with or without replacement. + p : 1-D array-like : + The probabilities associated with each entry in ``a``. + If not given the sample assumes a uniform distribution over all + entries in ``a``. + + Returns + ------- + out : ndarray : The generated random samples. + + + Raises + ------ + ValueError + If a is an int and less than zero, if a or p are not 1-dimensional, + if a is an array-like of size 0, if p is not a vector of + probabilities, if a and p have different lengths, or if + replace=False and the sample size is greater than the population + size + + See Also + -------- + numpy.random.choice + + Availability + -------- + Multiple GPUs, Multiple CPUs + """ + + if isinstance(a, int) and a <= 0: + raise ValueError + if not isinstance(size, int): + raise ValueError + if p is None: + if isinstance(a, int): + if replace: + return cunumeric.random.randint(0, a, size) + else: + if a < size: + raise ValueError + return cunumeric.random.permutation(a)[:size] + else: + if replace: + indices = cunumeric.random.randint(0, len(a), size) + return a[indices] + else: + if len(a) < size: + raise ValueError + return cunumeric.random.permutation(a)[:size] + else: + if not replace: + raise ValueError + cump = p.cumsum() + # check if p is a probability distribution + if abs(cump[-1] - 1.0) > len(p) * np.finfo(p.dtype).eps: + # does not sum up to 1 + raise ValueError + cump[-1] = 1.0 # fix rounding issues + # draw uniforms + uni = cunumeric.random.uniform(0.0, 1.0, size) + # inverse distribution + idx = cunumeric.searchsorted(cump, uni, "right") + # return samples + if isinstance(a, int): + return cunumeric.arange(a)[idx] + return a[idx] + + def exponential( scale: float = 1.0, size: Union[NdShapeLike, None] = None, @@ -925,14 +1015,35 @@ def pareto( return generator.get_static_generator().pareto(a, size, dtype) -def permuation(x: Union[int, ndarray]) -> ndarray: - if x is int: +def permutation(x: Union[int, ndarray]) -> ndarray: + """ + Returns a permuted range or a permutation of an array. + + Parameters + ---------- + a : (int or ndarray): The range or the array to be shuffled. + + Returns + ------- + out: If `a` is an integer, it is permutation range between 0 + and `a` - 1. + Otherwise, it is a permutation of `a`. + + See Also + -------- + numpy.random.permutation + + Availability + -------- + Multiple GPUs, Multiple CPUs + """ + if isinstance(x, int): count = x else: count = len(x) key = uniform(0.0, 1.0, count) indices = cunumeric.argsort(key) - if x is int: + if isinstance(x, int): return indices else: return x[indices] @@ -1291,7 +1402,22 @@ def rayleigh( sample = random_sample -def shuffle(a : ndarray) -> None: +def shuffle(a: ndarray) -> None: + """ + Shuffles an array. + + Parameters + ---------- + a : (ndarray): The array to be shuffled. + + See Also + -------- + numpy.random.shuffle + + Availability + -------- + Multiple GPUs, Multiple CPUs + """ b = cunumeric.random.permutation(a) a[:] = b diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index a21867d2b..5857f24cd 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -12,39 +12,106 @@ # See the License for the specific language governing permissions and # limitations under the License. # -import sys - import numpy as np import pytest -from utils.random import ModuleGenerator, assert_distribution import cunumeric as num def test_permutation_int(): - count = 1024 - p = num.random.permutation(count) - p.sort() - assert(num.linalg.norm(p - np.arange(count)) == 0.0) + count = 1024 + p = num.random.permutation(count) + p.sort() + assert num.linalg.norm(p - np.arange(count)) == 0.0 def test_permutation_array(): - count = 1024 - x = num.arange(count) - p = num.random.permutation(x) - assert(num.linalg.norm(x-p) != 0.0) - p.sort() - assert(num.linalg.norm(x-p) == 0.0) + count = 1024 + x = num.arange(count) + p = num.random.permutation(x) + assert num.linalg.norm(x - p) != 0.0 + p.sort() + assert num.linalg.norm(x - p) == 0.0 def test_shuffle(): - count = 16 - p = num.arange(count) - x = num.arange(count) - num.random.shuffle(x) - assert(num.linalg.norm(x-p) != 0.0) - x.sort() - assert(num.linalg.norm(x-p) == 0.0) + count = 16 + p = num.arange(count) + x = num.arange(count) + num.random.shuffle(x) + assert num.linalg.norm(x - p) != 0.0 + x.sort() + assert num.linalg.norm(x - p) == 0.0 + + +maxvalue = 1024 +count = 42 + + +def test_choice_1(): + a = num.random.choice(maxvalue, count) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 + + +def test_choice_2(): + a = num.random.choice(maxvalue, count, False) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 + for i in range(count): + for j in range(count): + if i == j: + continue + assert a[i] != a[j] + + +def test_choice_3(): + values = num.random.random_integers(0, maxvalue, maxvalue) + + a = num.random.choice(values, count) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 + + +def test_choice_4(): + values = num.arange(maxvalue) + + a = num.random.choice(values, count, False) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 + for i in range(count): + for j in range(count): + if i == j: + continue + assert a[i] != a[j] + + +def test_choice_5(): + values = num.arange(maxvalue) + + p = num.random.uniform(0, 1, maxvalue) + p /= p.sum() + + a = num.random.choice(values, count, True, p) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 + + +def test_choice_6(): + values = num.random.random_integers(0, maxvalue, maxvalue) + + p = num.random.uniform(0, 1, maxvalue) + p /= p.sum() + + a = num.random.choice(values, count, True, p) + assert len(a) == count + assert num.amax(a) <= maxvalue + assert num.amin(a) >= 0 if __name__ == "__main__": From 036efd52e4fd07bfd2efd8eecd2706ec7e2e9382 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Thu, 25 Aug 2022 11:32:38 +0200 Subject: [PATCH 4/8] fixing test --- tests/integration/test_random_permsort.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index 5857f24cd..cd9808db8 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -72,8 +72,8 @@ def test_choice_3(): a = num.random.choice(values, count) assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) def test_choice_4(): @@ -81,8 +81,8 @@ def test_choice_4(): a = num.random.choice(values, count, False) assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) for i in range(count): for j in range(count): if i == j: @@ -98,8 +98,8 @@ def test_choice_5(): a = num.random.choice(values, count, True, p) assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) def test_choice_6(): @@ -110,8 +110,8 @@ def test_choice_6(): a = num.random.choice(values, count, True, p) assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) if __name__ == "__main__": From 20d3364b647dc61702f668c8d303bb49da7727e9 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Thu, 25 Aug 2022 11:53:11 +0200 Subject: [PATCH 5/8] Fixing bug in rng code --- cunumeric/eager.py | 4 ++-- tests/integration/test_random_permsort.py | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/cunumeric/eager.py b/cunumeric/eager.py index eb9636634..56917a886 100644 --- a/cunumeric/eager.py +++ b/cunumeric/eager.py @@ -711,9 +711,9 @@ def bitgenerator_integers( ) else: if self.array.size == 1: - self.array.fill(np.random.random_integers(low, high)) + self.array.fill(np.random.randint(low, high)) else: - a = np.random.random_integers(low, high, size=self.array.shape) + a = np.random.randint(low, high, size=self.array.shape) self.array[:] = a def bitgenerator_lognormal( diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index cd9808db8..ff0c42960 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -44,18 +44,14 @@ def test_shuffle(): assert num.linalg.norm(x - p) == 0.0 -maxvalue = 1024 -count = 42 - - -def test_choice_1(): +def test_choice_1(maxvalue = 1024, count = 42): a = num.random.choice(maxvalue, count) assert len(a) == count assert num.amax(a) <= maxvalue assert num.amin(a) >= 0 -def test_choice_2(): +def test_choice_2(maxvalue = 1024, count = 42): a = num.random.choice(maxvalue, count, False) assert len(a) == count assert num.amax(a) <= maxvalue @@ -67,7 +63,7 @@ def test_choice_2(): assert a[i] != a[j] -def test_choice_3(): +def test_choice_3(maxvalue = 1024, count = 42): values = num.random.random_integers(0, maxvalue, maxvalue) a = num.random.choice(values, count) @@ -76,7 +72,7 @@ def test_choice_3(): assert num.amin(a) >= num.amin(values) -def test_choice_4(): +def test_choice_4(maxvalue = 1024, count = 42): values = num.arange(maxvalue) a = num.random.choice(values, count, False) @@ -90,7 +86,7 @@ def test_choice_4(): assert a[i] != a[j] -def test_choice_5(): +def test_choice_5(maxvalue = 1024, count = 42): values = num.arange(maxvalue) p = num.random.uniform(0, 1, maxvalue) @@ -102,7 +98,7 @@ def test_choice_5(): assert num.amin(a) >= num.amin(values) -def test_choice_6(): +def test_choice_6(maxvalue = 1024, count = 42): values = num.random.random_integers(0, maxvalue, maxvalue) p = num.random.uniform(0, 1, maxvalue) From b3ed39bc2c5773add90d033fc5c7c6192e2270f5 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Thu, 25 Aug 2022 12:02:12 +0200 Subject: [PATCH 6/8] precommit --- cunumeric/random/random.py | 8 ++++---- tests/integration/test_random_permsort.py | 20 ++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index 6ca159ef0..ae0630d99 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -218,10 +218,10 @@ def chisquare( def choice( - a : Union[int, ndarray], - size: Union[NdShapeLike, None]=None, - replace: bool=True, - p: Union[None, ndarray]=None + a: Union[int, ndarray], + size: Union[NdShapeLike, None] = None, + replace: bool = True, + p: Union[None, ndarray] = None, ) -> Union[int, ndarray, npt.NDArray[Any]]: """ Returns an array of random values from a given 1-D array. diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index ff0c42960..e224122b3 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -44,14 +44,14 @@ def test_shuffle(): assert num.linalg.norm(x - p) == 0.0 -def test_choice_1(maxvalue = 1024, count = 42): +def test_choice_1(maxvalue=1024, count=42): a = num.random.choice(maxvalue, count) assert len(a) == count assert num.amax(a) <= maxvalue assert num.amin(a) >= 0 -def test_choice_2(maxvalue = 1024, count = 42): +def test_choice_2(maxvalue=1024, count=42): a = num.random.choice(maxvalue, count, False) assert len(a) == count assert num.amax(a) <= maxvalue @@ -63,21 +63,21 @@ def test_choice_2(maxvalue = 1024, count = 42): assert a[i] != a[j] -def test_choice_3(maxvalue = 1024, count = 42): +def test_choice_3(maxvalue=1024, count=42): values = num.random.random_integers(0, maxvalue, maxvalue) a = num.random.choice(values, count) assert len(a) == count - assert num.amax(a) <= num.amax(values) + assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) -def test_choice_4(maxvalue = 1024, count = 42): +def test_choice_4(maxvalue=1024, count=42): values = num.arange(maxvalue) a = num.random.choice(values, count, False) assert len(a) == count - assert num.amax(a) <= num.amax(values) + assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) for i in range(count): for j in range(count): @@ -86,7 +86,7 @@ def test_choice_4(maxvalue = 1024, count = 42): assert a[i] != a[j] -def test_choice_5(maxvalue = 1024, count = 42): +def test_choice_5(maxvalue=1024, count=42): values = num.arange(maxvalue) p = num.random.uniform(0, 1, maxvalue) @@ -94,11 +94,11 @@ def test_choice_5(maxvalue = 1024, count = 42): a = num.random.choice(values, count, True, p) assert len(a) == count - assert num.amax(a) <= num.amax(values) + assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) -def test_choice_6(maxvalue = 1024, count = 42): +def test_choice_6(maxvalue=1024, count=42): values = num.random.random_integers(0, maxvalue, maxvalue) p = num.random.uniform(0, 1, maxvalue) @@ -106,7 +106,7 @@ def test_choice_6(maxvalue = 1024, count = 42): a = num.random.choice(values, count, True, p) assert len(a) == count - assert num.amax(a) <= num.amax(values) + assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) From ebf4590667a8b8c80d5b1a63d18dc14dfb640694 Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Tue, 30 Aug 2022 10:43:30 +0200 Subject: [PATCH 7/8] Applying review recommendations --- cunumeric/random/random.py | 12 +-- tests/integration/test_random_permsort.py | 101 +++++++++++----------- 2 files changed, 56 insertions(+), 57 deletions(-) diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index ae0630d99..d59c7d5f0 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -1037,16 +1037,12 @@ def permutation(x: Union[int, ndarray]) -> ndarray: -------- Multiple GPUs, Multiple CPUs """ - if isinstance(x, int): - count = x - else: - count = len(x) + count = x if isinstance(x, int) else len(x) + key = uniform(0.0, 1.0, count) indices = cunumeric.argsort(key) - if isinstance(x, int): - return indices - else: - return x[indices] + + return indices if isinstance(x,int) else x[indices] def poisson( diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index e224122b3..f33be54a6 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -17,7 +17,6 @@ import cunumeric as num - def test_permutation_int(): count = 1024 p = num.random.permutation(count) @@ -44,70 +43,74 @@ def test_shuffle(): assert num.linalg.norm(x - p) == 0.0 -def test_choice_1(maxvalue=1024, count=42): - a = num.random.choice(maxvalue, count) - assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 +class TestChoice: + + maxvalue=1024 + count=42 + def test_choice_1(self): + a = num.random.choice(self.maxvalue, self.count) + assert len(a) == self.count + assert num.amax(a) <= self.maxvalue + assert num.amin(a) >= 0 -def test_choice_2(maxvalue=1024, count=42): - a = num.random.choice(maxvalue, count, False) - assert len(a) == count - assert num.amax(a) <= maxvalue - assert num.amin(a) >= 0 - for i in range(count): - for j in range(count): - if i == j: - continue - assert a[i] != a[j] + def test_choice_2(self): + a = num.random.choice(self.maxvalue, self.count, False) + assert len(a) == self.count + assert num.amax(a) <= self.maxvalue + assert num.amin(a) >= 0 + for i in range(self.count): + for j in range(self.count): + if i == j: + continue + assert a[i] != a[j] -def test_choice_3(maxvalue=1024, count=42): - values = num.random.random_integers(0, maxvalue, maxvalue) + def test_choice_3(self): + values = num.random.random_integers(0, self.maxvalue, self.maxvalue) - a = num.random.choice(values, count) - assert len(a) == count - assert num.amax(a) <= num.amax(values) - assert num.amin(a) >= num.amin(values) + a = num.random.choice(values, self.count) + assert len(a) == self.count + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) -def test_choice_4(maxvalue=1024, count=42): - values = num.arange(maxvalue) + def test_choice_4(self): + values = num.arange(self.maxvalue) - a = num.random.choice(values, count, False) - assert len(a) == count - assert num.amax(a) <= num.amax(values) - assert num.amin(a) >= num.amin(values) - for i in range(count): - for j in range(count): - if i == j: - continue - assert a[i] != a[j] + a = num.random.choice(values, self.count, False) + assert len(a) == self.count + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) + for i in range(self.count): + for j in range(self.count): + if i == j: + continue + assert a[i] != a[j] -def test_choice_5(maxvalue=1024, count=42): - values = num.arange(maxvalue) + def test_choice_5(self): + values = num.arange(self.maxvalue) - p = num.random.uniform(0, 1, maxvalue) - p /= p.sum() + p = num.random.uniform(0, 1, self.maxvalue) + p /= p.sum() - a = num.random.choice(values, count, True, p) - assert len(a) == count - assert num.amax(a) <= num.amax(values) - assert num.amin(a) >= num.amin(values) + a = num.random.choice(values, self.count, True, p) + assert len(a) == self.count + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) -def test_choice_6(maxvalue=1024, count=42): - values = num.random.random_integers(0, maxvalue, maxvalue) + def test_choice_6(self): + values = num.random.random_integers(0, self.maxvalue, self.maxvalue) - p = num.random.uniform(0, 1, maxvalue) - p /= p.sum() + p = num.random.uniform(0, 1, self.maxvalue) + p /= p.sum() - a = num.random.choice(values, count, True, p) - assert len(a) == count - assert num.amax(a) <= num.amax(values) - assert num.amin(a) >= num.amin(values) + a = num.random.choice(values, self.count, True, p) + assert len(a) == self.count + assert num.amax(a) <= num.amax(values) + assert num.amin(a) >= num.amin(values) if __name__ == "__main__": From 1931d6e7415dd2f1948ee62fa83169181209e12e Mon Sep 17 00:00:00 2001 From: Florent DUGUET Date: Tue, 30 Aug 2022 16:13:57 +0200 Subject: [PATCH 8/8] precommit fix --- cunumeric/random/random.py | 2 +- tests/integration/test_random_permsort.py | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cunumeric/random/random.py b/cunumeric/random/random.py index d59c7d5f0..26c260e53 100644 --- a/cunumeric/random/random.py +++ b/cunumeric/random/random.py @@ -1042,7 +1042,7 @@ def permutation(x: Union[int, ndarray]) -> ndarray: key = uniform(0.0, 1.0, count) indices = cunumeric.argsort(key) - return indices if isinstance(x,int) else x[indices] + return indices if isinstance(x, int) else x[indices] def poisson( diff --git a/tests/integration/test_random_permsort.py b/tests/integration/test_random_permsort.py index f33be54a6..22b11dcff 100644 --- a/tests/integration/test_random_permsort.py +++ b/tests/integration/test_random_permsort.py @@ -17,6 +17,7 @@ import cunumeric as num + def test_permutation_int(): count = 1024 p = num.random.permutation(count) @@ -45,8 +46,8 @@ def test_shuffle(): class TestChoice: - maxvalue=1024 - count=42 + maxvalue = 1024 + count = 42 def test_choice_1(self): a = num.random.choice(self.maxvalue, self.count) @@ -65,7 +66,6 @@ def test_choice_2(self): continue assert a[i] != a[j] - def test_choice_3(self): values = num.random.random_integers(0, self.maxvalue, self.maxvalue) @@ -74,7 +74,6 @@ def test_choice_3(self): assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) - def test_choice_4(self): values = num.arange(self.maxvalue) @@ -88,7 +87,6 @@ def test_choice_4(self): continue assert a[i] != a[j] - def test_choice_5(self): values = num.arange(self.maxvalue) @@ -100,7 +98,6 @@ def test_choice_5(self): assert num.amax(a) <= num.amax(values) assert num.amin(a) >= num.amin(values) - def test_choice_6(self): values = num.random.random_integers(0, self.maxvalue, self.maxvalue)