Skip to content

Can now filter render_points using groups #154

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

Closed
Closed
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
13 changes: 12 additions & 1 deletion src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from copy import copy
from typing import Union

import dask
import geopandas as gpd
import matplotlib
import numpy as np
Expand All @@ -18,6 +19,7 @@
from spatialdata.models import (
Image2DModel,
Labels2DModel,
PointsModel,
)

from spatialdata_plot._logging import logger
Expand Down Expand Up @@ -155,6 +157,10 @@ def _render_points(
scalebar_params: ScalebarParams,
legend_params: LegendParams,
) -> None:
if isinstance(render_params.groups, str):
render_params.groups = [render_params.groups]
if not all(isinstance(g, str) for g in render_params.groups):
raise TypeError("All groups must be strings.")
elements = render_params.elements

sdata_filt = sdata.filter_by_coordinate_system(
Expand All @@ -173,6 +179,12 @@ def _render_points(
if render_params.color is not None:
color = [render_params.color] if isinstance(render_params.color, str) else render_params.color
coords.extend(color)
points = points[coords].compute()
points[color[0]].cat.set_categories(render_params.groups, inplace=True)
points = points[points[color].isin(render_params.groups).values]

points = dask.dataframe.from_pandas(points, npartitions=1)
sdata_filt.points[e] = PointsModel.parse(points)

point_df = points[coords].compute()

Expand All @@ -190,7 +202,6 @@ def _render_points(
key=render_params.color,
palette=render_params.palette,
)
# print(p)
color_source_vector, color_vector, _ = _set_color_source_vec(
sdata=sdata_filt,
element=points,
Expand Down