使用复合PseudoVoigt模型时的NameError

问题描述 投票:1回答:1

当使用复合PseudoVoigt模型与参数命名的前缀组合时,我得到一个NameError。

我几乎使用Lorentz配置文件(Fitting a multi-peak function to a DataSet using LMFIT)复制了上一个问题的复合模型示例。这对我来说很好,但Lorentz线形不是我想要的功能。

当我将PseudoVoigtModel用于单峰时,我没有任何问题。此外,LorentzModel可以正常使用下面的代码(我还将其包含在代码中,以便您可以仔细检查/确认自己)。

from lmfit.models import LorentzianModel, PseudoVoigtModel
import numpy as np
import matplotlib.pyplot as plt

def make_model_L(num):
    pref = "f{0}_".format(num)
    model = LorentzianModel(prefix = pref)
    model.set_param_hint(pref+'amplitude', value=amplitude[num], min=0, max=5*amplitude[num])
    model.set_param_hint(pref+'center', value=center[num], min=center[num]-0.5, max=center[num]+0.5)
    model.set_param_hint(pref+'sigma', value=width[num], min=0, max=2)
    return model


def make_model_V(num):
    pref = "f{0}_".format(num)
    model = PseudoVoigtModel(prefix = pref)
    print('before',model.param_names)
    model.set_param_hint(pref+'fraction',value = 0.7, vary = False)
    model.set_param_hint(pref+'amplitude', value=amplitude[num], min=0, max=5*amplitude[num])
    model.set_param_hint(pref+'center', value=center[num], min=center[num]-0.5, max=center[num]+0.5)
    model.set_param_hint(pref+'fwhm', value=3, min=3/5, max=3*5)
    model.set_param_hint(pref+'sigma', value=1, min=0, max=2)
    model.set_param_hint(pref+'height', value=1, min=-np.inf, max=np.inf, expr='(((1-fraction)*amplitude)/(sigma*sqrt(pi/log(2)))+(fraction*amplitude)/(pi*sigma))')
    print(model.param_names)
    return model

# Some really coarse "data"
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
y = [1,1,1,1,3,4,5,6,5,4,3,1,1,1,1,1,1,1,1,3,4,5,6,5,4,3,1,1,1,1]

peaks_in_interval = np.array([43, 159, 191, 296, 435, 544])
amplitude = [3,3]
width = [1,1]
center = [7,21]

mod = None
for i in range(len(center)):
    #this_mod = make_model_L(i)
    this_mod = make_model_V(i)
    if mod is None:
        mod = this_mod
    else:
        mod = mod + this_mod

out=mod.fit(y, x=x, method='leastsq')
plt.interactive(True)
print(out.fit_report())
plt.plot(x, y)
plt.plot(x, out.best_fit, label='best fit')
plt.plot(x, out.init_fit, 'r--', label='fit with initial values')
plt.show()

我收到的错误消息:

名称错误<_ast.Module对象位于0x7f562524dbe0> ^^^名称'小数'未定义

NameError:at expr ='<_ ast.Module object at 0x7f562524dbe0>'

我没有包含TraceBack。它从“out = mod.fit(y,x = x,method ='leastsq')”开始,以raise_exception中的〜/ anaconda3 / lib / python3.6 / site-packages / asteval / asteval.py结尾(self ,node,exc,msg,expr,lineno)“

如前所述,使用Lorentzian模型一切正常,我得到一个合适的(不是一个很好的,但这是由于测试数据)。

我对python不太熟悉,所以我无法真正提供有关问题可能的明确提示。但是,我怀疑它与分数的命名以及它在lmfit.fit() - 函数中的传递方式有关。

最好的,Jan

python python-3.x curve-fitting lmfit
1个回答
0
投票

最好找到并发布一个显示问题的最小示例,并且最好包括完整输出,包括回溯。

例如,您会看到您遇到的问题:

from lmfit.models import PseudoVoigtModel

pref = 'f1_'
model = PseudoVoigtModel(prefix = pref)
print('before',model.param_names)
model.set_param_hint(pref+'fraction',value = 0.7, vary = False)
model.set_param_hint(pref+'amplitude', value=2, min=0, max=5)
model.set_param_hint(pref+'center', value=0, min=-0.5, max=0.5)
model.set_param_hint(pref+'fwhm', value=3, min=3/5, max=3*5)
model.set_param_hint(pref+'sigma', value=1, min=0, max=2)
# suspect line:
model.set_param_hint(pref+'height', value=1, min=-np.inf, max=np.inf,
                     expr='(((1-fraction)*amplitude)/(sigma*sqrt(pi/log(2)))+(fraction*amplitude)/(pi*sigma))')

print(model.param_names)
params = model.make_params()
for p in params.values():
    print(p)

问题出现是因为没有名为fraction的参数。如上面几行所定义,它被命名为f1_fraction

要解决此问题,您应该将pref+'height'的表达式更改为还包括preffractionamplitude所需的sigma前缀字符串。

或者:您可以删除height的提示,因为无论如何都会自动完成,并正确使用您提供的前缀。

也:

a)绝对不鼓励使用参数提示来提供初始值。提示属于模型,不应依赖于任何特定数据集。将模型作为一般事物,然后使用每个数据集的初始值创建参数。

b)不要过于严格地设置边界或基于初始值。应该使用界限(尤其是参数提示)来防止参数偏离非物理值,例如“sigma为负值是没有意义的”,不是因为定义模型的人认为“应该足够接近” ”。让合适的人做好自己的工作。如果确实需要设置自定义边界,请按数据集执行此操作。

© www.soinside.com 2019 - 2024. All rights reserved.