Skip to content

Commit

Permalink
[python] Enforce if-not-exists semantics for append/registration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Apr 4, 2024
1 parent b1677be commit 9286b1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ def _acquire_experiment_mappings(
) -> Self:
"""Acquires label-to-ID mappings from the baseline, already-written SOMA experiment."""

if experiment_uri is not None and tiledbsoma.Experiment.exists(experiment_uri):
if experiment_uri is not None:
if not tiledbsoma.Experiment.exists(experiment_uri):
raise ValueError("cannot find experiment at URI {experiment_uri}")

# Pre-check
with tiledbsoma.Experiment.open(experiment_uri, context=context) as exp:
if measurement_name not in exp.ms:
Expand Down Expand Up @@ -387,7 +390,10 @@ def from_anndata_appends_on_experiment(
context: Optional[SOMATileDBContext] = None,
) -> Self:
"""Extends registration data from the baseline, already-written SOMA
experiment to include multiple H5AD input files."""
experiment to include multiple H5AD input files. If ``experiment_uri``
is ``None`` then you will be computing registrations only for the input
``AnnData`` objects. If ``experiment_uri`` is not ``None`` then it is
an error if the experiment is not accessible."""

registration_data = cls._acquire_experiment_mappings(
experiment_uri,
Expand Down
17 changes: 17 additions & 0 deletions apis/python/tests/test_registration_mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,3 +1130,20 @@ def test_ealm_expose():
# However, the pre-commit hook will strip out this import statement as "unused".
# So, assert something.
assert tiledbsoma.io.ExperimentAmbientLabelMapping is not None


def test_append_registration_with_nonexistent_storage(tmp_path):
anndata1 = create_anndata_canned(1, "obs_id", "var_id")
anndata2 = create_anndata_canned(2, "obs_id", "var_id")
soma_uri = tmp_path.as_posix()

tiledbsoma.io.from_anndata(soma_uri, anndata1, measurement_name="RNA")

with pytest.raises(ValueError):
tiledbsoma.io.register_anndatas(
soma_uri + "-nonesuch",
[anndata2],
measurement_name="RNA",
obs_field_name="obs_id",
var_field_name="var_id",
)

0 comments on commit 9286b1e

Please sign in to comment.