Skip to content

Commit

Permalink
transform 0-d arrays into 1-d to check their length (#952)
Browse files Browse the repository at this point in the history
* transform 0-d arrays into 1-d to check their length

* use recommended solution

* fix tests
  • Loading branch information
jcjaskula-aws authored Apr 18, 2024
1 parent de35309 commit f0fc28f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lmfit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ def eval_uncertainty(self, params=None, sigma=1, dscale=0.01, **kwargs):
nvarys = self.nvarys
# ensure fjac and df2 are correct size if independent var updated by kwargs
feval = self.model.eval(params, **userkws)
ndata = len(feval.view('float64')) # allows feval to be complex
ndata = np.atleast_1d(feval).view('float64').ravel().size # allows feval to be complex
covar = self.covar
if any(p.stderr is None for p in params.values()):
return np.zeros(ndata)
Expand Down Expand Up @@ -1720,8 +1720,8 @@ def eval_uncertainty(self, params=None, sigma=1, dscale=0.01, **kwargs):

pars[pname].value = val0
for key in fjac:
fjac[key][i] = (res1[key].view('float64')
- res2[key].view('float64')) / (2*dval)
fjac[key][i] = (np.atleast_1d(res1[key]).view('float64').ravel()
- np.atleast_1d(res2[key]).view('float64').ravel()) / (2*dval)

for i in range(nvarys):
for j in range(nvarys):
Expand All @@ -1738,7 +1738,10 @@ def eval_uncertainty(self, params=None, sigma=1, dscale=0.01, **kwargs):
# for complex data, convert back to real/imag pairs
if feval.dtype in ('complex64', 'complex128'):
for key in fjac:
df2[key] = df2[key][0::2] + 1j * df2[key][1::2]
df2[key] = df2[key].view(feval.dtype)

for key in fjac:
df2[key] = df2[key].reshape(feval.shape)

df2_total = df2.pop('0')
self.dely = scale * np.sqrt(df2_total)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_model_uncertainties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from numpy.testing import assert_allclose

from lmfit.lineshapes import gaussian
from lmfit.model import Model
from lmfit.models import ExponentialModel, GaussianModel, LinearModel


Expand Down Expand Up @@ -127,3 +128,23 @@ def test_component_uncertainties():
assert result.dely_comps['g2_'].mean() < 1.5
assert result.dely_comps['bkg_'].mean() > 0.5
assert result.dely_comps['bkg_'].mean() < 1.5


def test_scalar_independent_vars():
"""Github #951"""
mr = LinearModel().fit(data=[1, 2, 4], x=[1, 2, 3])

assert_allclose(mr.eval_uncertainty(x=1.2), np.array([0.60629034]))
assert_allclose(mr.eval_uncertainty(x=np.array(1.2j)), np.array([1.15903153+0.17475665j]))


def test_multidim_model():
"""test models that return a multi-dimension output"""
def fit(x, p=2):
return np.array([x, p*x])
model = Model(fit)

mr = model.fit(data=np.array([[1, 2, 3], [2, 3, 5]]), x=np.array([1, 2, 3]))

assert_allclose(mr.eval_uncertainty(x=np.array([3, 4])), np.array([[0, 0], [0.18432744, 0.24576991]]))
assert_allclose(mr.eval_uncertainty(x=np.array([3j, 4j])), np.array([[0, 0], [0.13033918+0.13033918j, 0.17378557+0.17378557j]]))

0 comments on commit f0fc28f

Please sign in to comment.