Skip to content

Views of views - #164

Merged
ivirshup merged 28 commits into
scverse:masterfrom
ivirshup:views-of-views
Jul 29, 2019
Merged

ivirshup merged 28 commits into
scverse:masterfrom
ivirshup:views-of-views

Conversation

@ivirshup

@ivirshup ivirshup commented Jun 16, 2019 •

Copy link
Copy Markdown
Member

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:

  • The test is a bit flaky, for certain combinations of subsetting techniques it generates incompatible indexers.
  • I should make sure AlignedMappings work well with this, I suspect they may not due to problems getting .X working right.

Update:

A few more features of this pull request:

  • Memory usage from taking a view are significantly reduced, since X is now lazy for views (You only subset X if the user accesses the attribute)
  • More extensive testing of subsetting anndata objects

@codecov

codecov Bot commented Jun 16, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #164 into master will increase coverage by 0.54%.
The diff coverage is 90.75%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
anndata/core/alignedmapping.py 91.97% <100%> (+0.41%) ⬆️
anndata/core/views.py 87.36% <78.57%> (-3.68%) ⬇️
anndata/core/anndata.py 80.47% <93.5%> (+0.92%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8dcc3cd...1b96a7c. Read the comment docs.

@ivirshup
ivirshup force-pushed the views-of-views branch 4 times, most recently from c6539bc to 0ebe187 Compare June 17, 2019 05:37
@ivirshup ivirshup changed the title [WIP] Views of views Views of views Jun 17, 2019
@ivirshup

Copy link
Copy Markdown
Member Author

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.

@falexwolf

Copy link
Copy Markdown
Member

This looks great, @ivirshup !

Comment thread anndata/core/anndata.py
'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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

This was referenced Jun 25, 2019
@ivirshup

Copy link
Copy Markdown
Member Author

@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:

  • I've updated the docs for AnnData, but this could definitely use another pass.
  • I've made sure the behaviour for adata[idx1].X[idx2] = val doesn't modify the original adata for an in memory dataset. This was the case in earlier versions of anndata as well (at least 0.6.19, 0.6.20). I feel like this wasn't the intended behavior, but I don't think we currently have the code to deal with this case otherwise.

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

@ivirshup ivirshup mentioned this pull request Jul 12, 2019
ivirshup added 14 commits July 26, 2019 15:29
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
ivirshup added 11 commits July 26, 2019 15:29
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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm putting this off so it can be fixed with other changes to IO.

ivirshup added 3 commits July 28, 2019 18:07
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.
@ivirshup
ivirshup merged commit 16615d7 into scverse:master Jul 29, 2019
@falexwolf

Copy link
Copy Markdown
Member

Congrats on the merge! Fantastic work, @ivirshup ! :D

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.

2 participants