mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-13 17:42:42 +08:00
6e8a4b8144
- Add an `ascending=True` keyword to `rank()`. - Add `top(N)` and `bottom(N)` methods to Factor. These return Filters that pass the top and bottom N elements each day. - Add a slightly faster path for rank(method='ordinal'). I had originally thought the fast path was 2-3x faster because I had my benchmark data axes flipped. The actual speedup is only 5-10%, which means it probably wasn't worth the effort to Cythonize...but we have a slightly faster version now so we might as well use it. - Refactor test_filter and test_factor to make it easier to implement and test transformations on factors. These tests now subclass BaseFFCTestCase, which provides facilities for passing a dict of terms and an "initial_workspace", the values for which are used by SimpleFFCEngine rather than needing to manually manage the inputs and outputs of each term.
54 lines
1.2 KiB
Cython
54 lines
1.2 KiB
Cython
"""
|
|
Functions for ranking and sorting.
|
|
"""
|
|
cimport cython
|
|
from numpy cimport (
|
|
float64_t,
|
|
import_array,
|
|
intp_t,
|
|
ndarray,
|
|
NPY_DOUBLE,
|
|
NPY_MERGESORT,
|
|
PyArray_ArgSort,
|
|
PyArray_DIMS,
|
|
PyArray_EMPTY,
|
|
)
|
|
from numpy import nan
|
|
|
|
import_array()
|
|
|
|
|
|
cdef double NAN = nan
|
|
|
|
|
|
@cython.boundscheck(False)
|
|
@cython.wraparound(False)
|
|
@cython.embedsignature(True)
|
|
def rankdata_2d_ordinal(ndarray[float64_t, ndim=2] array):
|
|
"""
|
|
Equivalent to:
|
|
|
|
numpy.apply_over_axis(scipy.stats.rankdata, 1, array, method='ordinal')
|
|
"""
|
|
cdef:
|
|
int nrows, ncols
|
|
ndarray[intp_t, ndim=2] sort_idxs
|
|
ndarray[float64_t, ndim=2] out
|
|
|
|
nrows = array.shape[0]
|
|
ncols = array.shape[1]
|
|
|
|
# scipy.stats.rankdata explicitly uses MERGESORT instead of QUICKSORT for
|
|
# the ordinal branch. c.f. commit ab21d2fee2d27daca0b2c161bbb7dba7e73e70ba
|
|
sort_idxs = PyArray_ArgSort(array, 1, NPY_MERGESORT)
|
|
|
|
# Roughly, "out = np.empty_like(array)"
|
|
out = PyArray_EMPTY(2, PyArray_DIMS(array), NPY_DOUBLE, False)
|
|
|
|
cdef intp_t i, j
|
|
for i in range(nrows):
|
|
for j in range(ncols):
|
|
out[i, sort_idxs[i, j]] = j + 1.0
|
|
|
|
return out
|