Skip to content

Aggregation with multiscale objects #215

Open

Description

@LucaMarconato

We need to check that the following works when doing aggregation, and add tests:

  • we can use multiscale images in values
  • we can use multiscale labels in by
  • important subtask, we need to check that in a multiscale labels object, when to_multiscale() is called, this doesn't use interpolation, but it keeps the labels.

Activity

  1. LucaMarconato commented on Sep 13, 2026

    @LucaMarconato
    MemberAuthor

    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.

    I re-attempted to reproduce this issue on the current main branch (commit ccf1ea048d054b6624214bf618008a9f9ae223e0) as part of a triage pass over open issues, and confirmed it reproduces.

    Reproduction script (PEP 723 inline metadata; run with uv run repro.py, no other setup needed):

    # /// script
    # requires-python = ">=3.12"
    # dependencies = [
    #     "spatialdata @ git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0",
    # ]
    # ///
    """Issue #215: aggregation with multiscale objects (multiscale images in `values`, multiscale labels in `by`)
    and check that multiscale labels are downscaled without interpolation."""
    import numpy as np
    
    from spatialdata import aggregate
    from spatialdata.models import Image2DModel, Labels2DModel
    
    rng = np.random.default_rng(0)
    img = rng.random((2, 32, 32))
    lab = np.zeros((32, 32), dtype=np.int32); lab[2:12, 2:12] = 5; lab[16:30, 16:30] = 9  # uint8 labels crash aggregate(), see #1229
    single_i, single_l = Image2DModel.parse(img), Labels2DModel.parse(lab)
    ms_i, ms_l = Image2DModel.parse(img, scale_factors=[2, 2]), Labels2DModel.parse(lab, scale_factors=[2, 2])
    
    ref = aggregate(values=single_i, by=single_l, agg_func="mean")["table"]
    ok = {}
    for name, v, b in [("ms image by labels", ms_i, single_l), ("image by ms labels", single_i, ms_l), ("ms image by ms labels", ms_i, ms_l)]:
        try:
            t = aggregate(values=v, by=b, agg_func="mean")["table"]
            dense = lambda X: X.toarray() if hasattr(X, 'toarray') else np.asarray(X)  # noqa: E731
            same = np.allclose(dense(t.X), dense(ref.X))
            ok[name] = same
            print(f"{name:22} -> OK, identical to single-scale result: {same}")
        except Exception as e:  # noqa: BLE001
            ok[name] = False
            print(f"{name:22} -> {type(e).__name__}: {str(e)[:100]}")
    
    # label values must be preserved (no interpolation) at lower scales
    vals = {k: np.unique(np.asarray(v["image"])).tolist() for k, v in ms_l.children.items()}
    print("unique label values per scale:", vals)
    no_interp = all(set(v) <= {0, 5, 9} for v in vals.values())
    if all(ok.values()) and no_interp:
        print("VERDICT: NOT REPRODUCED (multiscale values/by work and labels are downscaled without interpolation)")
    else:
        print("VERDICT: REPRODUCED (see failures above)")

    Environment: uv run --no-project --python 3.13, which resolves spatialdata from git+https://github.com/scverse/spatialdata.git@ccf1ea048d054b6624214bf618008a9f9ae223e0 (extra dependencies, if any, are pinned in the script's inline metadata above).

    Output (verbatim, only uv package-resolution/install log lines removed):

    ms image by labels     -> OK, identical to single-scale result: True
    image by ms labels     -> AttributeError: 'DataTree' object has no attribute 'dtype'
    ms image by ms labels  -> AttributeError: 'DataTree' object has no attribute 'dtype'
    unique label values per scale: {'scale0': [0, 5, 9], 'scale1': [0, 5, 9], 'scale2': [0, 5, 9]}
    VERDICT: REPRODUCED (see failures above)
    exit=0
    

    Agent's severity assessment: medium (see the priority: * label added to this issue).


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions