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

Implement power law estimation with 2 disconnect intervals #3381

Open
wants to merge 12 commits into
base: RELEASE_next_minor
Choose a base branch
from

Conversation

francisco-dlp
Copy link
Member

@francisco-dlp francisco-dlp commented Jun 2, 2024

Description of the change

So far, hs.model.compones1D.PowerLaw.estimate_parameters could estimate the parameters of the power law using the two-area method on an interval defined by the x1 and x2. Here we implement defining 2 disconnected intervals for estimation by adding the x3 and x4 keyword arguments.

Progress of the PR

  • Change implemented (can be split into several points),
  • 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

pl = hs.model.components1D.PowerLaw()
pl.r.value = 2
pl.A.value = 1e4
axis = np.arange(10, 20, 0.1)
s = hs.signals.Signal1D(pl.function(axis))
s.axes_manager[-1].scale = 0.1
s.axes_manager[-1].offset = 10
s.isig[15.:16.].data[:] = 0
s.add_poissonian_noise()
roi1 = hs.roi.SpanROI(11,14)
roi2 = hs.roi.SpanROI(17, 19)

pl.estimate_parameters(s,
                       roi1.left, roi1.right,
                       roi2.left, roi2.right,

                       only_current=True)

s2 = hs.signals.Signal1D(pl.function(axis))
s2.axes_manager[-1].scale = 0.1
s2.axes_manager[-1].offset = 10
cs = s + s2 * 1j
cs.plot()
roi1.add_widget(cs)
roi2.add_widget(cs)

PL_two_intervals

Copy link

codecov bot commented Jun 2, 2024

Codecov Report

Attention: Patch coverage is 78.57143% with 6 lines in your changes are missing coverage. Please review.

Project coverage is 80.61%. Comparing base (b742845) to head (4175422).

Files Patch % Lines
hyperspy/_components/power_law.py 78.57% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@                  Coverage Diff                   @@
##           RELEASE_next_minor    #3381      +/-   ##
======================================================
- Coverage               80.66%   80.61%   -0.06%     
======================================================
  Files                     146      146              
  Lines                   21941    21927      -14     
  Branches                 5171     5175       +4     
======================================================
- Hits                    17699    17676      -23     
- Misses                   3026     3031       +5     
- Partials                 1216     1220       +4     

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

@ericpre
Copy link
Member

ericpre commented Jun 3, 2024

Is it possible to do more than 2 interval?
I am wondering, if we should think of a more generic API that would easily allow more than two internals, that could be used for other components?

@jlaehne
Copy link
Contributor

jlaehne commented Jun 3, 2024

Is it possible to do more than 2 interval? I am wondering, if we should think of a more generic API that would easily allow more than two internals, that could be used for other components?

I would also be interested to have two intervals, e.g. for background fitting of a linear function.

@francisco-dlp
Copy link
Member Author

Using the two-area method the function can only take 2 intervals. Eventually one could use the same function to estimate the background on more intervals and then find a way to consolidate the results.

Another approach could be linearizing the problem and using linear least squares to estimate the parameters. The we can have as many regions as we like. I have just tested this approach and the estimation is less robust to noise than the two-area method, and takes 2x more time for a single interval. However, it might be competitive timewise for more than 2 intervals.

@jlaehne, since the polynomial component uses polyfit to estimate the parameters, it should be possible to implement multiple-interval estimation for a linear function.

I think that in this PR we could stick to the two-area method with 2 intervals. We can consider adding more intervals and/or adding multi-interval estimation to other components in a separate pull request. However, we should agree on the API for multiple intervals. Any suggestions?

@ericpre
Copy link
Member

ericpre commented Jun 5, 2024

ok, then we should implement an API that can support more than two intervals. The options could be:

  1. Extend the API: x1/x2 can be numbers or arrays, if arrays, x1 and x2 specify the start and the end of all intervals, respectively.
  2. break the API with a deprecation: values need to be passed as an array of shape (n, 2), with n the number of interval (columns) and each row containing the start and end of a single interval.

Option 2 needs a bit more work to go through the deprecation but is possibly more convenient to use, as it avoid having to define start and end of a given interval in two different arrays. Another consideration is that this would consistent with the way the integration_windows are defined in https://hyperspy.org/exspy/reference/signals.html#exspy.signals.EDSSpectrum.get_lines_intensity. Off the top of my head, I can't of other functionalities that deals with parameters defining multiple intervals.

@francisco-dlp
Copy link
Member Author

Here are some alternatives:

  • A mask keyword argument, which is consistent with how Model works.
  • A intervals keyword argument that can take a list of tuples or ROIs defining the intervals.

@ericpre
Copy link
Member

ericpre commented Jun 5, 2024

  • A intervals keyword argument that can take a list of tuples or ROIs defining the intervals.

Yes, that would be convenient to support ROIs.

@jlaehne
Copy link
Contributor

jlaehne commented Jun 5, 2024

  • A intervals keyword argument that can take a list of tuples or ROIs defining the intervals.

Yes, that would be convenient to support ROIs.

I guess for using it with the background removal tool, ROIs would also make sense? But I have not looked into the inner workings of the function.

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