Skip to content

Commit

Permalink
no expensive copy
Browse files Browse the repository at this point in the history
  • Loading branch information
eroell committed Dec 6, 2023
2 parents 02148f5 + f0f4867 commit a6f5606
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions ehrapy/tools/feature_ranking/_rank_features_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,19 @@ def rank_features_groups(
>>> ep.tl.rank_features_groups(adata, "service_unit")
>>> ep.pl.rank_features_groups(adata)
"""
# if rank_obs_columns is indicated, layer must be None
if layer is not None and rank_obs_columns is not None:
raise ValueError("Only one of layer and rank_obs_columns can be specified.")
raise ValueError("Only one of 'layer' and 'rank_obs_columns' can be specified.")

adata = adata.copy() if copy else adata

if rank_obs_columns is not None:
# keep reference to original adata, needed if copy=False
adata_orig = adata
# copy adata to work on
adata = adata.copy()
adata = sc.AnnData(
X=np.zeros((len(adata), 1)),
obs=adata.obs.copy(),
uns={"numerical_columns": [], "non_numerical_columns": [], "encoded_non_numerical_columns": []},
)

if isinstance(rank_obs_columns, str):
if rank_obs_columns == "all":
Expand All @@ -353,20 +355,15 @@ def rank_features_groups(
f"Columns `{[col for col in rank_obs_columns if col not in adata.obs.columns.values]}` are not in obs."
)

# if groupby in rank_obs_columns:
# rank_obs_columns.remove(groupby)

# move obs columns to X
# want columns of obs to become variables in X to be able to use rank_features_groups
adata_with_moved_columns = move_to_x(adata, list(rank_obs_columns))

# remove columns previously in X
# don't want columns that have been in X before, as only consider columns from obs
columns_to_select = adata_with_moved_columns.var_names.difference(adata.var_names)
adata_with_moved_columns = adata_with_moved_columns[:, columns_to_select]

# encode categoricals
adata_with_moved_columns = encode(adata_with_moved_columns, autodetect=True, encodings="label")

# assign numeric and categorical columns
adata_with_moved_columns.uns[
"non_numerical_columns"
] = [] # this should be empty, as have only numeric and encoded
Expand Down

0 comments on commit a6f5606

Please sign in to comment.