From aec8bc55d3e8da87d4f27ca1428fefba86bbc6b6 Mon Sep 17 00:00:00 2001 From: Tiomat85 Date: Wed, 7 Aug 2024 15:24:26 +0100 Subject: [PATCH] Use Gaussian noise in place of Poisson noise beyond threshold to improve performance --- nionswift_plugin/usim/Noise.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/nionswift_plugin/usim/Noise.py b/nionswift_plugin/usim/Noise.py index 0b64508..5a3886b 100644 --- a/nionswift_plugin/usim/Noise.py +++ b/nionswift_plugin/usim/Noise.py @@ -14,11 +14,16 @@ def __init__(self) -> None: self.enabled = True self.poisson_level: typing.Optional[float] = None - def apply(self, input: DataAndMetadata.DataAndMetadata) -> DataAndMetadata.DataAndMetadata: + def apply(self, input: DataAndMetadata.DataAndMetadata, lambda_thresh: float = 1.0) -> DataAndMetadata.DataAndMetadata: if self.enabled and self.poisson_level: rs = numpy.random.RandomState() # use this to avoid blocking other calls to poisson input_data = input.data assert input_data is not None - poisson_data = typing.cast(_NDArray, rs.poisson(self.poisson_level, size=input_data.shape).astype(input_data.dtype)) + if self.poisson_level > lambda_thresh: + # Since it is 'high' lambda, we can approximate it to a normal distribution + poisson_data = typing.cast(_NDArray, rs.normal(loc=self.poisson_level, scale=numpy.sqrt(self.poisson_level), size=input_data.shape).astype(input_data.dtype)) + else: + poisson_data = typing.cast(_NDArray, rs.poisson(self.poisson_level, size=input_data.shape).astype(input_data.dtype)) return input + (poisson_data - self.poisson_level) return input +