FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

MAINT: gracefully shuffle memoryviews by charris · Pull Request #18327 · numpy/numpy · GitHub

/ numpy Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .pyx  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions numpy/random/_generator.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
from collections.abc import MutableSequence
from collections.abc import Sequence

from cpython.pycapsule cimport PyCapsule_IsValid, PyCapsule_GetPointer
from cpython cimport (Py_INCREF, PyFloat_AsDouble)
Expand Down Expand Up @@ -4436,7 +4436,7 @@ cdef class Generator:
x[i] = buf
else:
# Untyped path.
if not isinstance(x, MutableSequence):
if not isinstance(x, Sequence):
# See gh-18206. We may decide to deprecate here in the future.
warnings.warn(
"`x` isn't a recognized object; `shuffle` is not guaranteed "
Expand Down
4 changes: 2 additions & 2 deletions numpy/random/mtrand.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#cython: wraparound=False, nonecheck=False, boundscheck=False, cdivision=True, language_level=3
import operator
import warnings
from collections.abc import MutableSequence
from collections.abc import Sequence

import numpy as np

Expand Down Expand Up @@ -4473,7 +4473,7 @@ cdef class RandomState:
x[i] = buf
else:
# Untyped path.
if not isinstance(x, MutableSequence):
if not isinstance(x, Sequence):
# See gh-18206. We may decide to deprecate here in the future.
warnings.warn(
"`x` isn't a recognized object; `shuffle` is not guaranteed "
Expand Down
15 changes: 15 additions & 0 deletions numpy/random/tests/test_random.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,21 @@ def test_shuffle_masked(self):
assert_equal(
sorted(b.data[~b.mask]), sorted(b_orig.data[~b_orig.mask]))

def test_shuffle_memoryview(self):
# gh-18273
# allow graceful handling of memoryviews
# (treat the same as arrays)
np.random.seed(self.seed)
a = np.arange(5).data
np.random.shuffle(a)
assert_equal(np.asarray(a), [0, 1, 4, 3, 2])
rng = np.random.RandomState(self.seed)
rng.shuffle(a)
assert_equal(np.asarray(a), [0, 1, 2, 3, 4])
rng = np.random.default_rng(self.seed)
rng.shuffle(a)
assert_equal(np.asarray(a), [4, 1, 0, 3, 2])

def test_beta(self):
np.random.seed(self.seed)
actual = np.random.beta(.1, .9, size=(3, 2))
Expand Down

Back | FazBrowse Home | New Git URL