rasterize_bins(value_key=None) declares dtype=uint32 while blocks hold X.dtype; writing the result silently zeroes non-integer values
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 72/100
Direzione di ricerca
Inizia eseguendo repro.py e ispezionando src/spatialdata/_core/operations/rasterize_bins.py, in particolare il percorso lazy per tutti i geni e la relativa dichiarazione di dtype. Il lavoro è completato quando il dtype del blocco calcolato corrisponde a X.dtype della tabella e il round trip di scrittura/lettura preserva i valori non interi invece di produrre zeri; aggiungi o aggiorna un test di regressione se il progetto fornisce una posizione adatta per i test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
[!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 thespatialdatacode base. It has not been verified or triaged by a human yet; theneeds: triagelabel 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
The lazy (all-genes) path reports img.dtype == uint32 but the computed blocks are float32 (the table's X.dtype). Writing the element to Zarr creates a uint32 array, so values in (0, 1) become 0. Raw integer counts stored as floats survive the cast, which is why this went unnoticed. The explicit value_key=[...] path uses the right dtype.
Severity (agent's assessment): high — silent data loss for normalised/log-transformed tables
Where: src/spatialdata/_core/operations/rasterize_bins.py (da.map_blocks(channel_rasterization, chunks=..., dtype=np.uint32) while channel_rasterization allocates np.zeros(..., dtype=table.X.dtype))
Expected behaviour
The declared dtype equals the block dtype and the written data equals the in-memory data.
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",
# ]
# ///
"""rasterize_bins(value_key=None) declares dtype=uint32 while blocks hold X.dtype; writing zeroes float values."""
import os
import shutil
import tempfile
import warnings
import numpy as np
import pandas as pd
import geopandas as gpd
from anndata import AnnData
from scipy.sparse import csc_matrix
from shapely.geometry import box
from spatialdata import SpatialData, rasterize_bins, read_zarr
from spatialdata.models import ShapesModel, TableModel
warnings.simplefilter("ignore")
n = 6
rows, cols = np.meshgrid(range(n), range(n), indexing="ij")
rows, cols = rows.ravel(), cols.ravel()
bins = ShapesModel.parse(gpd.GeoDataFrame({"geometry": [box(c, r, c + 1, r + 1) for r, c in zip(rows, cols)]}, index=np.arange(n * n)))
X = csc_matrix(np.random.default_rng(0).uniform(0.1, 0.9, (n * n, 3)).astype(np.float32)) # e.g. normalised expression
obs = pd.DataFrame({"region": pd.Categorical(["bins"] * (n * n)), "instance_id": np.arange(n * n), "row": rows, "col": cols})
table = TableModel.parse(AnnData(X=X, obs=obs, var=pd.DataFrame(index=["g1", "g2", "g3"])), region="bins", region_key="region", instance_key="instance_id")
sdata = SpatialData(shapes={"bins": bins}, tables={"table": table})
img = rasterize_bins(sdata, "bins", "table", col_key="col", row_key="row", value_key=None)
computed = img.data.compute()
print(f"declared dtype: {img.dtype} | computed block dtype: {computed.dtype} | computed max: {float(computed.max()):.3f}")
tmp = tempfile.mkdtemp()
sdata.images["raster"] = img
sdata.write(os.path.join(tmp, "store.zarr"))
back = read_zarr(os.path.join(tmp, "store.zarr"))["raster"].data.compute()
print(f"after write + read: dtype {back.dtype} | max {float(back.max()):.3f} | all zero: {bool((back == 0).all())} (expected: floats ~0.9)")
shutil.rmtree(tmp)
bug = str(img.dtype) != str(computed.dtype) or bool((back == 0).all())
print("VERDICT:", "BUG REPRODUCED" if bug else "NOT REPRODUCED")
Observed output
declared dtype: uint32 | computed block dtype: float32 | computed max: 0.898
after write + read: dtype uint32 | max 0.000 | all zero: True (expected: floats ~0.9)
VERDICT: BUG REPRODUCED
Possible fix direction (unverified)
Pass dtype=dtype (the table's X.dtype) and a matching meta to da.map_blocks; add a round-trip test with non-integer values.
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.
- Lingua principale
- Python
- Stelle
- 394
- Fork
- 95
- Merge medio
- 3g 9h
- PR unite (30g)
- 5
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di scverse/spatialdata
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
scverse/spatialdata#1256 ·
-
bug 🚨 element: labels 🏷️ method: aggregation 🔢 needs: triage priority: medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
scverse/spatialdata#1249 ·
-
bug 🚨 element: images 🌌 element: labels 🏷️ needs: triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
scverse/spatialdata#1239 ·
-
bug 🚨 element: shapes ▲ models needs: triage priority: medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
scverse/spatialdata#1234 ·
-
bug 🚨 element: labels 🏷️ method: aggregation 🔢 needs: triage priority: medium
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
scverse/spatialdata#1230 ·
Tutte le issue di scverse/spatialdata
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
gradio-app/gradio#13895 ·
-
build-error
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
spack/spack-packages#6713 ·
-
Use issue templates Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
ActivityWatch/activitywatch#1464 · 1 reazione ·
-
[Bug]: The ckg tool drops the return type of every decorated Python method in class search results Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
bytedance/trae-agent#483 ·