Skip to content

Commit

Permalink
Merge pull request #37 from CSOgroup/boundaries
Browse files Browse the repository at this point in the history
Fix plot_boundaries
  • Loading branch information
marcovarrone authored May 27, 2024
2 parents 15c1164 + 852ffef commit 156fbda
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/cellcharter/pl/_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import pandas as pd
import seaborn as sns
import spatialdata as sd
import spatialdata_plot # noqa: F401
from anndata import AnnData
from scipy.stats import ttest_ind
from spatialdata_plot.pl.utils import _get_colors_for_categorical_obs
from squidpy._docs import d

from ._utils import adjust_box_widths
Expand Down Expand Up @@ -107,7 +107,6 @@ def boundaries(
if cluster in clusters
}
gdf = geopandas.GeoDataFrame(geometry=list(boundaries.values()))
# adata.obs[cluster_key] = pd.Categorical(adata.obs[cluster_key])
adata.obs.loc[adata.obs[component_key] == -1, component_key] = np.nan
adata.obs.index = "cell_" + adata.obs.index
adata.obs["instance_id"] = adata.obs.index
Expand All @@ -126,7 +125,6 @@ def boundaries(
adata = ad.concat((adata, adata_obs), join="outer")

adata.obs["region"] = adata.obs["region"].astype("category")
adata.obs[component_key] = adata.obs[component_key].astype("category")

table = sd.models.TableModel.parse(
adata, region_key="region", region=["clusters", "cells"], instance_key="instance_id"
Expand All @@ -136,32 +134,22 @@ def boundaries(
"clusters": sd.models.ShapesModel.parse(gdf),
"cells": sd.models.ShapesModel.parse(cell_circles),
}
adata.obs["instance_id"] = pd.Categorical(adata.obs["instance_id"])

sdata = sd.SpatialData(shapes=shapes, table=table)

ax = plt.gca()
if show_cells:
# TODO: remove after spatialdata-plot issue #256 is fixed
sdata.tables["table"].obs[component_key] = sdata.tables["table"].obs[component_key].cat.add_categories([-1])
sdata.tables["table"].obs[component_key] = sdata.tables["table"].obs[component_key].fillna(-1)

# TODO: spatialdata-plot doesn't support legend_loc=False to make the legend disappear.
sdata.pl.render_shapes(elements="cells", color=component_key).pl.show(ax=ax, legend_loc=None)
sdata.table.uns[f"{component_key}_colors"] = _get_colors_for_categorical_obs(
adata.obs[component_key].cat.categories
)

# Groups in spatialdata needs to be string
# However, this often gives ordering problems if the labels names are numbers
# So I transform it into strings but keep the order as if they where numbers
# TODO: ask spatialdata-plot to support integers in categorical format
groups = sdata.table.obs[component_key].cat.categories.astype(str)
sdata.table.obs[component_key] = sdata.table.obs[component_key].astype(str).astype("category")
sdata.table.obs[component_key] = sdata.table.obs[component_key].cat.set_categories(groups)

sdata.pl.render_shapes(
elements="clusters",
color=component_key,
groups=list(groups),
fill_alpha=alpha_boundary,
palette=sdata.table.uns[f"{component_key}_colors"],
).pl.show(ax=ax)

if save is not None:
Expand Down

0 comments on commit 156fbda

Please sign in to comment.