Skip to content

Commit

Permalink
Merge pull request #177 from pycroscopy/use_dict3
Browse files Browse the repository at this point in the history
update
  • Loading branch information
gduscher authored Nov 19, 2024
2 parents ed90ac6 + 3ad8e16 commit b4aefb9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
38 changes: 32 additions & 6 deletions pyTEMlib/eels_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def fit_plasmon(dataset: Union[sidpy.Dataset, np.ndarray], startFitEnergy: float
"""
# define Drude function for plasmon fitting

anglog, T = angle_correction(dataset)
anglog, T, _ = angle_correction(dataset)
def energy_loss_function(E: np.ndarray, Ep: float, Ew: float, A: float) -> np.ndarray:

eps = 1 - Ep**2/(E**2+Ew**2) + 1j * Ew * Ep**2/E/(E**2+Ew**2)
Expand Down Expand Up @@ -626,12 +626,37 @@ def angle_correction(spectrum):

acceleration_voltage = spectrum.metadata['experiment']['acceleration_voltage']
energy_scale = spectrum.get_spectral_dims(return_axis=True)[0].values
eff_beta = effective_collection_angle(energy_scale, spectrum.metadata['experiment']['convergence_angle'],
spectrum.metadata['experiment']['collection_angle'],acceleration_voltage)
# eff_beta = effective_collection_angle(energy_scale, spectrum.metadata['experiment']['convergence_angle'],
# spectrum.metadata['experiment']['collection_angle'],acceleration_voltage)


epc = energy_scale[1] - energy_scale[0] # input('ev per channel : ');


alpha = spectrum.metadata['experiment']['convergence_angle'] # input('Alpha (mrad) : ');
beta = spectrum.metadata['experiment']['collection_angle']# input('Beta (mrad) : ');
e = energy_scale
e0 = acceleration_voltage/1000 # input('E0 (keV) : ');

T = 1000.0*e0*(1.+e0/1022.12)/(1.0+e0/511.06)**2 # %eV # equ.5.2a or Appendix E p 427

tgt=e0*(1.+e0/1022.)/(1+e0/511.);
thetae=(e+1e-6)/tgt; # % avoid NaN for e=0
# % A2,B2,T2 ARE SQUARES OF ANGLES IN RADIANS**2
a2=alpha*alpha*1e-6 + 1e-7; # % avoid inf for alpha=0
b2=beta*beta*1e-6;
t2=thetae*thetae*1e-6;
eta1=np.sqrt((a2+b2+t2)**2-4*a2*b2)-a2-b2-t2;
eta2=2.*b2*np.log(0.5/t2*(np.sqrt((a2+t2-b2)**2+4.*b2*t2)+a2+t2-b2));
eta3=2.*a2*np.log(0.5/t2*(np.sqrt((b2+t2-a2)**2+4.*a2*t2)+b2+t2-a2));
eta=(eta1+eta2+eta3)/a2/np.log(4./t2);
f1=(eta1+eta2+eta3)/2./a2/np.log(1.+b2/t2);
f2=f1;
if(alpha/beta>1):
f2=f1*a2/b2;

bstar=thetae*np.sqrt(np.exp(f2*np.log(1.+b2/t2))-1.);
anglog = f2
"""
b = eff_beta/1000.0 # %rad
e0 = acceleration_voltage/1000.0 # %keV
T = 1000.0*e0*(1.+e0/1022.12)/(1.0+e0/511.06)**2 # %eV # equ.5.2a or Appendix E p 427
Expand All @@ -640,7 +665,8 @@ def angle_correction(spectrum):
the = energy_scale/tgt # varies with energy loss! # Appendix E p 427
anglog = np.log(1.0+ b*b/the/the)
# 2 * T = m_0 v**2 !!! a_0 = 0.05292 nm epc is for sum over I0
return anglog, (np.pi*0.05292* T / 2.0)/epc
"""
return anglog, (np.pi*0.05292* T / 2.0)/epc, bstar[0],

def energy_loss_function(energy_scale: np.ndarray, p: np.ndarray, anglog=1) -> np.ndarray:
eps = 1 - p[0]**2/(energy_scale**2+p[1]**2) + 1j * p[1] * p[0]**2/energy_scale/(energy_scale**2+p[1]**2)
Expand Down Expand Up @@ -1622,7 +1648,7 @@ def residuals(pp, xx, yy):
for key in edges:
if key.isdigit():
edges[key]['areal_density'] = p[int(key)+5]
print(p)
# print(p)
edges['model'] = {}
edges['model']['background'] = ( p[0] * np.power(x, -p[1])+ p[2]+ x**p[3] + p[4] * x * x)
edges['model']['background-poly_0'] = p[2]
Expand Down
4 changes: 1 addition & 3 deletions pyTEMlib/info_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,6 @@ def __init__(self, datasets=None):
self.low_loss = LowLoss(self.low_loss_tab, self)
self.core_loss = CoreLoss(self.core_loss_tab, self)



self.set_action()

def set_action(self):
Expand Down Expand Up @@ -914,7 +912,7 @@ def update_sidebar(self):
self.info_tab[5, 0].value = np.round(self.parent.datasets[self.key].metadata['experiment']['convergence_angle'], 1)
self.info_tab[6, 0].value = np.round(self.parent.datasets[self.key].metadata['experiment']['collection_angle'], 1)
self.info_tab[7, 0].value = np.round(self.parent.datasets[self.key].metadata['experiment']['acceleration_voltage']/1000, 1)
print(self.parent.datasets[self.key].metadata['experiment']['acceleration_voltage'])
# print(self.parent.datasets[self.key].metadata['experiment']['acceleration_voltage'])
self.info_tab[10, 0].value = np.round(self.parent.datasets[self.key].metadata['experiment']['exposure_time'], 4)
if 'flux_ppm' not in self.parent.datasets[self.key].metadata['experiment']:
self.parent.datasets[self.key].metadata['experiment']['flux_ppm'] = 0
Expand Down
18 changes: 12 additions & 6 deletions pyTEMlib/low_loss_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_low_loss_sidebar() -> Any:
layout=ipywidgets.Layout(width='200px'))
side_bar[row, 2] = ipywidgets.widgets.Label(value="eV", layout=ipywidgets.Layout(width='100px'))
row += 1
side_bar[row, :2] = ipywidgets.FloatText(value=25, description='End Fit:', disabled=False, color='black',
side_bar[row, :2] = ipywidgets.FloatText(value=-1, description='End Fit:', disabled=False, color='black',
layout=ipywidgets.Layout(width='200px'))
side_bar[row, 2] = ipywidgets.widgets.Label(value="eV", layout=ipywidgets.Layout(width='50px'))
row +=1
Expand Down Expand Up @@ -137,6 +137,7 @@ def update_ll_sidebar(self):
ll_index = index+1
self.low_loss_tab[0, 0].options = spectrum_list
self.low_loss_tab[0, 0].value = spectrum_list[ll_index]

self.update_ll_dataset()

def update_ll_dataset(self, value=0):
Expand All @@ -146,6 +147,9 @@ def update_ll_dataset(self, value=0):
return
self.parent.set_dataset(self.ll_key)
self.dataset = self.parent.dataset
if self.low_loss_tab[13, 0].value < 0:
energy_scale = self.dataset.get_spectral_dims(return_axis=True)[0]
self.low_loss_tab[13, 0].value = np.round(self.dataset.get_spectral_dims(return_axis=True)[0][-2], 3)


def get_resolution_function(self, value=0):
Expand Down Expand Up @@ -181,17 +185,19 @@ def get_drude(self, value=0):
self.low_loss_tab[8, 0].value = np.round(p[1],3)
self.low_loss_tab[9, 0].value = np.round(p[2],1)

_, dsdo = eels_tools.angle_correction(self.parent.spectrum)
_, dsdo, _ = eels_tools.angle_correction(self.parent.spectrum)


I0 = self.parent.datasets['resolution_function'].sum() + p[2]
# I0 = self.parent.spectrum.sum()
print(I0)
# print(I0)
# T = m_0 v**2 !!! a_0 = 0.05292 nm p[2] = S(E)/elf
t_nm = p[2]/I0*dsdo #Egerton equ 4.26% probability per eV
relative_thickness = self.low_loss_tab[14, 0].value

self.parent.status_message(f'Fitted plasmon peak: thickness :{t_nm:.1f} nm and IMFP: {t_nm/relative_thickness:.1f} nm')
imfp, _ = eels_tools.inelatic_mean_free_path(p[0], self.parent.spectrum)
t_nm = float(relative_thickness * imfp)
# print(t_nm, relative_thickness, imfp)
self.parent.status_message(f'Fitted plasmon peak: thickness :{t_nm:.1f} nm and IMFP: {t_nm/relative_thickness:.1f} nm in free electron approximation')

plasmon.metadata['plasmon']['thickness'] = t_nm
plasmon.metadata['plasmon']['relative_thickness'] = relative_thickness
Expand Down Expand Up @@ -240,7 +246,7 @@ def _update(self, ev=0):

self.parent._update(ev)
spectrum = self.parent.spectrum
anglog, _ = eels_tools.angle_correction(spectrum)
anglog, _, _ = eels_tools.angle_correction(spectrum)
resolution_function = None
if self .low_loss_tab[3, 0].value:
if 'resolution_function' in self.parent.datasets:
Expand Down

0 comments on commit b4aefb9

Please sign in to comment.