Skip to content

Commit

Permalink
update write_loom to write layers (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
VolkerBergen authored and flying-sheep committed Aug 31, 2018
1 parent 46ba287 commit db90523
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
12 changes: 6 additions & 6 deletions anndata/readwrite/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def write_loom(filename: PathLike, adata: AnnData):
row_attrs['var_names'] = adata.var_names.values
col_attrs = adata.obs.to_dict('list')
col_attrs['obs_names'] = adata.obs_names.values
X = adata.X.T
if issparse(X):
logg.info(
'... writing to \'.loom\' file densifies sparse matrix')
X = X.tocoo()

layers = {'': adata.X.T}
for key in adata.layers.keys():
layers[key] = adata.layers[key].T

from loompy import create
if filename.exists():
filename.unlink()
create(fspath(filename), X, row_attrs=row_attrs, col_attrs=col_attrs)
create(fspath(filename), layers, row_attrs=row_attrs, col_attrs=col_attrs)

# TODO: store can be a MutableMapping or a string - accept PathLike too?
def write_zarr(store, adata: AnnData, **kwargs):
Expand Down
14 changes: 13 additions & 1 deletion anndata/tests/layers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from importlib.util import find_spec
import pytest
import numpy as np
import anndata as ad
import os
Expand Down Expand Up @@ -38,14 +40,24 @@ def test_views():
def test_readwrite():
adata = ad.AnnData(X=X, layers={'L':L.copy()})
adata.write('test.h5ad')

adata_read = ad.read_h5ad('test.h5ad')

assert adata.layers.keys() == adata_read.layers.keys()
assert (adata.layers['L'] == adata_read.layers['L']).all()

os.remove('test.h5ad')

@pytest.mark.skipif(find_spec('loompy') is None, reason="loompy not installed")
def test_readwrite_loom():
adata = ad.AnnData(X=X, layers={'L': L.copy()})
adata.write_loom('test.loom')
adata_read = ad.read_loom('test.loom', X_name='')

assert adata.layers.keys() == adata_read.layers.keys()
assert (adata.layers['L'] == adata_read.layers['L']).all()

os.remove('test.loom')

def test_backed():
#backed mode for layers isn't implemented, layers stay in memory
pass
4 changes: 3 additions & 1 deletion requires_tests.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
loompy>=2.0; python_version>='3.6'
loompy>=2.0.12; python_version>='3.6'
pytest-cov
codecov
zarr
matplotlib
sklearn

0 comments on commit db90523

Please sign in to comment.