Skip to content

Commit

Permalink
[python] Fix context-type for tiledbsoma_build_index (#2224)
Browse files Browse the repository at this point in the history
* Fix context-type for `tiledbsoma_build_index`

* more

* more testing
  • Loading branch information
johnkerl committed Mar 6, 2024
1 parent 409a487 commit 92593fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apis/python/src/tiledbsoma/_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def axis_query( # type: ignore
obs_query=obs_query or query.AxisQuery(),
var_query=var_query or query.AxisQuery(),
index_factory=functools.partial(
tiledbsoma_build_index, context=self.context.native_context
tiledbsoma_build_index,
context=self.context,
),
)
3 changes: 2 additions & 1 deletion apis/python/src/tiledbsoma/_index_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def tiledbsoma_build_index(
Lifecycle:
Experimental.
"""
reindexer = clib.IntIndexer(context)
native_context = None if context is None else context.native_context
reindexer = clib.IntIndexer(native_context)
reindexer.map_locations(keys)
return reindexer # type: ignore[no-any-return]
6 changes: 4 additions & 2 deletions apis/python/src/tiledbsoma/_read_iters.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def __init__(
assert context is not None
self.minor_axes_indexer = {
d: tiledbsoma_build_index(
self.joinids[d].to_numpy(), context=context.native_context
self.joinids[d].to_numpy(),
context=context,
)
for d in (self.axes_to_reindex - set((self.major_axis,)))
}
Expand Down Expand Up @@ -256,7 +257,8 @@ def _reindexed_table_reader(
if d == self.major_axis:
assert self.context is not None
col = tiledbsoma_build_index(
coords[self.major_axis], context=self.context.native_context
coords[self.major_axis],
context=self.context,
).get_indexer(
col.to_numpy(),
)
Expand Down
15 changes: 11 additions & 4 deletions apis/python/tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ def test_duplicate_key_indexer_error(
):
context = _validate_soma_tiledb_context(SOMATileDBContext())
with pytest.raises(RuntimeError, match="There are duplicate keys."):
tiledbsoma_build_index(keys, context=context.native_context)
tiledbsoma_build_index(keys, context=context)

pd_index = pd.Index(keys)
with pytest.raises(pd.errors.InvalidIndexError):
pd_index.get_indexer(lookups)


@pytest.mark.parametrize(
"contextual",
[True, False],
)
@pytest.mark.parametrize(
"keys, lookups",
[
Expand Down Expand Up @@ -88,13 +92,16 @@ def test_duplicate_key_indexer_error(
),
],
)
def test_indexer(keys: np.array, lookups: np.array):
context = _validate_soma_tiledb_context(SOMATileDBContext())
def test_indexer(contextual: bool, keys: np.array, lookups: np.array):
if contextual:
context = _validate_soma_tiledb_context(SOMATileDBContext())
else:
context = None
all_results = []
num_threads = 10

def target():
indexer = tiledbsoma_build_index(keys, context=context.native_context)
indexer = tiledbsoma_build_index(keys, context=context)
results = indexer.get_indexer(lookups)
all_results.append(results)

Expand Down
2 changes: 1 addition & 1 deletion apis/python/tests/test_reindexer_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def test_reindexer_api_context():
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.native_context)
indexer = tiledbsoma_build_index(keys, context=context)
result = indexer.get_indexer(ids)
assert np.equal(result.all(), expected.all())

0 comments on commit 92593fd

Please sign in to comment.