Skip to content

Commit

Permalink
[python] Adds the spatial encoding version to Scene and `PointCloud…
Browse files Browse the repository at this point in the history
…DataFrame` objects (#3559)

* Add spatial version to `Scene` and `PointCloudDataFrame`
* Add python tests for spatial version number
jp-dark authored Jan 14, 2025
1 parent dc118ee commit 686f4f4
Showing 5 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apis/python/src/tiledbsoma/_point_cloud_dataframe.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@
from . import pytiledbsoma as clib
from ._constants import (
SOMA_COORDINATE_SPACE_METADATA_KEY,
SOMA_SPATIAL_ENCODING_VERSION,
SOMA_SPATIAL_VERSION_METADATA_KEY,
SPATIAL_DISCLAIMER,
)
from ._dataframe import (
@@ -256,6 +258,9 @@ def create(
raise map_exception_for_create(e, uri) from None

handle = cls._wrapper_type.open(uri, "w", context, tiledb_timestamp)
handle.metadata[SOMA_SPATIAL_VERSION_METADATA_KEY] = (
SOMA_SPATIAL_ENCODING_VERSION
)
handle.meta[SOMA_COORDINATE_SPACE_METADATA_KEY] = coordinate_space_to_json(
coordinate_space
)
5 changes: 5 additions & 0 deletions apis/python/src/tiledbsoma/_scene.py
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@
from ._collection import CollectionBase
from ._constants import (
SOMA_COORDINATE_SPACE_METADATA_KEY,
SOMA_SPATIAL_ENCODING_VERSION,
SOMA_SPATIAL_VERSION_METADATA_KEY,
SPATIAL_DISCLAIMER,
)
from ._exception import SOMAError, map_exception_for_create
@@ -116,6 +118,9 @@ def create(
timestamp=(0, timestamp_ms),
)
handle = cls._wrapper_type.open(uri, "w", context, tiledb_timestamp)
handle.metadata[SOMA_SPATIAL_VERSION_METADATA_KEY] = (
SOMA_SPATIAL_ENCODING_VERSION
)
if coordinate_space is not None:
if not isinstance(coordinate_space, CoordinateSpace):
coordinate_space = CoordinateSpace.from_axis_names(coordinate_space)
6 changes: 6 additions & 0 deletions apis/python/tests/test_multiscale_image.py
Original file line number Diff line number Diff line change
@@ -108,6 +108,12 @@ def test_multiscale_basic(tmp_path):
assert shape == expected_shapes[index]
assert image.level_shape(index) == expected_shapes[index]

# Check the spatial version encoding was written.
assert (
image.metadata[soma._constants.SOMA_SPATIAL_VERSION_METADATA_KEY]
== soma._constants.SOMA_SPATIAL_ENCODING_VERSION
)

# Check the levels mapping.
levels = image.levels()
assert len(levels) == 3
5 changes: 5 additions & 0 deletions apis/python/tests/test_point_cloud_dataframe.py
Original file line number Diff line number Diff line change
@@ -88,6 +88,11 @@ def test_point_cloud_basic_read(tmp_path):
assert [e.as_py() for e in table["x"]] == pydict["x"]
assert [e.as_py() for e in table["y"]] == pydict["y"]

assert (
ptc.metadata[soma._constants.SOMA_SPATIAL_VERSION_METADATA_KEY]
== soma._constants.SOMA_SPATIAL_ENCODING_VERSION
)

# Ensure it cannot be opened by another type
with pytest.raises(soma.SOMAError):
soma.DataFrame.open(urljoin(baseuri, "default"))
5 changes: 5 additions & 0 deletions apis/python/tests/test_scene.py
Original file line number Diff line number Diff line change
@@ -75,6 +75,11 @@ def test_scene_basic(tmp_path):
assert len(scene) == 3
assert scene.soma_type == "SOMAScene"

assert (
scene.metadata[soma._constants.SOMA_SPATIAL_VERSION_METADATA_KEY]
== soma._constants.SOMA_SPATIAL_ENCODING_VERSION
)

assert scene.obsl == scene["obsl"]
assert len(scene.obsl) == 1
assert scene.obsl["df"] == scene["obsl"]["df"]

0 comments on commit 686f4f4

Please sign in to comment.