scverse / scverse/spatialdata

Points: the transform dict is shared between a dask DataFrame and every frame derived from it, so set_transformation on a copy/subset also modifies the original

Open
#1,224 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug 🚨 element: points ⊙ method: transforms 🔃 needs: triage priority: medium
Dominant language
Python
Stars
394
Forks
95
Avg merge
4d 3h
Merged PRs (30d)
7

Description

[!NOTE]
This whole message is AI-generated. The issue was automatically discovered and reported by an AI agent (Claude) during an autonomous bug hunt on the spatialdata code base. It has not been verified or triaged by a human yet; the needs: triage label is set so that a maintainer can confirm it. The reproduction script below was executed by the agent in an isolated environment (see Environment) and its output is pasted verbatim.

Summary

sub = pts[["x", "y"]] (or .copy(), .drop(), .loc[...], .map_partitions()), then set_transformation(sub, T, "aligned") adds aligned to pts as well; remove_transformation(sub, "global") removes it from pts. Shapes are not affected because pandas deep-copies attrs in __finalize__.

Severity (agent's assessment): medium/high — silent aliasing; derived frames are common in user code and in rasterize_shapes_points (data = data[columns])

Where: src/spatialdata/models/_accessor.py::wrap_method_with_attrs / wrap_indexer_with_attrs (old_attrs = self.attrs.copy() is a shallow copy) and transformations/operations.py::set_transformation / remove_transformation (mutate the inner dict in place)

Expected behaviour

Derived frames get an independent copy of the transformations.

Reproduction

Save as repro.py and run uv run repro.py (the PEP 723 header pins spatialdata to the commit the bug was found on; replace the URL fragment with @main to test the current main branch).

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "spatialdata @ git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0",
# ]
# ///
"""The transform dict is shared between a points element and every dask frame derived from it (even .copy())."""
import warnings
import pandas as pd
from spatialdata.models import PointsModel
from spatialdata.transformations import Scale, get_transformation, set_transformation

warnings.simplefilter("ignore")
points = PointsModel.parse(pd.DataFrame({"x": [0.0, 1.0], "y": [0.0, 1.0], "v": [1.0, 2.0]}))
print("original coordinate systems:", list(get_transformation(points, get_all=True)))
bug = False
for label, derived in [("points[['x', 'y']]", points[["x", "y"]]), ("points.copy()", points.copy()), ("points.loc[[0]]", points.loc[[0]])]:
    set_transformation(derived, Scale([2.0, 2.0], axes=("x", "y")), "cs_" + label)
    shared = derived.attrs["transform"] is points.attrs["transform"]
    print(f"{label:20s}: transform dict shared with original: {shared}; original now has: {list(get_transformation(points, get_all=True))}")
    bug |= shared
print("VERDICT:", "BUG REPRODUCED (set_transformation on a derived frame modified the original)" if bug else "NOT REPRODUCED")
Observed output
original coordinate systems: ['global']
points[['x', 'y']]  : transform dict shared with original: True; original now has: ['global', "cs_points[['x', 'y']]"]
points.copy()       : transform dict shared with original: True; original now has: ['global', "cs_points[['x', 'y']]", 'cs_points.copy()']
points.loc[[0]]     : transform dict shared with original: True; original now has: ['global', "cs_points[['x', 'y']]", 'cs_points.copy()', 'cs_points.loc[[0]]']
VERDICT: BUG REPRODUCED (set_transformation on a derived frame modified the original)

Possible fix direction (unverified)

Deep-copy the transform entry in the accessor (transformations are small), or make set_transformation/remove_transformation copy-on-write (transformations = dict(_get_transformations(element)) before mutating). Add a regression test.

Environment

uv run repro.py with the PEP 723 metadata in the script (fresh, isolated environment; spatialdata built from main @ ccf1ea0 (2026-08-28); Python 3.13, latest releases of the dependencies at run time: pandas 3.0, anndata 0.13, zarr 3.3, dask 2026.8, numpy 2.5, geopandas 1.1, shapely 2.1). macOS (arm64). Also reproduced in a second environment with pandas 2.3.3 / anndata 0.12.11 / numpy 2.4.4 / zarr 3.2.1.


Automatically generated; discovered by an AI agent (Claude) and not yet reviewed by a human.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided repro.py with uv, then inspect src/spatialdata/models/_accessor.py::wrap_method_with_attrs and wrap_indexer_with_attrs alongside transformations/operations.py::set_transformation and remove_transformation. Add a regression test covering derived frames such as column subsets, copy(), and loc selections; done means transforming or removing a derived frame no longer changes the original.

Written by the indexing model from the issue text.

Assessment

Tech stack
pandas, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.