Skip to content

Commit

Permalink
Merge pull request hyperspy#3407 from ericpre/fix_rebin_nonuniform
Browse files Browse the repository at this point in the history
Allow rebinning uniform axis of signal containing non-uniform axis
  • Loading branch information
jlaehne authored Jul 18, 2024
2 parents a5de11d + ce9902f commit 75b5a84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
14 changes: 8 additions & 6 deletions hyperspy/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3575,8 +3575,8 @@ def _validate_rebin_args_and_get_factors(self, new_shape=None, scale=None):
elif new_shape:
if len(new_shape) != len(self.data.shape):
raise ValueError("Wrong new_shape size")
for axis in self.axes_manager._axes:
if axis.is_uniform is False:
for i, axis in enumerate(self.axes_manager._axes):
if axis.is_uniform is False and new_shape[i] != self.data.shape[i]:
raise NotImplementedError(
"Rebinning of non-uniform axes is not yet implemented."
)
Expand All @@ -3590,8 +3590,8 @@ def _validate_rebin_args_and_get_factors(self, new_shape=None, scale=None):
else:
if len(scale) != len(self.data.shape):
raise ValueError("Wrong scale size")
for axis in self.axes_manager._axes:
if axis.is_uniform is False:
for i, axis in enumerate(self.axes_manager._axes):
if axis.is_uniform is False and scale[i] != 1:
raise NotImplementedError(
"Rebinning of non-uniform axes is not yet implemented."
)
Expand Down Expand Up @@ -3684,8 +3684,10 @@ def rebin(self, new_shape=None, scale=None, crop=True, dtype=None, out=None):
s.get_dimensions_from_data()
for axis, axis_src in zip(s.axes_manager._axes, self.axes_manager._axes):
factor = factors[axis.index_in_array]
axis.scale = axis_src.scale * factor
axis.offset = axis_src.offset + (factor - 1) * axis_src.scale / 2
if factor != 1:
# Change scale, offset only when necessary
axis.scale = axis_src.scale * factor
axis.offset = axis_src.offset + (factor - 1) * axis_src.scale / 2
if s.metadata.has_item("Signal.Noise_properties.variance"):
if isinstance(s.metadata.Signal.Noise_properties.variance, BaseSignal):
var = s.metadata.Signal.Noise_properties.variance
Expand Down
7 changes: 4 additions & 3 deletions hyperspy/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ def test_file_reader_warning(caplog, tmp_path):
try:
with caplog.at_level(logging.WARNING):
_ = hs.load(f, reader="some_unknown_file_extension")

assert "Unable to infer file type from extension" in caplog.text
except (ValueError, OSError):
except (ValueError, OSError, IndexError):
# Test fallback to Pillow imaging library
# IndexError is for oldest supported version build on Github CI
pass

assert "Unable to infer file type from extension" in caplog.text


def test_file_reader_options(tmp_path):
# Remove when fixed in rosettasciio
Expand Down
14 changes: 12 additions & 2 deletions hyperspy/tests/test_non-uniform_not-implemented.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with HyperSpy. If not, see <https://www.gnu.org/licenses/#GPL>.

import numpy as np
import pytest

from hyperspy.signals import (
Expand All @@ -33,8 +34,6 @@ def test_signal():
s.ifft()
with pytest.raises(NotImplementedError):
s.diff(0)
with pytest.raises(NotImplementedError):
s.rebin(scale=[1])
with pytest.raises(NotImplementedError):
s.split(number_of_parts=2, axis=0)

Expand Down Expand Up @@ -71,3 +70,14 @@ def test_lazy():
print(s)
with pytest.raises(NotImplementedError):
s.diff(0)


def test_rebin():
s = Signal1D(np.arange(100).reshape(10, 10))
s.axes_manager[-1].convert_to_non_uniform_axis()
s.rebin(scale=(2, 1))
s.rebin(new_shape=(5, 10))
with pytest.raises(NotImplementedError):
s.rebin(scale=(1, 2))
with pytest.raises(NotImplementedError):
s.rebin(new_shape=(1, 5))
1 change: 1 addition & 0 deletions upcoming_changes/3407.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow rebinning uniform axis of signal containing non-uniform axis.

0 comments on commit 75b5a84

Please sign in to comment.