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

Use Gaussian noise in place of Poisson noise beyond threshold to improve performance #45

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions nionswift_plugin/usim/Noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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