Skip to content

Commit

Permalink
Cap negative values below 0 before quantifying to an unsigned int.
Browse files Browse the repository at this point in the history
Negative flux should be prevented, but casting it to an unsigned
int will wrap around to MAXINT, which is even worse.
  • Loading branch information
hugobuddel committed May 14, 2024
1 parent 8549c75 commit 42921c8
Showing 1 changed file with 13 additions and 4 deletions.
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 @@ def apply_to(self, det, **kwargs):
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 @@ def apply_to(self, obj, **kwargs):
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

0 comments on commit 42921c8

Please sign in to comment.