scverse / scverse/scanpy

scanpy.pp.log1p with backed h5ad produces copy error

Open
#1,153 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I am fairly new with using scanpy, and so I may be performing this incorrectly. I encountered an error when trying to create a backed AnnData object from an h5ad file, and then logarithmizing the data matrix within the object using scanpy.pp.log1p. However I get an error within the AnnData object code because the preprocessing/_simple.py script is not passing a filename in the copy() function.

Right now my current workaround is to create the AnnData object as non-backed, do the log1p, and then create a "filename" property to the AnnData object afterwards to make it backed for other scanpy functions.

Example
import scanpy as sc

dataset_path = "/path/to/test/data.h5ad"   # Subbing out actual filenames for data
adata = sc.read_h5ad(dataset_path, backed='r')
print(adata)   # To ensure there is a backed filepath

adata.raw = sc.pp.log1p(adata, copy=True)    # Error is here
Error output
# I printed the AnnData object to ensure it was backed
AnnData object with n_obs × n_vars = 4166 × 16852 backed at '/tmp/1b12dde9-1762-7564-8fbd-1b07b750505f.h5ad'
    obs: 'cell_type', 'barcode', 'tSNE_1', 'tSNE_2', 'replicate', 'louvain', 'n_genes', 'percent_mito', 'n_counts'
    var: 'gene_symbol', 'n_cells'
    obsm: 'X_tsne'

# Actual error after calling log1p
Traceback (most recent call last):
  File "log1p_test.cgi", line 129, in <module>
    main()
  File "log1p_test.cgi", line 81, in main
    adata.raw = sc.pp.log1p(adata, copy=True)
  File "/opt/Python-3.7.3/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py", line 292, in log1p
    data = data.copy()
  File "/opt/Python-3.7.3/lib/python3.7/site-packages/anndata/_core/anndata.py", line 1457, in copy
    "To copy an AnnData object in backed mode, "
ValueError: To copy an AnnData object in backed mode, pass a filename: `.copy(filename='myfilename.h5ad')`.
Versions:

scanpy==1.4.6 anndata==0.7.1 umap==0.3.10 numpy==1.16.3 scipy==1.4.1 pandas==0.24.2 scikit-learn==0.22.2.post1 statsmodels==0.11.1 python-igraph==0.8.0 louvain==0.6.1

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 by reproducing the example with the listed scanpy and anndata versions, then read scanpy/preprocessing/_simple.py at log1p where data.copy() is called. Check how anndata.AnnData.copy handles backed objects and the filename argument. Done means log1p(adata, copy=True) works for the backed h5ad example and the result can be assigned to adata.raw.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
bioinformatics, data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.