-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
/
Copy pathalgos.pyx
777 lines (629 loc) · 20.2 KB
/
algos.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
import cython
from cython import Py_ssize_t
from libc.stdlib cimport malloc, free
from libc.string cimport memmove
from libc.math cimport fabs, sqrt
import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
NPY_INT64, NPY_INT32, NPY_INT16, NPY_INT8,
NPY_UINT64, NPY_UINT32, NPY_UINT16, NPY_UINT8,
NPY_FLOAT32, NPY_FLOAT64,
NPY_OBJECT,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t)
cnp.import_array()
cimport pandas._libs.util as util
from pandas._libs.util cimport numeric, get_nat
from pandas._libs.khash cimport (
khiter_t, kh_destroy_int64, kh_put_int64, kh_init_int64, kh_int64_t,
kh_resize_int64, kh_get_int64)
import pandas._libs.missing as missing
cdef float64_t FP_ERR = 1e-13
cdef float64_t NaN = <float64_t>np.NaN
cdef int64_t NPY_NAT = get_nat()
tiebreakers = {
'average': TIEBREAK_AVERAGE,
'min': TIEBREAK_MIN,
'max': TIEBREAK_MAX,
'first': TIEBREAK_FIRST,
'dense': TIEBREAK_DENSE,
}
cdef inline bint are_diff(object left, object right):
try:
return fabs(left - right) > FP_ERR
except TypeError:
return left != right
class Infinity:
""" provide a positive Infinity comparison method for ranking """
__lt__ = lambda self, other: False
__le__ = lambda self, other: isinstance(other, Infinity)
__eq__ = lambda self, other: isinstance(other, Infinity)
__ne__ = lambda self, other: not isinstance(other, Infinity)
__gt__ = lambda self, other: (not isinstance(other, Infinity) and
not missing.checknull(other))
__ge__ = lambda self, other: not missing.checknull(other)
class NegInfinity:
""" provide a negative Infinity comparison method for ranking """
__lt__ = lambda self, other: (not isinstance(other, NegInfinity) and
not missing.checknull(other))
__le__ = lambda self, other: not missing.checknull(other)
__eq__ = lambda self, other: isinstance(other, NegInfinity)
__ne__ = lambda self, other: not isinstance(other, NegInfinity)
__gt__ = lambda self, other: False
__ge__ = lambda self, other: isinstance(other, NegInfinity)
@cython.wraparound(False)
@cython.boundscheck(False)
cpdef ndarray[int64_t, ndim=1] unique_deltas(const int64_t[:] arr):
"""
Efficiently find the unique first-differences of the given array.
Parameters
----------
arr : ndarray[in64_t]
Returns
-------
result : ndarray[int64_t]
result is sorted
"""
cdef:
Py_ssize_t i, n = len(arr)
int64_t val
khiter_t k
kh_int64_t *table
int ret = 0
list uniques = []
table = kh_init_int64()
kh_resize_int64(table, 10)
for i in range(n - 1):
val = arr[i + 1] - arr[i]
k = kh_get_int64(table, val)
if k == table.n_buckets:
kh_put_int64(table, val, &ret)
uniques.append(val)
kh_destroy_int64(table)
result = np.array(uniques, dtype=np.int64)
result.sort()
return result
@cython.wraparound(False)
@cython.boundscheck(False)
def is_lexsorted(list_of_arrays: list) -> bint:
cdef:
Py_ssize_t i
Py_ssize_t n, nlevels
int64_t k, cur, pre
ndarray arr
bint result = True
nlevels = len(list_of_arrays)
n = len(list_of_arrays[0])
cdef int64_t **vecs = <int64_t**>malloc(nlevels * sizeof(int64_t*))
for i in range(nlevels):
arr = list_of_arrays[i]
assert arr.dtype.name == 'int64'
vecs[i] = <int64_t*>cnp.PyArray_DATA(arr)
# Assume uniqueness??
with nogil:
for i in range(1, n):
for k in range(nlevels):
cur = vecs[k][i]
pre = vecs[k][i -1]
if cur == pre:
continue
elif cur > pre:
break
else:
result = False
break
free(vecs)
return result
@cython.boundscheck(False)
@cython.wraparound(False)
def groupsort_indexer(const int64_t[:] index, Py_ssize_t ngroups):
"""
compute a 1-d indexer that is an ordering of the passed index,
ordered by the groups. This is a reverse of the label
factorization process.
Parameters
----------
index: int64 ndarray
mappings from group -> position
ngroups: int64
number of groups
return a tuple of (1-d indexer ordered by groups, group counts)
"""
cdef:
Py_ssize_t i, loc, label, n
ndarray[int64_t] counts, where, result
counts = np.zeros(ngroups + 1, dtype=np.int64)
n = len(index)
result = np.zeros(n, dtype=np.int64)
where = np.zeros(ngroups + 1, dtype=np.int64)
with nogil:
# count group sizes, location 0 for NA
for i in range(n):
counts[index[i] + 1] += 1
# mark the start of each contiguous group of like-indexed data
for i in range(1, ngroups + 1):
where[i] = where[i - 1] + counts[i - 1]
# this is our indexer
for i in range(n):
label = index[i] + 1
result[where[label]] = i
where[label] += 1
return result, counts
@cython.boundscheck(False)
@cython.wraparound(False)
def kth_smallest(numeric[:] a, Py_ssize_t k) -> numeric:
cdef:
Py_ssize_t i, j, l, m, n = a.shape[0]
numeric x
with nogil:
l = 0
m = n - 1
while l < m:
x = a[k]
i = l
j = m
while 1:
while a[i] < x: i += 1
while x < a[j]: j -= 1
if i <= j:
swap(&a[i], &a[j])
i += 1; j -= 1
if i > j: break
if j < k: l = i
if k < i: m = j
return a[k]
# ----------------------------------------------------------------------
# Pairwise correlation/covariance
@cython.boundscheck(False)
@cython.wraparound(False)
def nancorr(const float64_t[:, :] mat, bint cov=0, minp=None):
cdef:
Py_ssize_t i, j, xi, yi, N, K
bint minpv
ndarray[float64_t, ndim=2] result
ndarray[uint8_t, ndim=2] mask
int64_t nobs = 0
float64_t vx, vy, sumx, sumy, sumxx, sumyy, meanx, meany, divisor
N, K = (<object>mat).shape
if minp is None:
minpv = 1
else:
minpv = <int>minp
result = np.empty((K, K), dtype=np.float64)
mask = np.isfinite(mat).view(np.uint8)
with nogil:
for xi in range(K):
for yi in range(xi + 1):
nobs = sumxx = sumyy = sumx = sumy = 0
for i in range(N):
if mask[i, xi] and mask[i, yi]:
vx = mat[i, xi]
vy = mat[i, yi]
nobs += 1
sumx += vx
sumy += vy
if nobs < minpv:
result[xi, yi] = result[yi, xi] = NaN
else:
meanx = sumx / nobs
meany = sumy / nobs
# now the cov numerator
sumx = 0
for i in range(N):
if mask[i, xi] and mask[i, yi]:
vx = mat[i, xi] - meanx
vy = mat[i, yi] - meany
sumx += vx * vy
sumxx += vx * vx
sumyy += vy * vy
divisor = (nobs - 1.0) if cov else sqrt(sumxx * sumyy)
if divisor != 0:
result[xi, yi] = result[yi, xi] = sumx / divisor
else:
result[xi, yi] = result[yi, xi] = NaN
return result
# ----------------------------------------------------------------------
# Pairwise Spearman correlation
@cython.boundscheck(False)
@cython.wraparound(False)
def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1):
cdef:
Py_ssize_t i, j, xi, yi, N, K
ndarray[float64_t, ndim=2] result
ndarray[float64_t, ndim=2] ranked_mat
ndarray[float64_t, ndim=1] maskedx
ndarray[float64_t, ndim=1] maskedy
ndarray[uint8_t, ndim=2] mask
int64_t nobs = 0
float64_t vx, vy, sumx, sumxx, sumyy, mean, divisor
N, K = (<object>mat).shape
result = np.empty((K, K), dtype=np.float64)
mask = np.isfinite(mat).view(np.uint8)
ranked_mat = np.empty((N, K), dtype=np.float64)
for i in range(K):
ranked_mat[:, i] = rank_1d_float64(mat[:, i])
for xi in range(K):
for yi in range(xi + 1):
nobs = 0
# Keep track of whether we need to recompute ranks
all_ranks = True
for i in range(N):
all_ranks &= not (mask[i, xi] ^ mask[i, yi])
if mask[i, xi] and mask[i, yi]:
nobs += 1
if nobs < minp:
result[xi, yi] = result[yi, xi] = NaN
else:
maskedx = np.empty(nobs, dtype=np.float64)
maskedy = np.empty(nobs, dtype=np.float64)
j = 0
for i in range(N):
if mask[i, xi] and mask[i, yi]:
maskedx[j] = ranked_mat[i, xi]
maskedy[j] = ranked_mat[i, yi]
j += 1
if not all_ranks:
maskedx = rank_1d_float64(maskedx)
maskedy = rank_1d_float64(maskedy)
mean = (nobs + 1) / 2.
# now the cov numerator
sumx = sumxx = sumyy = 0
for i in range(nobs):
vx = maskedx[i] - mean
vy = maskedy[i] - mean
sumx += vx * vy
sumxx += vx * vx
sumyy += vy * vy
divisor = sqrt(sumxx * sumyy)
if divisor != 0:
result[xi, yi] = result[yi, xi] = sumx / divisor
else:
result[xi, yi] = result[yi, xi] = NaN
return result
# ----------------------------------------------------------------------
ctypedef fused algos_t:
float64_t
float32_t
object
int64_t
int32_t
int16_t
int8_t
uint64_t
uint32_t
uint16_t
uint8_t
@cython.boundscheck(False)
@cython.wraparound(False)
def pad(ndarray[algos_t] old, ndarray[algos_t] new, limit=None):
cdef:
Py_ssize_t i, j, nleft, nright
ndarray[int64_t, ndim=1] indexer
algos_t cur, next
int lim, fill_count = 0
nleft = len(old)
nright = len(new)
indexer = np.empty(nright, dtype=np.int64)
indexer[:] = -1
if limit is None:
lim = nright
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
if nleft == 0 or nright == 0 or new[nright - 1] < old[0]:
return indexer
i = j = 0
cur = old[0]
while j <= nright - 1 and new[j] < cur:
j += 1
while True:
if j == nright:
break
if i == nleft - 1:
while j < nright:
if new[j] == cur:
indexer[j] = i
elif new[j] > cur and fill_count < lim:
indexer[j] = i
fill_count += 1
j += 1
break
next = old[i + 1]
while j < nright and cur <= new[j] < next:
if new[j] == cur:
indexer[j] = i
elif fill_count < lim:
indexer[j] = i
fill_count += 1
j += 1
fill_count = 0
i += 1
cur = next
return indexer
@cython.boundscheck(False)
@cython.wraparound(False)
def pad_inplace(algos_t[:] values,
const uint8_t[:] mask,
limit=None):
cdef:
Py_ssize_t i, N
algos_t val
int lim, fill_count = 0
N = len(values)
# GH#2778
if N == 0:
return
if limit is None:
lim = N
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
val = values[0]
for i in range(N):
if mask[i]:
if fill_count >= lim:
continue
fill_count += 1
values[i] = val
else:
fill_count = 0
val = values[i]
@cython.boundscheck(False)
@cython.wraparound(False)
def pad_2d_inplace(algos_t[:, :] values,
const uint8_t[:, :] mask,
limit=None):
cdef:
Py_ssize_t i, j, N, K
algos_t val
int lim, fill_count = 0
K, N = (<object>values).shape
# GH#2778
if N == 0:
return
if limit is None:
lim = N
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
for j in range(K):
fill_count = 0
val = values[j, 0]
for i in range(N):
if mask[j, i]:
if fill_count >= lim:
continue
fill_count += 1
values[j, i] = val
else:
fill_count = 0
val = values[j, i]
"""
Backfilling logic for generating fill vector
Diagram of what's going on
Old New Fill vector Mask
. 0 1
. 0 1
. 0 1
A A 0 1
. 1 1
. 1 1
. 1 1
. 1 1
. 1 1
B B 1 1
. 2 1
. 2 1
. 2 1
C C 2 1
. 0
. 0
D
"""
@cython.boundscheck(False)
@cython.wraparound(False)
def backfill(ndarray[algos_t] old, ndarray[algos_t] new, limit=None):
cdef:
Py_ssize_t i, j, nleft, nright
ndarray[int64_t, ndim=1] indexer
algos_t cur, prev
int lim, fill_count = 0
nleft = len(old)
nright = len(new)
indexer = np.empty(nright, dtype=np.int64)
indexer[:] = -1
if limit is None:
lim = nright
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
if nleft == 0 or nright == 0 or new[0] > old[nleft - 1]:
return indexer
i = nleft - 1
j = nright - 1
cur = old[nleft - 1]
while j >= 0 and new[j] > cur:
j -= 1
while True:
if j < 0:
break
if i == 0:
while j >= 0:
if new[j] == cur:
indexer[j] = i
elif new[j] < cur and fill_count < lim:
indexer[j] = i
fill_count += 1
j -= 1
break
prev = old[i - 1]
while j >= 0 and prev < new[j] <= cur:
if new[j] == cur:
indexer[j] = i
elif new[j] < cur and fill_count < lim:
indexer[j] = i
fill_count += 1
j -= 1
fill_count = 0
i -= 1
cur = prev
return indexer
@cython.boundscheck(False)
@cython.wraparound(False)
def backfill_inplace(algos_t[:] values,
const uint8_t[:] mask,
limit=None):
cdef:
Py_ssize_t i, N
algos_t val
int lim, fill_count = 0
N = len(values)
# GH#2778
if N == 0:
return
if limit is None:
lim = N
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
val = values[N - 1]
for i in range(N - 1, -1, -1):
if mask[i]:
if fill_count >= lim:
continue
fill_count += 1
values[i] = val
else:
fill_count = 0
val = values[i]
@cython.boundscheck(False)
@cython.wraparound(False)
def backfill_2d_inplace(algos_t[:, :] values,
const uint8_t[:, :] mask,
limit=None):
cdef:
Py_ssize_t i, j, N, K
algos_t val
int lim, fill_count = 0
K, N = (<object>values).shape
# GH#2778
if N == 0:
return
if limit is None:
lim = N
else:
if not util.is_integer_object(limit):
raise ValueError('Limit must be an integer')
if limit < 1:
raise ValueError('Limit must be greater than 0')
lim = limit
for j in range(K):
fill_count = 0
val = values[j, N - 1]
for i in range(N - 1, -1, -1):
if mask[j, i]:
if fill_count >= lim:
continue
fill_count += 1
values[j, i] = val
else:
fill_count = 0
val = values[j, i]
@cython.boundscheck(False)
@cython.wraparound(False)
def is_monotonic(ndarray[algos_t, ndim=1] arr, bint timelike):
"""
Returns
-------
is_monotonic_inc, is_monotonic_dec, is_unique
"""
cdef:
Py_ssize_t i, n
algos_t prev, cur
bint is_monotonic_inc = 1
bint is_monotonic_dec = 1
bint is_unique = 1
bint is_strict_monotonic = 1
n = len(arr)
if n == 1:
if arr[0] != arr[0] or (timelike and <int64_t>arr[0] == NPY_NAT):
# single value is NaN
return False, False, True
else:
return True, True, True
elif n < 2:
return True, True, True
if timelike and <int64_t>arr[0] == NPY_NAT:
return False, False, True
if algos_t is not object:
with nogil:
prev = arr[0]
for i in range(1, n):
cur = arr[i]
if timelike and <int64_t>cur == NPY_NAT:
is_monotonic_inc = 0
is_monotonic_dec = 0
break
if cur < prev:
is_monotonic_inc = 0
elif cur > prev:
is_monotonic_dec = 0
elif cur == prev:
is_unique = 0
else:
# cur or prev is NaN
is_monotonic_inc = 0
is_monotonic_dec = 0
break
if not is_monotonic_inc and not is_monotonic_dec:
is_monotonic_inc = 0
is_monotonic_dec = 0
break
prev = cur
else:
# object-dtype, identical to above except we cannot use `with nogil`
prev = arr[0]
for i in range(1, n):
cur = arr[i]
if timelike and <int64_t>cur == NPY_NAT:
is_monotonic_inc = 0
is_monotonic_dec = 0
break
if cur < prev:
is_monotonic_inc = 0
elif cur > prev:
is_monotonic_dec = 0
elif cur == prev:
is_unique = 0
else:
# cur or prev is NaN
is_monotonic_inc = 0
is_monotonic_dec = 0
break
if not is_monotonic_inc and not is_monotonic_dec:
is_monotonic_inc = 0
is_monotonic_dec = 0
break
prev = cur
is_strict_monotonic = is_unique and (is_monotonic_inc or is_monotonic_dec)
return is_monotonic_inc, is_monotonic_dec, is_strict_monotonic
# generated from template
include "algos_common_helper.pxi"
include "algos_rank_helper.pxi"
include "algos_take_helper.pxi"