Skip to content

Commit

Permalink
expfeat: implement anndata transform adaptor (#341)
Browse files Browse the repository at this point in the history
* expfeat: implement anndata transform adaptor

* hotfix: fix anndata adaptor and cell feat graph

---------

Co-authored-by: Wenzhuo Tang <tangwen2@msu.edu>
  • Loading branch information
RemyLau and WenzhuoTang authored Oct 2, 2023
1 parent f468601 commit 1a8a7f6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions dance/transforms/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import hashlib
import warnings
from abc import ABC, abstractmethod

from anndata import AnnData

from dance import logger
from dance.data.base import Data
from dance.typing import LogLevel, Optional, Tuple
Expand Down Expand Up @@ -44,3 +47,30 @@ def __repr__(self) -> str:
@abstractmethod
def __call__(self, data: Data) -> Data:
raise NotImplementedError


class AnnDataAdaptor:
"""Adaptor for transforming AnnData instead of dance data object.
Example
-------
Modify an :class:`AnnData` object inplace
>>> AnnDataAdaptor(FilterGenes(mode="sum"))(adata)
"""

def __init__(self, transform, **data_init_kwargs):
warnings.warn(
"This is a temporary patch to enable transforming AnnData, and "
"might have potential incompatibility issues. Use cautiously.",
UserWarning,
stacklevel=2,
)
self.transform = transform
self.data_init_kwargs = data_init_kwargs

def __call__(self, adata: AnnData) -> AnnData:
data = Data(adata, **self.data_init_kwargs)
self.transform(data)
return data.data
2 changes: 1 addition & 1 deletion dance/transforms/graph/cell_feature_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __call__(self, data):
num_cells, num_feats = feat.shape

row, col = np.nonzero(feat)
edata = np.array(feat[row, col])[:, None]
edata = np.array(feat[row, col]).ravel()[:, None]
self.logger.info(f"Number of nonzero entries: {edata.size:,}")
self.logger.info(f"Nonzero rate = {edata.size / num_cells / num_feats:.1%}")

Expand Down

0 comments on commit 1a8a7f6

Please sign in to comment.