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

Refactor _get_color function to stop using private scanpy functions and to work with mudata #388

Open
grst opened this issue Mar 29, 2023 · 0 comments

Comments

@grst
Copy link
Collaborator

grst commented Mar 29, 2023

See

scirpy/scirpy/pl/styling.py

Lines 123 to 164 in 8ad2e8b

def _get_colors(
params: DataHandler,
obs_key: str,
palette: Union[str, Sequence[str], Cycler, None] = None,
) -> Optional[Dict[str, str]]:
"""Return colors for a category stored in AnnData.
If colors are not stored, new ones are assigned.
Since we currently don't plot expression values, only keys from `obs`
are supported, while in scanpy `values_to_plot` (used instead of `obs_key`)
can be a key from either `obs` or `var`.
TODO: This makes use of private scanpy functions. This is evil and
should be changed in the future.
"""
# required to turn into categoricals
params.data.strings_to_categoricals()
# we can only get a palette for columns that are now categorical. Boolean/int/... won't work
if isinstance(params.data, MuData):
if obs_key in params.data.obs.columns:
uns_lookup = params.data
else:
uns_lookup = params.adata
else:
uns_lookup = params.adata
if uns_lookup.obs[obs_key].dtype.name == "category":
values = uns_lookup.obs[obs_key].values
categories = values.categories # type: ignore
color_key = f"{obs_key}_colors"
if palette is not None:
_set_colors_for_categorical_obs(uns_lookup, obs_key, palette)
elif color_key not in uns_lookup.uns or len(uns_lookup.uns[color_key]) < len(
categories
):
# set a default palette in case that no colors or few colors are found
_set_default_colors_for_categorical_obs(uns_lookup, obs_key)
else:
_validate_palette(uns_lookup, obs_key)
return {cat: col for cat, col in zip(categories, uns_lookup.uns[color_key])}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: prio2
Development

No branches or pull requests

1 participant