Skip to content

Commit

Permalink
fix rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaehne committed Jun 11, 2021
1 parent 9b6a944 commit a50e108
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 0 additions & 2 deletions hyperspy/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,6 @@ def value2index(self, value, rounding=round):
else:
value = np.asarray(value)



#Should evaluate on both arrays and scalars. Raises error if there are
#nan values in array
if np.all((value >= self.low_value)*(value <= self.high_value)):
Expand Down
9 changes: 6 additions & 3 deletions hyperspy/misc/array_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,13 @@ def numba_closest_index_round(axis_array,value_array):
#initialise the index same dimension as input, force type to int
index_array = np.empty_like(value_array,dtype='uint')
#assign on flat, iterate on flat.
rtol=1e-12
machineepsilon = np.min(np.abs(np.diff(axis_array)))*rtol
for i,v in enumerate(value_array.flat):
rtol=1e-10
machineepsilon = np.min(np.diff(axis_array))*rtol
index_array.flat[i] = np.abs(axis_array - v + machineepsilon).argmin()
if v>= 0:
index_array.flat[i] = np.abs(axis_array - v - machineepsilon).argmin()
else:
index_array.flat[i] = np.abs(axis_array - v + machineepsilon).argmin()
return index_array

@njit(cache=True)
Expand Down
4 changes: 4 additions & 0 deletions hyperspy/tests/axes/test_data_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ def test_value2index(self):
assert self.axis.value2index(2.5,rounding=math.floor) == 1
# Test that output is integer
assert isinstance(self.axis.value2index(60), (int, np.integer))
self.axis.axis = self.axis.axis - 2
# test rounding on negative value
assert self.axis.value2index(-1.5,rounding=round) == 0


def test_value2index_error(self):
with pytest.raises(ValueError):
Expand Down

0 comments on commit a50e108

Please sign in to comment.