@@ -96,7 +96,6 @@ class NegInfinity(object):
96
96
__ge__ = lambda self , other : self is other
97
97
98
98
99
-
100
99
@ cython.wraparound (False )
101
100
@ cython.boundscheck (False )
102
101
def is_lexsorted (list list_of_arrays ):
@@ -105,6 +104,7 @@ def is_lexsorted(list list_of_arrays):
105
104
Py_ssize_t n, nlevels
106
105
int64_t k, cur, pre
107
106
ndarray arr
107
+ bint result = 1
108
108
109
109
nlevels = len (list_of_arrays)
110
110
n = len (list_of_arrays[0 ])
@@ -115,18 +115,20 @@ def is_lexsorted(list list_of_arrays):
115
115
vecs[i] = < int64_t* > arr.data
116
116
117
117
# 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
118
+ with nogil:
119
+ for i from 1 <= i < n:
120
+ for k from 0 <= k < nlevels:
121
+ cur = vecs[k][i]
122
+ pre = vecs[k][i - 1 ]
123
+ if cur == pre:
124
+ continue
125
+ elif cur > pre:
126
+ break
127
+ else :
128
+ result = 0
129
+ break
128
130
free(vecs)
129
- return True
131
+ return result
130
132
131
133
132
134
@ cython.boundscheck (False )
@@ -177,10 +179,11 @@ def groupsort_indexer(ndarray[int64_t] index, Py_ssize_t ngroups):
177
179
178
180
@ cython.boundscheck (False )
179
181
@ cython.wraparound (False )
180
- cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k):
182
+ cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k) nogil :
181
183
cdef:
182
- Py_ssize_t i, j, l, m, n = a.size
184
+ Py_ssize_t i, j, l, m, n = a.shape[ 0 ]
183
185
numeric x
186
+
184
187
with nogil:
185
188
l = 0
186
189
m = n - 1
@@ -201,7 +204,7 @@ cpdef numeric kth_smallest(numeric[:] a, Py_ssize_t k):
201
204
202
205
if j < k: l = i
203
206
if k < i: m = j
204
- return a[k]
207
+ return a[k]
205
208
206
209
207
210
cpdef numeric median(numeric[:] arr):
@@ -238,17 +241,18 @@ def max_subseq(ndarray[double_t] arr):
238
241
S = m
239
242
T = 0
240
243
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
244
+ with nogil:
245
+ for i in range (1 , n):
246
+ # S = max { S + A[i], A[i] )
247
+ if (S > 0 ):
248
+ S = S + arr[i]
249
+ else :
250
+ S = arr[i]
251
+ T = i
252
+ if S > m:
253
+ s = T
254
+ e = i
255
+ m = S
252
256
253
257
return (s, e, m)
254
258
@@ -268,9 +272,10 @@ def min_subseq(ndarray[double_t] arr):
268
272
269
273
@ cython.boundscheck (False )
270
274
@ cython.wraparound (False )
271
- def nancorr (ndarray[float64_t , ndim = 2 ] mat, cov = False , minp = None ):
275
+ def nancorr (ndarray[float64_t , ndim = 2 ] mat, bint cov = 0 , minp = None ):
272
276
cdef:
273
277
Py_ssize_t i, j, xi, yi, N, K
278
+ bint minpv
274
279
ndarray[float64_t, ndim= 2 ] result
275
280
ndarray[uint8_t, ndim= 2 ] mask
276
281
int64_t nobs = 0
@@ -279,46 +284,49 @@ def nancorr(ndarray[float64_t, ndim=2] mat, cov=False, minp=None):
279
284
N, K = (< object > mat).shape
280
285
281
286
if minp is None :
282
- minp = 1
287
+ minpv = 1
288
+ else :
289
+ minpv = < int > minp
283
290
284
291
result = np.empty((K, K), dtype = np.float64)
285
292
mask = np.isfinite(mat).view(np.uint8)
286
293
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
-
294
+ with nogil:
295
+ for xi in range (K):
296
+ for yi in range (xi + 1 ):
297
+ nobs = sumxx = sumyy = sumx = sumy = 0
307
298
for i in range (N):
308
299
if mask[i, xi] and mask[i, yi]:
309
- vx = mat[i, xi] - meanx
310
- vy = mat[i, yi] - meany
300
+ vx = mat[i, xi]
301
+ vy = mat[i, yi]
302
+ nobs += 1
303
+ sumx += vx
304
+ sumy += vy
305
+
306
+ if nobs < minpv:
307
+ result[xi, yi] = result[yi, xi] = NaN
308
+ else :
309
+ meanx = sumx / nobs
310
+ meany = sumy / nobs
311
311
312
- sumx += vx * vy
313
- sumxx += vx * vx
314
- sumyy += vy * vy
312
+ # now the cov numerator
313
+ sumx = 0
315
314
316
- divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
315
+ for i in range (N):
316
+ if mask[i, xi] and mask[i, yi]:
317
+ vx = mat[i, xi] - meanx
318
+ vy = mat[i, yi] - meany
317
319
318
- if divisor != 0 :
319
- result[xi, yi] = result[yi, xi] = sumx / divisor
320
- else :
321
- result[xi, yi] = result[yi, xi] = np.NaN
320
+ sumx += vx * vy
321
+ sumxx += vx * vx
322
+ sumyy += vy * vy
323
+
324
+ divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
325
+
326
+ if divisor != 0 :
327
+ result[xi, yi] = result[yi, xi] = sumx / divisor
328
+ else :
329
+ result[xi, yi] = result[yi, xi] = NaN
322
330
323
331
return result
324
332
@@ -351,7 +359,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
351
359
nobs += 1
352
360
353
361
if nobs < minp:
354
- result[xi, yi] = result[yi, xi] = np. NaN
362
+ result[xi, yi] = result[yi, xi] = NaN
355
363
else :
356
364
maskedx = np.empty(nobs, dtype = np.float64)
357
365
maskedy = np.empty(nobs, dtype = np.float64)
@@ -382,7 +390,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
382
390
if divisor != 0 :
383
391
result[xi, yi] = result[yi, xi] = sumx / divisor
384
392
else :
385
- result[xi, yi] = result[yi, xi] = np. NaN
393
+ result[xi, yi] = result[yi, xi] = NaN
386
394
387
395
return result
388
396
0 commit comments