Skip to content

Commit 23bcb72

Browse files
Merge pull request #352 from scverse/vhd_dataset_id
Better dataset_id inference and minor fullres image fix
2 parents d5fa3f2 + 3c7425e commit 23bcb72

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

src/spatialdata_io/readers/visium_hd.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,7 @@ def visium_hd(
157157
and FILTERED_MATRIX_2U_PATH.exists()
158158
)
159159

160-
if dataset_id is None:
161-
dataset_id = _infer_dataset_id(path)
162-
163-
filename_prefix = _get_filename_prefix(path, dataset_id)
160+
filename_prefix, dataset_id = _get_filename_prefix(path, dataset_id)
164161

165162
def load_image(path: Path, suffix: str, scale_factors: list[int] | None = None) -> None:
166163
_load_image(
@@ -401,10 +398,7 @@ def _get_bins(path_bins: Path) -> list[str]:
401398
)
402399

403400
# Read all images and add transformations
404-
fullres_image_file_paths = []
405-
if fullres_image_file is not None:
406-
fullres_image_file_paths.append(Path(fullres_image_file))
407-
else:
401+
if fullres_image_file is None:
408402
path_fullres = path / VisiumHDKeys.MICROSCOPE_IMAGE
409403
if path_fullres.exists():
410404
fullres_image_paths = [file for file in path_fullres.iterdir() if file.is_file()]
@@ -430,7 +424,7 @@ def _get_bins(path_bins: Path) -> list[str]:
430424

431425
if fullres_image_file is not None:
432426
load_image(
433-
path=fullres_image_file_paths[0],
427+
path=Path(fullres_image_file),
434428
suffix="_full_image",
435429
scale_factors=[2, 2, 2, 2],
436430
)
@@ -677,14 +671,41 @@ def _decompose_projective_matrix(
677671
return affine_matrix, projective_shift
678672

679673

680-
def _get_filename_prefix(path: Path, dataset_id: str) -> str:
674+
def _get_filename_prefix(path: Path, dataset_id: str | None) -> tuple[str, str]:
675+
"""Determine the filename prefix and dataset ID for Visium HD files.
676+
677+
The returned ``filename_prefix`` is used to locate files on disk (e.g., ``{prefix}feature_slice.h5``),
678+
while ``dataset_id`` is used to name elements in the ``SpatialData`` object.
679+
680+
Parameters
681+
----------
682+
path
683+
Path to the Visium HD output directory.
684+
dataset_id
685+
Optional identifier for naming elements. If ``None``, inferred from prefixed files.
686+
687+
Returns
688+
-------
689+
A tuple of ``(filename_prefix, dataset_id)``.
690+
691+
Raises
692+
------
693+
RuntimeError
694+
If the feature slice file cannot be located.
695+
"""
696+
if (path / VisiumHDKeys.FEATURE_SLICE_FILE.value).exists():
697+
return "", dataset_id if dataset_id is not None else ""
698+
699+
if dataset_id is None:
700+
dataset_id = _infer_dataset_id(path)
701+
681702
if (path / f"{dataset_id}_{VisiumHDKeys.FEATURE_SLICE_FILE.value}").exists():
682-
return f"{dataset_id}_"
683-
assert (path / VisiumHDKeys.FEATURE_SLICE_FILE.value).exists(), (
684-
f"Cannot locate the feature slice file, please ensure the file is present in the {path} directory and/or adjust"
685-
"the `dataset_id` parameter"
703+
return f"{dataset_id}_", dataset_id
704+
705+
raise RuntimeError(
706+
f"Cannot locate the feature slice file, please ensure the file is present in the {path} directory and/or "
707+
"adjust the `dataset_id` parameter"
686708
)
687-
return ""
688709

689710

690711
def _parse_metadata(path: Path, filename_prefix: str) -> tuple[dict[str, Any], dict[str, Any]]:

0 commit comments

Comments
 (0)