Repository navigation
Views of views - #164
Views of views#164
Conversation
Codecov Report
@@ Coverage Diff @@
## master #164 +/- ##
==========================================
+ Coverage 78.93% 79.48% +0.54%
==========================================
Files 12 12
Lines 2108 2130 +22
==========================================
+ Hits 1664 1693 +29
+ Misses 444 437 -7
Continue to review full report at Codecov.
|
c6539bc to
0ebe187
Compare
|
I've just pushed an update which significantly speeds up indexing. Instead of creating a new pd.Series with it's own index (a fairly expensive computation, I think this has to do with making a new index) I've just relied on the existing index. |
|
This looks great, @ivirshup ! |
| 'that is, you cannot make a view of a view.') | ||
| self._isview = True | ||
| if isinstance(oidx, (int, np.integer)): oidx = slice(oidx, oidx+1, 1) | ||
| if isinstance(vidx, (int, np.integer)): vidx = slice(vidx, vidx+1, 1) |
There was a problem hiding this comment.
This touches on #145, since it effectively does what I said I didn't want and maintains the dimensionality of all constituent objects when the container object is subset. For example: adata[1, :].layers["counts"] is now a two dimensional ndarray.
This did make it much easier to get everything working, since I know which axis of all the contained array aligns to which axis of the AnnData object without having to check either's dimensions.
However, this makes some tests fail, since there are effectively checks on the dimensionality of X. To get around this I modified the accessor for X to return a one dimensional array if any dimension of X had a length of 1. This has opened a new can of worms with inconsistencies between X and layers as well as adata[0, :].X.shape == adata[[0], :].X.shape (which previously was not the case).
I'm trying to figure out what the right way forward here is. It makes our lives easier if contained arrays match up with the shape of their AnnData, but there's code that relies on this not being the case (sc.pl.umap for example, though this currently doesn't work with sparse layers anyways).
|
@falexwolf I'm thinking I'd like to merge this soon, mostly so that we can have the always-2d and the improved test suite on master. Potential issues
Some things to note:
Just to be clear on current behaviour: from anndata import AnnData
import numpy as np
a1 = AnnData(np.zeros((10, 10)))
a2 = a1.copy()
a1[0, 0].X = 1
assert a1.X[0, 0] == 1
a2[:5, :5].X[0, 0] = 1 # Prints warning about making copy
assert a2.X[0, 0] == 0 |
Still needs doing: * Fix flaky test (subset of subsets aren't always valid) * Check that the aligned mappings get subset right.
This took a fair amount of fiddling. Could probably use another pass of cleaning.
* Turns out, modifying views of csr matrices was not making the parent actual
* Additionally, modifying layers seemed to recurse infinitley.
* I feel a strong sense of deja vu about this, I swear I've fixed it before
Fewer warnings and more readable parameterized test names. I'm not sure why pytest.mark.filterwarnings doesn't seem to work...
* Previously, Zappy and dask arrays were being converted to numpy arrays, which caused tests to pass, but with the wrong behaviour.
* This is being corrected by scverse/scanpy#733, though I'm not sure how we should be dealing with dask arrays.
* Additionally, I've changed behaviour for `adata[idx].X[other_idx] = value` to making a copy-on-modify for the parent view, as it was in `<0.6.21`. I'm not totally sure if this is the intended behaviour, but it certainly is easier and now tested for.
For how pandas 0.25.0 compares categoricals.
| assert np.all(adata.X[2, :] == np.array([7, 0, 9])) | ||
|
|
||
|
|
||
| # TODO: Work around h5py not supporting this |
There was a problem hiding this comment.
I'm putting this off so it can be fixed with other changes to IO.
This solves some issues with the distributed backend, Namely, most errors for scverse/scanpy#733 stop occuring. `normalize_per_cell` still fails for dask, but the array doesn't appear to get modified. This will need more looking into.
|
Congrats on the merge! Fantastic work, @ivirshup ! :D |
Addressing #62.
This PR aims to make it so when you subset an AnnDataView, it makes a reference to the original AnnData.
Things left to do:
.Xworking right.Update:
A few more features of this pull request: