From afa89df143d648f59de3108828df45158643c825 Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Mon, 18 Mar 2024 13:44:11 +0100 Subject: [PATCH] Relocated scripts --- src/napp_plotlib.py | 54 +++++++++++++++++++++++++++++++++++++++++ src/utils_bge.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/napp_plotlib.py create mode 100644 src/utils_bge.py diff --git a/src/napp_plotlib.py b/src/napp_plotlib.py new file mode 100644 index 0000000..c22ca93 --- /dev/null +++ b/src/napp_plotlib.py @@ -0,0 +1,54 @@ +import pandas as pd +import numpy as np +import matplotlib.pyplot as plt + +def plot_image(dataframe,filter): + + for meas_idx in dataframe.loc[filter,:].index: + meas = dataframe.loc[meas_idx,:] # pandas Series + fig = plt.figure() + ax = plt.gca() + rows, cols = meas['image'].shape + scientaEkin_eV = meas['scientaEkin_eV'].flatten() + x_min, x_max = np.min(scientaEkin_eV), np.max(scientaEkin_eV) + y_min, y_max = 0, rows + ax.imshow(meas['image'],extent = [x_min,x_max,y_min,y_max]) + ax.set_xlabel('scientaEkin_eV') + ax.set_ylabel('Replicates') + ax.set_title(meas['name'][0] + '\n' + meas['sample'][0]+ '\n' + meas['lastModifiedDatestr'][0]) + +def plot_spectra(dataframe,filter): + + """ plot_spectra plots XPS spectra associated to 'dataframe' after row reduced by 'filter'. + When more than one row are specified by the 'filter' input, indivial spectrum are superimposed + on the same plot. + + Parameters: + dataframe (pandas.DataFrame): table with heterogenous entries obtained by read_hdf5_as_dataframe.py. + filter (binaray array): binary indexing array with same number of entries as rows in dataframe. + + """ + fig = plt.figure() + ax = plt.gca() + + for meas_idx in dataframe.loc[filter,:].index: + meas = dataframe.loc[meas_idx,:] # pandas Series + + rows, cols = meas['image'].shape + bindingEnergy_eV = meas['bindingEnergy_eV'].flatten() + spectrum_countsPerSecond = meas['spectrum_countsPerSecond'].flatten() + x_min, x_max = np.min(bindingEnergy_eV), np.max(bindingEnergy_eV) + y_min, y_max = 0, rows + #for i in range(cols): + #ax.plot(bindingEnergy_eV, spectrum_countsPerSecond,label = meas['name'][0]) + ax.plot(bindingEnergy_eV, spectrum_countsPerSecond,label = meas['name']) + + ax.set_xlabel('bindingEnergy_eV') + ax.set_ylabel('counts Per Second') + ax.set_title('\n'+meas['sample']+ '\n' + 'PE spectra') + #ax.set_title('\n'+meas['sample'][0]+ '\n' + 'PE spectra') + #ax.set_title(meas['name'][0] + '\n'+meas['sample'][0]+ '\n' + meas['lastModifiedDatestr'][0]) + ax.legend() + + + diff --git a/src/utils_bge.py b/src/utils_bge.py new file mode 100644 index 0000000..c350ebf --- /dev/null +++ b/src/utils_bge.py @@ -0,0 +1,58 @@ +import scipy.optimize as sp_opt +import pandas as pd + + + +def construct_mask(x, subinterval_list): + + """ constructs a mask of length len(x) that indicates whether the entries of x lie within the subintervals, + speficified in the subinterval_list. + + Parameters: + x (array_like): + subinterval_list (list of two-element tuples): + + Returns: + mask (Bool array_like): + + Usage: + + x = np.array([0.0 0.25 0.5 0.75 1.5 2.0 2.5 3.0 3.5 4.0]) + subinterval_list = [(0.25,0.75),(2.5,3.5)] + mask = contruct_mask(x,subinterval_list) + + """ + + mask = x < x.min() + for subinterval in subinterval_list: + mask = mask | ((x >= subinterval[0]) & (x <= subinterval[1])) + + return mask + + +def estimate_background(x,y,mask,method: str): + + """fits a background model based on the values of x and y indicated by a mask using a method, among available options. + + Parameters: + x,y (array_like, e.g., np.array, pd.Series): + mask (Bool array_like): + method (str): + + Returns: + y_bg (array_like): values of the fitted model at x, or similarly the obtained background estimate + + """ + + if method == 'linear': + def linear_model(x,m,b): + return (m*x) + b + + popt, pcov = sp_opt.curve_fit(linear_model,x[mask],y[mask]) + + y_bg = linear_model(x,*popt) + + else: + raise ValueError("Parameter 'method' can only be set as 'linear'. Future code releases may include more options. ") + + return y_bg