forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: first hack at sliding window apply_along_axis in Cython
- Loading branch information
Showing
6 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from numpy cimport * | ||
import numpy as np | ||
|
||
import_array() | ||
|
||
cdef class ArrayCruncher: | ||
|
||
cdef: | ||
ndarray arr | ||
object f | ||
bint raw | ||
Py_ssize_t N, K | ||
|
||
def __init__(self, arr, f, axis=0, raw=True): | ||
self.arr = arr | ||
self.f = f | ||
self.raw = raw | ||
self.N, self.K = arr.shape | ||
|
||
def reduce(self): | ||
cdef: | ||
char* dummy_buf | ||
ndarray arr, result, chunk | ||
Py_ssize_t i, increment | ||
flatiter it | ||
|
||
if not self.arr.flags.c_contiguous: | ||
arr = self.arr.copy('C') | ||
else: | ||
arr = self.arr | ||
|
||
increment = self.K * self.arr.dtype.itemsize | ||
chunk = np.empty(self.K, dtype=arr.dtype) | ||
result = np.empty(self.N, dtype=arr.dtype) | ||
it = <flatiter> PyArray_IterNew(result) | ||
|
||
dummy_buf = chunk.data | ||
chunk.data = arr.data | ||
|
||
for i in range(self.N): | ||
PyArray_SETITEM(result, PyArray_ITER_DATA(it), self.f(chunk)) | ||
chunk.data = chunk.data + increment | ||
PyArray_ITER_NEXT(it) | ||
|
||
# so we don't free the wrong memory | ||
chunk.data = dummy_buf | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters