Repository navigation
Closed
Labels
Description
This may be intentional, but there seems to be an issue when the raw data only has one row. We have come across this in our unit tests when creating a small example data set with only one observation.
In [1]: import anndata
In [2]: anndata.__version__
Out[2]: '0.6.10'
In [3]: import pandas as pd
In [4]: d = [
...: (1, 'A', 'a', 'Z', 'z'),
...: (2, 'A', 'b', 'Z', 'z'),
...: (3, 'B', 'c', 'Z', 'z'),
...: (4, 'B', 'd', 'Z', 'z'),
...: ]
In [5]: df = pd.DataFrame(d, columns='c0 c1 c2 c3 c4'.split())
...: df
Out[5]:
c0 c1 c2 c3 c4
0 1 A a Z z
1 2 A b Z z
2 3 B c Z z
3 4 B d Z z
In [6]: df = df.set_index('c1 c2 c3 c4'.split())['c0'].unstack(level=[2, 3]).T
...: df
Out[6]:
c1 A B
c2 a b c d
c3 c4
Z z 1 2 3 4
In [7]: def convert_idx(x): return x.to_frame().reset_index(drop=True)
In [8]: obs = convert_idx(df.index)
In [9]: var = convert_idx(df.columns)
In [10]: a = anndata.AnnData(X=df.values, obs=obs, var=var)
In [11]: a
Out[11]:
AnnData object with n_obs × n_vars = 1 × 4
obs: 'c3', 'c4'
var: 'c1', 'c2'
In [12]: a.obs
Out[12]:
c3 c4
0 Z z
In [13]: a.var
Out[13]:
c1 c2
0 A a
1 A b
2 B c
3 B d
In [14]: assert a.shape == a.X.shape, '{} != {}'.format(a.shape, a.X.shape)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-14-7f38676dce05> in <module>()
----> 1 assert a.shape == a.X.shape, '{} != {}'.format(a.shape, a.X.shape)
AssertionError: (1, 4) != (4,)
In [15]: a[0, 0]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-15-0d768f50cb80> in <module>()
----> 1 a[0, 0]
~/anaconda3/envs/dev-procanswon-py/lib/python3.6/site-packages/anndata/base.py in __getitem__(self, index)
1292 def __getitem__(self, index):
1293 """Returns a sliced view of the object."""
-> 1294 return self._getitem_view(index)
1295
1296 def _getitem_view(self, index):
~/anaconda3/envs/dev-procanswon-py/lib/python3.6/site-packages/anndata/base.py in _getitem_view(self, index)
1296 def _getitem_view(self, index):
1297 oidx, vidx = self._normalize_indices(index)
-> 1298 return AnnData(self, oidx=oidx, vidx=vidx, asview=True)
1299
1300 # this is used in the setter for uns, if a view
~/anaconda3/envs/dev-procanswon-py/lib/python3.6/site-packages/anndata/base.py in __init__(self, X, obs, var, uns, obsm, varm, layers, raw, dtype, shape, filename, filemode, asview, oidx, vidx)
674 if not isinstance(X, AnnData):
675 raise ValueError('`X` has to be an AnnData object.')
--> 676 self._init_as_view(X, oidx, vidx)
677 else:
678 self._init_as_actual(
~/anaconda3/envs/dev-procanswon-py/lib/python3.6/site-packages/anndata/base.py in _init_as_view(self, adata_ref, oidx, vidx)
735 # set data
736 if self.isbacked: self._X = None
--> 737 else: self._init_X_as_view()
738
739 self._layers = AnnDataLayers(self, adata_ref=adata_ref, oidx=oidx, vidx=vidx)
~/anaconda3/envs/dev-procanswon-py/lib/python3.6/site-packages/anndata/base.py in _init_X_as_view(self)
750 self._X = None
751 return
--> 752 X = self._adata_ref.X[self._oidx, self._vidx]
753 if len(X.shape) == 2:
754 n_obs, n_vars = X.shape
IndexError: too many indices for array
Activity
falexwolf commentedon Sep 11, 2018
This is intentional and the same behavior as of numpy arrays, see
as mentioned in the docs: https://anndata.readthedocs.io/en/latest/anndata.AnnData.html
Just recently we had a discussion of whether this helpful or not. I personally think it's helpful. What do you think.
bretttully commentedon Sep 13, 2018
I can see why in your example this behaviour is good, but in the two errors in the original post it is very confusing.
Without knowing more, my intuition was:
Xshould be the same.obsis>=1and size of.varis>=1I would expect the be able to slice the AnnData object with a 2D index.Generally, I've come across scenarios in numpy and pandas where applying the same function on an array will return different 'shape' results based on the input. With pandas, there are times when it remains as a DataFrame or can switch to a Series. These are pretty annoying to deal with as a library author, but it probably indicates that these edge cases are hard... With numpy, the
reshapefunction with a-1is normally an easy way out, so maybe you could surface this with AnnData?falexwolf commentedon Sep 17, 2018
Yes, very happy to add a
AnnData.reshapefunction!Also happy to change the output of
AnnData.shapeto(,1)if.Xgets flattened.That's an interesting point that you make about pandas. I know that
returns a
Series- hence a 1d object - in both of the last lines; so this is consistent with slicing a numpy array and an AnnData. As for numpy arrays, consider it highly convenient that a DataFrame behaves this way. Slicing out a single vector is one of the most common operations that you apply to a DataFrame and I'd consider it highly bothersome if one had to.flatten()a(1, n)-shaped object every time.Why is it bothersome to you the other way round?
bretttully commentedon Sep 18, 2018
Here is the anti-pattern we are trying to avoid for our users:
If we can deal with the
ifstatement inside oura_function_filtering_our_db_based_on_user_params()function so that we promise our users they can always index with combination of obs and var with an AnnData object returned by our API.Thinking about the
reshapefunction, it really doesn't need the power of numpy's version (in fact, that might be actively bad, as the shape ofXmay get out of sync withn_obsandn_var). Really, the method isenforce_2d(flag: bool)-- perhaps this sets a flag in the metadata of the object so that it is persisted through the hdf backend?falexwolf commentedon Sep 18, 2018
Makes sense! If it's only about retaining indexing with
varandobsindices, this can easily be enabled even if AnnData's with 1d X are allowed (also right now, the AnnData knows whether the 1d vector is anobsor avar).But I see that an
.enforce_2d(flag: bool)could be useful in several instances. I'm sure you'd like to avoid your user having to call this explicitly. So, do you want, additionally to a method.enforce_2d()anenforce_2dparameter in the constructor?I'd tend to call it
force_2dif that's not bad English (we have aforce_sparseparameter somewhere else, which I could alternatively rename toenforce_sparse).bretttully commentedon Sep 19, 2018
This would be useful in the constructor, yes -- would you try and persist that when creating a view/copy of the object? I'm ambivalent as to how that should work.
No worries, not bad English at all
falexwolf commentedon Sep 19, 2018
Of course, this would persist throughout copies and views. Wouldn't be a problem.
Just yesterday, another question came up regarding efficient extraction of a slice of
.Xfrom an AnnData (scverse/scanpy#244 (comment)). As it's likely needed that another accessor is written for this, and as this could serve as a convenience accessor that provides (n x 1) matrices as vectors to users, I can now think of an AnnData whose data matrix is always 2d. (I previously admitted that "another access operation" that would provide 1d vectors would have been the better design choice and @flying-sheep always wanted to keep things 2d: #55)adata[:, 'gene_name'].Xin their code and expect it to return a vector. While enforcing._Xto be always 2d could be done in a backwards compatible way by calling.flattenin the property.Xif._Xis (n x 1), this wouldn't make you happy, right? Your test above would still break..sliceas.sliceXis ugly. Calling.slicewouldn't slice the whole AnnData but only return a slice of the data matrix potentially returning something flattened.@flying-sheep: You don't need to go through the whole issue, just consider the two bullets above. What do you think? I guess you would be happier with this
falexwolf commentedon Sep 19, 2018
Another option: Instead of letting
.Xdirectly point to._X, we make it a convenience accessor that enables slicing by strings and get's flattened if shaped (n x 1). At the same time, we provide the underlying data._X(np.ndarray,sp.spmatrix,h5py.Dataset), which cannot be sliced by strings and is always 2d, with a different name (e.g..X2d).The big advantage is that this is backwards compatible. But I fear it's super confusing to have access to the data matrix twice in the object...
bretttully commentedon Sep 19, 2018
Not withstanding backwards compatibility, I would take a different approach with something akin to the following:
That way, you have better encapsulation and if you need to change implementation details like the shape of
_Xyou can do it without impacting users. It also seemed like that other thread was not sure about most efficient way to do things, so perhaps this makes it more explicit?bretttully commentedon Sep 19, 2018
Worth mentioning that this definitely makes users' code more verbose and is not as simple as the pandas
df[vars]-- however, having watched people new to pandas get up to speed, the amount of confusion betweendf[var_name],df[[var_name]],df[vars]anddf.loc[:, vars], and when to use which -- and then patterns emerge likedf.loc[:, [var_name]].copy()to always get a DataFrame back that you can go on an add more columns. There is a part of me that thinks more verbosity and explicitness can be beneficial.I'm on the fence, and you guys have better knowledge about how AnnData is currently being used.
falexwolf commentedon Sep 26, 2018
OK, so now we at least have
._Xalways 2d. #55 I'm still not fully convinced by your super-verbose solution. ;) Also not by my own.One other solution: go away completely from
.Xand transition to.values, which returns a container for arrays, sparse matrices and backing files.Calling
adata[:, 'gene1'].valueswill always return something 2d. But callingadata.values[:, 'gene1']will automatically be flattened, as in numpy, pandas, tensorflow....Xwill remain for backwards compat.What do you think, @flying-sheep (also @bretttully if you want to comment).
github-actions commentedon Jul 24, 2023
This issue has been automatically marked as stale because it has not had recent activity.
Please add a comment if you want to keep the issue open. Thank you for your contributions!
flying-sheep commentedon Aug 18, 2023
Fixed by #171