Repository navigation
fix: most immutable setting situtations - #2095
Conversation
❌ 134 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
backed mode + initialization logic
So the issue here is apparently doing anything after the object is a view and the file is closed. You can't just reopen it so something like backed_v = backed_adata[obs_idx, var_idx]
backed_v.to_memory() # closes file handle
backed_v.write_h5ad(final_pth)doesn't work because |
…ndata into ig/backed_mode_unify_x_layers
…ndata into ig/backed_mode_unify_x_layers
backed mode + initialization logic| # TODO: This is helpful in backed mode where `X is not None` but `None not in layers`. | ||
| # Does this filter make sense in general? Is there a case where we explicitly want to check `None in layers`? |
There was a problem hiding this comment.
Yeah, backed mode complicates things.
My plan for the PR was that these two invariants should hold:
adata.X is adata.layers.get(None)(adata.X is None) is (None not in adata.layers)
I get why you’re going the route of making X’s getter/setter more complex instead of layers, but I think my initial idea is more obvious from an API standpoint, don’t you think?
There was a problem hiding this comment.
Totally agree, and I absolutely would go that way, but to do that we would need to deprecate parts of our current X functionality. Things like
import anndata as ad, numpy as np
adata = ad.AnnData(np.random.rand(3, 10), layers = { "a_layer":np.random.rand(3, 10) })
# updates adata.X
adata[:2].X = 1
# does not update adata.layers["a_layer"]
adata[:2].layers["a_layer"] = np.ones((2, 10))actually do two different things so if were to route the former through the later, we would be dropping functionality that the former offers. That's the same reason adata.X is adata.layers.get(None) doesn't hold; if it were to hold, we would be dropping functionality from what I can tell (in this case, backed mode more or less, where we never hold an array-like reference to the object, instead reopening the file on access). Like I mentioned here, I would love to be able to remedy this, but we would be breaking even more currently standardized behavior then.
Maybe I'm missing what you're saying, but from my perspective, that was most of the idea with this PR - to bring back this functionality (largely).
There was a problem hiding this comment.
In summation
- backed mode currently has various edge cases, like never holding anything other than a reference to a file handle for
X - Setting on
adata.Xhas fundamentally different semantics than other elems
If we broke both of these, then I could see your invariants holding / complexity being removed.
There was a problem hiding this comment.
yeah, that’s what I meant with
I get why you’re going the route of making
X’s getter/setter more complex instead oflayers
→ in order to make it all work transparently, we’d have to modify the Layers* classes more heavily to make all of this work the same.
but anyway, your approach here is “in backed mode, .layers[None] behaves as if .X was None”?
| # Should now have same value as default | ||
| assert_equal(getattr(subset, attr), getattr(empty, attr)) | ||
| # Should now have same value as default, except for `layers`, which still has the `None` key for `subset` | ||
| if attr == "layers": | ||
| assert_equal( | ||
| {k: v for k, v in getattr(subset, attr).items() if k is not None}, | ||
| getattr(empty, attr), | ||
| ) | ||
| else: | ||
| assert_equal(getattr(subset, attr), getattr(empty, attr)) | ||
|
|
There was a problem hiding this comment.
I wonder if we should make Layers with only None inside falsey.
That way del ad.layers; assert not layers would work.
This is a PR to help push things along a bit here. There are two commits here:
backedmode in its former stateRawis initialized with anAnnDataobject that has a validlayers(hence the change of where theRawdeclaration happens) and(b) better handle the interaction betweenlayersandXThere are still quite a few errors but at least they fall into only a few categories I can address a bit more clearly now, I think.Most of the remaining issues have to do with setting on a view or similar now, which is a bit fraught because setting onXis special-casedOne of the issues with the current logic is that
layersnow may or may not containNonebut inbackedmode, it never does even ifXis notNone, which is a problem for testing but I think also a bit of a rough edge. I'm going to play around with holding aSparseDatasetclass in memory atNoneand seeing how that goes.