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

Feat: Add fluorescene background fitting to RSoXS.py #165

Open
dsunday opened this issue Jan 2, 2025 · 1 comment
Open

Feat: Add fluorescene background fitting to RSoXS.py #165

dsunday opened this issue Jan 2, 2025 · 1 comment
Assignees

Comments

@dsunday
Copy link
Collaborator

dsunday commented Jan 2, 2025

Add fluorescence fitting function to the RSoXS.py module, see example of the code below. This function will estimate the fluorescence background around the carbon edge, with the assumption that the power law scaling is identical at all energies.

def XRFBackground(sample, q_lower1, q_upper1, q_lower2, q_upper2,pre, post, printE):
def exp_func(q, para):
A, B, C = para
return A * q**B + C

def err(para,q,y):
    return abs(exp_func(q, para)-y)

# para: [A1 A2 B C2], B1=B2, C1=0
def err_global(para, q1, q2, y1, y2):
    p1 = para[0], para[2], 0
    p2 = para[1], para[2], para[3]

    err1 = err(p1,q1,y1)
    err2 = err(p2,q2,y2)

    return np.concatenate((err1,err2))

pre_int=sample.sel(energy=pre, q=slice(q_lower1,q_upper1)).mean('chi').values
pre_q=sample.sel(energy=pre, q=slice(q_lower1,q_upper1)).mean('chi')['q']
pre_int=pre_int[:,0,0]


post_int=sample.sel(energy=post, q=slice(q_lower1,q_upper1)).mean('chi').values
post_q=sample.sel(energy=post, q=slice(q_lower1,q_upper1)).mean('chi')['q']
post_int=post_int[:,0,0]

plt.plot(post_q,post_int)
plt.plot(pre_q,pre_int)
plt.show()
# initial guess of para:
init_guess = [1, 1, -4, 0]

para_best, ier = scipy.optimize.leastsq(err_global, init_guess, args=(pre_q,post_q,pre_int, post_int))
para_best1 = [para_best[0], para_best[2], 0]
para_best2 = [para_best[1], para_best[2], para_best[3]]

xrf_power_law_fit = para_best[2]
para_best

sample.sel(energy=pre ).mean('chi').plot(yscale='log',xscale='log', label=str(pre))
sample.sel(energy=post ).mean('chi').plot(yscale='log',xscale='log', label=str(post))

plt.plot(pre_q,exp_func(pre_q,para_best1),label= str(pre)+' ev fit',color='blue',linewidth=3)
plt.plot(pre_q,exp_func(post_q,para_best2),label=str(post)+' ev fit',color='blue',linewidth=3)
plt.show()
plt.legend(loc='lower left')

# exponential function
def exp_func2(q, para):
    A, C = para
    return A * q**(xrf_power_law_fit) + C

def err2(para,q,y):
    return abs(exp_func2(q, para)-y)

xrf_fit = []
xrf_fit_A = []
Elist=sample['energy']

for i,v in enumerate(Elist):
    if v ==printE:
        intensity=sample.sel(energy=v, q=slice(q_lower2,q_upper2)).mean('chi').values
        intensity=intensity[:,0,0]
        q_values=sample.sel(energy=v, q=slice(q_lower2,q_upper2)).mean('chi')['q']
        init_guess = [1, 80]
        para_best, ier = scipy.optimize.leastsq(err2, init_guess, args=(q_values,intensity))
        xrf_fit.append(para_best[1])
        xrf_fit_A.append(para_best[0])
        sample.sel(energy=printE ).mean('chi').plot(yscale='log',xscale='log', label=str(printE))
        plt.plot(q_values,exp_func2(q_values,para_best),label= str(printE)+' ev fit',color='blue',linewidth=3)
        plt.show()
        plt.legend(loc='lower left')
    else:
        intensity=sample.sel(energy=v, q=slice(q_lower2,q_upper2)).mean('chi').values
        intensity=intensity[:,0,0]
        q_values=sample.sel(energy=v, q=slice(q_lower2,q_upper2)).mean('chi')['q']
        init_guess = [1, 80]
        para_best, ier = scipy.optimize.leastsq(err2, init_guess, args=(q_values,intensity))
        xrf_fit.append(para_best[1])
        xrf_fit_A.append(para_best[0])

xrf_fit=np.asarray(xrf_fit)
Elist=np.asarray(Elist)
xrf_fit[xrf_fit<0]=0
xrf_fit[Elist<285]=0
plt.plot(Elist,xrf_fit,marker='.')
ax = plt.gca()
ax.set(xlabel='eV',ylabel='XRF Offset Value')

return(xrf_fit, xrf_fit_A)
@dsunday dsunday self-assigned this Jan 2, 2025
@EliotGann
Copy link
Collaborator

the assumption that the power law scaling is identical at all energies.

If there is resonant scattering this is not a good assumption. For a simple example, assume there is a three material system with two materials resonant and relatively well mixed, but the third phase (e.g. gold) has sharp boundaries and non resonant. The power law scaling far from the edge would be expected to be q^-4, while when you are getting resonant scattering between the phases nearer the edge, it would lessen. This assumption would fit that change as fluorescence.

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