-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[python] Implement
IntIndexer
as class that wraps around `clib.IntI…
…ndexer` (#2310) Replace the `tiledbsoma_build_index` function with direct use of an `IntIndexer` class that wraps `clib.IntIndexer`.
- Loading branch information
Showing
11 changed files
with
93 additions
and
94 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
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 was deleted.
Oops, something went wrong.
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,65 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any, List, Optional, Union | ||
|
||
import numpy as np | ||
import numpy.typing as npt | ||
import pandas as pd | ||
import pyarrow as pa | ||
|
||
from tiledbsoma import pytiledbsoma as clib | ||
|
||
if TYPE_CHECKING: | ||
from .options import SOMATileDBContext | ||
|
||
IndexerDataType = Union[ | ||
npt.NDArray[np.int64], | ||
pa.Array, | ||
pa.IntegerArray, | ||
pd.Series, | ||
pd.arrays.IntegerArray, | ||
pa.ChunkedArray, | ||
List[int], | ||
] | ||
|
||
|
||
class IntIndexer: | ||
"""A re-indexer for unique integer indices. | ||
Lifecycle: | ||
Experimental. | ||
""" | ||
|
||
def __init__( | ||
self, data: IndexerDataType, *, context: Optional["SOMATileDBContext"] = None | ||
): | ||
"""Initialize re-indexer for provied indices. | ||
Args: | ||
data: | ||
Integer keys used to build the index (hash) table. | ||
context: | ||
``SOMATileDBContext`` object containing concurrecy level. | ||
Lifecycle: | ||
Experimental. | ||
""" | ||
self._context = context | ||
self._reindexer = clib.IntIndexer( | ||
None if self._context is None else self._context.native_context | ||
) | ||
self._reindexer.map_locations(data) | ||
|
||
def get_indexer(self, target: IndexerDataType) -> Any: | ||
"""Compute underlying indices of index for target data. | ||
Compatible with Pandas' Index.get_indexer method. | ||
Args: | ||
target: Data to return re-index data for. | ||
""" | ||
return ( | ||
self._reindexer.get_indexer_pyarrow(target) | ||
if isinstance(target, (pa.Array, pa.ChunkedArray)) | ||
else self._reindexer.get_indexer_general(target) | ||
) |
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
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 |
---|---|---|
@@ -1,22 +1,16 @@ | ||
import numpy as np | ||
|
||
from tiledbsoma import SOMATileDBContext, tiledbsoma_build_index | ||
from typing import Optional | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
def test_reindexer_api_thread_count(): | ||
keys = np.arange(3, 10, 2) | ||
ids = np.arange(3, 10, 2) | ||
expected = np.array([0, 1, 2, 3]) | ||
indexer = tiledbsoma_build_index(keys) | ||
result = indexer.get_indexer(ids) | ||
assert np.equal(result.all(), expected.all()) | ||
from tiledbsoma import IntIndexer, SOMATileDBContext | ||
|
||
|
||
def test_reindexer_api_context(): | ||
context = SOMATileDBContext() | ||
@pytest.mark.parametrize("context", [None, SOMATileDBContext()]) | ||
def test_reindexer_api(context: Optional[SOMATileDBContext]): | ||
keys = np.arange(3, 10, 2) | ||
ids = np.arange(3, 10, 2) | ||
expected = np.array([0, 1, 2, 3]) | ||
indexer = tiledbsoma_build_index(keys, context=context) | ||
indexer = IntIndexer(keys, context=context) | ||
result = indexer.get_indexer(ids) | ||
assert np.equal(result.all(), expected.all()) |
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 |
---|---|---|
|
@@ -94,4 +94,4 @@ class IntIndexer { | |
|
||
} // namespace tiledbsoma | ||
|
||
#endif // TILEDBSOMA_REINDEXER_H | ||
#endif // TILEDBSOMA_REINDEXER_H |