From 6d4789d2a8f4ec9a7950f1832f7b586cbf604cc0 Mon Sep 17 00:00:00 2001 From: Beale John Henry Date: Wed, 31 Jan 2024 10:28:12 +0100 Subject: [PATCH] labels to graph and removal of argparse --- clen_tools/distance-scan-analysis.py | 88 +++++++++++++++++----------- 1 file changed, 54 insertions(+), 34 deletions(-) diff --git a/clen_tools/distance-scan-analysis.py b/clen_tools/distance-scan-analysis.py index ddff249..4425e81 100644 --- a/clen_tools/distance-scan-analysis.py +++ b/clen_tools/distance-scan-analysis.py @@ -9,7 +9,7 @@ given a regular array of crystfel folders with different detector distances script will generate a graph analysing the detector distance as a function of the unit-cell constants # usage -python update-geom-from-lab6.py -f either coarse or fine +python update-geom-from-lab6.py # output creates plots of the unit cell axis against clen @@ -21,7 +21,9 @@ import regex as re import os import numpy as np import matplotlib.pyplot as plt -import argparse +import sys +from scipy.optimize import curve_fit +from scipy.signal import peak_widths, find_peaks def scrub_clen( stream_pwd ): @@ -84,7 +86,7 @@ def scrub_us( stream ): except AttributeError: return np.nan -def find_clen_values(stats_df): +def find_clen_values( stats_df ): def find_min_clen(col_name): min_val = stats_df[col_name].min() @@ -92,17 +94,45 @@ def find_clen_values(stats_df): min_clen = min_row['clen'].values[0] return min_val, min_clen + def gauss(x, *p): + + A, mu, sigma = p + return A * np.exp(-(x-mu)**2/(2.*sigma**2)) + + p0 = [ 30, 0.111, 0.01 ] + parameters, covariance = curve_fit( gauss, stats_df.clen, stats_df.indexed, p0=p0 ) + + # Get the fitted curve + stats_df[ "gaus" ] = gauss( stats_df.clen, *parameters) + # find peak centre + peaks = find_peaks( stats_df.gaus.values ) + + # find full peak width + fwhm = peak_widths( stats_df.gaus.values, peaks[0], rel_height=0.5 ) + + fwhm_str = int( round( fwhm[2][0], 0 ) ) + fwhm_end = int( round( fwhm[3][0], 0 ) ) + + # translate width into motor values + indexed_start = stats_df.iloc[ fwhm_str, 0 ] + indexed_end = stats_df.iloc[ fwhm_end, 0 ] + mid_gauss = stats_df.clen.iloc[ peaks[0] ].values[0] + + # cut df to only include indexed patterns + stats_df = stats_df[ ( stats_df.clen < indexed_end ) & ( stats_df.clen > indexed_start ) ] + + # calculate minimum values min_alpha_val, min_alpha_clen = find_min_clen('std_alpha') min_beta_val, min_beta_clen = find_min_clen('std_beta') min_gamma_val, min_gamma_clen = find_min_clen('std_gamma') min_c_val, min_c_clen = find_min_clen('std_c') + + # find possible clens + suggested_clen = (min_alpha_clen + min_beta_clen + min_gamma_clen )/3 + suggested_clen = round(suggested_clen, 4) - print("The value of clen for the minimum alpha value of {} is {}".format(min_alpha_val, min_alpha_clen)) - print("The value of clen for the minimum beta value of {} is {}".format(min_beta_val, min_beta_clen)) - print("The value of clen for the minimum gamma value of {} is {}".format(min_gamma_val, min_gamma_clen)) - print("The value of clen for the minimum c value of {} is {}".format(min_c_val, min_c_clen)) - -# return min_alpha_clen, min_beta_clen, min_gamma_clen, min_c_clen, min_alpha_val, min_beta_val, min_gamma_val, min_c_val + print( "middle of indexing gaussion fit of scan = {0}".format( mid_gauss ) ) + print( "mean minimum of alpha, beta, gamma of scan = {0}".format( suggested_clen ) ) def plot_indexed_std( stats_df, ax1, ax2 ): @@ -120,17 +150,16 @@ def plot_indexed_std( stats_df, ax1, ax2 ): ax2.tick_params(axis='y', labelcolor=color) # std_a plot - color = "lightsteelblue" - ax2.plot(stats_df.clen, stats_df.std_a, color=color) + color = "turquoise" + ax2.plot(stats_df.clen, stats_df.std_a, color=color, label="a" ) # std_b plot - color = "cornflowerblue" - ax2.plot(stats_df.clen, stats_df.std_b, color=color) + color = "deepskyblue" + ax2.plot(stats_df.clen, stats_df.std_b, color=color, label="b" ) # std_c plot color = "royalblue" - ax2.plot(stats_df.clen, stats_df.std_c, color=color) - + ax2.plot(stats_df.clen, stats_df.std_c, color=color, label="c" ) def plot_indexed_std_alpha_beta_gamma( stats_df, ax1, ax2 ): @@ -147,16 +176,16 @@ def plot_indexed_std_alpha_beta_gamma( stats_df, ax1, ax2 ): ax2.tick_params(axis='y', labelcolor=color) # std_alpha plot - color = "limegreen" - ax2.plot(stats_df.clen, stats_df.std_alpha, color=color) + color = "yellow" + ax2.plot(stats_df.clen, stats_df.std_alpha, color=color, label="alpha" ) # std_beta plot - color = "darkgreen" - ax2.plot(stats_df.clen, stats_df.std_beta, color=color) + color = "green" + ax2.plot(stats_df.clen, stats_df.std_beta, color=color, label="beta" ) # std_gamma plot - color = "green" - ax2.plot(stats_df.clen, stats_df.std_gamma, color=color) + color = "darkolivegreen" + ax2.plot(stats_df.clen, stats_df.std_gamma, color=color, label="gamma" ) def main( top_dir ): @@ -175,7 +204,6 @@ def main( top_dir ): stream_pwd, clen = row[ "stream_pwd" ], row[ "clen" ] # open stream file - print( "scrubbing stream for clen={0}".format( clen ) ) stream = open( stream_pwd, "r" ).read() # scrub unit cell information @@ -208,7 +236,7 @@ def main( top_dir ): stats_df_1 = pd.DataFrame( stats ) stats_df = pd.concat( ( stats_df, stats_df_1 ) ) - print( "done" ) + print( "done" ) # reset index stats_df = stats_df.reset_index( drop=True ) @@ -224,20 +252,12 @@ def main( top_dir ): plot_indexed_std(stats_df, ax1, ax2) plot_indexed_std_alpha_beta_gamma(stats_df, ax3, ax4) + fig.legend(loc="upper center") fig.tight_layout() plt.show() - if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument( - "-f", - "--scan_folder", - help="give the scan folder path used in the earlier part of the calculation", - type=str, - ) - args = parser.parse_args() - # run geom converter - main( args.scan_folder ) + stream_pwd = sys.argv[1] + main( stream_pwd )