ShiftIntensity(safe=True) still wraps uint8 values when `offset` is an int

Abierto Apto para principiantes
#9,258 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
84/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
numpy, python, pytorch
Área
data

Línea de trabajo

Lee ShiftIntensity.__call__ en monai/transforms/intensity/array.py y las pruebas de tests/transforms/test_shift_intensity*.py. Reproduce el caso informado con uint8 y desplazamientos enteros y de coma flotante. Después, añade cobertura para safe=True y las transformaciones Random o Dictionary relacionadas si resulta sencillo hacerlo con las pruebas existentes. El trabajo estará terminado cuando los desplazamientos enteros produzcan los valores limitados esperados y pasen las pruebas.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

[Describe the bug
safe=True in ShiftIntensity (added for #5621) clamps overflow for float offsets but not for int offsets. With a uint8 image and an int offset, img + offset stays uint8, so values wrap during the addition, before convert_data_type(..., safe=True) runs (monai/transforms/intensity/array.py, ShiftIntensity.__call__). The safe check then has nothing to clamp. This also affects RandShiftIntensity and the dictionary versions, which use ShiftIntensity.

To Reproduce

import torch
from monai.transforms import ShiftIntensity

img = torch.tensor([0, 100, 255], dtype=torch.uint8)

print(ShiftIntensity(offset=1.0, safe=True)(img))  # [1, 101, 255]  clamped, as documented
print(ShiftIntensity(offset=1, safe=True)(img))    # [1, 101, 0]    wraps despite safe=True
print(ShiftIntensity(offset=-1, safe=True)(img))   # [255, 99, 254] wraps despite safe=True

Expected behavior
With safe=True, an int offset should give the same result as the equivalent float offset: [1, 101, 255] and [0, 99, 254]

Screenshots
If applicable, add screenshots to help explain your problem.

Environment

MONAI 1.6.1rc0+65.gc5bddb21 (dev @ c5bddb21), PyTorch 2.15.0, NumPy 2.4.6, Python 3.11.5, macOS arm64.

Additional context
One possible fix is to compute the shift in a wider dtype (e.g. cast offset to float, or promote img) before the safe conversion. As far as I can tell there's no test covering safe in tests/transforms/test_shift_intensity*.py. I'm happy to open a PR with a fix and a test if this is considered a bug.

Investigation and draft assisted by Claude (AI); I reproduced and verified the behavior myself.

](https://github.com/Project-MONAI/MONAI/issues/new)

Lenguaje dominante
Python
Estrellas
8.8k
Forks
1.7k
Merge medio
6 d 1 h
PR fusionados (30 d)
15

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Project-MONAI/MONAI

Todos los issues de Project-MONAI/MONAI

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.