Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements modes for
read_zarr
that allow to continue reading SpatialData elements even if some elements caused read errors, for example due to corrupted files (crash during write, a race condition or copy errors). This is useful, because with growing datasets (by number of elements), a corruption of a single file can make all other elements inaccessible. The ability to read at least the unaffected data helps for data recovery.This is implemented by wrapping element-specific reader calls into a context manager
handle_read_errors
. If enabled withon_bad_files="warn"
, it converts exceptions into warnings, and continues execution. Since execution is not aborted, all subsequent code within an iteration must be wrapped in the context manager to avoid that it is executed after a handled error. In order to identify which element failed to be read, the warning message is prepended with its location, e.g.:At the end, a SpatialData object is always returned, containing only the successfully read elements.
The current implementation is at the granularity level of whole elements, that means if anything in an element causes an error, the whole element is skipped. In principle it could be made more fine-granular, e.g. returning an element with just invalid attributes skipped, or returning a table with unreadable columns skipped. Especially for large single tables with annotations for all elements, this would be very beneficial (although they are column-based, so that a corrupted file would destroy a column's values for all annotations). However, the relevant code is not contained in this repository, but requires changes in
anndata.read_zarr
. To avoid losing annotations due to file corruption, I would recommend not to use a single table, but one table per SpatialData element.For tests, see
test_read_zarr_with_error
andtest_read_zarr_with_warnings
.Closes #457