Skip to content

Commit

Permalink
Merge remote-tracking branch 'francisco/BUGFIX_poisson_noise' into Wo…
Browse files Browse the repository at this point in the history
…rkHorse
  • Loading branch information
francisco-dlp committed Jun 3, 2024
2 parents 13205b0 + 38097be commit 7bd8474
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
uses: actions/checkout@v4
- name: Create Release
if: ${{ startsWith(github.ref, 'refs/tags/') && github.repository_owner == 'hyperspy' }}
uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564
uses: softprops/action-gh-release@69320dbe05506a9a39fc8ae11030b214ec2d1f87
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.4.2
rev: v0.4.4
hooks:
# Run the linter.
- id: ruff
Expand Down
44 changes: 13 additions & 31 deletions hyperspy/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6550,17 +6550,14 @@ def _plot_permanent_markers(self):
def add_poissonian_noise(self, keep_dtype=True, random_state=None):
"""Add Poissonian noise to the data.
This method works in-place. The resulting data type is ``int64``.
If this is different from the original data type then a warning
is added to the log.
This method works in-place.
Parameters
----------
keep_dtype : bool, default True
If ``True``, keep the original data type of the signal data. For
example, if the data type was initially ``'float64'``, the result of
the operation (usually ``'int64'``) will be converted to
``'float64'``.
This parameter is deprecated and will be removed in HyperSpy 3.0.
Currently, it does not have any effect. This method always
keeps the original dtype of the signal.
random_state : None, int or numpy.random.Generator, default None
Seed for the random generator.
Expand All @@ -6573,28 +6570,17 @@ def add_poissonian_noise(self, keep_dtype=True, random_state=None):
"""
kwargs = {}
random_state = check_random_state(random_state, lazy=self._lazy)
if not keep_dtype:
warnings.warn(
"The `keep_dtype` parameter is deprecated and will be removed in HyperSpy 3.0.",
DeprecationWarning,
)

if self._lazy:
kwargs["chunks"] = self.data.chunks

original_dtype = self.data.dtype

self.data = random_state.poisson(lam=self.data, **kwargs)

if self.data.dtype != original_dtype:
if keep_dtype:
_logger.warning(
f"Changing data type from {self.data.dtype} "
f"to the original {original_dtype}"
)
# Don't change the object if possible
self.data = self.data.astype(original_dtype, copy=False)
else:
_logger.warning(
f"The data type changed from {original_dtype} "
f"to {self.data.dtype}"
)

self.data[:] = random_state.poisson(lam=self.data, **kwargs)
self.events.data_changed.trigger(obj=self)
self.events.data_changed.trigger(obj=self)

def add_gaussian_noise(self, std, random_state=None):
Expand Down Expand Up @@ -6635,12 +6621,8 @@ def add_gaussian_noise(self, std, random_state=None):

noise = random_state.normal(loc=0, scale=std, size=self.data.shape, **kwargs)

if self._lazy:
# With lazy data we can't keep the same array object
self.data = self.data + noise
else:
# Don't change the object
self.data += noise
self.data += noise
self.events.data_changed.trigger(obj=self)

self.events.data_changed.trigger(obj=self)

Expand Down
19 changes: 7 additions & 12 deletions hyperspy/tests/signals/test_2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,33 +439,28 @@ def test_add_poisson_noise(self):
rng2 = default_rng(123)
else:
data = s.data.copy()
original_data = s.data
rng1 = np.random.default_rng(123)
rng2 = np.random.default_rng(123)

s.add_poissonian_noise(keep_dtype=False, random_state=rng1)
s.add_poissonian_noise(random_state=rng1)

if s._lazy:
s.compute()
assert s.data is original_data

np.testing.assert_array_almost_equal(s.data, rng2.poisson(lam=data, **kwargs))
s.change_dtype("float64")

s.add_poissonian_noise(keep_dtype=True, random_state=rng1)
original_data = s.data
s.add_poissonian_noise(random_state=rng1)
if s._lazy:
s.compute()
assert s.data is original_data

assert s.data.dtype == np.dtype("float64")

def test_add_poisson_noise_warning(self, caplog):
s = self.signal
s.change_dtype("float64")

with caplog.at_level(logging.WARNING):
s.add_poissonian_noise(keep_dtype=True)

assert "Changing data type from" in caplog.text

with caplog.at_level(logging.WARNING):
with pytest.warns(DeprecationWarning):
s.add_poissonian_noise(keep_dtype=False)

assert "The data type changed from" in caplog.text
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ classifiers = [
]
dependencies = [
"cloudpickle",
"dask[array]>=2021.3.1",
"dask[array]>=2021.5.1",
"importlib-metadata>=3.6",
"jinja2",
"matplotlib>=3.1.3",
Expand Down Expand Up @@ -227,8 +227,7 @@ where = ["."]
"*" = ["*.png"]

[tool.setuptools_scm]
fallback_version = "2.2.0.dev0"
version_scheme = "release-branch-semver"
fallback_version = "2.1.1.dev0"

[tool.towncrier]
directory = "upcoming_changes/"
Expand Down

0 comments on commit 7bd8474

Please sign in to comment.