2021-09-10 15:38:27 +02:00
|
|
|
import pandas as pd
|
|
|
|
|
import numpy as np
|
|
|
|
|
import os
|
2022-03-31 17:05:32 +02:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
import beamtime.auxillary as aux
|
2022-04-06 21:10:40 +02:00
|
|
|
import beamtime.xanes as xas
|
|
|
|
|
import beamtime.xanes.io as io
|
2021-09-10 16:29:31 +02:00
|
|
|
def rbkerbest():
|
|
|
|
|
print("ROSENBORG!<3")
|
|
|
|
|
|
2021-10-14 14:18:39 +02:00
|
|
|
#def split_xanes_scan(filename, destination=None):
|
2021-09-10 16:29:31 +02:00
|
|
|
|
2021-10-14 14:18:39 +02:00
|
|
|
# with open(filename, 'r') as f:
|
2021-09-10 15:38:27 +02:00
|
|
|
|
2021-10-14 14:18:39 +02:00
|
|
|
|
|
|
|
|
##Better to make a new function that loops through the files, and performing the split_xanes_scan on
|
|
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
#Tryiung to make a function that can decide which edge it is based on the first ZapEnergy-value
|
|
|
|
|
def finding_edge(df):
|
|
|
|
|
if 5.9 < df["ZapEnergy"][0] < 6.5:
|
|
|
|
|
edge='Mn'
|
|
|
|
|
return(edge)
|
|
|
|
|
if 8.0 < df["ZapEnergy"][0] < 8.6:
|
|
|
|
|
edge='Ni'
|
|
|
|
|
return(edge)
|
|
|
|
|
|
|
|
|
|
#def pre_edge_subtraction(df,filenames, options={}):
|
|
|
|
|
def test(innmat):
|
|
|
|
|
df_test= xas.io.put_in_dataframe(innmat)
|
|
|
|
|
print(df_test)
|
|
|
|
|
|
|
|
|
|
def pre_edge_subtraction(path, options={}):
|
|
|
|
|
required_options = ['print','troubleshoot']
|
2022-03-31 17:05:32 +02:00
|
|
|
default_options = {
|
2022-04-06 21:10:40 +02:00
|
|
|
'print': False,
|
|
|
|
|
'troubleshoot': False
|
2022-03-31 17:05:32 +02:00
|
|
|
}
|
|
|
|
|
options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
|
2021-09-10 15:38:27 +02:00
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
filenames = xas.io.get_filenames(path)
|
|
|
|
|
df= xas.io.put_in_dataframe(path)
|
|
|
|
|
edge=finding_edge(df)
|
|
|
|
|
|
|
|
|
|
#Defining the end of the region used to define the background, thus start of the edge
|
|
|
|
|
#implement widget
|
|
|
|
|
if edge == 'Mn':
|
2022-04-07 11:34:44 +02:00
|
|
|
edge_start = 6.42
|
2022-04-06 21:10:40 +02:00
|
|
|
if edge == 'Ni':
|
2022-03-31 17:05:32 +02:00
|
|
|
edge_start = 8.3
|
2021-09-10 15:38:27 +02:00
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
#making a dataframe only containing the rows that are included in the background subtraction (points lower than where the edge start is defined)
|
|
|
|
|
df_start=df.loc[df["ZapEnergy"] < edge_start]
|
|
|
|
|
|
|
|
|
|
#Making a new dataframe, with only the ZapEnergies as the first column -> will be filled to include the background data
|
|
|
|
|
df_bkgd = pd.DataFrame(df["ZapEnergy"])
|
2021-09-10 15:38:27 +02:00
|
|
|
|
2022-03-31 17:05:32 +02:00
|
|
|
for files in filenames:
|
2021-09-10 15:38:27 +02:00
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
#Fitting linear function to the background
|
2022-03-31 17:05:32 +02:00
|
|
|
d = np.polyfit(df_start["ZapEnergy"],df_start[files],1)
|
2022-04-06 21:10:40 +02:00
|
|
|
function_bkgd = np.poly1d(d)
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
#making a list, y_pre,so the background will be applied to all ZapEnergy-values
|
2022-04-06 21:10:40 +02:00
|
|
|
y_bkgd=function_bkgd(df["ZapEnergy"])
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
#adding a new column in df_background with the y-values of the background
|
2022-04-06 21:10:40 +02:00
|
|
|
df_bkgd.insert(1,files,y_bkgd)
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
if options['troubleshoot'] == True:
|
|
|
|
|
### FOR FIGURING OUT WHERE IT GOES WRONG/WHICH FILE IS CORRUPT
|
|
|
|
|
ax = df.plot(x = "ZapEnergy",y=files)
|
|
|
|
|
#Plotting the calculated pre-edge background with the region used for the regression
|
2022-03-31 17:05:32 +02:00
|
|
|
if options['print'] == True:
|
|
|
|
|
#Plotting an example of the edge_start region and the fitted background that will later be subtracted
|
2022-04-07 11:34:44 +02:00
|
|
|
fig, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(15,5))
|
|
|
|
|
df.plot(x="ZapEnergy", y=filenames,color="Black",ax=ax1)
|
|
|
|
|
df_bkgd.plot(x="ZapEnergy", y=filenames,color="Red",ax=ax1)
|
2022-04-06 21:10:40 +02:00
|
|
|
plt.axvline(x = max(df_start["ZapEnergy"]))
|
|
|
|
|
#fig = plt.figure(figsize=(15,15))
|
2022-04-07 11:34:44 +02:00
|
|
|
df_bkgd.plot(x="ZapEnergy", y=filenames,color="Red",ax=ax2)
|
2022-04-06 21:10:40 +02:00
|
|
|
ax1.set_title('Data and fitted background')
|
2022-04-07 11:34:44 +02:00
|
|
|
#Zooming into bacground region to confirm fit and limits looks reasonable
|
|
|
|
|
df.plot(x = "ZapEnergy",y=filenames,ax=ax2) #defining x and y)
|
|
|
|
|
ax2.set_xlim([min(df_start["ZapEnergy"]),max(df_start["ZapEnergy"])+0.01])
|
|
|
|
|
#finding maximum and minimum values in the backgrounds
|
|
|
|
|
min_values=[]
|
|
|
|
|
max_values=[]
|
|
|
|
|
for file in filenames:
|
|
|
|
|
min_values.append(min(df_start[file]))
|
|
|
|
|
max_values.append(max(df_start[file]))
|
|
|
|
|
ax2.set_ylim([min(min_values),max(max_values)])
|
|
|
|
|
plt.axvline(x = max(df_start["ZapEnergy"]))
|
|
|
|
|
#ax2.set_xlim([25, 50])
|
2022-03-31 17:05:32 +02:00
|
|
|
###################### Subtracting the pre edge from xmap_roi00 ################
|
2022-04-07 11:34:44 +02:00
|
|
|
|
2022-03-31 17:05:32 +02:00
|
|
|
#making a new dataframe to insert the background subtracted intensities
|
2022-04-06 21:10:40 +02:00
|
|
|
df_bkgd_sub = pd.DataFrame(df["ZapEnergy"])
|
2022-04-07 11:34:44 +02:00
|
|
|
#inserting the background subtracted original xmap_roi00 data
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
for files in filenames:
|
2022-04-06 21:10:40 +02:00
|
|
|
newintensity_calc=df[files]-df_bkgd[files]
|
|
|
|
|
df_bkgd_sub.insert(1,files,newintensity_calc)
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
if options['print'] == True:
|
2022-04-07 11:34:44 +02:00
|
|
|
df.plot(x = "ZapEnergy",y=filenames, color="Black", ax=ax3, legend=False)
|
|
|
|
|
#plt.axvline(x = max(df_start["ZapEnergy"]))
|
|
|
|
|
df_bkgd_sub.plot(x="ZapEnergy", y=filenames,color="Red",ax=ax3, legend=False)
|
|
|
|
|
ax3.set_title('Data and background-subtracted data')
|
2022-04-06 21:10:40 +02:00
|
|
|
|
2022-04-07 11:34:44 +02:00
|
|
|
return df_bkgd_sub,filenames,edge
|
2022-03-31 17:05:32 +02:00
|
|
|
|
2022-04-07 11:34:44 +02:00
|
|
|
def post_edge_normalization(path, options={}):
|
2022-03-31 17:05:32 +02:00
|
|
|
|
2022-04-06 21:10:40 +02:00
|
|
|
required_options = ['print']
|
2022-03-31 17:05:32 +02:00
|
|
|
default_options = {
|
|
|
|
|
'print': False
|
|
|
|
|
}
|
|
|
|
|
options = aux.update_options(options=options, required_options=required_options, default_options=default_options)
|
2022-04-06 21:10:40 +02:00
|
|
|
|
2022-04-07 11:34:44 +02:00
|
|
|
df_bkgd_sub,filenames,edge = pre_edge_subtraction(path)
|
2022-03-31 17:05:32 +02:00
|
|
|
#Defining the end of the pre-edge-region for Mn/Ni, thus start of the edge
|
2022-04-06 21:10:40 +02:00
|
|
|
#Implement widget
|
|
|
|
|
if edge == 'Mn':
|
2022-03-31 17:05:32 +02:00
|
|
|
edge_stop = 6.565
|
2022-04-06 21:10:40 +02:00
|
|
|
if edge == 'Ni':
|
2022-03-31 17:05:32 +02:00
|
|
|
edge_stop = 8.361
|
|
|
|
|
|
2022-04-07 11:34:44 +02:00
|
|
|
#=============================================================
|
|
|
|
|
#absolute_difference_function = lambda list_value : abs(list_value - edge_stop)
|
|
|
|
|
#edge_stop_value = min(df_bkgd_sub["ZapEnergy"], key=absolute_difference_function)
|
|
|
|
|
#end_index=df_bkgd_sub[df_bkgd_sub["ZapEnergy"]==edge_stop_value].index.values[0]
|
2022-03-31 17:05:32 +02:00
|
|
|
#Defining x-range for linear fit
|
2022-04-07 11:34:44 +02:00
|
|
|
#df_fix=df_bkgd_sub
|
|
|
|
|
#df_fix.dropna(inplace=True)
|
|
|
|
|
#df_end=df_fix[end_index:] #The region of interest for the post edge
|
|
|
|
|
|
2022-03-31 17:05:32 +02:00
|
|
|
#Fitting linear function to the pre-edge using the background corrected intensities to make the post edge fit
|
2022-04-07 11:34:44 +02:00
|
|
|
#===============================================================
|
|
|
|
|
df_end= df_bkgd_sub.loc[df_bkgd_sub["ZapEnergy"] > edge_stop] # new dataframe only containing the post edge -> to be fitted
|
|
|
|
|
df_end.dropna(inplace=True) #Removing all indexes without any value, as some of the data sets misses the few last data points and fucks up the fit
|
|
|
|
|
df_postedge = pd.DataFrame(df_bkgd_sub["ZapEnergy"]) #making a new dataframe
|
2022-03-31 17:05:32 +02:00
|
|
|
|
|
|
|
|
function_post_list=[]
|
|
|
|
|
for files in filenames:
|
|
|
|
|
d = np.polyfit(df_end["ZapEnergy"],df_end[files],1)
|
|
|
|
|
function_post = np.poly1d(d)
|
2022-04-07 11:34:44 +02:00
|
|
|
y_post=function_post(df_bkgd_sub["ZapEnergy"])
|
2022-03-31 17:05:32 +02:00
|
|
|
function_post_list.append(function_post)
|
|
|
|
|
df_postedge.insert(1,files,y_post) #adding a new column with the y-values of the fitted post edge
|
|
|
|
|
|
|
|
|
|
#print(filenames[0])
|
|
|
|
|
#print(df_postedge)
|
|
|
|
|
#Plotting the background subtracted signal with the post-edge regression line and the start point for the linear regression line
|
|
|
|
|
if options['print'] == True:
|
2022-04-07 11:34:44 +02:00
|
|
|
ax = df_bkgd_sub.plot(x = "ZapEnergy",y=filenames) #defining x and y
|
|
|
|
|
plt.axvline(x = min(df_end["ZapEnergy"]))
|
2022-03-31 17:05:32 +02:00
|
|
|
fig = plt.figure(figsize=(15,15))
|
|
|
|
|
df_postedge.plot(x="ZapEnergy", y=filenames,color="Green",ax=ax, legend=False)
|
|
|
|
|
#print(function_post_list)
|
|
|
|
|
#print(function_post)
|
2022-04-07 11:34:44 +02:00
|
|
|
ax = df_bkgd_sub.plot(x = "ZapEnergy",y=filenames, legend=False) #defining x and y
|
2022-03-31 17:05:32 +02:00
|
|
|
df_postedge.plot(x="ZapEnergy", y=filenames,color="Green",ax=ax, legend=False)
|
2022-04-07 11:34:44 +02:00
|
|
|
plt.axvline(x = min(df_end["ZapEnergy"]))
|
2022-04-06 21:10:40 +02:00
|
|
|
|