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

Suspend plotting during fit #2796

Merged
merged 3 commits into from Aug 15, 2021

Conversation

ericpre
Copy link
Member

@ericpre ericpre commented Jul 8, 2021

Progress of the PR

  • Suspend plotting when calling smart_fit to avoid error with event connections,
  • 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)
  • ready for review.

Minimal example of the bug fix or the new feature

import hyperspy.api as hs

s = hs.datasets.eelsdb(title="Hexagonal Boron Nitride", spectrum_type="coreloss")[0]
ll = hs.datasets.eelsdb(title="Hexagonal Boron Nitride", spectrum_type="lowloss")[0]

s.set_microscope_parameters(beam_energy=300,
                            convergence_angle=0.2,
                            collection_angle=2.55)

s.add_elements(('B', 'N'))
m = s.create_model()
# m = s.create_model(ll=ll)
m.plot()
m.smart_fit()
m.enable_fine_structure()
m.smart_fit()

traceback:

Traceback (most recent call last):

  File "/home/eric/Dev/hyperspy/hyperspy/model.py", line 582, in update_plot
    self._model_line.update(render_figure=render_figure,

  File "/home/eric/Dev/hyperspy/hyperspy/drawing/signal1d.py", line 473, in update
    ydata = self._get_data()

  File "/home/eric/Dev/hyperspy/hyperspy/drawing/signal1d.py", line 431, in _get_data
    ydata = self.data_function(axes_manager=self.axes_manager,

  File "/home/eric/Dev/hyperspy/hyperspy/models/model1d.py", line 683, in _model2plot
    s = self.__call__(non_convolved=False, onlyactive=True)

  File "/home/eric/Dev/hyperspy/hyperspy/models/model1d.py", line 413, in __call__
    sum_ += component.function(axis)

  File "/home/eric/Dev/hyperspy/hyperspy/_components/eels_cl_edge.py", line 315, in function
    cts[bfs] = splev(

  File "/opt/miniconda3/lib/python3.8/site-packages/scipy/interpolate/fitpack.py", line 374, in splev
    return _impl.splev(x, tck, der, ext)

  File "/opt/miniconda3/lib/python3.8/site-packages/scipy/interpolate/_fitpack_impl.py", line 598, in splev
    raise ValueError("Invalid input data")

ValueError: Invalid input data


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "/home/eric/Dev/hyperspy/untitled1.py", line 23, in <module>
    m.smart_fit()

  File "/home/eric/Dev/hyperspy/hyperspy/models/eelsmodel.py", line 358, in smart_fit
    self._fit_edge(i, start_energy, **kwargs)

  File "/home/eric/Dev/hyperspy/hyperspy/models/eelsmodel.py", line 528, in _fit_edge
    self.remove_fine_structure_data(to_activate_fs)

  File "/home/eric/Dev/hyperspy/hyperspy/models/eelsmodel.py", line 597, in remove_fine_structure_data
    self.remove_signal_range(start, stop)

  File "/home/eric/Dev/hyperspy/hyperspy/decorators.py", line 107, in wrapper
    cm(self, *args, **kwargs)

  File "/home/eric/Dev/hyperspy/hyperspy/models/model1d.py", line 505, in remove_signal_range
    self._remove_signal_range_in_pixels(*indices)

  File "/home/eric/Dev/hyperspy/hyperspy/models/model1d.py", line 493, in _remove_signal_range_in_pixels
    self.update_plot()

  File "/home/eric/Dev/hyperspy/hyperspy/model.py", line 589, in update_plot
    self._disconnect_parameters2update_plot(components=self)

  File "/home/eric/Dev/hyperspy/hyperspy/model.py", line 563, in _disconnect_parameters2update_plot
    component.events.active_changed.disconnect(

  File "/home/eric/Dev/hyperspy/hyperspy/events.py", line 392, in disconnect
    raise ValueError("The %s function is not connected to %s." %

ValueError: The <bound method Signal1DLine._auto_update_line of <hyperspy.drawing.signal1d.Signal1DLine object at 0x7f0a51cf7cd0>> function is not connected to <hyperspy.events.Event: Event that triggers when the `Component.active` changes.: set()>.

It is surprising that with convolution with the low loss, smart_fit works fine when the model is plotted and without convolution, it raise an error (only when the model is plotted)! I am not sure if the ValueError("Invalid input data") is a red herring or not.
In any case, the plot should be suspended during calling smart_fit, because it slows down the fit, as it does for multifit

@codecov
Copy link

codecov bot commented Jul 8, 2021

Codecov Report

Merging #2796 (8c7c936) into RELEASE_next_patch (2c61da2) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@                 Coverage Diff                 @@
##           RELEASE_next_patch    #2796   +/-   ##
===================================================
  Coverage               77.36%   77.37%           
===================================================
  Files                     202      202           
  Lines                   30105    30108    +3     
  Branches                 6579     6579           
===================================================
+ Hits                    23292    23295    +3     
  Misses                   5066     5066           
  Partials                 1747     1747           
Impacted Files Coverage Δ
hyperspy/models/eelsmodel.py 57.76% <100.00%> (+0.34%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2c61da2...8c7c936. Read the comment docs.

@jlaehne
Copy link
Contributor

jlaehne commented Aug 3, 2021

lgtm

@ericpre ericpre merged commit 69e8a23 into hyperspy:RELEASE_next_patch Aug 15, 2021
@ericpre ericpre deleted the fix_smart_fit_plotting branch August 19, 2021 20:04
@ericpre ericpre added this to the v1.6.5 milestone Oct 26, 2021
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

2 participants