Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: scale to minmax range #562

Closed
valhassan opened this issue May 13, 2024 · 0 comments · Fixed by #567
Closed

BUG: scale to minmax range #562

valhassan opened this issue May 13, 2024 · 0 comments · Fixed by #567
Labels
bug Something isn't working

Comments

@valhassan
Copy link
Collaborator

Describe the bug
The original min and max ranges are inferred from the array in this function. The correct case is to provide the range as parameters to avoid floating point errors
"Expected: (0, 1)
Actual: (-4.768371586472142e-09, 1.0000000190734863)"

def minmax_scale(img, scale_range=(0, 1), orig_range=(0, 255)):
"""
Scale data values from original range to specified range
:param img: (numpy array) Image to be scaled
:param scale_range: Desired range of transformed data (0, 1) or (-1, 1).
:param orig_range: Original range of input data.
:return: (numpy array) Scaled image
"""
if img.min() < orig_range[0] or img.max() > orig_range[1]:
raise ValueError(f"Actual original range exceeds expected original range.\n"
f"Expected: {orig_range}\n"
f"Actual: ({img.min()}, {img.max()})")
o_r = (orig_range[1] - orig_range[0])
s_r = (scale_range[1] - scale_range[0])
if isinstance(img, (np.ndarray, torch.Tensor)):
scale_img = (s_r * (img - orig_range[0]) / o_r) + scale_range[0]
else:
raise TypeError(f"Expected a numpy array or torch tensor, got {type(img)}")
return scale_img

To Reproduce
Steps to reproduce the behavior:

  1. Test the function with an array of high-precision floating points that exceed the expected range (0, 1)

Expected behavior
The function should scale based on the provided parameters and shift from dynamic to static scaling.

Screenshots
NULL

Additional context
NULL

@valhassan valhassan added the bug Something isn't working label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant