1111from pathlib import Path
1212from typing import TYPE_CHECKING
1313
14+ import anndata
1415import py
1516import pytest
16- import zarr
1717from pyarrow import ArrowInvalid
1818from zarr .errors import ArrayNotFoundError , ZarrUserWarning
1919
@@ -397,21 +397,24 @@ def sdata_with_invalid_zarr_json_element_violating_spec(session_tmp_path: Path)
397397 )
398398
399399
400- @ pytest . fixture ( scope = "module" )
401- def sdata_with_table_region_not_found_zarrv3 ( session_tmp_path : Path ) -> PartialReadTestCase :
400+ def _create_sdata_with_table_region_not_found ( session_tmp_path : Path , zarr_version : int ) -> PartialReadTestCase :
401+ """Helper for table region not found test cases (zarr v2 and v3)."""
402402 # table/table/.zarr referring to a region that is not found
403403 # This has been emitting just a warning, but does not fail reading the table element.
404404 sdata = blobs ()
405- sdata_path = session_tmp_path / "sdata_with_invalid_table_region_not_found_zarrv3.zarr"
406- sdata .write (sdata_path )
405+ sdata_path = session_tmp_path / f"sdata_with_table_region_not_found_zarrv{ zarr_version } .zarr"
406+ if zarr_version == 2 :
407+ sdata .write (sdata_path , sdata_formats = SpatialDataContainerFormatV01 ())
408+ else :
409+ sdata .write (sdata_path )
407410
408411 corrupted = "blobs_labels"
409412 # The element data is missing
410413 sdata .delete_element_from_disk (corrupted )
411414 # But the labels element is referenced as a region in a table
412- regions = zarr . open_group (sdata_path / "tables" / "table" / "obs" / "region" , mode = "r " )
413- arrs = dict ( regions . arrays ())
414- assert corrupted in arrs [ "categories" ][ arrs [ "codes" ]]
415+ adata = anndata . read_zarr (sdata_path / "tables" / "table" )
416+ assert corrupted in adata . obs [ "region" ]. values
417+
415418 not_corrupted = [name for _ , name , _ in sdata .gen_elements () if name != corrupted ]
416419
417420 return PartialReadTestCase (
@@ -425,30 +428,13 @@ def sdata_with_table_region_not_found_zarrv3(session_tmp_path: Path) -> PartialR
425428
426429
427430@pytest .fixture (scope = "module" )
428- def sdata_with_table_region_not_found_zarrv2 (session_tmp_path : Path ) -> PartialReadTestCase :
429- # table/table/.zarr referring to a region that is not found
430- # This has been emitting just a warning, but does not fail reading the table element.
431- sdata = blobs ()
432- sdata_path = session_tmp_path / "sdata_with_invalid_zattrs_table_region_not_found.zarr"
433- sdata .write (sdata_path , sdata_formats = SpatialDataContainerFormatV01 ())
431+ def sdata_with_table_region_not_found_zarrv3 (session_tmp_path : Path ) -> PartialReadTestCase :
432+ return _create_sdata_with_table_region_not_found (session_tmp_path , zarr_version = 3 )
434433
435- corrupted = "blobs_labels"
436- # The element data is missing
437- sdata .delete_element_from_disk (corrupted )
438- # But the labels element is referenced as a region in a table
439- regions = zarr .open_group (sdata_path / "tables" / "table" / "obs" / "region" , mode = "r" )
440- arrs = dict (regions .arrays ())
441- assert corrupted in arrs ["categories" ][arrs ["codes" ]]
442- not_corrupted = [name for _ , name , _ in sdata .gen_elements () if name != corrupted ]
443434
444- return PartialReadTestCase (
445- path = sdata_path ,
446- expected_elements = not_corrupted ,
447- expected_exceptions = (),
448- warnings_patterns = [
449- rf"The table is annotating '{ re .escape (corrupted )} ', which is not present in the SpatialData object"
450- ],
451- )
435+ @pytest .fixture (scope = "module" )
436+ def sdata_with_table_region_not_found_zarrv2 (session_tmp_path : Path ) -> PartialReadTestCase :
437+ return _create_sdata_with_table_region_not_found (session_tmp_path , zarr_version = 2 )
452438
453439
454440@pytest .mark .parametrize (
0 commit comments