Skip to content

Commit

Permalink
Merge pull request #2690 from ericpre/fix_rebin_offset
Browse files Browse the repository at this point in the history
Fix setting offset in rebin: the offset was changed in the wrong axis.
  • Loading branch information
jlaehne committed Mar 28, 2021
2 parents 9c6be17 + 19a4f99 commit 64698e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ RELEASE_next_patch (Unreleased)
* Add option not to snap ROI when calling the `interactive` method of a ROI (`#2686 <https://github.com/hyperspy/hyperspy/pull/2686>`_)
* Fix broken events when changing signal type `#2683 <https://github.com/hyperspy/hyperspy/pull/2683>`_
* Make DictionaryTreeBrowser lazy by default - see `#368 <https://github.com/hyperspy/hyperspy/issues/368>`_ (`#2623 <https://github.com/hyperspy/hyperspy/pull/2623>`_)
* Fix setting offset in rebin: the offset was changed in the wrong axis (`#2690 <https://github.com/hyperspy/hyperspy/pull/2690>`_)


Changelog
Expand Down
7 changes: 3 additions & 4 deletions hyperspy/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3080,12 +3080,11 @@ def rebin(self, new_shape=None, scale=None, crop=True, out=None):
else:
s.data = data
s.get_dimensions_from_data()
for i, factor in enumerate(factors):
s.axes_manager[i].offset += ((factor - 1)
* s.axes_manager[i].scale) / 2
for axis, axis_src in zip(s.axes_manager._axes,
self.axes_manager._axes):
axis.scale = axis_src.scale * factors[axis.index_in_array]
factor = factors[axis.index_in_array]
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):
Expand Down
23 changes: 10 additions & 13 deletions hyperspy/tests/signals/test_linear_rebin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@


class TestLinearRebin:

@pytest.mark.parametrize('dtype', ['<u2', 'u2', '>u2', '<f4', 'f4', '>f4'])
def test_linear_downsize(self, dtype):
spectrum = EDSTEMSpectrum(np.ones([3, 5, 1],dtype=dtype))
spectrum = EDSTEMSpectrum(np.ones([3, 5, 1], dtype=dtype))
scale = (1.5, 2.5, 1)
res = spectrum.rebin(scale=scale, crop=True)
np.testing.assert_allclose(res.data, 3.75 * np.ones((1, 3, 1)))
Expand All @@ -37,7 +38,7 @@ def test_linear_downsize(self, dtype):

@pytest.mark.parametrize('dtype', ['<u2', 'u2', '>u2', '<f4', 'f4', '>f4'])
def test_linear_upsize(self, dtype):
spectrum = EDSTEMSpectrum(np.ones([4, 5, 10],dtype=dtype))
spectrum = EDSTEMSpectrum(np.ones([4, 5, 10], dtype=dtype))
scale = [0.3, 0.2, 0.5]
res = spectrum.rebin(scale=scale)
np.testing.assert_allclose(res.data, 0.03 * np.ones((20, 16, 20)))
Expand All @@ -48,31 +49,26 @@ def test_linear_upsize(self, dtype):

def test_linear_downscale_out(self):
spectrum = EDSTEMSpectrum(np.ones([4, 1, 1]))
scale = [1, 0.4, 1]
scale = [1, 2, 1]
offset = [0, 0.5, 0]
res = spectrum.rebin(scale=scale)
spectrum.data[2][0] = 5
spectrum.rebin(scale=scale, out=res)
np.testing.assert_allclose(
res.data,
[
[[0.4]],
[[0.4]],
[[0.4]],
[[0.4]],
[[0.4]],
[[2.0]],
[[2.0]],
[[1.2]],
[[0.4]],
[[0.4]],
[[2.]],
[[6.]],
],
)
for axis in res.axes_manager._axes:
assert scale[axis.index_in_axes_manager] == axis.scale
assert offset[axis.index_in_axes_manager] == axis.offset

def test_linear_upscale_out(self):
spectrum = EDSTEMSpectrum(np.ones([4, 1, 1]))
scale = [1, 0.4, 1]
offset = [0, -0.3, 0]
res = spectrum.rebin(scale=scale)
spectrum.data[2][0] = 5
spectrum.rebin(scale=scale, out=res)
Expand All @@ -94,3 +90,4 @@ def test_linear_upscale_out(self):
)
for axis in res.axes_manager._axes:
assert scale[axis.index_in_axes_manager] == axis.scale
assert offset[axis.index_in_axes_manager] == axis.offset

0 comments on commit 64698e6

Please sign in to comment.