Dear Larch developers (Matt),I believe I found an error in the mback function where I get a NameError saying the name ‘f1_chantler’ is not defined. I found that simply adding f1_chantler to the larch_plugins.xray import section in mback.py fixes the problem. I have included the modified mback.py code, my input data (SigScan226827.txt), project code (mback_test.py), and the error I received (mback_error.txt).I am running python 2.7 with anaconda as my manager, macOS Sierra v10.12, on a mid-2015 15” MacBook pro. I hope this report is correct and helpful.
Regards,SeanSean Fackler, PhDJoint Center for Artificial PhotosynthesisLawrence Berkeley National Lab1 Cyclotron Rd Mail Stop 30R0205Berkeley CA, 94720Mobile: 609-613-8734
import pandas as pd
import matplotlib.pylab as plt
from larch import *
from larch_plugins.xafs import pre_edge
from larch_plugins.xafs import mback
scandata_f = []
#Working for SigScans
def rdin(filename):
scandata_f = pd.read_csv(filename, sep='\t', skiprows=15)
if not ("Counter 0" in scandata_f.columns):
scandata_f = pd.read_csv(filename, sep='\t', skiprows=14)
print scandata_f.columns
if not ("Counter 0" in scandata_f.columns):
print ("Problem with header. skipping 12 or 10 lines did not make it. Check input file.")
return None
return scandata_f
scandata_f = rdin("/Users/SeanFackler/data/ALS092016/160923/SigScan26827.txt")
# print scandata_f.Energy
def prepare_scan(scandata_f, datacounter="TEY_up", reference_counter='Izero'):
# Preparing Scan (normalization)
if 'Counter 4' in scandata_f.columns:
clockname = 'Counter 4'
elif 'Counter 6' in scandata_f.columns:
clockname = 'Counter 6'
else:
print("No counter for clock found (looked for 'Counter 4' and 'Counter 6'). Defaulting to 'Counter 0'.")
clockname = 'Counter 0'
scandata_f["I_Norm0"] = scandata_f[datacounter].astype(float) / scandata_f[reference_counter].astype(float)
scandata_f["I_Normt"] = scandata_f[datacounter].astype(float) / scandata_f[clockname].astype(float)
scandata_f["Energy"] = scandata_f["Energy"].round(1)
# scandata_f["Z"] = scandata_f["Z"].round(2)
return (scandata_f)
prepare_scan(scandata_f)
mylarch = Interpreter()
scandata_f.e = scandata_f.Energy#
scandata_f.mu = scandata_f.I_Norm0#
mback(scandata_f.e, scandata_f.mu, group=scandata_f, z=8, edge='K', order=1, whiteline=6, fit_erfc=True,
_larch=mylarch)
print scandata_f.mback_params.e0
plt.plot(scandata_f.e, scandata_f.f2)
plt.plot(scandata_f.e, scandata_f.fpp)
plt.plot(scandata_f.e, 5*scandata_f.mu)
plt.show()
from larch import Group, Parameter, isgroup, Minimizer, parse_group_args
from larch_plugins.math import index_of
from larch_plugins.xray import xray_edge, xray_line, f1_chantler, f2_chantler, f1f2
from larch_plugins.xafs import set_xafsGroup, find_e0
import numpy as np
from scipy.special import erfc
MAXORDER = 6
def match_f2(p):
"""
Objective function for matching mu(E) data to tabulated f''(E) using the MBACK
algorithm and, optionally, the Lee & Xiang extension.
"""
s = p.s.value
a = p.a.value
em = p.em.value
xi = p.xi.value
c0 = p.c0.value
eoff = p.en - p.e0.value
norm = a*erfc((p.en-em)/xi) + c0 # erfc function + constant term of polynomial
for i in range(MAXORDER):# successive orders of polynomial
j = i+1
attr = 'c%d' % j
if hasattr(p, attr):
norm = norm + getattr(getattr(p, attr), 'value') * eoff**j
func = (p.f2 + norm - s*p.mu) * p.theta / p.weight
if p.leexiang:
func = func / s*p.mu
return func
def mback(energy, mu, group=None, order=3, z=None, edge='K', e0=None, emin=None, emax=None,
whiteline=None, leexiang=False, tables='chantler', fit_erfc=False, return_f1=False,
_larch=None):
"""
Match mu(E) data for tabulated f''(E) using the MBACK algorithm and,
optionally, the Lee & Xiang extension
Arguments:
energy, mu:arrays of energy and mu(E)
order: order of polynomial [3]
group: output group (and input group for e0)
z: Z number of absorber
edge: absorption edge (K, L3)
e0:edge energy
emin: beginning energy for fit
emax: ending energy for fit
whiteline: exclusion zone around white lines
leexiang: flag to use the Lee & Xiang extension
tables:'chantler' (default) or 'cl'
fit_erfc: True to float parameters of error function
return_f1: True to put the f1 array in the group
Returns:
group.f2: tabulated f2(E)
group.f1: tabulated f1(E) (if return_f1 is True)
group.fpp: matched data
group.mback_params: Group of parameters for the minimization
References:
* MBACK (Weng, Waldo, Penner-Hahn): http://dx.doi.org/10.1086/303711
*