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

Saving and loading of SplineModel yields incorrect result.init_fit #990

Open
paulmueller opened this issue Jan 28, 2025 · 4 comments
Open

Comments

@paulmueller
Copy link
Contributor

While working on #985, I noticed that the init_fit property is not properly loaded after loading a saved SplineModel:

import numpy as np
from lmfit.models import SplineModel
from lmfit.model import save_modelresult, load_modelresult

model_file = 'spline_modelresult.sav'
xx = np.linspace(-10, 10, 100)
yy = 0.6*np.exp(-(xx**2)/(1.3**2))

spl_model = SplineModel(xknots=np.linspace(-10, 10, 5))
params = spl_model.guess(yy, xx)
result = spl_model.fit(yy, params, x=xx)

save_modelresult(result, model_file)

result2 = load_modelresult(model_file)
print(result.init_fit)
[3.14000311e-26 3.57373261e-04 1.40993305e-03 3.12840834e-03
 5.48352810e-03 8.44602130e-03 1.19866169e-02 1.60760439e-02
 2.06850312e-02 2.57843078e-02 3.13446027e-02 3.73366449e-02
 4.37311633e-02 5.04988868e-02 5.76105445e-02 6.50368654e-02
 7.27485783e-02 8.07164123e-02 8.89110963e-02 9.73033593e-02
 1.05863930e-01 1.14563538e-01 1.23372912e-01 1.32262781e-01
 1.41203873e-01 1.50166919e-01 1.59122646e-01 1.68041784e-01
 1.76895061e-01 1.85653207e-01 1.94286951e-01 2.02767022e-01
 2.11064148e-01 2.19149059e-01 2.26992483e-01 2.34565150e-01
 2.41837788e-01 2.48781126e-01 2.55365894e-01 2.61562821e-01
 2.67342635e-01 2.72676065e-01 2.77533840e-01 2.81886689e-01
 2.85705342e-01 2.88960527e-01 2.91622973e-01 2.93663409e-01
 2.95052565e-01 2.95761168e-01 2.95761168e-01 2.95052565e-01
 2.93663409e-01 2.91622973e-01 2.88960527e-01 2.85705342e-01
 2.81886689e-01 2.77533840e-01 2.72676065e-01 2.67342635e-01
 2.61562821e-01 2.55365894e-01 2.48781126e-01 2.41837788e-01
 2.34565150e-01 2.26992483e-01 2.19149059e-01 2.11064148e-01
 2.02767022e-01 1.94286951e-01 1.85653207e-01 1.76895061e-01
 1.68041784e-01 1.59122646e-01 1.50166919e-01 1.41203873e-01
 1.32262781e-01 1.23372912e-01 1.14563538e-01 1.05863930e-01
 9.73033593e-02 8.89110963e-02 8.07164123e-02 7.27485783e-02
 6.50368654e-02 5.76105445e-02 5.04988868e-02 4.37311633e-02
 3.73366449e-02 3.13446027e-02 2.57843078e-02 2.06850312e-02
 1.60760439e-02 1.19866169e-02 8.44602130e-03 5.48352810e-03
 3.12840834e-03 1.40993305e-03 3.57373261e-04 3.14000311e-26]
print(result2.init_fit)
[-10.          -9.7979798   -9.5959596   -9.39393939  -9.19191919
  -8.98989899  -8.78787879  -8.58585859  -8.38383838  -8.18181818
  -7.97979798  -7.77777778  -7.57575758  -7.37373737  -7.17171717
  -6.96969697  -6.76767677  -6.56565657  -6.36363636  -6.16161616
  -5.95959596  -5.75757576  -5.55555556  -5.35353535  -5.15151515
  -4.94949495  -4.74747475  -4.54545455  -4.34343434  -4.14141414
  -3.93939394  -3.73737374  -3.53535354  -3.33333333  -3.13131313
  -2.92929293  -2.72727273  -2.52525253  -2.32323232  -2.12121212
  -1.91919192  -1.71717172  -1.51515152  -1.31313131  -1.11111111
  -0.90909091  -0.70707071  -0.50505051  -0.3030303   -0.1010101
   0.1010101    0.3030303    0.50505051   0.70707071   0.90909091
   1.11111111   1.31313131   1.51515152   1.71717172   1.91919192
   2.12121212   2.32323232   2.52525253   2.72727273   2.92929293
   3.13131313   3.33333333   3.53535354   3.73737374   3.93939394
   4.14141414   4.34343434   4.54545455   4.74747475   4.94949495
   5.15151515   5.35353535   5.55555556   5.75757576   5.95959596
   6.16161616   6.36363636   6.56565657   6.76767677   6.96969697
   7.17171717   7.37373737   7.57575758   7.77777778   7.97979798
   8.18181818   8.38383838   8.58585859   8.78787879   8.98989899
   9.19191919   9.39393939   9.5959596    9.7979798   10.        ]

Apparently, there is something going wrong when parsing the parameters from the saved model. After digging in the code, I noticed that the methods result.eval and result2.eval return different results.

@paulmueller
Copy link
Contributor Author

I would also be interested in working on this one, but maybe you @newville already have an idea where to start exactly.

@newville
Copy link
Member

newville commented Feb 2, 2025

@paulmueller can this be closed? I think that #989 probably fixed this sufficiently.

@paulmueller
Copy link
Contributor Author

paulmueller commented Feb 2, 2025

@newville No, this is a different issue that is still present after #989. Note that this only affects the result.init_fit property. This is not about loading the model with the correct number of knots.

@newville
Copy link
Member

newville commented Feb 2, 2025

@paulmueller Ah, thanks -- I somehow missed that important part of this Issue! I will investigate.

@newville newville mentioned this issue Feb 2, 2025
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants