forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 3
/
algos_take_helper.pxi.in
298 lines (236 loc) · 9.5 KB
/
algos_take_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
"""
Template for each `dtype` helper function for take
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
# ----------------------------------------------------------------------
# take_1d, take_2d
# ----------------------------------------------------------------------
{{py:
# name, dest, c_type_in, c_type_out, preval, postval, can_copy, nogil
dtypes = [
('bool', 'bool', 'uint8_t', 'uint8_t', '', '', True, True),
('bool', 'object', 'uint8_t', 'object',
'True if ', ' > 0 else False', False, False),
('int8', 'int8', 'int8_t', 'int8_t', '', '', True, False),
('int8', 'int32', 'int8_t', 'int32_t', '', '', False, True),
('int8', 'int64', 'int8_t', 'int64_t', '', '', False, True),
('int8', 'float64', 'int8_t', 'float64_t', '', '', False, True),
('int16', 'int16', 'int16_t', 'int16_t', '', '', True, True),
('int16', 'int32', 'int16_t', 'int32_t', '', '', False, True),
('int16', 'int64', 'int16_t', 'int64_t', '', '', False, True),
('int16', 'float64', 'int16_t', 'float64_t', '', '', False, True),
('int32', 'int32', 'int32_t', 'int32_t', '', '', True, True),
('int32', 'int64', 'int32_t', 'int64_t', '', '', False, True),
('int32', 'float64', 'int32_t', 'float64_t', '', '', False, True),
('int64', 'int64', 'int64_t', 'int64_t', '', '', True, True),
('int64', 'float64', 'int64_t', 'float64_t', '', '', False, True),
('float32', 'float32', 'float32_t', 'float32_t', '', '', True, True),
('float32', 'float64', 'float32_t', 'float64_t', '', '', False, True),
('float64', 'float64', 'float64_t', 'float64_t', '', '', True, True),
('object', 'object', 'object', 'object', '', '', False, False)]
def get_dispatch(dtypes):
inner_take_1d_template = """
cdef:
Py_ssize_t i, n, idx
%(c_type_out)s fv
n = indexer.shape[0]
fv = fill_value
%(nogil_str)s
%(tab)sfor i in range(n):
%(tab)s idx = indexer[i]
%(tab)s if idx == -1:
%(tab)s out[i] = fv
%(tab)s else:
%(tab)s out[i] = %(preval)svalues[idx]%(postval)s
"""
inner_take_2d_axis0_template = """\
cdef:
Py_ssize_t i, j, k, n, idx
%(c_type_out)s fv
n = len(indexer)
k = values.shape[1]
fv = fill_value
IF %(can_copy)s:
cdef:
%(c_type_out)s *v
%(c_type_out)s *o
#GH3130
if (values.strides[1] == out.strides[1] and
values.strides[1] == sizeof(%(c_type_out)s) and
sizeof(%(c_type_out)s) * n >= 256):
for i in range(n):
idx = indexer[i]
if idx == -1:
for j in range(k):
out[i, j] = fv
else:
v = &values[idx, 0]
o = &out[i, 0]
memmove(o, v, <size_t>(sizeof(%(c_type_out)s) * k))
return
for i in range(n):
idx = indexer[i]
if idx == -1:
for j in range(k):
out[i, j] = fv
else:
for j in range(k):
out[i, j] = %(preval)svalues[idx, j]%(postval)s
"""
inner_take_2d_axis1_template = """\
cdef:
Py_ssize_t i, j, k, n, idx
%(c_type_out)s fv
n = len(values)
k = len(indexer)
if n == 0 or k == 0:
return
fv = fill_value
for i in range(n):
for j in range(k):
idx = indexer[j]
if idx == -1:
out[i, j] = fv
else:
out[i, j] = %(preval)svalues[i, idx]%(postval)s
"""
for (name, dest, c_type_in, c_type_out, preval, postval,
can_copy, nogil) in dtypes:
if nogil:
nogil_str = "with nogil:"
tab = ' '
else:
nogil_str = ''
tab = ''
args = dict(name=name, dest=dest, c_type_in=c_type_in,
c_type_out=c_type_out, preval=preval, postval=postval,
can_copy=can_copy, nogil_str=nogil_str, tab=tab)
inner_take_1d = inner_take_1d_template % args
inner_take_2d_axis0 = inner_take_2d_axis0_template % args
inner_take_2d_axis1 = inner_take_2d_axis1_template % args
yield (name, dest, c_type_in, c_type_out, preval, postval, can_copy,
inner_take_1d, inner_take_2d_axis0, inner_take_2d_axis1)
}}
{{for name, dest, c_type_in, c_type_out, preval, postval, can_copy,
inner_take_1d, inner_take_2d_axis0, inner_take_2d_axis1
in get_dispatch(dtypes)}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef inline take_1d_{{name}}_{{dest}}_memview({{c_type_in}}[:] values,
int64_t[:] indexer,
{{c_type_out}}[:] out,
fill_value=np.nan):
{{inner_take_1d}}
@cython.wraparound(False)
@cython.boundscheck(False)
def take_1d_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=1] values,
int64_t[:] indexer,
{{c_type_out}}[:] out,
fill_value=np.nan):
if values.flags.writeable:
# We can call the memoryview version of the code
take_1d_{{name}}_{{dest}}_memview(values, indexer, out,
fill_value=fill_value)
return
# We cannot use the memoryview version on readonly-buffers due to
# a limitation of Cython's typed memoryviews. Instead we can use
# the slightly slower Cython ndarray type directly.
{{inner_take_1d}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef inline take_2d_axis0_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values,
int64_t[:] indexer,
{{c_type_out}}[:, :] out,
fill_value=np.nan):
{{inner_take_2d_axis0}}
@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis0_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values,
ndarray[int64_t] indexer,
{{c_type_out}}[:, :] out,
fill_value=np.nan):
if values.flags.writeable:
# We can call the memoryview version of the code
take_2d_axis0_{{name}}_{{dest}}_memview(values, indexer, out,
fill_value=fill_value)
return
# We cannot use the memoryview version on readonly-buffers due to
# a limitation of Cython's typed memoryviews. Instead we can use
# the slightly slower Cython ndarray type directly.
{{inner_take_2d_axis0}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef inline take_2d_axis1_{{name}}_{{dest}}_memview({{c_type_in}}[:, :] values,
int64_t[:] indexer,
{{c_type_out}}[:, :] out,
fill_value=np.nan):
{{inner_take_2d_axis1}}
@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_axis1_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values,
ndarray[int64_t] indexer,
{{c_type_out}}[:, :] out,
fill_value=np.nan):
if values.flags.writeable:
# We can call the memoryview version of the code
take_2d_axis1_{{name}}_{{dest}}_memview(values, indexer, out,
fill_value=fill_value)
return
# We cannot use the memoryview version on readonly-buffers due to
# a limitation of Cython's typed memoryviews. Instead we can use
# the slightly slower Cython ndarray type directly.
{{inner_take_2d_axis1}}
@cython.wraparound(False)
@cython.boundscheck(False)
def take_2d_multi_{{name}}_{{dest}}(ndarray[{{c_type_in}}, ndim=2] values,
indexer,
ndarray[{{c_type_out}}, ndim=2] out,
fill_value=np.nan):
cdef:
Py_ssize_t i, j, k, n, idx
ndarray[int64_t] idx0 = indexer[0]
ndarray[int64_t] idx1 = indexer[1]
{{c_type_out}} fv
n = len(idx0)
k = len(idx1)
fv = fill_value
for i in range(n):
idx = idx0[i]
if idx == -1:
for j in range(k):
out[i, j] = fv
else:
for j in range(k):
if idx1[j] == -1:
out[i, j] = fv
else:
out[i, j] = {{preval}}values[idx, idx1[j]]{{postval}}
{{endfor}}
# ----------------------------------------------------------------------
# take_2d internal function
# ----------------------------------------------------------------------
ctypedef fused take_t:
float64_t
uint64_t
int64_t
object
cdef _take_2d(ndarray[take_t, ndim=2] values, object idx):
cdef:
Py_ssize_t i, j, N, K
ndarray[Py_ssize_t, ndim=2, cast=True] indexer = idx
ndarray[take_t, ndim=2] result
object val
N, K = (<object>values).shape
if take_t is object:
# evaluated at compile-time
result = values.copy()
else:
result = np.empty_like(values)
for i in range(N):
for j in range(K):
result[i, j] = values[i, indexer[i, j]]
return result
_take_2d_object = _take_2d[object]
_take_2d_float64 = _take_2d[float64_t]
_take_2d_int64 = _take_2d[int64_t]
_take_2d_uint64 = _take_2d[uint64_t]