scverse / scverse/scanpy

Improve documentation for violin plot to illustrate per-gene usage – violin(adata.T, 'gene_property')

Open
#375 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Documentation 📒
Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

Hey! I thought plotting .var columns in sc.pl.violin() worked previously (and the error message seems to suggest this as well).

I am doing the following:

adata_counts.var['dropout_per_gene'] = (adata_counts.X > 0).mean(0)
adata_counts.obs['dropout_per_cell'] = (adata_counts.X > 0).mean(1)

sc.pl.violin(adata_counts, keys='dropout_per_gene')

and I get this error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-8-463060c90a0b> in <module>()
----> 1 sc.pl.violin(adata_counts, keys='dropout_per_gene')

~/scanpy/scanpy/plotting/anndata.py in violin(adata, keys, groupby, log, use_raw, stripplot, jitter, size, scale, order, multi_panel, show, xlabel, rotation, save, ax, **kwds)
    630                 X_col = adata.raw[:, key].X
    631             else:
--> 632                 X_col = adata[:, key].X
    633             obs_df[key] = X_col
    634     if groupby is None:

~/anndata/anndata/base.py in __getitem__(self, index)
   1303     def __getitem__(self, index):
   1304         """Returns a sliced view of the object."""
-> 1305         return self._getitem_view(index)
   1306 
   1307     def _getitem_view(self, index):

~/anndata/anndata/base.py in _getitem_view(self, index)
   1306 
   1307     def _getitem_view(self, index):
-> 1308         oidx, vidx = self._normalize_indices(index)
   1309         return AnnData(self, oidx=oidx, vidx=vidx, asview=True)
   1310 

~/anndata/anndata/base.py in _normalize_indices(self, index)
   1283         obs, var = super(AnnData, self)._unpack_index(index)
   1284         obs = _normalize_index(obs, self.obs_names)
-> 1285         var = _normalize_index(var, self.var_names)
   1286         return obs, var
   1287 

~/anndata/anndata/base.py in _normalize_index(index, names)
    261         return slice(start, stop, step)
    262     elif isinstance(index, (int, str)):
--> 263         return name_idx(index)
    264     elif isinstance(index, (Sequence, np.ndarray, pd.Index)):
    265         # here, we replaced the implementation based on name_idx with this

~/anndata/anndata/base.py in name_idx(i)
    248                 raise IndexError(
    249                     'Key "{}" is not valid observation/variable name/index.'
--> 250                     .format(i))
    251             i = i_found[0]
    252         return i

IndexError: Key "dropout_per_gene" is not valid observation/variable name/index.

The whole thing works for:

sc.pl.violin(adata_counts.T, keys='dropout_per_gene')
sc.pl.violin(adata_counts, keys='dropout_per_cell)

So it's clearly just not taking .var columns for sc.pl.violing().

I've also reproduced this with adata = sc.datasets.blob().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sc.pl.violin() entry point and reproduce the examples using adata_counts, adata_counts.T, and the blob dataset. Update the violin documentation to explain per-gene .var usage and the demonstrated working form, with the examples serving as the completion check.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.