Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial reading #765

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

aeisenbarth
Copy link
Contributor

@aeisenbarth aeisenbarth commented Nov 6, 2024

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 with on_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.:

shapes/blobs_polygons: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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 and test_read_zarr_with_warnings.

Closes #457

Copy link

codecov bot commented Nov 6, 2024

Codecov Report

Attention: Patch coverage is 89.89899% with 10 lines in your changes missing coverage. Please review.

Project coverage is 91.97%. Comparing base (6b6f0e5) to head (f30fdc2).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/spatialdata/_io/io_zarr.py 87.71% 7 Missing ⚠️
src/spatialdata/_io/io_table.py 86.36% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #765      +/-   ##
==========================================
+ Coverage   91.89%   91.97%   +0.08%     
==========================================
  Files          45       45              
  Lines        6919     6956      +37     
==========================================
+ Hits         6358     6398      +40     
+ Misses        561      558       -3     
Files with missing lines Coverage Δ
src/spatialdata/_io/_utils.py 88.23% <100.00%> (+1.33%) ⬆️
src/spatialdata/_io/io_table.py 94.44% <86.36%> (+0.56%) ⬆️
src/spatialdata/_io/io_zarr.py 89.00% <87.71%> (+0.49%) ⬆️

... and 3 files with indirect coverage changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tolerance when reading corrupted data
1 participant