Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/test_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,25 @@ def test_top_n_mask(self):
np.testing.assert_array_equal(
mapped_array.top_n_mask(1), np.array([False, False, True, False, True, False, True, False, False])
)
np.testing.assert_array_equal(
mapped_array.top_n_mask(0), np.array([False, False, False, False, False, False, False, False, False])
)

def test_bottom_n_mask(self):
np.testing.assert_array_equal(
mapped_array.bottom_n_mask(1), np.array([True, False, False, True, False, False, False, False, True])
)
np.testing.assert_array_equal(
mapped_array.bottom_n_mask(0), np.array([False, False, False, False, False, False, False, False, False])
)

def test_top_n(self):
np.testing.assert_array_equal(mapped_array.top_n(1).id_arr, np.array([2, 4, 6]))
np.testing.assert_array_equal(mapped_array.top_n(0).id_arr, np.array([], dtype=np.int64))

def test_bottom_n(self):
np.testing.assert_array_equal(mapped_array.bottom_n(1).id_arr, np.array([0, 3, 8]))
np.testing.assert_array_equal(mapped_array.bottom_n(0).id_arr, np.array([], dtype=np.int64))

def test_to_pd(self):
target = pd.DataFrame(
Expand Down
46 changes: 43 additions & 3 deletions vectorbt/records/nb.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,54 @@ def mapped_to_mask_nb(
@njit(cache=True)
def top_n_inout_map_nb(inout: tp.Array1d, idxs: tp.Array1d, col: int, mapped_arr: tp.Array1d, n: int) -> None:
"""`inout_map_func_nb` that returns indices of top N elements."""
# TODO: np.argpartition
inout[idxs[np.argsort(mapped_arr)[-n:]]] = True
if n <= 0:
return
col_len = mapped_arr.shape[0]
if n >= col_len:
inout[idxs] = True
return
n_partition = col_len - n
pivot = np.partition(mapped_arr, n_partition)[n_partition]
n_greater = 0
for i in range(col_len):
if mapped_arr[i] > pivot:
n_greater += 1
inout[idxs[i]] = True
n_remaining = n - n_greater
if n_remaining <= 0:
return
for i in range(col_len - 1, -1, -1):
if mapped_arr[i] == pivot:
inout[idxs[i]] = True
n_remaining -= 1
if n_remaining == 0:
break


@njit(cache=True)
def bottom_n_inout_map_nb(inout: tp.Array1d, idxs: tp.Array1d, col: int, mapped_arr: tp.Array1d, n: int) -> None:
"""`inout_map_func_nb` that returns indices of bottom N elements."""
inout[idxs[np.argsort(mapped_arr)[:n]]] = True
if n <= 0:
return
col_len = mapped_arr.shape[0]
if n >= col_len:
inout[idxs] = True
return
pivot = np.partition(mapped_arr, n - 1)[n - 1]
n_less = 0
for i in range(col_len):
if mapped_arr[i] < pivot:
n_less += 1
inout[idxs[i]] = True
n_remaining = n - n_less
if n_remaining <= 0:
return
for i in range(col_len):
if mapped_arr[i] == pivot:
inout[idxs[i]] = True
n_remaining -= 1
if n_remaining == 0:
break


@njit
Expand Down
Loading