Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve speed for transfer fields if using many categories. #3132

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/scvi/data/fields/_dataframe_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Literal

import numpy as np
import pandas as pd
import rich
from anndata import AnnData
from pandas.api.types import CategoricalDtype
Expand Down Expand Up @@ -211,15 +212,17 @@ def transfer_field(
mapping = state_registry[self.CATEGORICAL_MAPPING_KEY].copy()

# extend mapping for new categories
for c in np.unique(self._get_original_column(adata_target)):
if c not in mapping:
if extend_categories:
mapping = np.concatenate([mapping, [c]])
else:
raise ValueError(
f"Category {c} not found in source registry. "
f"Cannot transfer setup without `extend_categories = True`."
)
missing_categories = (
pd.Index(np.unique(self._get_original_column(adata_target)))
.difference(pd.Index(mapping))
.to_numpy()
)
if missing_categories.any() and not extend_categories:
raise ValueError(
f"Category {missing_categories[0]} not found in source registry. "
f"Cannot transfer setup without `extend_categories = True`."
)
mapping = np.concatenate([mapping, missing_categories])
cat_dtype = CategoricalDtype(categories=mapping, ordered=True)
new_mapping = _make_column_categorical(
getattr(adata_target, self.attr_name),
Expand Down
Loading