本文整理汇总了Python中larch.Group.init_bkg方法的典型用法代码示例。如果您正苦于以下问题:Python Group.init_bkg方法的具体用法?Python Group.init_bkg怎么用?Python Group.init_bkg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类larch.Group
的用法示例。
在下文中一共展示了Group.init_bkg方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: autobk
# 需要导入模块: from larch import Group [as 别名]
# 或者: from larch.Group import init_bkg [as 别名]
#.........这里部分代码省略.........
# pre-load FT window
ftwin = kout**kweight * ftwindow(kout, xmin=kmin, xmax=kmax,
window=win, dx=dk)
# calc k-value and initial guess for y-values of spline params
nspl = max(4, min(128, 2*int(rbkg*(kmax-kmin)/np.pi) + 1))
spl_y, spl_k, spl_e = np.zeros(nspl), np.zeros(nspl), np.zeros(nspl)
for i in range(nspl):
q = kmin + i*(kmax-kmin)/(nspl - 1)
ik = index_nearest(kraw, q)
i1 = min(len(kraw)-1, ik + 5)
i2 = max(0, ik - 5)
spl_k[i] = kraw[ik]
spl_e[i] = energy[ik+ie0]
spl_y[i] = (2*mu[ik+ie0] + mu[i1+ie0] + mu[i2+ie0] ) / 4.0
# get spline represention: knots, coefs, order=3
# coefs will be varied in fit.
knots, coefs, order = splrep(spl_k, spl_y)
# set fit parameters from initial coefficients
params = Group()
for i in range(len(coefs)):
name = FMT_COEF % i
p = Parameter(coefs[i], name=name, vary=i<len(spl_y))
p._getval()
setattr(params, name, p)
initbkg, initchi = spline_eval(kraw[:iemax-ie0+1], mu[ie0:iemax+1],
knots, coefs, order, kout)
# do fit
fit = Minimizer(__resid, params, _larch=_larch, toler=1.e-4,
fcn_kws = dict(ncoefs=len(coefs), chi_std=chi_std,
knots=knots, order=order,
kraw=kraw[:iemax-ie0+1],
mu=mu[ie0:iemax+1], irbkg=irbkg, kout=kout,
ftwin=ftwin, kweight=kweight,
nfft=nfft, nclamp=nclamp,
clamp_lo=clamp_lo, clamp_hi=clamp_hi))
fit.leastsq()
# write final results
coefs = [getattr(params, FMT_COEF % i) for i in range(len(coefs))]
bkg, chi = spline_eval(kraw[:iemax-ie0+1], mu[ie0:iemax+1],
knots, coefs, order, kout)
obkg = np.copy(mu)
obkg[ie0:ie0+len(bkg)] = bkg
# outputs to group
group = set_xafsGroup(group, _larch=_larch)
group.bkg = obkg
group.chie = (mu-obkg)/edge_step
group.k = kout
group.chi = chi/edge_step
# now fill in 'autobk_details' group
params.init_bkg = np.copy(mu)
params.init_bkg[ie0:ie0+len(bkg)] = initbkg
params.init_chi = initchi/edge_step
params.knots_e = spl_e
params.knots_y = np.array([coefs[i] for i in range(nspl)])
params.init_knots_y = spl_y
params.nfev = params.fit_details.nfev
params.kmin = kmin
params.kmax = kmax
group.autobk_details = params
# uncertainties in mu0 and chi: fairly slow!!
if HAS_UNCERTAIN and calc_uncertainties:
vbest, vstd = [], []
for n in fit.var_names:
par = getattr(params, n)
vbest.append(par.value)
vstd.append(par.stderr)
uvars = uncertainties.correlated_values(vbest, params.covar)
# uncertainty in bkg (aka mu0)
# note that much of this is working around
# limitations in the uncertainty package that make it
# 1. take an argument list (not array)
# 2. work on returned scalars (but not arrays)
# 3. not handle kw args and *args well (so use
# of global "index" is important here)
nkx = iemax-ie0 + 1
def my_dsplev(*args):
coefs = np.array(args)
return splev(kraw[:nkx], [knots, coefs, order])[index]
fdbkg = uncertainties.wrap(my_dsplev)
dmu0 = [fdbkg(*uvars).std_dev() for index in range(len(bkg))]
group.delta_bkg = np.zeros(len(mu))
group.delta_bkg[ie0:ie0+len(bkg)] = np.array(dmu0)
# uncertainty in chi (see notes above)
def my_dchi(*args):
coefs = np.array(args)
b,chi = spline_eval(kraw[:nkx], mu[ie0:iemax+1],
knots, coefs, order, kout)
return chi[index]
fdchi = uncertainties.wrap(my_dchi)
dchi = [fdchi(*uvars).std_dev() for index in range(len(kout))]
group.delta_chi = np.array(dchi)/edge_step
示例2: autobk
# 需要导入模块: from larch import Group [as 别名]
# 或者: from larch.Group import init_bkg [as 别名]
#.........这里部分代码省略.........
# coefs will be varied in fit.
knots, coefs, order = splrep(spl_k, spl_y)
# set fit parameters from initial coefficients
params = Parameters()
for i in range(len(coefs)):
params.add(name = FMT_COEF % i, value=coefs[i], vary=i<len(spl_y))
initbkg, initchi = spline_eval(kraw[:iemax-ie0+1], mu[ie0:iemax+1],
knots, coefs, order, kout)
# do fit
result = minimize(__resid, params, method='leastsq',
gtol=1.e-5, ftol=1.e-5, xtol=1.e-5, epsfcn=1.e-5,
kws = dict(ncoefs=len(coefs), chi_std=chi_std,
knots=knots, order=order,
kraw=kraw[:iemax-ie0+1],
mu=mu[ie0:iemax+1], irbkg=irbkg, kout=kout,
ftwin=ftwin, kweight=kweight,
nfft=nfft, nclamp=nclamp,
clamp_lo=clamp_lo, clamp_hi=clamp_hi))
# write final results
coefs = [result.params[FMT_COEF % i].value for i in range(len(coefs))]
bkg, chi = spline_eval(kraw[:iemax-ie0+1], mu[ie0:iemax+1],
knots, coefs, order, kout)
obkg = np.copy(mu)
obkg[ie0:ie0+len(bkg)] = bkg
# outputs to group
group = set_xafsGroup(group, _larch=_larch)
group.bkg = obkg
group.chie = (mu-obkg)/edge_step
group.k = kout
group.chi = chi/edge_step
group.e0 = e0
# now fill in 'autobk_details' group
details = Group(params=result.params)
details.init_bkg = np.copy(mu)
details.init_bkg[ie0:ie0+len(bkg)] = initbkg
details.init_chi = initchi/edge_step
details.knots_e = spl_e
details.knots_y = np.array([coefs[i] for i in range(nspl)])
details.init_knots_y = spl_y
details.nfev = result.nfev
details.kmin = kmin
details.kmax = kmax
group.autobk_details = details
# uncertainties in mu0 and chi: can be fairly slow.
if calc_uncertainties:
nchi = len(chi)
nmue = iemax-ie0 + 1
redchi = result.redchi
covar = result.covar / redchi
jac_chi = np.zeros(nchi*nspl).reshape((nspl, nchi))
jac_bkg = np.zeros(nmue*nspl).reshape((nspl, nmue))
cvals, cerrs = [], []
for i in range(len(coefs)):
par = result.params[FMT_COEF % i]
cvals.append(getattr(par, 'value', 0.0))
cdel = getattr(par, 'stderr', 0.0)
if cdel is None:
cdel = 0.0
cerrs.append(cdel/2.0)
cvals = np.array(cvals)
cerrs = np.array(cerrs)
# find derivatives by hand!
_k = kraw[:nmue]
_m = mu[ie0:iemax+1]
for i in range(nspl):
cval0 = cvals[i]
cvals[i] = cval0 + cerrs[i]
bkg1, chi1 = spline_eval(_k, _m, knots, cvals, order, kout)
cvals[i] = cval0 - cerrs[i]
bkg2, chi2 = spline_eval(_k, _m, knots, cvals, order, kout)
cvals[i] = cval0
jac_chi[i] = (chi1 - chi2) / (2*cerrs[i])
jac_bkg[i] = (bkg1 - bkg2) / (2*cerrs[i])
dfchi = np.zeros(nchi)
dfbkg = np.zeros(nmue)
for i in range(nspl):
for j in range(nspl):
dfchi += jac_chi[i]*jac_chi[j]*covar[i,j]
dfbkg += jac_bkg[i]*jac_bkg[j]*covar[i,j]
prob = 0.5*(1.0 + erf(err_sigma/np.sqrt(2.0)))
dchi = t.ppf(prob, nchi-nspl) * np.sqrt(dfchi*redchi)
dbkg = t.ppf(prob, nmue-nspl) * np.sqrt(dfbkg*redchi)
group.delta_chi = dchi
group.delta_bkg = 0.0*mu
group.delta_bkg[ie0:ie0+len(dbkg)] = dbkg