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

Cap negative values below 0 before quantifying to an unsigned int. #414

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions scopesim/effects/electronic.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,10 @@
data = det._hdu.data

# Check if there are negative values in the data
negvals = np.sum(data < 0)
if negvals:
logger.warning(f"Effect ShotNoise: {negvals} negative pixels")
data[data < 0] = 0
negvals_mask = data < 0
if negvals_mask.any():
logger.warning(f"Effect ShotNoise: {negvals_mask.sum()} negative pixels")
data[negvals_mask] = 0

Check warning on line 471 in scopesim/effects/electronic.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/electronic.py#L470-L471

Added lines #L470 - L471 were not covered by tests

below = data < 2**20
above = np.invert(below)
Expand Down Expand Up @@ -721,6 +721,15 @@
logger.warning("Setting quantized data to dtype %s, which is not "
"an integer subtype.", new_dtype)

# Remove values below 0, because for unsigned types these get wrapped
# around to MAXINT, which is even worse than negative values.
# TODO: Write a test for this.
if np.issubdtype(new_dtype, np.unsignedinteger):
negvals_mask = obj._hdu.data < 0
if negvals_mask.any():
logger.warning(f"Effect Quantization: {negvals_mask.sum()} negative pixels")
obj._hdu.data[negvals_mask] = 0

Check warning on line 731 in scopesim/effects/electronic.py

View check run for this annotation

Codecov / codecov/patch

scopesim/effects/electronic.py#L730-L731

Added lines #L730 - L731 were not covered by tests

# This used to create a new ImageHDU with the same header but the data
# set to the modified data. It should be fine to simply re-assign the
# data attribute, but just in case it's not...
Expand Down
Loading