fix(std_daq): prevent division by zero in flat correction

This commit is contained in:
2025-06-16 20:59:57 +02:00
parent 44d02790f4
commit 8a1ceecc1b
2 changed files with 26 additions and 1 deletions

View File

@@ -148,3 +148,25 @@ def test_std_daq_live_processing_apply_flat_dark_correction_with_dark(std_daq_li
assert isinstance(corrected_image, np.ndarray)
assert corrected_image.shape == (100, 100)
assert np.all(corrected_image >= 0), "Corrected image should not have negative values"
def test_std_daq_live_processing_apply_flat_correction_zero_division(std_daq_live_processing):
# Create a mock image
image = np.random.rand(100, 100) * 1000 + 10 # Scale to simulate a realistic image
# Set flat reference with epsilon values
flat = np.ones((100, 100)) * 2
std_daq_live_processing.references["flat_(100, 100)"] = flat
# Set dark reference to ones
dark = np.ones((100, 100)) * 2
std_daq_live_processing.references["dark_(100, 100)"] = dark
# Apply flat correction
corrected_image = std_daq_live_processing.apply_flat_dark_correction(image)
assert isinstance(corrected_image, np.ndarray)
assert corrected_image.shape == (100, 100)
assert np.all(corrected_image >= 0), "Corrected image should not have negative values"
assert np.any(corrected_image < np.inf), "Corrected image should not have infinite values"

View File

@@ -121,7 +121,10 @@ class StdDaqLiveProcessing:
corrected_data = data - dark
return corrected_data
corrected_data = (data - dark) / (flat - dark)
# Ensure that the division does not lead to division by zero
corrected_data = np.divide(
data - dark, flat - dark, out=np.zeros_like(data), where=(flat - dark) != 0
)
return corrected_data
@typechecked