Skip to content

Commit f911bba

Browse files
Sheppard, KevinSheppard, Kevin
authored andcommitted
UPD: Sync with recent NumPy changes
Enfore dtype Add tests for dtype Docstring changes
1 parent 9a3f96e commit f911bba

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

randomstate/bounded_integers.pxi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cdef object _rand_int64(low, high, size, aug_state *state, lock):
2020
if size is None:
2121
with lock:
2222
random_bounded_uint64_fill(state, off, rng, 1, &buf)
23-
return <int64_t>buf
23+
return np.int64(<int64_t>buf)
2424
else:
2525
array = <np.ndarray>np.empty(size, np.int64)
2626
cnt = np.PyArray_SIZE(array)
@@ -75,7 +75,7 @@ cdef object _rand_int32(low, high, size, aug_state *state, lock):
7575
if size is None:
7676
with lock:
7777
random_bounded_uint32_fill(state, off, rng, 1, &buf)
78-
return <int32_t>buf
78+
return np.int32(<int32_t>buf)
7979
else:
8080
array = <np.ndarray>np.empty(size, np.int32)
8181
cnt = np.PyArray_SIZE(array)
@@ -95,7 +95,7 @@ cdef object _rand_int16(low, high, size, aug_state *state, lock):
9595
if size is None:
9696
with lock:
9797
random_bounded_uint16_fill(state, off, rng, 1, &buf)
98-
return <int16_t>buf
98+
return np.int16(<int16_t>buf)
9999
else:
100100
array = <np.ndarray>np.empty(size, np.int16)
101101
cnt = np.PyArray_SIZE(array)
@@ -115,7 +115,7 @@ cdef object _rand_int8(low, high, size, aug_state *state, lock):
115115
if size is None:
116116
with lock:
117117
random_bounded_uint8_fill(state, off, rng, 1, &buf)
118-
return <int8_t>buf
118+
return np.int8(<int8_t>buf)
119119
else:
120120
array = <np.ndarray>np.empty(size, np.int8)
121121
cnt = np.PyArray_SIZE(array)
@@ -138,7 +138,7 @@ cdef object _rand_uint64(low, high, size, aug_state *state, lock):
138138
if size is None:
139139
with lock:
140140
random_bounded_uint64_fill(state, off, rng, 1, &buf)
141-
return <uint64_t>buf
141+
return np.uint64(<uint64_t>buf)
142142
else:
143143
array = <np.ndarray>np.empty(size, np.uint64)
144144
cnt = np.PyArray_SIZE(array)
@@ -158,7 +158,7 @@ cdef object _rand_uint32(low, high, size, aug_state *state, lock):
158158
if size is None:
159159
with lock:
160160
random_bounded_uint32_fill(state, off, rng, 1, &buf)
161-
return <uint32_t>buf
161+
return np.uint32(<uint32_t>buf)
162162
else:
163163
array = <np.ndarray>np.empty(size, np.uint32)
164164
cnt = np.PyArray_SIZE(array)
@@ -178,7 +178,7 @@ cdef object _rand_uint16(low, high, size, aug_state *state, lock):
178178
if size is None:
179179
with lock:
180180
random_bounded_uint16_fill(state, off, rng, 1, &buf)
181-
return <uint16_t>buf
181+
return np.uint16(<uint16_t>buf)
182182
else:
183183
array = <np.ndarray>np.empty(size, np.uint16)
184184
cnt = np.PyArray_SIZE(array)
@@ -198,7 +198,7 @@ cdef object _rand_uint8(low, high, size, aug_state *state, lock):
198198
if size is None:
199199
with lock:
200200
random_bounded_uint8_fill(state, off, rng, 1, &buf)
201-
return <uint8_t>buf
201+
return np.uint8(<uint8_t>buf)
202202
else:
203203
array = <np.ndarray>np.empty(size, np.uint8)
204204
cnt = np.PyArray_SIZE(array)
@@ -219,7 +219,7 @@ cdef object _rand_bool(low, high, size, aug_state *state, lock):
219219
if size is None:
220220
with lock:
221221
random_bool_fill(state, off, rng, 1, &buf)
222-
return <np.npy_bool>buf
222+
return np.bool_(<np.npy_bool>buf)
223223
else:
224224
array = <np.ndarray>np.empty(size, np.bool)
225225
cnt = np.PyArray_SIZE(array)

randomstate/randomstate.pyx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ cdef class RandomState:
761761
randoms_data[i] = random_positive_int(&self.rng_state)
762762
return randoms
763763

