Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion src/spatialdata_plot/pl/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,9 @@ def _render_points(
)
added_color_from_table = True

# Reset to sequential index so row order matches after _reparse_points round-trip (#358).
points = points.reset_index(drop=True)

n_points = len(points)
points_pd_with_color = points
# When we pull colors from a table, keep the raw points (with color) for later,
Expand All @@ -758,7 +761,7 @@ def _render_points(
if table_name is None:
adata = AnnData(
X=points[["x", "y"]].values,
obs=points[coords].reset_index(),
obs=points[coords],
dtype=points[["x", "y"]].values.dtype,
)
else:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/pl/test_render_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,27 @@ def test_plot_groups_na_color_none_filters_points_datashader(self, sdata_blobs:
"blobs_points", color="cat_color", groups=["a"], size=30, method="datashader"
).pl.show(ax=axs[1], title="default (filtered)")

@staticmethod
def _make_sampled_sdata() -> SpatialData:
"""Points with two spatially separated clusters, shuffled via .sample() (#358)."""
rng = get_standard_RNG()
n = 100
x = np.concatenate([rng.uniform(0, 10, n // 2), rng.uniform(90, 100, n // 2)])
y = np.concatenate([rng.uniform(0, 10, n // 2), rng.uniform(90, 100, n // 2)])
df = pd.DataFrame({"x": x, "y": y, "cluster": pd.Categorical(["A"] * (n // 2) + ["B"] * (n // 2))})
sdata = SpatialData(points={"pts": PointsModel.parse(df)})
sampled = sdata.points["pts"].compute().sample(frac=0.8, random_state=42)
sdata.points["pts"] = PointsModel.parse(sampled)
return sdata

def test_plot_sampled_points_categorical_color_matplotlib(self):
"""Regression test for #358: .sample() must not shuffle categorical colors."""
self._make_sampled_sdata().pl.render_points("pts", color="cluster", method="matplotlib").pl.show()

def test_plot_sampled_points_categorical_color_datashader(self):
"""Regression test for #358: .sample() must not shuffle categorical colors."""
self._make_sampled_sdata().pl.render_points("pts", color="cluster", method="datashader").pl.show()


def test_groups_na_color_none_no_match_points(sdata_blobs: SpatialData):
"""When no elements match the groups, the plot should render without error."""
Expand Down
Loading