forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalgos_rank_helper.pxi.in
391 lines (331 loc) · 11.7 KB
/
algos_rank_helper.pxi.in
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
"""
Template for each `dtype` helper function for rank
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
#----------------------------------------------------------------------
# rank_1d, rank_2d
#----------------------------------------------------------------------
{{py:
# dtype ctype pos_nan_value neg_nan_value
dtypes = [('object', 'object', 'Infinity()', 'NegInfinity()'),
('float64', 'float64_t', 'np.inf', '-np.inf'),
('uint64', 'uint64_t', '', ''),
('int64', 'int64_t', 'np.iinfo(np.int64).max',
'np.iinfo(np.int64).min')]
}}
{{for dtype, ctype, pos_nan_value, neg_nan_value in dtypes}}
@cython.wraparound(False)
@cython.boundscheck(False)
def rank_1d_{{dtype}}(object in_arr, ties_method='average',
ascending=True, na_option='keep', pct=False):
"""
Fast NaN-friendly version of scipy.stats.rankdata
"""
cdef:
Py_ssize_t i, j, n, dups = 0, total_tie_count = 0, non_na_idx = 0
{{if dtype == 'object'}}
ndarray sorted_data, values
{{else}}
ndarray[{{ctype}}] sorted_data, values
{{endif}}
ndarray[float64_t] ranks
ndarray[int64_t] argsorted
ndarray[uint8_t, cast=True] sorted_mask
{{if dtype == 'uint64'}}
{{ctype}} val
{{else}}
{{ctype}} val, nan_value
{{endif}}
float64_t sum_ranks = 0
int tiebreak = 0
bint keep_na = 0
bint isnan
float count = 0.0
tiebreak = tiebreakers[ties_method]
{{if dtype == 'float64'}}
values = np.asarray(in_arr).copy()
{{elif dtype == 'object'}}
values = np.array(in_arr, copy=True)
if values.dtype != np.object_:
values = values.astype('O')
{{else}}
values = np.asarray(in_arr)
{{endif}}
keep_na = na_option == 'keep'
{{if dtype == 'object'}}
mask = missing.isnaobj(values)
{{elif dtype == 'float64'}}
mask = np.isnan(values)
{{elif dtype == 'int64'}}
mask = values == iNaT
# create copy in case of iNaT
# values are mutated inplace
if mask.any():
values = values.copy()
{{endif}}
# double sort first by mask and then by values to ensure nan values are
# either at the beginning or the end. mask/(~mask) controls padding at
# tail or the head
{{if dtype != 'uint64'}}
if ascending ^ (na_option == 'top'):
nan_value = {{pos_nan_value}}
order = (values, mask)
else:
nan_value = {{neg_nan_value}}
order = (values, ~mask)
np.putmask(values, mask, nan_value)
{{else}}
mask = np.zeros(shape=len(values), dtype=bool)
order = (values, mask)
{{endif}}
n = len(values)
ranks = np.empty(n, dtype='f8')
{{if dtype == 'object'}}
try:
_as = np.lexsort(keys=order)
except TypeError:
# lexsort on object array will raise TypeError for numpy version
# earlier than 1.11.0. Use argsort with order argument instead.
_dt = [('values', 'O'), ('mask', '?')]
_values = np.asarray(list(zip(order[0], order[1])), dtype=_dt)
_as = np.argsort(_values, kind='mergesort', order=('mask', 'values'))
{{else}}
if tiebreak == TIEBREAK_FIRST:
# need to use a stable sort here
_as = np.lexsort(keys=order)
if not ascending:
tiebreak = TIEBREAK_FIRST_DESCENDING
else:
_as = np.lexsort(keys=order)
{{endif}}
if not ascending:
_as = _as[::-1]
sorted_data = values.take(_as)
sorted_mask = mask.take(_as)
_indices = np.diff(sorted_mask).nonzero()[0]
non_na_idx = _indices[0] if len(_indices) > 0 else -1
argsorted = _as.astype('i8')
{{if dtype == 'object'}}
for i in range(n):
sum_ranks += i + 1
dups += 1
isnan = sorted_mask[i]
val = util.get_value_at(sorted_data, i)
if isnan and keep_na:
ranks[argsorted[i]] = nan
continue
count += 1.0
if (i == n - 1 or
are_diff(util.get_value_at(sorted_data, i + 1), val) or
i == non_na_idx):
if tiebreak == TIEBREAK_AVERAGE:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = sum_ranks / dups
elif tiebreak == TIEBREAK_MIN:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = i - dups + 2
elif tiebreak == TIEBREAK_MAX:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = i + 1
elif tiebreak == TIEBREAK_FIRST:
raise ValueError('first not supported for non-numeric data')
elif tiebreak == TIEBREAK_FIRST_DESCENDING:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = 2 * i - j - dups + 2
elif tiebreak == TIEBREAK_DENSE:
total_tie_count += 1
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = total_tie_count
sum_ranks = dups = 0
{{else}}
with nogil:
for i in range(n):
sum_ranks += i + 1
dups += 1
val = sorted_data[i]
{{if dtype != 'uint64'}}
isnan = sorted_mask[i]
if isnan and keep_na:
ranks[argsorted[i]] = nan
continue
{{endif}}
count += 1.0
if (i == n - 1 or sorted_data[i + 1] != val or
i == non_na_idx):
if tiebreak == TIEBREAK_AVERAGE:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = sum_ranks / dups
elif tiebreak == TIEBREAK_MIN:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = i - dups + 2
elif tiebreak == TIEBREAK_MAX:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = i + 1
elif tiebreak == TIEBREAK_FIRST:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = j + 1
elif tiebreak == TIEBREAK_FIRST_DESCENDING:
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = 2 * i - j - dups + 2
elif tiebreak == TIEBREAK_DENSE:
total_tie_count += 1
for j in range(i - dups + 1, i + 1):
ranks[argsorted[j]] = total_tie_count
sum_ranks = dups = 0
{{endif}}
if pct:
if tiebreak == TIEBREAK_DENSE:
return ranks / total_tie_count
else:
return ranks / count
else:
return ranks
def rank_2d_{{dtype}}(object in_arr, axis=0, ties_method='average',
ascending=True, na_option='keep', pct=False):
"""
Fast NaN-friendly version of scipy.stats.rankdata
"""
cdef:
Py_ssize_t i, j, z, k, n, dups = 0, total_tie_count = 0
{{if dtype == 'object'}}
Py_ssize_t infs
{{endif}}
ndarray[float64_t, ndim=2] ranks
{{if dtype == 'int64' or dtype == 'uint64'}}
ndarray[{{ctype}}, ndim=2, cast=True] values
{{else}}
ndarray[{{ctype}}, ndim=2] values
{{endif}}
ndarray[int64_t, ndim=2] argsorted
{{if dtype == 'uint64'}}
{{ctype}} val
{{else}}
{{ctype}} val, nan_value
{{endif}}
float64_t sum_ranks = 0
int tiebreak = 0
bint keep_na = 0
float count = 0.0
tiebreak = tiebreakers[ties_method]
keep_na = na_option == 'keep'
in_arr = np.asarray(in_arr)
if axis == 0:
values = in_arr.T.copy()
else:
values = in_arr.copy()
{{if dtype == 'object'}}
if values.dtype != np.object_:
values = values.astype('O')
{{endif}}
{{if dtype != 'uint64'}}
if ascending ^ (na_option == 'top'):
nan_value = {{pos_nan_value}}
else:
nan_value = {{neg_nan_value}}
{{if dtype == 'object'}}
mask = missing.isnaobj2d(values)
{{elif dtype == 'float64'}}
mask = np.isnan(values)
{{elif dtype == 'int64'}}
mask = values == iNaT
{{endif}}
np.putmask(values, mask, nan_value)
{{endif}}
n, k = (<object> values).shape
ranks = np.empty((n, k), dtype='f8')
{{if dtype == 'object'}}
try:
_as = values.argsort(1)
except TypeError:
values = in_arr
for i in range(len(values)):
ranks[i] = rank_1d_object(in_arr[i], ties_method=ties_method,
ascending=ascending, pct=pct)
if axis == 0:
return ranks.T
else:
return ranks
{{else}}
if tiebreak == TIEBREAK_FIRST:
# need to use a stable sort here
_as = values.argsort(axis=1, kind='mergesort')
if not ascending:
tiebreak = TIEBREAK_FIRST_DESCENDING
else:
_as = values.argsort(1)
{{endif}}
if not ascending:
_as = _as[:, ::-1]
values = _take_2d_{{dtype}}(values, _as)
argsorted = _as.astype('i8')
for i in range(n):
{{if dtype == 'object'}}
dups = sum_ranks = infs = 0
{{else}}
dups = sum_ranks = 0
{{endif}}
total_tie_count = 0
count = 0.0
for j in range(k):
{{if dtype != 'object'}}
sum_ranks += j + 1
dups += 1
{{endif}}
val = values[i, j]
{{if dtype != 'uint64'}}
{{if dtype == 'object'}}
if (val is nan_value) and keep_na:
{{else}}
if (val == nan_value) and keep_na:
{{endif}}
ranks[i, argsorted[i, j]] = nan
{{if dtype == 'object'}}
infs += 1
{{endif}}
continue
{{endif}}
count += 1.0
{{if dtype == 'object'}}
sum_ranks += (j - infs) + 1
dups += 1
{{endif}}
{{if dtype == 'object'}}
if j == k - 1 or are_diff(values[i, j + 1], val):
{{else}}
if j == k - 1 or values[i, j + 1] != val:
{{endif}}
if tiebreak == TIEBREAK_AVERAGE:
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = sum_ranks / dups
elif tiebreak == TIEBREAK_MIN:
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = j - dups + 2
elif tiebreak == TIEBREAK_MAX:
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = j + 1
elif tiebreak == TIEBREAK_FIRST:
{{if dtype == 'object'}}
raise ValueError('first not supported '
'for non-numeric data')
{{else}}
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = z + 1
{{endif}}
elif tiebreak == TIEBREAK_FIRST_DESCENDING:
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = 2 * j - z - dups + 2
elif tiebreak == TIEBREAK_DENSE:
total_tie_count += 1
for z in range(j - dups + 1, j + 1):
ranks[i, argsorted[i, z]] = total_tie_count
sum_ranks = dups = 0
if pct:
if tiebreak == TIEBREAK_DENSE:
ranks[i, :] /= total_tie_count
else:
ranks[i, :] /= count
if axis == 0:
return ranks.T
else:
return ranks
{{endfor}}