764-
def randint(self, low, high=None, size=None, dtype='l'):
764+
def randint(self, low, high=None, size=None, dtype=int):
765765
"""
766766
randint(low, high=None, size=None, dtype='l')
767767
@@ -788,7 +788,7 @@ cdef class RandomState:
788788
Desired dtype of the result. All dtypes are determined by their
789789
name, i.e., 'int64', 'int', etc, so byteorder is not available
790790
and a specific precision may have different C types depending
791-
on the platform. The default value is 'l' (C long).
791+
on the platform. The default value is 'np.int'.
792792
793793
.. versionadded:: 1.11.0
794794
@@ -836,23 +836,28 @@ cdef class RandomState:
836836
raise ValueError("low >= high")
837837

838838
if key == 'int32':
839-
return _rand_int32(low, high - 1, size, &self.rng_state, self.lock)
839+
ret = _rand_int32(low, high - 1, size, &self.rng_state, self.lock)
840840
elif key == 'int64':
841-
return _rand_int64(low, high - 1, size, &self.rng_state, self.lock)
841+
ret = _rand_int64(low, high - 1, size, &self.rng_state, self.lock)
842842
elif key == 'int16':
843-
return _rand_int16(low, high - 1, size, &self.rng_state, self.lock)
843+
ret = _rand_int16(low, high - 1, size, &self.rng_state, self.lock)
844844
elif key == 'int8':
845-
return _rand_int8(low, high - 1, size, &self.rng_state, self.lock)
845+
ret = _rand_int8(low, high - 1, size, &self.rng_state, self.lock)
846846
elif key == 'uint64':
847-
return _rand_uint64(low, high - 1, size, &self.rng_state, self.lock)
847+
ret = _rand_uint64(low, high - 1, size, &self.rng_state, self.lock)
848848
elif key == 'uint32':
849-
return _rand_uint32(low, high - 1, size, &self.rng_state, self.lock)
849+
ret = _rand_uint32(low, high - 1, size, &self.rng_state, self.lock)
850850
elif key == 'uint16':
851-
return _rand_uint16(low, high - 1, size, &self.rng_state, self.lock)
851+
ret = _rand_uint16(low, high - 1, size, &self.rng_state, self.lock)
852852
elif key == 'uint8':
853-
return _rand_uint8(low, high - 1, size, &self.rng_state, self.lock)
853+
ret = _rand_uint8(low, high - 1, size, &self.rng_state, self.lock)
854854
elif key == 'bool':
855-
return _rand_bool(low, high - 1, size, &self.rng_state, self.lock)
855+
ret = _rand_bool(low, high - 1, size, &self.rng_state, self.lock)
856+
857+
if size is None:
858+
if dtype in (np.bool, np.int, np.long):
859+
return dtype(ret)
860+
return ret
856861

857862
def bytes(self, np.npy_intp length):
858863
"""
@@ -3567,11 +3572,13 @@ cdef class RandomState:
35673572
the probability density function:
35683573
35693574
>>> import matplotlib.pyplot as plt
3570-
>>> import scipy.special as sps
3571-
Truncate s values at 50 so plot is interesting
3575+
>>> from scipy import special
3576+
3577+
Truncate s values at 50 so plot is interesting:
3578+
35723579
>>> count, bins, ignored = plt.hist(s[s<50], 50, normed=True)
35733580
>>> x = np.arange(1., 50.)
3574-
>>> y = x**(-a)/sps.zetac(a)
3581+
>>> y = x**(-a) / special.zetac(a)
35753582
>>> plt.plot(x, y/max(y), linewidth=2, color='r')
35763583
>>> plt.show()
35773584

randomstate/tests/tests_numpy_mt19937.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,25 @@ class TestRandint(TestCase):
137137
rfunc = random.randint
138138

139139
# valid integer/boolean types
140-
itype = [np.bool, np.int8, np.uint8, np.int16, np.uint16,
140+
itype = [np.bool_, np.int8, np.uint8, np.int16, np.uint16,
141141
np.int32, np.uint32, np.int64, np.uint64]
142142

143143
def test_unsupported_type(self):
144144
assert_raises(TypeError, self.rfunc, 1, dtype=np.float)
145145

146146
def test_bounds_checking(self):
147147
for dt in self.itype:
148-
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
149-
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
148+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
149+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
150150
assert_raises(ValueError, self.rfunc, lbnd - 1, ubnd, dtype=dt)
151151
assert_raises(ValueError, self.rfunc, lbnd, ubnd + 1, dtype=dt)
152152
assert_raises(ValueError, self.rfunc, ubnd, lbnd, dtype=dt)
153153
assert_raises(ValueError, self.rfunc, 1, 0, dtype=dt)
154154

155155
def test_rng_zero_and_extremes(self):
156156
for dt in self.itype:
157-
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
158-
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
157+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
158+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
159159
tgt = ubnd - 1
160160
assert_equal(self.rfunc(tgt, tgt + 1, size=1000, dtype=dt), tgt)
161161
tgt = lbnd
@@ -208,6 +208,24 @@ def test_repeatability(self):
208208
res = hashlib.md5(val).hexdigest()
209209
assert_(tgt[np.dtype(np.bool).name] == res)
210210

211+
def test_respect_dtype_singleton(self):
212+
# See gh-7203
213+
for dt in self.itype:
214+
lbnd = 0 if dt is np.bool_ else np.iinfo(dt).min
215+
ubnd = 2 if dt is np.bool_ else np.iinfo(dt).max + 1
216+
217+
sample = self.rfunc(lbnd, ubnd, dtype=dt)
218+
self.assertEqual(sample.dtype, np.dtype(dt))
219+
220+
for dt in (np.bool, np.int, np.long):
221+
lbnd = 0 if dt is np.bool else np.iinfo(dt).min
222+
ubnd = 2 if dt is np.bool else np.iinfo(dt).max + 1
223+
224+
# gh-7284: Ensure that we get Python data types
225+
sample = self.rfunc(lbnd, ubnd, dtype=dt)
226+
self.assertFalse(hasattr(sample, 'dtype'))
227+
self.assertEqual(type(sample), dt)
228+
211229

212230
class TestRandomDist(TestCase):
213231
# Make sure the random distribution returns the correct value for a

0 commit comments

Comments
 (0)