Skip to content

value_key is ignored in aggregate() for images #274

Open

Description

@LucaMarconato

When aggregating images by labels, all the channels are used and value_key is ignored, we should use it instead.

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 #274: `value_key` is ignored in `aggregate()` when aggregating images by labels
    (all channels are used)."""
    import numpy as np
    
    from spatialdata import aggregate
    from spatialdata.models import Image2DModel, Labels2DModel
    
    img = Image2DModel.parse(np.random.default_rng(0).random((3, 20, 20)), c_coords=["ch0", "ch1", "ch2"])
    lab = np.zeros((20, 20), dtype=np.int32)  # uint8 labels crash aggregate(), see #1229
    lab[2:8, 2:8] = 1
    lab[10:18, 10:18] = 2
    labels = Labels2DModel.parse(lab)
    
    res_all = aggregate(values=img, by=labels, agg_func="mean")
    print("no value_key      -> var:", res_all["table"].var_names.tolist())
    try:
        res = aggregate(values=img, by=labels, value_key=["ch1"], agg_func="mean")
        var = res["table"].var_names.tolist()
        print("value_key=['ch1'] -> var:", var)
        honoured = var == ["ch1"]
    except Exception as e:  # noqa: BLE001
        print("value_key=['ch1'] ->", type(e).__name__, str(e)[:150])
        honoured = None
    if honoured:
        print("VERDICT: NOT REPRODUCED (value_key selects the channels)")
    elif honoured is False:
        print("VERDICT: REPRODUCED (value_key silently ignored for images)")
    else:
        print("VERDICT: REPRODUCED (value_key not supported for images: raises instead of selecting channels)")

    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):

    no value_key      -> var: ['channel_ch0_mean', 'channel_ch1_mean', 'channel_ch2_mean']
    value_key=['ch1'] -> var: ['channel_ch0_mean', 'channel_ch1_mean', 'channel_ch2_mean']
    VERDICT: REPRODUCED (value_key silently ignored for images)
    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