Skip to content

Commit 53648c0

Browse files
committed
Merge pull request #46 from bashtage/update-to-latest-numpy
UPD: Update to latest NumPy changes
2 parents 48e188b + 503104a commit 53648c0

File tree

6 files changed

+57
-43
lines changed

6 files changed

+57
-43
lines changed

randomstate/array_utilities.pxi

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,15 @@ cdef object cont(aug_state* state, void* func, object size, object lock, int nar
254254
check_constraint(_c, c_name, c_constraint)
255255

256256
if size is None:
257-
if narg == 0:
258-
return (<random_double_0>func)(state)
259-
elif narg == 1:
260-
return (<random_double_1>func)(state, _a)
261-
elif narg == 2:
262-
return (<random_double_2>func)(state, _a, _b)
263-
elif narg == 3:
264-
return (<random_double_3>func)(state, _a, _b, _c)
257+
with lock:
258+
if narg == 0:
259+
return (<random_double_0>func)(state)
260+
elif narg == 1:
261+
return (<random_double_1>func)(state, _a)
262+
elif narg == 2:
263+
return (<random_double_2>func)(state, _a, _b)
264+
elif narg == 3:
265+
return (<random_double_3>func)(state, _a, _b, _c)
265266

266267
cdef np.npy_intp i, n = compute_numel(size)
267268
cdef np.ndarray randoms = np.empty(n, np.double)
@@ -541,20 +542,21 @@ cdef object disc(aug_state* state, void* func, object size, object lock,
541542
check_constraint(<double>_ic, c_name, c_constraint)
542543

543544
if size is None:
544-
if narg_long == 0:
545-
if narg_double == 0:
546-
return (<random_uint_0>func)(state)
547-
elif narg_double == 1:
548-
return (<random_uint_d>func)(state, _da)
549-
elif narg_double == 2:
550-
return (<random_uint_dd>func)(state, _da, _db)
551-
elif narg_long == 1:
552-
if narg_double == 0:
553-
return (<random_uint_i>func)(state, _ia)
554-
if narg_double == 1:
555-
return (<random_uint_di>func)(state, _da, _ib)
556-
else:
557-
return (<random_uint_iii>func)(state, _ia, _ib, _ic)
545+
with lock:
546+
if narg_long == 0:
547+
if narg_double == 0:
548+
return (<random_uint_0>func)(state)
549+
elif narg_double == 1:
550+
return (<random_uint_d>func)(state, _da)
551+
elif narg_double == 2:
552+
return (<random_uint_dd>func)(state, _da, _db)
553+
elif narg_long == 1:
554+
if narg_double == 0:
555+
return (<random_uint_i>func)(state, _ia)
556+
if narg_double == 1:
557+
return (<random_uint_di>func)(state, _da, _ib)
558+
else:
559+
return (<random_uint_iii>func)(state, _ia, _ib, _ic)
558560

559561
cdef np.npy_intp i, n = compute_numel(size)
560562
cdef np.int_t [::1] randoms = np.empty(n, np.int)

randomstate/bounded_integers.pxi

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,22 @@ cdef object _rand_uint8(low, high, size, aug_state *state, lock):
207207
random_bounded_uint8_fill(state, off, rng, cnt, out)
208208
return array
209209

210-
# TODO: Needs to change types
211210
cdef object _rand_bool(low, high, size, aug_state *state, lock):
212-
cdef int8_t off, rng, buf
213-
cdef int8_t *out
211+
cdef np.npy_bool off, rng, buf
212+
cdef np.npy_bool *out
214213
cdef np.ndarray array
215214
cdef np.npy_intp cnt
216215

217-
rng = <uint8_t>(high - low)
218-
off = <uint8_t>low
216+
rng = <np.npy_bool>(high - low)
217+
off = <np.npy_bool>low
219218
if size is None:
220219
with lock:
221220
random_bool_fill(state, off, rng, 1, &buf)
222221
return np.bool_(<np.npy_bool>buf)
223222
else:
224223
array = <np.ndarray>np.empty(size, np.bool)
225224
cnt = np.PyArray_SIZE(array)
226-
out = <int8_t *>np.PyArray_DATA(array)
225+
out = <np.npy_bool *>np.PyArray_DATA(array)
227226
with lock, nogil:
228227
random_bool_fill(state, off, rng, cnt, out)
229228
return array

randomstate/distributions.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,8 +1579,7 @@ void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_i
15791579
* Fills an array with cnt random npy_bool between off and off + rng
15801580
* inclusive.
15811581
*/
1582-
/* TODO: This needs to use bools. See original */
1583-
void random_bool_fill(aug_state *state, int8_t off, int8_t rng, npy_intp cnt, int8_t *out)
1582+
void random_bool_fill(aug_state *state, npy_bool off, npy_bool rng, npy_intp cnt, npy_bool *out)
15841583
{
15851584
int i;
15861585
uint32_t buf = 0;

randomstate/distributions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ extern void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t
134134

135135
extern void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, npy_intp cnt, uint8_t *out);
136136

137-
extern void random_bool_fill(aug_state *state, int8_t off, int8_t rng, npy_intp cnt, int8_t *out);
137+
extern void random_bool_fill(aug_state *state, npy_bool off, npy_bool rng, npy_intp cnt, npy_bool *out);
138138

139139
extern void random_uniform_fill(aug_state* state, npy_intp count, double *out);
140140

randomstate/interface/dSFMT/dSFMT-shim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static inline double random_double(aug_state* state)
6767
static inline uint64_t random_raw_values(aug_state* state)
6868
{
6969
double d = random_double_from_buffer(state);
70-
return (uint64_t *)&d;
70+
return *((uint64_t *)&d);
7171
}
7272

7373

randomstate/randomstate.pyx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ cdef extern from "distributions.h":
111111
cdef void random_bounded_uint32_fill(aug_state *state, uint32_t off, uint32_t rng, intptr_t cnt,uint32_t *out) nogil
112112
cdef void random_bounded_uint16_fill(aug_state *state, uint16_t off, uint16_t rng, intptr_t cnt, uint16_t *out) nogil
113113
cdef void random_bounded_uint8_fill(aug_state *state, uint8_t off, uint8_t rng, intptr_t cnt, uint8_t *out) nogil
114-
cdef void random_bool_fill(aug_state *state, int8_t off, int8_t rng, intptr_t cnt, int8_t *out) nogil
114+
cdef void random_bool_fill(aug_state *state, np.npy_bool off, np.npy_bool rng, intptr_t cnt, np.npy_bool *out) nogil
115115
cdef void random_uniform_fill(aug_state *state, intptr_t cnt, double *out) nogil
116116
cdef void random_standard_exponential_fill(aug_state* state, intptr_t count, double *out) nogil
117117
cdef void random_gauss_fill(aug_state* state, intptr_t count, double *out) nogil
@@ -580,24 +580,28 @@ cdef class RandomState:
580580
"""
581581
cdef uint32_t [:] randoms32
582582
cdef uint64_t [:] randoms64
583+
cdef aug_state* rng_state
583584
cdef Py_ssize_t i, n
585+
rng_state = &self.rng_state
584586
if bits == 64:
585587
if size is None:
586-
return random_uint64(&self.rng_state)
588+
with self.lock:
589+
return random_uint64(rng_state)
587590
n = compute_numel(size)
588591
randoms64 = np.empty(n, np.uint64)
589592
with self.lock, nogil:
590593
for i in range(n):
591-
randoms64[i] = random_uint64(&self.rng_state)
594+
randoms64[i] = random_uint64(rng_state)
592595
return np.asarray(randoms64).reshape(size)
593596
elif bits == 32:
594597
if size is None:
595-
return random_uint32(&self.rng_state)
598+
with self.lock:
599+
return random_uint32(rng_state)
596600
n = compute_numel(size)
597601
randoms32 = np.empty(n, np.uint32)
598602
with self.lock, nogil:
599603
for i in range(n):
600-
randoms32[i] = random_uint32(&self.rng_state)
604+
randoms32[i] = random_uint32(rng_state)
601605
return np.asarray(randoms32).reshape(size)
602606
else:
603607
raise ValueError('Unknown value of bits. Must be either 32 or 64.')
@@ -631,17 +635,20 @@ cdef class RandomState:
631635
cdef np.ndarray randoms
632636
cdef uint64_t *randoms_data
633637
cdef Py_ssize_t i, n
638+
cdef aug_state* rng_state
639+
rng_state = &self.rng_state
634640

635641
if size is None:
636-
return random_raw_values(&self.rng_state)
642+
with self.lock:
643+
return random_raw_values(rng_state)
637644

638645
randoms = <np.ndarray>np.empty(size, np.uint64)
639646
randoms_data = <uint64_t*>np.PyArray_DATA(randoms)
640647
n = np.PyArray_SIZE(randoms)
641648

642649
with self.lock, nogil:
643650
for i in range(n):
644-
randoms_data[i] = random_raw_values(&self.rng_state)
651+
randoms_data[i] = random_raw_values(rng_state)
645652
return randoms
646653

647654
# Pickling support:
@@ -749,16 +756,20 @@ cdef class RandomState:
749756
cdef np.npy_intp n
750757
cdef np.ndarray randoms
751758
cdef long *randoms_data
759+
cdef aug_state* rng_state
760+
rng_state = &self.rng_state
752761

753762
if size is None:
754-
return random_positive_int(&self.rng_state)
763+
with self.lock:
764+
return random_positive_int(rng_state)
755765

756766
randoms = <np.ndarray>np.empty(size, dtype=np.int)
757767
randoms_data = <long*>np.PyArray_DATA(randoms)
758768
n = np.PyArray_SIZE(randoms)
759769

760770
for i in range(n):
761-
randoms_data[i] = random_positive_int(&self.rng_state)
771+
with self.lock, nogil:
772+
randoms_data[i] = random_positive_int(rng_state)
762773
return randoms
763774

764775
def randint(self, low, high=None, size=None, dtype=int):
@@ -4239,6 +4250,10 @@ cdef class RandomState:
42394250
42404251
Modify a sequence in-place by shuffling its contents.
42414252
4253+
This function only shuffles the array along the first axis of a
4254+
multi-dimensional array. The order of sub-arrays is changed but
4255+
their contents remains the same.
4256+
42424257
Parameters
42434258
----------
42444259
x : array_like
@@ -4255,8 +4270,7 @@ cdef class RandomState:
42554270
>>> arr
42564271
[1 7 5 2 9 4 3 6 0 8]
42574272
4258-
This function only shuffles the array along the first index of a
4259-
multi-dimensional array:
4273+
Multi-dimensional arrays are only shuffled along the first axis:
42604274
42614275
>>> arr = np.arange(9).reshape((3, 3))
42624276
>>> np.random.shuffle(arr)

0 commit comments

Comments
 (0)