question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'AnnData' object error

See original GitHub issue

Hi there, Facing the following error:

scv.utils.show_proportions(sdata)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-6e397acd5750> in <module>()
----> 1 scv.utils.show_proportions(sdata)

/Users/awilk/anaconda3/lib/python3.6/site-packages/scvelo/preprocessing/utils.py in show_proportions(adata)
     20     """
     21     layers_keys = [key for key in ['spliced', 'unspliced', 'ambiguous'] if key in adata.layers.keys()]
---> 22     counts_per_cell_layers = [adata.layers[key].sum(1) for key in layers_keys]
     23 
     24     counts_per_cell_sum = np.sum(counts_per_cell_layers, 0)

/Users/awilk/anaconda3/lib/python3.6/site-packages/scvelo/preprocessing/utils.py in <listcomp>(.0)
     20     """
     21     layers_keys = [key for key in ['spliced', 'unspliced', 'ambiguous'] if key in adata.layers.keys()]
---> 22     counts_per_cell_layers = [adata.layers[key].sum(1) for key in layers_keys]
     23 
     24     counts_per_cell_sum = np.sum(counts_per_cell_layers, 0)

AttributeError: 'AnnData' object has no attribute 'sum'

Same error is coming up with scv.pp.filter_genes(), scv.pp.normalize_per_cell(), etc. Some more info:

sdata
AnnData object with n_obs × n_vars = 17829 × 14152 
    obs: 'orig.ident', 'clusters'
    obsm: 'UMAP_1', 'UMAP_2'
    layers: 'spliced', 'unspliced', 'ambiguous'
sdata.layers['spliced'], sdata.layers['unspliced']
(<17829x14152 sparse matrix of type '<class 'numpy.float32'>'
 	with 25134338 stored elements in Compressed Sparse Row format>,
 AnnData object with n_obs × n_vars = 17829 × 14152 )
sdata.layers['spliced'].sum(1), sdata.layers['unspliced'].sum(1)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-2dd9862f1041> in <module>()
----> 1 sdata.layers['spliced'].sum(1), sdata.layers['unspliced'].sum(1)

AttributeError: 'AnnData' object has no attribute 'sum'

The raw data are there, but it seems there must be something wrong about the way I’m storing it? (probably a simple fix I’m just missing). Thanks!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajwilkcommented, Aug 8, 2019

You’re right, the way I was writing the AnnData object only wrote into the spliced layer. I do all of my pre-processing in R, and so I’m trying to create the AnnData object using .mtx files written from R. This odd-looking workaround allowed me to write into all layers of the AnnData object, but still appears to throw errors downstream:

sdata = ad.read_mtx("~/combined.emat.mtx")
sdata.layers['spliced'] = sdata.X
ndata = ad.read_mtx("~/combined.nmat.mtx")
sdata.layers['unspliced'] = ndata.X
mdata = ad.read_mtx("~/combined.smat.mtx")
sdata.layers['ambiguous'] = mdata.X
sdata
AnnData object with n_obs × n_vars = 17829 × 14152 
    layers: 'spliced', 'unspliced', 'ambiguous'
pd_obs = pd.read_csv("~/combined.obs.csv")
pd_obsm = pd.read_csv("~/combined.obsm.csv")
sdata.obs = pd_obs
sdata.obsm = pd_obsm

sdata
AnnData expects string indices for some functionality, but your first two indices are: RangeIndex(start=0, stop=2, step=1). 
/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/alignedmapping.py:162: UserWarning: AnnData does not currently support writing or reading of 'Series' objects in obsm for either hdf5 or zarr formats.
  self._validate_value(value, key)
AnnData object with n_obs × n_vars = 17829 × 14152 
    obs: 'orig.ident', 'clusters'
    obsm: 'UMAP_1', 'UMAP_2'
    layers: 'spliced', 'unspliced', 'ambiguous'

sdata.layers['spliced'], sdata.layers['unspliced'], sdata.layers['ambiguous']
(<17829x14152 sparse matrix of type '<class 'numpy.float32'>'
 	with 25134338 stored elements in Compressed Sparse Row format>,
 <17829x14152 sparse matrix of type '<class 'numpy.float32'>'
 	with 11552274 stored elements in Compressed Sparse Row format>,
 <17829x14152 sparse matrix of type '<class 'numpy.float32'>'
 	with 3400639 stored elements in Compressed Sparse Row format>)

scv.utils.show_proportions(sdata)
Abundance of ['spliced', 'unspliced', 'ambiguous']: [0.63 0.31 0.06]

scv.pp.normalize_per_cell(sdata)
scv.pp.filter_genes_dispersion(sdata, n_top_genes=3000)
scv.pp.log1p(sdata)
scv.pp.filter_and_normalize(sdata, min_shared_counts=10, n_top_genes=3000)
scv.pp.moments(sdata, n_pcs=30, n_neighbors=30)
Normalized count data: X, unspliced.
/Users/awilk/anaconda3/lib/python3.6/site-packages/scanpy/preprocessing/_deprecated/highly_variable_genes.py:176: RuntimeWarning: invalid value encountered in greater_equal
  gene_subset = df['dispersion_norm'].values >= disp_cut_off
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-29-0feaa876caed> in <module>()
      1 scv.pp.normalize_per_cell(sdata)
----> 2 scv.pp.filter_genes_dispersion(sdata, n_top_genes=3000)
      3 scv.pp.log1p(sdata)
      4 scv.pp.filter_and_normalize(sdata, min_shared_counts=10, n_top_genes=3000)
      5 scv.pp.moments(sdata, n_pcs=30, n_neighbors=30)

/Users/awilk/anaconda3/lib/python3.6/site-packages/scvelo/preprocessing/utils.py in filter_genes_dispersion(data, flavor, min_disp, max_disp, min_mean, max_mean, n_bins, n_top_genes, log, copy)
    251             from scanpy.api.pp import filter_genes_dispersion
    252             filter_genes_dispersion(adata, flavor=flavor, min_disp=min_disp, max_disp=max_disp, min_mean=min_mean,
--> 253                                     max_mean=max_mean, n_bins=n_bins, n_top_genes=n_top_genes, log=log)
    254     return adata if copy else None
    255 

/Users/awilk/anaconda3/lib/python3.6/site-packages/scanpy/preprocessing/_deprecated/highly_variable_genes.py in filter_genes_dispersion(data, flavor, min_disp, max_disp, min_mean, max_mean, n_bins, n_top_genes, log, subset, copy)
    110         adata.var['dispersions_norm'] = result['dispersions_norm']
    111         if subset:
--> 112             adata._inplace_subset_var(result['gene_subset'])
    113         else:
    114             adata.var['highly_variable'] = result['gene_subset']

/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/anndata.py in _inplace_subset_var(self, index)
   1362         Same as ``adata = adata[:, index]``, but inplace.
   1363         """
-> 1364         adata_subset = self[:, index].copy()
   1365         self._init_as_actual(adata_subset, dtype=self._X.dtype)
   1366 

/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/anndata.py in copy(self, filename)
   1555                            # as uns was copied already before
   1556                            self._uns.copy() if isinstance(self._uns, DictView) else deepcopy(self._uns),
-> 1557                            self._obsm.copy(), self._varm.copy(),
   1558                            raw=None if self._raw is None else self._raw.copy(),
   1559                            layers=dict(self.layers),

/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/alignedmapping.py in copy(self)
     89     def copy(self):
     90         d = self._actual_class(self.parent, self._axis)
---> 91         for k, v in self.items():
     92             d[k] = v.copy()
     93         return d

/Users/awilk/anaconda3/lib/python3.6/_collections_abc.py in __iter__(self)
    742     def __iter__(self):
    743         for key in self._mapping:
--> 744             yield (key, self._mapping[key])
    745 
    746 ItemsView.register(dict_items)

/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/alignedmapping.py in __getitem__(self, key)
    119             asview(
    120                 self.parent_mapping[key],
--> 121                 ViewArgs(self.parent, self.attrname, (key,))
    122             ),
    123             self.subset_idx

/Users/awilk/anaconda3/lib/python3.6/functools.py in wrapper(*args, **kw)
    805                             '1 positional argument')
    806 
--> 807         return dispatch(args[0].__class__)(*args, **kw)
    808 
    809     funcname = getattr(func, '__name__', 'singledispatch function')

/Users/awilk/anaconda3/lib/python3.6/site-packages/anndata/core/views.py in asview(obj, view_args)
     95 def asview(obj, view_args):
     96     raise NotImplementedError(
---> 97         "No view type has been registered for {}".format(type(obj))
     98     )
     99 

NotImplementedError: No view type has been registered for <class 'pandas.core.series.Series'>
1reaction
VolkerBergencommented, Aug 8, 2019

Looks like a whole anndata object is stored in sdata.layers['unspliced'], which does not make sense… The raw data - if needed later on - is to be stored in sdata.raw. Did you write that to unspliced layers?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Release Notes — anndata 0.7.8 documentation
AnnData object created from dataframes with sparse values will have sparse .X PR 395 I Virshup. Bug fixes. Fixed error from AnnData.concatenate by...
Read more >
Downstream analysis based on Anndata object | DESC
Convert AnnData object in python to Seurat object in R ... raw.data.matrix <- tryCatch( expr = t(py_to_r(from$raw$X)), error = function(e) { stop("No ...
Read more >
Error of inspect anndata - usegalaxy.eu support - Galaxy Help
... do “the full data matrix” inspect with " Inspect AnnData object", I always get the ERROR, I help you can help me...
Read more >
Pegasus - Google Groups
I'm getting the error “'AnnData' object has no attribute 'register_attr'” when trying to use pg.calc_kBET() and also pg.neighbors().
Read more >
theislab/zellkonverter source: R/AnnData2SCE.R - Rdrr.io
Values stored in the `varm` slot of an `AnnData` object are stored in a ... error = function(err) { # If whole list...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found