@@ -105,6 +105,7 @@ def is_lexsorted(list list_of_arrays):
105105 Py_ssize_t n, nlevels
106106 int64_t k, cur, pre
107107 ndarray arr
108+ bint result = 1
108109
109110 nlevels = len (list_of_arrays)
110111 n = len (list_of_arrays[0 ])
@@ -115,18 +116,20 @@ def is_lexsorted(list list_of_arrays):
115116 vecs[i] = < int64_t* > arr.data
116117
117118 # Assume uniqueness??
118- for i from 1 <= i < n:
119- for k from 0 <= k < nlevels:
120- cur = vecs[k][i]
121- pre = vecs[k][i - 1 ]
122- if cur == pre:
123- continue
124- elif cur > pre:
125- break
126- else :
127- return False
119+ with nogil:
120+ for i from 1 <= i < n:
121+ for k from 0 <= k < nlevels:
122+ cur = vecs[k][i]
123+ pre = vecs[k][i - 1 ]
124+ if cur == pre:
125+ continue
126+ elif cur > pre:
127+ break
128+ else :
129+ result = 0
130+ break
128131 free(vecs)
129- return True
132+ return result
130133
131134
132135@ cython.boundscheck (False )
@@ -177,10 +180,11 @@ def groupsort_indexer(ndarray[int64_t] index, Py_ssize_t ngroups):
177180
178181@ cython.boundscheck (False )
179182@ cython.wraparound (False )
180- cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k):
183+ cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k) nogil :
181184 cdef:
182- Py_ssize_t i, j, l, m, n = a.size
185+ Py_ssize_t i, j, l, m, n = a.shape[ 0 ]
183186 numeric x
187+
184188 with nogil:
185189 l = 0
186190 m = n - 1
@@ -201,7 +205,7 @@ cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k):
201205
202206 if j < k: l = i
203207 if k < i: m = j
204- return a[k]
208+ return a[k]
205209
206210
207211cpdef numeric median(numeric[:] arr):
@@ -238,17 +242,18 @@ def max_subseq(ndarray[double_t] arr):
238242 S = m
239243 T = 0
240244
241- for i in range (1 , n):
242- # S = max { S + A[i], A[i] )
243- if (S > 0 ):
244- S = S + arr[i]
245- else :
246- S = arr[i]
247- T = i
248- if S > m:
249- s = T
250- e = i
251- m = S
245+ with nogil:
246+ for i in range (1 , n):
247+ # S = max { S + A[i], A[i] )
248+ if (S > 0 ):
249+ S = S + arr[i]
250+ else :
251+ S = arr[i]
252+ T = i
253+ if S > m:
254+ s = T
255+ e = i
256+ m = S
252257
253258 return (s, e, m)
254259
@@ -268,9 +273,10 @@ def min_subseq(ndarray[double_t] arr):
268273
269274@ cython.boundscheck (False )
270275@ cython.wraparound (False )
271- def nancorr (ndarray[float64_t , ndim = 2 ] mat, cov = False , minp = None ):
276+ def nancorr (ndarray[float64_t , ndim = 2 ] mat, bint cov = 0 , minp = None ):
272277 cdef:
273278 Py_ssize_t i, j, xi, yi, N, K
279+ bint minpv
274280 ndarray[float64_t, ndim= 2 ] result
275281 ndarray[uint8_t, ndim= 2 ] mask
276282 int64_t nobs = 0
@@ -279,46 +285,49 @@ def nancorr(ndarray[float64_t, ndim=2] mat, cov=False, minp=None):
279285 N, K = (< object > mat).shape
280286
281287 if minp is None :
282- minp = 1
288+ minpv = 1
289+ else :
290+ minpv = < int > minp
283291
284292 result = np.empty((K, K), dtype = np.float64)
285293 mask = np.isfinite(mat).view(np.uint8)
286294
287- for xi in range (K):
288- for yi in range (xi + 1 ):
289- nobs = sumxx = sumyy = sumx = sumy = 0
290- for i in range (N):
291- if mask[i, xi] and mask[i, yi]:
292- vx = mat[i, xi]
293- vy = mat[i, yi]
294- nobs += 1
295- sumx += vx
296- sumy += vy
297-
298- if nobs < minp:
299- result[xi, yi] = result[yi, xi] = np.NaN
300- else :
301- meanx = sumx / nobs
302- meany = sumy / nobs
303-
304- # now the cov numerator
305- sumx = 0
306-
295+ with nogil:
296+ for xi in range (K):
297+ for yi in range (xi + 1 ):
298+ nobs = sumxx = sumyy = sumx = sumy = 0
307299 for i in range (N):
308300 if mask[i, xi] and mask[i, yi]:
309- vx = mat[i, xi] - meanx
310- vy = mat[i, yi] - meany
301+ vx = mat[i, xi]
302+ vy = mat[i, yi]
303+ nobs += 1
304+ sumx += vx
305+ sumy += vy
306+
307+ if nobs < minpv:
308+ result[xi, yi] = result[yi, xi] = NaN
309+ else :
310+ meanx = sumx / nobs
311+ meany = sumy / nobs
311312
312- sumx += vx * vy
313- sumxx += vx * vx
314- sumyy += vy * vy
313+ # now the cov numerator
314+ sumx = 0
315315
316- divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
316+ for i in range (N):
317+ if mask[i, xi] and mask[i, yi]:
318+ vx = mat[i, xi] - meanx
319+ vy = mat[i, yi] - meany
317320
318- if divisor != 0 :
319- result[xi, yi] = result[yi, xi] = sumx / divisor
320- else :
321- result[xi, yi] = result[yi, xi] = np.NaN
321+ sumx += vx * vy
322+ sumxx += vx * vx
323+ sumyy += vy * vy
324+
325+ divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
326+
327+ if divisor != 0 :
328+ result[xi, yi] = result[yi, xi] = sumx / divisor
329+ else :
330+ result[xi, yi] = result[yi, xi] = NaN
322331
323332 return result
324333
@@ -351,7 +360,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
351360 nobs += 1
352361
353362 if nobs < minp:
354- result[xi, yi] = result[yi, xi] = np. NaN
363+ result[xi, yi] = result[yi, xi] = NaN
355364 else :
356365 maskedx = np.empty(nobs, dtype = np.float64)
357366 maskedy = np.empty(nobs, dtype = np.float64)
@@ -382,7 +391,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
382391 if divisor != 0 :
383392 result[xi, yi] = result[yi, xi] = sumx / divisor
384393 else :
385- result[xi, yi] = result[yi, xi] = np. NaN
394+ result[xi, yi] = result[yi, xi] = NaN
386395
387396 return result
388397
0 commit comments