LumiSpy / lumispy

Luminescence data analysis with HyperSpy.
https://lumispy.org
GNU General Public License v3.0
26 stars 17 forks source link

Slicing of energy/wavenumber signal with isig fails #155

Closed jlaehne closed 1 year ago

jlaehne commented 1 year ago

Describe the bug

Error occurs when slicing a signal with isig[x:y] that was converted using the lumispy functions to_eV() or to_invcm().

To Reproduce

Steps to reproduce the behavior:

import hyperspy.api as hs
S = hs.signals.Signal1D(np.arange(100),axes=[{'axis': np.arange(100)+300}])
S.set_signal_type("CL")
S.to_eV(inplace=True)
S.isig[3.251:4.052]

Leads to TypingError:

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function diff at 0x7fad54e2c310>) found for signature:

 >>> diff(array(float64, 1d, A))

There are 2 candidate implementations:
  - Of which 2 did not match due to:
  Overload in function 'np_diff_impl': File: numba/np/arraymath.py: Line 3684.
    With argument(s): '(array(float64, 1d, A))':
   Rejected as the implementation raised a specific error:
     TypingError: Failed in nopython mode pipeline (step: nopython frontend)
   - Resolution failure for literal arguments:
   reshape() supports contiguous array only
   - Resolution failure for non-literal arguments:
   reshape() supports contiguous array only

   During: resolving callee type: BoundFunction(array.reshape for array(float64, 1d, A))
   During: typing of call at /usr/lib/python3.10/site-packages/numba/np/arraymath.py (3702)

   File "../../../../usr/lib/python3.10/site-packages/numba/np/arraymath.py", line 3702:
       def diff_impl(a, n=1):
           <source elided>
           # To make things easier, normalize input and output into 2d arrays
           a2 = a.reshape((-1, size))
           ^

  raised from /usr/lib/python3.10/site-packages/numba/core/typeinfer.py:1086

During: resolving callee type: Function(<function diff at 0x7fad54e2c310>)
During: typing of call at /home/jonas/medien/pdi/git/hyperspy/hyperspy/misc/array_tools.py (428)

File "../../medien/pdi/git/hyperspy/hyperspy/misc/array_tools.py", line 428:
def numba_closest_index_round(axis_array, value_array):
    <source elided>
    rtol = 1e-10
    machineepsilon = np.min(np.abs(np.diff(axis_array))) * rtol
    ^

Expected behavior

If the energy is set up manually it works

import hyperspy.api as hs
import lumispy as lum
S1 = hs.signals.Signal1D(np.arange(100),axes=[{'axis': lum.nm2eV((np.arange(100)+300)[::-1])}])
S.set_signal_type("CL")
S.isig[3.251:4.052]

Additional context

Fix will be submitted, forcing the dtype in axis2eV and axis2invcm.