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

Interpolate on axis #3214

Merged
merged 17 commits into from Sep 24, 2023
Merged

Conversation

pietsjoh
Copy link
Contributor

@pietsjoh pietsjoh commented Aug 25, 2023

Description of the change

The idea for this feature came from the wish to create the functionality to remove the background from a signal (remove_background_signal). For this problem the signal dimension is usually 1 and the background signal is loaded from a seperate file. The axis of the background signal b doesn't necessarily match the axis of the measured signal s. Thus for the subtraction of the background it would be nice to simply interpolate the b onto the axis of s and then subtract s-b.

This PR aims to solve the interpolation part, which may be more generally useful. The function interpolate_on_axis() takes an axis (UniformDataAxis, FunctionalDataAxis or DataAxis) as input and replaces an axis (specified by an index). The data is interpolated using scipy.interpolate.interp1d in the process. This feature works both on signal and navigation axes and replaces one axes for a signal with arbitrary dimensions. Moreover, it can be used to switch between different axis types.

Progress of the PR

  • Change implemented
  • handle extrapolation (custom error msg or use scipy bounds_error?)
  • which index to use (from set_axis or from axes_manager?)
  • update docstring (if appropriate),
  • update user guide (if appropriate),
  • add an changelog entry in the upcoming_changes folder (see upcoming_changes/README.rst),
  • Check formatting changelog entry in the readthedocs doc build of this PR (link in github checks)
  • add tests,
  • ready for review.

Minimal example of the bug fix or the new feature

import hyperspy.api as hs
import numpy as np

x = {"offset": 0, "scale": 1, "size": 10, "name": "X", "units": "µm", "navigate": True}
s = {"offset": 0, "scale": 1, "size": 50, "name": "S", "units": "eV", "navigate": False}

x_new = UniformDataAxis(offset=1.5, scale=0.8, size=7, navigate=True, name="X", units="nm")
s_new = DataAxis(axis=np.arange(23)**2, navigate=False, name="S", units="eV")

d = np.random.random((10, 50))
s = hs.signals.Signal1D(d, axes=[x, s])

s2 = s.interpolate_on_axis(x_new, 0, inplace=False)
s2.interpolate_on_axis(s_new, 1, inplace=True)

@codecov
Copy link

codecov bot commented Aug 25, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.12% 🎉

Comparison is base (c1a0de8) 81.25% compared to head (aebbd25) 81.38%.
Report is 140 commits behind head on RELEASE_next_major.

Additional details and impacted files
@@                  Coverage Diff                   @@
##           RELEASE_next_major    #3214      +/-   ##
======================================================
+ Coverage               81.25%   81.38%   +0.12%     
======================================================
  Files                     173      173              
  Lines                   24192    24352     +160     
  Branches                 5621     5677      +56     
======================================================
+ Hits                    19657    19818     +161     
+ Misses                   3236     3222      -14     
- Partials                 1299     1312      +13     
Files Changed Coverage Δ
hyperspy/signal.py 77.73% <100.00%> (+0.09%) ⬆️

... and 19 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jlaehne jlaehne added this to the v2.0 Split milestone Aug 27, 2023
@pietsjoh pietsjoh marked this pull request as ready for review August 30, 2023 14:38
doc/user_guide/signal/generic_tools.rst Outdated Show resolved Hide resolved
doc/user_guide/signal/generic_tools.rst Outdated Show resolved Hide resolved
doc/user_guide/signal/generic_tools.rst Outdated Show resolved Hide resolved
hyperspy/signal.py Outdated Show resolved Hide resolved
doc/user_guide/signal/generic_tools.rst Outdated Show resolved Hide resolved
hyperspy/signal.py Show resolved Hide resolved
hyperspy/signal.py Outdated Show resolved Hide resolved
@jlaehne jlaehne requested a review from ericpre September 1, 2023 10:04
@jlaehne jlaehne mentioned this pull request Sep 1, 2023
hyperspy/signal.py Outdated Show resolved Hide resolved
hyperspy/signal.py Outdated Show resolved Hide resolved
hyperspy/signal.py Show resolved Hide resolved
@ericpre ericpre merged commit 2109811 into hyperspy:RELEASE_next_major Sep 24, 2023
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants