@@ -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,28 +104,31 @@ 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 = True
108
108
109
109
nlevels = len (list_of_arrays)
110
110
n = len (list_of_arrays[0 ])
111
111
112
112
cdef int64_t ** vecs = < int64_t** > malloc(nlevels * sizeof(int64_t* ))
113
- for i from 0 <= i < nlevels:
113
+ for i in range ( nlevels) :
114
114
arr = list_of_arrays[i]
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 in range (n):
120
+ for k in range (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 = False
129
+ break
128
130
free(vecs)
129
- return True
131
+ return result
130
132
131
133
132
134
@ cython.boundscheck (False )
@@ -159,15 +161,15 @@ def groupsort_indexer(ndarray[int64_t] index, Py_ssize_t ngroups):
159
161
with nogil:
160
162
161
163
# count group sizes, location 0 for NA
162
- for i from 0 <= i < n :
164
+ for i in range (n) :
163
165
counts[index[i] + 1 ] += 1
164
166
165
167
# mark the start of each contiguous group of like-indexed data
166
- for i from 1 <= i < ngroups + 1 :
168
+ for i in range ( 1 , ngroups + 1 ) :
167
169
where[i] = where[i - 1 ] + counts[i - 1 ]
168
170
169
171
# this is our indexer
170
- for i from 0 <= i < n :
172
+ for i in range (n) :
171
173
label = index[i] + 1
172
174
result[where[label]] = i
173
175
where[label] += 1
@@ -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):
@@ -224,6 +227,8 @@ cpdef numeric median(numeric[:] arr):
224
227
225
228
# -------------- Min, Max subsequence
226
229
230
+ @ cython.boundscheck (False )
231
+ @ cython.wraparound (False )
227
232
def max_subseq (ndarray[double_t] arr ):
228
233
cdef:
229
234
Py_ssize_t i= 0 , s= 0 , e= 0 , T, n
@@ -238,21 +243,24 @@ def max_subseq(ndarray[double_t] arr):
238
243
S = m
239
244
T = 0
240
245
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
246
+ with nogil:
247
+ for i in range (1 , n):
248
+ # S = max { S + A[i], A[i] )
249
+ if (S > 0 ):
250
+ S = S + arr[i]
251
+ else :
252
+ S = arr[i]
253
+ T = i
254
+ if S > m:
255
+ s = T
256
+ e = i
257
+ m = S
252
258
253
259
return (s, e, m)
254
260
255
261
262
+ @ cython.boundscheck (False )
263
+ @ cython.wraparound (False )
256
264
def min_subseq (ndarray[double_t] arr ):
257
265
cdef:
258
266
Py_ssize_t s, e
@@ -268,9 +276,10 @@ def min_subseq(ndarray[double_t] arr):
268
276
269
277
@ cython.boundscheck (False )
270
278
@ cython.wraparound (False )
271
- def nancorr (ndarray[float64_t , ndim = 2 ] mat, cov = False , minp = None ):
279
+ def nancorr (ndarray[float64_t , ndim = 2 ] mat, bint cov = 0 , minp = None ):
272
280
cdef:
273
281
Py_ssize_t i, j, xi, yi, N, K
282
+ bint minpv
274
283
ndarray[float64_t, ndim= 2 ] result
275
284
ndarray[uint8_t, ndim= 2 ] mask
276
285
int64_t nobs = 0
@@ -279,46 +288,49 @@ def nancorr(ndarray[float64_t, ndim=2] mat, cov=False, minp=None):
279
288
N, K = (< object > mat).shape
280
289
281
290
if minp is None :
282
- minp = 1
291
+ minpv = 1
292
+ else :
293
+ minpv = < int > minp
283
294
284
295
result = np.empty((K, K), dtype = np.float64)
285
296
mask = np.isfinite(mat).view(np.uint8)
286
297
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
-
298
+ with nogil:
299
+ for xi in range (K):
300
+ for yi in range (xi + 1 ):
301
+ nobs = sumxx = sumyy = sumx = sumy = 0
307
302
for i in range (N):
308
303
if mask[i, xi] and mask[i, yi]:
309
- vx = mat[i, xi] - meanx
310
- vy = mat[i, yi] - meany
304
+ vx = mat[i, xi]
305
+ vy = mat[i, yi]
306
+ nobs += 1
307
+ sumx += vx
308
+ sumy += vy
309
+
310
+ if nobs < minpv:
311
+ result[xi, yi] = result[yi, xi] = NaN
312
+ else :
313
+ meanx = sumx / nobs
314
+ meany = sumy / nobs
311
315
312
- sumx += vx * vy
313
- sumxx += vx * vx
314
- sumyy += vy * vy
316
+ # now the cov numerator
317
+ sumx = 0
315
318
316
- divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
319
+ for i in range (N):
320
+ if mask[i, xi] and mask[i, yi]:
321
+ vx = mat[i, xi] - meanx
322
+ vy = mat[i, yi] - meany
317
323
318
- if divisor != 0 :
319
- result[xi, yi] = result[yi, xi] = sumx / divisor
320
- else :
321
- result[xi, yi] = result[yi, xi] = np.NaN
324
+ sumx += vx * vy
325
+ sumxx += vx * vx
326
+ sumyy += vy * vy
327
+
328
+ divisor = (nobs - 1.0 ) if cov else sqrt(sumxx * sumyy)
329
+
330
+ if divisor != 0 :
331
+ result[xi, yi] = result[yi, xi] = sumx / divisor
332
+ else :
333
+ result[xi, yi] = result[yi, xi] = NaN
322
334
323
335
return result
324
336
@@ -351,7 +363,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
351
363
nobs += 1
352
364
353
365
if nobs < minp:
354
- result[xi, yi] = result[yi, xi] = np. NaN
366
+ result[xi, yi] = result[yi, xi] = NaN
355
367
else :
356
368
maskedx = np.empty(nobs, dtype = np.float64)
357
369
maskedy = np.empty(nobs, dtype = np.float64)
@@ -382,7 +394,7 @@ def nancorr_spearman(ndarray[float64_t, ndim=2] mat, Py_ssize_t minp=1):
382
394
if divisor != 0 :
383
395
result[xi, yi] = result[yi, xi] = sumx / divisor
384
396
else :
385
- result[xi, yi] = result[yi, xi] = np. NaN
397
+ result[xi, yi] = result[yi, xi] = NaN
386
398
387
399
return result
388
400
0 commit comments