-
Notifications
You must be signed in to change notification settings - Fork 2
Fix adata.raw handling after slicing (Python ≥3.11 / 3.13) #7
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
Fix adata.raw handling after slicing (Python ≥3.11 / 3.13) #7
Conversation
…ty with newer anndata versions
|
Can you try to remove the |
|
In the end, the fix for save_anndata was to avoid AnnData.copy() entirely on views. When self.anndata is a view, calling .copy() (or implicitly making AnnData create a copy when setting .obsm) triggers a SciPy sparse advanced-indexing path (arrayXarray) that crashes in Python ≥3.11 / newer SciPy (ValueError: could not convert integer scalar). The solution was to materialize a real AnnData directly from the parent (_adata_ref) using name-based indexers for obs/var, then safely add obsm["X_spatial_registered"] and write. Not the cleanest solution, because it rebuilding the AnnData from its separate components, but it works. Additionally, save_anndata now restores a full .raw (aligned by cells) from the parent AnnData so that non-HVG genes remain accessible. This keeps the napari sc3D viewer fully functional without modifying the viewer code. With these changes, CodeS1 now runs end-to-end (including saving and 3D differential expression) on Python 3.13 / AnnData 0.12.7, and the saved object works OK in the napari sc3D viewer. |
leoguignard
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize the tests aren't passing
|
Right.. I did not run pytest locally, my bad. The version with the modifications you did yesterday also fail. The original sc3D does not fail, so it must be something that we changed on the .raw treatment. Fails at Because the changes we made in Also, maybe we should separate the test function into multiple test functions, because there might be something else failing down below and we cannot see it. |
This PR is basically the changes you made on my computer this evening to fix the Python ≥3.11 / AnnData ≥0.10 issue where sc3D converted
.rawto an AnnData object after subsetting. You found that this pattern can lead to.raw/.obsmisalignment and trigger crash a crash in newer AnnData versions because the .obs for .raw are taken from the Anndata original object and that creates a mismatch.I ran the codeS1 notebook (bioprotocols paper) with this updated code and the main workflow now runs correctly through the registration steps. However,

SpatialOmicArray.save_anndata()still fails when saving the registered AnnData. The error is the same as it happened in loading. takes 5min and then same error, happens during save-time operations (copy/obsm assignment/raw handling), and needs a separate fix.ValueError: could not convert integer scalarTested
save_anndatain sc3D.py/I am leaving this PR open to track the raw-handling fix and will follow up with a patch for
save_anndata.