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

Ensure correct endian in numba_histogram #2678

Merged
merged 4 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion hyperspy/misc/array_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ def _linear_bin(dat, scale, crop=True):
# Set up the result np.array to have a new axis[0] size for after
# cropping.
result = np.zeros((dim,) + dat.shape[1:])
# Make sure that native endian is used
if not dat.dtype.isnative:
dat = dat.astype(dat.dtype.type)
# Carry out binning over axis[0]
_linear_bin_loop(result=result, data=dat, scale=s)
# Swap axis[0] back to the original axis location.
Expand Down Expand Up @@ -375,7 +378,6 @@ def dict2sarray(dictionary, sarray=None, dtype=None):
return sarray


@njit(cache=True)
def numba_histogram(data, bins, ranges):
"""
Parameters
Expand All @@ -392,6 +394,17 @@ def numba_histogram(data, bins, ranges):
hist : array
The values of the histogram.
"""
# Make sure that native endian is used
if not data.dtype.isnative:
data = data.astype(data.dtype.type)
return _numba_histogram(data, bins, ranges)


@njit(cache=True)
def _numba_histogram(data, bins, ranges):
"""
Numba histogram computation requiring native endian datatype.
"""
# Adapted from https://iscinumpy.gitlab.io/post/histogram-speeds-in-python/
hist = np.zeros((bins,), dtype=np.intp)
delta = 1 / ((ranges[1] - ranges[0]) / bins)
Expand Down
13 changes: 11 additions & 2 deletions hyperspy/misc/lowess_smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
from numba import njit


@njit(cache=True, nogil=True)
def lowess(y, x, f=2.0 / 3.0, n_iter=3): # pragma: no cover
def lowess(y, x, f=2.0 / 3.0, n_iter=3):
"""Lowess smoother (robust locally weighted regression).

Fits a nonparametric regression curve to a scatterplot.
Expand All @@ -46,6 +45,16 @@ def lowess(y, x, f=2.0 / 3.0, n_iter=3): # pragma: no cover
yest : np.ndarray
The estimated (smooth) values of y.

"""
if not y.dtype.isnative:
y = y.astype(y.dtype.type)
return _lowess(y, x, f, n_iter)


@njit(cache=True, nogil=True)
def _lowess(y, x, f=2.0 / 3.0, n_iter=3): # pragma: no cover
"""Lowess smoother requiring native endian datatype (for numba).

"""
n = len(x)
r = int(np.ceil(f * n))
Expand Down
8 changes: 8 additions & 0 deletions hyperspy/tests/misc/test_array_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
dict2sarray,
get_array_memory_size_in_GiB,
get_signal_chunk_slice,
numba_histogram,
)

dt = [("x", np.uint8), ("y", np.uint16), ("text", (bytes, 6))]
Expand Down Expand Up @@ -156,6 +157,7 @@ def test_d2s_arrayX():
((5, 5), (1, 12), [slice(0, 5, None), slice(10, 15, None)]),
((5, ), (1, ), [slice(0, 5, None), ]),
((20, ), (1, ), [slice(0, 20, None), ]),
((5, ), [1], [slice(0, 5, None), ]),
((5, ), (25, ), 'error'),
((20, 20), (25, 21), 'error'),
]
Expand Down Expand Up @@ -185,3 +187,9 @@ def test_get_signal_chunk_slice_not_square(sig_chunks, index, expected):
else:
chunk_slice = get_signal_chunk_slice(index, data.chunks)
assert chunk_slice == expected

@pytest.mark.parametrize('dtype', ['<u2', 'u2', '>u2', '<f4', 'f4', '>f4'])
def test_numba_histogram(dtype):
arr = np.arange(100, dtype=dtype)
np.testing.assert_array_equal(numba_histogram(arr, 5, (0, 100)), [20, 20, 20, 20, 20])

6 changes: 4 additions & 2 deletions hyperspy/tests/signal/test_1D_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,13 @@ def setup_method(self, method):
self.atol = 0

@pytest.mark.parametrize('parallel', [True, False])
def test_lowess(self, parallel):
@pytest.mark.parametrize('dtype', ['<f4', 'f4', '>f4'])
def test_lowess(self, parallel, dtype):
from hyperspy.misc.lowess_smooth import lowess
f = 0.5
n_iter = 1
data = np.asanyarray(self.s.data, dtype='float')
self.rtol = 1e-5
data = np.asanyarray(self.s.data, dtype=dtype)
for i in range(data.shape[0]):
data[i, :] = lowess(
x=self.s.axes_manager[-1].axis,
Expand Down
26 changes: 25 additions & 1 deletion hyperspy/tests/signal/test_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from hyperspy.decorators import lazifyTestClass
from hyperspy.signals import Signal1D
from hyperspy.misc.array_tools import rebin


def _test_default_navigation_signal_operations_over_many_axes(self, op):
Expand Down Expand Up @@ -98,7 +99,6 @@ def test_rebin(self):
var = new_s.metadata.Signal.Noise_properties.variance
assert new_s.data.shape == (1, 2, 6)
assert var.data.shape == (1, 2, 6)
from hyperspy.misc.array_tools import rebin

np.testing.assert_array_equal(
rebin(self.signal.data, scale=(2, 2, 1)), var.data
Expand All @@ -115,6 +115,22 @@ def test_rebin(self):
rebin(self.signal.data, scale=(2, 2, 1)), new_s.data
)

def test_rebin_new_shape(self):
self.signal.estimate_poissonian_noise_variance()
new_s = self.signal.rebin(new_shape=(1, 2, 6))
var = new_s.metadata.Signal.Noise_properties.variance
assert new_s.data.shape == (2, 1, 6)
assert var.data.shape == (2, 1, 6)
np.testing.assert_array_equal(
rebin(self.signal.data, new_shape=(2, 1, 6)), new_s.data
)
np.testing.assert_array_equal(
rebin(self.signal.data, new_shape=(2, 1, 6)), var.data
)
new_s = self.signal.rebin(new_shape=(4, 2, 6))
assert new_s.data.shape == self.data.shape


def test_rebin_no_variance(self):
new_s = self.signal.rebin(scale=(2, 2, 1))
with pytest.raises(AttributeError):
Expand All @@ -131,6 +147,14 @@ def test_rebin_dtype(self):
s2 = s.rebin(scale=(3, 3, 1), crop=False)
assert s.sum() == s2.sum()

def test_rebin_errors(self):
with pytest.raises(ValueError):
self.signal.rebin()
with pytest.raises(ValueError):
rebin(self.signal.data, scale=(3, 3, 1), new_shape=(1, 2, 6))
with pytest.raises(ValueError):
rebin(self.signal.data, scale=(2, 2, 2, 2))

def test_swap_axes_simple(self):
s = self.signal
assert s.swap_axes(0, 1).data.shape == (4, 2, 6)
Expand Down
11 changes: 7 additions & 4 deletions hyperspy/tests/signal/test_linear_rebin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@


import numpy as np
import pytest

from hyperspy.signals import EDSTEMSpectrum


class TestLinearRebin:
def test_linear_downsize(self):
spectrum = EDSTEMSpectrum(np.ones([3, 5, 1]))
@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))
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 @@ -33,8 +35,9 @@ def test_linear_downsize(self):
res = spectrum.rebin(scale=scale, crop=False)
np.testing.assert_allclose(res.data.sum(), spectrum.data.sum())

def test_linear_upsize(self):
spectrum = EDSTEMSpectrum(np.ones([4, 5, 10]))
@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))
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 Down