Bug: slices with a negative step and a negative stop return wrong elements
I maintainer di solito rispondono entro 2 giorni
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 75/100
Direzione di ricerca
Start with normalize_index in sparse/numba_backend/_slicing.py and DOK._setitem; compare how negative-step slices are normalized and applied. Reproduce the examples in the issue for COO, GCXS and DOK, then add regression coverage for the reported slicing and assignment cases. Done when results match NumPy for those cases and the broader slice comparison no longer shows these discrepancies.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
sparse version checks
- I checked that this issue has not been reported before.
- I confirmed this bug exists on the main branch of sparse (95f0da5).
Describe the bug
Basic slicing of COO, GCXS and DOK arrays returns the wrong elements when the slice has a negative step and a negative stop that is greater than or equal to the step, for example x[👎-1] or x[:-2:-2]. No error is raised. NumPy returns an empty array for x[👎-1], but sparse returns the whole axis reversed.
The cause is in normalize_index in sparse/numba_backend/_slicing.py. replace_none turns a missing stop of a negative-step slice into -1 (meaning past the start of the axis), and posify_index then tries to tell that -1 apart from a negative stop written by the caller with the check "not (0 > stop >= step)". Any caller-provided stop in that range is left unconverted.
DOK assignment has a related problem: DOK._setitem reads a start of 0 as a missing start (ind.start or shape - 1), so d[0::-1] = [9] writes 9 into every element.
Comparing against NumPy over 845 one-dimensional slices (start and stop in None and -8..8, step in -3, -2, -1, 1, 2), 66 differ for COO, 66 for GCXS, 96 for DOK getitem and 34 for DOK setitem.
Steps or code to reproduce the bug
import numpy as np
import sparse
x = np.arange(1, 6, dtype=float)
for fmt in ["coo", "gcxs"]:
s = sparse.asarray(x, format=fmt)
print(fmt, x[:-1:-1], s[:-1:-1].todense())
print(fmt, x[:-2:-2], s[:-2:-2].todense())
d = sparse.DOK((5,))
d[0::-1] = [9.0]
print(d.todense())
Expected results
coo [] []
coo [5.] [5.]
gcxs [] []
gcxs [5.] [5.]
[9. 0. 0. 0. 0.]
Actual results
coo [] [5. 4. 3. 2. 1.]
coo [5.] [5. 3. 1.]
gcxs [] [5. 4. 3. 2. 1.]
gcxs [5.] [5. 3. 1.]
[9. 9. 9. 9. 9.]
Please describe your system.
macOS 15, Python 3.12, numpy 2.x, sparse main at 95f0da5 (0.19.4.dev1).
Possible fix
Resolve negative stops in replace_none before -1 takes on its sentinel meaning, and let DOK use the already normalized slice instead of normalizing twice and re-clipping. I have this with regression tests for COO, GCXS and DOK and can open a PR if that is welcome.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
- Lingua principale
- Python
- Stelle
- 670
- Fork
- 167
- Merge medio
- 3g 4h
- PR unite (30g)
- 7
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi 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 pydata/sparse
-
Bug: full einsum contractions upcast the dtype and count booleansForse già presa @RaphaelFakhri l’ha presa 17 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 2 giorni
-
BUG: ufunc outer rejects Python scalar and sequence operandsForse già presa @nawaaaaaAaar l’ha presa 29 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 2 giorni
-
BUG: matmul fails with zero-length broadcast batch dimensionsForse già presa @nawaaaaaAaar l’ha presa 29 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 40/100
I maintainer di solito rispondono entro 2 giorni
-
BUG: zero-length tensor contractions ignore requested output typesForse già presa @nawaaaaaAaar l’ha presa 29 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
I maintainer di solito rispondono entro 2 giorni
-
BUG: inferred COO reshape dimensions lose integer precisionForse già presa @nawaaaaaAaar l’ha presa 29 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 25/100
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di pydata/sparse
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
abetlen/llama-cpp-python#2424 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug IronPython Python 3
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pyrevitlabs/pyRevit#4184 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno