Repository navigation
Add obs_array and var_array functions - #144
Conversation
Codecov Report
@@ Coverage Diff @@
## master #144 +/- ##
=========================================
Coverage ? 75.13%
=========================================
Files ? 11
Lines ? 2031
Branches ? 0
=========================================
Hits ? 1526
Misses ? 505
Partials ? 0
Continue to review full report at Codecov.
|
| pass | ||
| else: | ||
| # TODO: Raise deprecation warning | ||
| layer = None |
There was a problem hiding this comment.
I just checked the previous implementations of this, which got really complicated by several people iterating on it... This here is much better.
I would do:
if use_raw:
obj = self.raw
else:
obj = self
So that if use_raw==True this looks into self.raw.var.
There was a problem hiding this comment.
Hmm. I don't think I like that the var accessor could return something with a different shape that adata.shape[1]. What if you have to ask adata.raw for those values? Like adata.raw.var_array?
There was a problem hiding this comment.
Your alternative solution is 100% fine, too!
| return a | ||
|
|
||
|
|
||
| def var_array( |
There was a problem hiding this comment.
I don't think the naming is optimal and I'm also not sure whether we really want this as a public method. If public, I'd think that something like
adata.get_var_1dslice()
would make it more clear that this is not another data storage attribute but a convenience function that gives access to existing data.
There was a problem hiding this comment.
I'd find it useful enough to have as a public function, since it'd be good to advertise that there's a solution to having:
if issparse(a):
a.toarray().reshape(-1)which is all over the place in my analysis code.
I agree there's a better name. get_1d_obs or obs_array_1d. I'll think about it a bit.
There was a problem hiding this comment.
Good! 😄
How come you have this all over your analysis code and I don't? A sparse matrix slice is cast to a 1d array automatically: https://github.com/theislab/anndata/blob/34f4eb63710628fbc15e7050e5efcac1d7806062/anndata/base.py#L965
There was a problem hiding this comment.
I get this:
from scipy.sparse import random, issparse
issparse(random(10, 10, format="csr")[0, :])
# True
issparse(random(10, 10, format="csr")[:, 0])
# TrueBut I might be working more directly with the sparse matrices.
There was a problem hiding this comment.
This does remind me of something. Are there any benchmarks for the overhead of subsetting AnnData? It'd be nice if subsetting was lazy enough to have low overhead, but I'm not sure if this is the case.
There was a problem hiding this comment.
But I might be working more directly with the sparse matrices.
Scipy sparse matrices obviously maintain their type, .X does the cast for you. That's why I linked to .toarray() above. :)
There was a problem hiding this comment.
This does remind me of something. Are there any benchmarks for the overhead of subsetting AnnData? It'd be nice if subsetting was lazy enough to have low overhead, but I'm not sure if this is the case.
No, there are no benchmarks and you are absolutely right. It should be lazier. I think the subsets on all the arrays, dataframes etc. are fine because they are themselves lazy (that's at least what I expect). But the deepcopy(adata.uns) is awful...
| X = self._X | ||
| return pd.DataFrame(X, index=self.obs_names, columns=self.var_names) | ||
|
|
||
| def _get_expression(self, use_raw=False, layer=None): |
There was a problem hiding this comment.
AnnData is not meant for expression data only. Hence this should be _get_matrix or _get_X, I'd say. :)
There was a problem hiding this comment.
Totally agree. Though I might just get rid of this function if I go with obj = self.raw
|
Implemented methods for |
|
I'd leave them as public!
You don't like this other suggestion we had: But you're doing all the work, so, you should decide! |
|
When I put I think if this is going to be part of the API, I'd like a shorter name (ideally two words), especially since it's a pretty simple operation. Just some brainstorming:
adata.get_obs_vector("leiden")
adata.get_obs_vec("leiden")
adata.get_var_vector("pct_dropouts")
adata.get_vec(obs="Oct4", layer="counts")
adata.get_vec(var="pct_dropout")
adata.get_vector(obs="leiden")
adata.obs_vector("Oct4", layer="counts")
for k in adata.obs_keys():
adata.obs_vector(k) |
|
I think I like |
|
@falexwolf, thoughts on this? |
|
As mentioned before, I'd vote for prepending a Don't you think there might be confusion otherwise? The |
|
I'm resistant to a three word name for such a short method, but could be swayed. I also like leading with the dimension name, since I think it fits better with the existing (though small) anndata api. adata.obs_names
adata.obs_keys()
adata.obs_names_make_unique()To me, If we were to start a adata.get_vector(key, dim="obs")
# Or the equivalent
adata.get_vector(key, axis=0)Of course, this doesn't have to live here or be a public method (it could always become a public method in the future). I do want this functionality somewhere, so I can use it in and merge scverse/scanpy#619 followed by scverse/scanpy#467. |
|
OK. I'm convinced. 😄 Please make it PS: I had always thought that, e.g., |
|
I think those should be the names right now. Any idea what codecov is trying to tell me? I'm not sure how I've increased coverage but also decreased coverage. |
|
Hm, I think that 69.35% of diff hit (target 73.61%) means that in the changes you made, only 69% are covered with tests and that in the previous corresponding code pieces, this fraction was 73%. But as you reduced the total lines of untested code, the total coverage even slightly increased. [If one replaces 100 lines with coverage 80% with 3 lines with coverage 30%, one still reduced the total number of uncovered lines from 20 to 2.] Don't worry... Should we merge, now? |
|
The issue codacy reports might also be valid:
This should be assert type(r1) is type(r2)but no biggie |
|
@flying-sheep, I thought it wasn't going to like either since it recommended @falexwolf so what's the other passing codecov check telling me? And yeah, I think this is good to go. |
|
Awesome! 😄 |
|
Despite telling me that merging in master (rebasing on the command line would also have been an option), it tells me that I can only squash and merge. So, I'd go ahead and do that. But, after all your work here and in the other PR, @ivirshup, you should be an admin (also on the author list) of anndata. I'll quickly make you one and then you can merge this in yourself. |
|
You now have write access and can merge yourself, Isaac. |
|
Thanks for the access! It wouldn't let me do any sort of merging without getting that codecov check to pass. It now tells me that I can only squash and merge, and that both "Rebase and Merge" and "Create a merge commit" are not enabled for this repo. Is that what we want to do? If not I think the repository owner can modify this at |
|
OK, now all options are available. "Rebase and Merge" is definitely fine. I don't know why this wasn't enabled by default. |
Actually caught two bugs. A call in a deprecated function which would've failed, and places I seem to have forgotten to add an assert command
|
Since tests weren't passing on master (an f-string was added in a recent commit) I've gone ahead and bumped the minimum python version to 3.6. This was previously discussed in #140 (comment). |
|
Thanks for changing those settings! Let me know when you think this is ready for a point release so I can merge those PRs over on scanpy. |
Convenience functions for retrieving an array of values along either the
obsorvardimension.A proposed update to
_get_{obs,var}_arrayas discussed with @falexwolf in scverse/scanpy#619.As I was writing it, it got more complicated that I expected (of course). I think
use_rawmay be poorly posed here, unless it's meant to only refer to.raw.X. Forvar_array(..., use_raw=True), how many values should be returned?adata.shape[1]oradata.raw.shape[1]? Should we only checkadata.raw.varfor keys? This would change_get_var_array's current behavior: https://github.com/theislab/anndata/blob/34f4eb63710628fbc15e7050e5efcac1d7806062/anndata/base.py#L1488-L1510