Skip to content

Commit

Permalink
(WIP) Initial spatial collection addition
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-dark committed Apr 24, 2024
1 parent ca5bbba commit 1a13b40
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apis/python/src/tiledbsoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
)
from ._indexer import IntIndexer, tiledbsoma_build_index
from ._measurement import Measurement
from ._scene import Scene
from ._sparse_nd_array import SparseNDArray
from .options import SOMATileDBContext, TileDBCreateOptions
from .pytiledbsoma import (
Expand Down Expand Up @@ -204,6 +205,7 @@
"SOMA_JOINID",
"SOMAError",
"SOMATileDBContext",
"Scene",
"SparseNDArray",
"TileDBCreateOptions",
"tiledbsoma_build_index",
Expand Down
7 changes: 6 additions & 1 deletion apis/python/src/tiledbsoma/_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""Implementation of a SOMA Experiment.
"""
import functools
from typing import Any, Optional
from typing import Any, Optional, Union

from somacore import experiment, query
from typing_extensions import Self
Expand All @@ -15,6 +15,7 @@
from ._dataframe import DataFrame
from ._indexer import IntIndexer
from ._measurement import Measurement
from ._scene import Scene
from ._tdb_handles import Wrapper
from ._tiledb_object import AnyTileDBObject

Expand All @@ -24,6 +25,7 @@ class Experiment( # type: ignore[misc] # __eq__ false positive
experiment.Experiment[ # type: ignore[type-var]
DataFrame,
Collection[Measurement],
Collection[Union[DataFrame, Scene]],
AnyTileDBObject,
],
):
Expand All @@ -43,6 +45,8 @@ class Experiment( # type: ignore[misc] # __eq__ false positive
defined in this dataframe.
ms (Collection):
A collection of named measurements.
spatial (Collection):
A collection of spatial scenes.
Example:
>>> import tiledbsoma
Expand All @@ -68,6 +72,7 @@ class Experiment( # type: ignore[misc] # __eq__ false positive
_subclass_constrained_soma_types = {
"obs": ("SOMADataFrame",),
"ms": ("SOMACollection",),
"spatial": ("SOMACollection",),
}

@classmethod
Expand Down
39 changes: 39 additions & 0 deletions apis/python/src/tiledbsoma/_scene.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) 2024 TileDB, Inc.
#
# Licensed under the MIT License.

"""Implementation of a SOMA Scene."""


from typing import Union

from somacore import scene

from ._collection import Collection, CollectionBase
from ._dataframe import DataFrame
from ._dense_nd_array import DenseNDArray
from ._sparse_nd_array import SparseNDArray
from ._tiledb_object import AnyTileDBObject


class Scene( # type: ignore[misc] # __eq__ false positive
CollectionBase[AnyTileDBObject],
scene.Scene[ # type: ignore[type-var]
Collection[
Union[DataFrame, DenseNDArray, SparseNDArray]
], # not just DataFrame and NDArray since NDArray does not have a common `read`
AnyTileDBObject,
],
):
"""TODO: Add documentation for a Scene
Lifecycle:
Experimental.
"""

__slots__ = ()

_subclass_constrained_soma_types = {
"exp": ("SOMACollection",),
"ms": ("SOMACollection",),
}
6 changes: 5 additions & 1 deletion apis/python/tests/test_experiment_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def test_experiment_basic(tmp_path):
measurement = ms.add_new_collection("RNA", soma.Measurement)
assert soma.Measurement.exists(measurement.uri)
assert not soma.Collection.exists(measurement.uri)
spatial = experiment.add_new_collection("spatial", soma.Collection)
assert soma.Collection.exists(spatial.uri)

measurement["var"] = create_and_populate_var(urljoin(measurement.uri, "var"))

Expand All @@ -99,11 +101,13 @@ def test_experiment_basic(tmp_path):
x.set("data", nda, use_relative_uri=False)

# ----------------------------------------------------------------
assert len(experiment) == 2
assert len(experiment) == 3
assert isinstance(experiment.obs, soma.DataFrame)
assert isinstance(experiment.ms, soma.Collection)
assert isinstance(experiment.spatial, soma.Collection)
assert "obs" in experiment
assert "ms" in experiment
assert "spatial" in experiment
assert "nonesuch" not in experiment

assert experiment.obs == experiment["obs"]
Expand Down
2 changes: 2 additions & 0 deletions libtiledbsoma/src/soma/soma_experiment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ void SOMAExperiment::create(
platform_config,
timestamp);
SOMACollection::create(exp_uri + "/ms", ctx, timestamp);
SOMACollection::create(exp_uri + "/spatial", ctx, timestamp);

auto name = std::string(std::filesystem::path(uri).filename());
auto group = SOMAGroup::open(
OpenMode::write, exp_uri, ctx, name, timestamp);
group->set(exp_uri + "/obs", URIType::absolute, "obs");
group->set(exp_uri + "/ms", URIType::absolute, "ms");
group->set(exp_uri + "/spatial", URIType::absolute, "spatial");
group->close();
}

Expand Down
5 changes: 4 additions & 1 deletion libtiledbsoma/src/soma/soma_experiment.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class SOMAExperiment : public SOMACollection {

// A collection of named measurements
std::shared_ptr<SOMACollection> ms_;

// A collection of spatial scenes
std::shared_ptr<SOMACollection> spatial_;
};
} // namespace tiledbsoma

#endif // SOMA_EXPERIMENT
#endif // SOMA_EXPERIMENT

0 comments on commit 1a13b40

Please sign in to comment.