-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Kim
committed
Sep 13, 2023
1 parent
dfcb296
commit 19a0984
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
from anndata import AnnData | ||
from scvi.data.fields import CategoricalDataFrameField | ||
|
||
|
||
class ExtendableCategoricalDataFrameField(CategoricalDataFrameField): | ||
EXTENDED_CATEGORIES_KEY = "extended_categories" | ||
|
||
def transfer_field( | ||
self, | ||
state_registry: dict, | ||
adata_target: AnnData, | ||
**kwargs, | ||
) -> dict: | ||
"""Transfer field from registry to target AnnData.""" | ||
new_state_registry = super().transfer_field( | ||
state_registry, | ||
adata_target, | ||
extend_categories=True, | ||
**kwargs, | ||
) | ||
extended_categories = [] | ||
mapping = state_registry[self.CATEGORICAL_MAPPING_KEY].copy() | ||
for c in np.unique(self._get_original_column(adata_target)): | ||
if c not in mapping: | ||
extended_categories.append(c) | ||
|
||
new_state_registry[self.EXTENDED_CATEGORIES_KEY] = extended_categories | ||
return new_state_registry | ||
|
||
def register_field(self, adata: AnnData) -> dict: | ||
"""Register field.""" | ||
state_registry = super().register_field(adata) | ||
state_registry[self.EXTENDED_CATEGORIES_KEY] = [] | ||
return state_registry | ||
|
||
|
||
class ExtendableCategoricalObsField(ExtendableCategoricalDataFrameField): | ||
"""An AnnDataField for categorical .obs attributes in the AnnData data structure.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, field_type="obs", **kwargs) |
Empty file.
Empty file.