diff --git a/hyperspy/signal.py b/hyperspy/signal.py index 0d7e4e5f7c..3d20d9660d 100644 --- a/hyperspy/signal.py +++ b/hyperspy/signal.py @@ -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." ) @@ -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." ) @@ -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 diff --git a/hyperspy/tests/test_io.py b/hyperspy/tests/test_io.py index a3260b4f1a..ea7d2f9046 100644 --- a/hyperspy/tests/test_io.py +++ b/hyperspy/tests/test_io.py @@ -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 diff --git a/hyperspy/tests/test_non-uniform_not-implemented.py b/hyperspy/tests/test_non-uniform_not-implemented.py index d68e0bb2d0..eb03413b78 100644 --- a/hyperspy/tests/test_non-uniform_not-implemented.py +++ b/hyperspy/tests/test_non-uniform_not-implemented.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with HyperSpy. If not, see . +import numpy as np import pytest from hyperspy.signals import ( @@ -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) @@ -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)) diff --git a/upcoming_changes/3407.bugfix.rst b/upcoming_changes/3407.bugfix.rst new file mode 100644 index 0000000000..5cc5454e6a --- /dev/null +++ b/upcoming_changes/3407.bugfix.rst @@ -0,0 +1 @@ +Allow rebinning uniform axis of signal containing non-uniform axis. \ No newline at end of file