Skip to content

Commit

Permalink
Merge pull request #414 from AstarVienna/hb/preventnegativequantization
Browse files Browse the repository at this point in the history
Cap negative values below 0 before quantifying to an unsigned int.
  • Loading branch information
hugobuddel committed May 14, 2024
2 parents 8549c75 + 42921c8 commit 9911159
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

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

# 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 9911159

Please sign in to comment.