added wilson plot + d to second x-axis + improved log file

This commit is contained in:
Beale John Henry
2025-01-13 17:09:37 +01:00
parent 152e11dfea
commit 175ca597fe

View File

@@ -38,6 +38,8 @@ from tqdm import tqdm
import regex as re
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import warnings
warnings.filterwarnings( "ignore", category=RuntimeWarning )
from loguru import logger
def submit_job( job_file, reservation ):
@@ -195,9 +197,15 @@ def get_metric( d2_series, cc_series, cut_off ):
# get curve for plotting
cc_tanh = tanh( d2_series, r, s0 )
return cc_stat, cc_tanh
return round( cc_stat, 3 ), cc_tanh
def summary_fig( stats_df, cc_tanh, ccstar_tanh ):
def summary_fig( stats_df, cc_tanh, ccstar_tanh, cc_cut, ccstar_cut ):
def dto1_d( x ):
return 1/x
def dto1_d2( x ):
return 1/x**2
# plot results
cc_fig, axs = plt.subplots(2, 2)
@@ -212,9 +220,11 @@ def summary_fig( stats_df, cc_tanh, ccstar_tanh ):
# plot cc
axs[0,0].plot( stats_df[ "1_d" ], stats_df.cc, color=color )
# plot fit
axs[0,0].plot( stats_df[ "1_d2" ], cc_tanh, color="tab:grey", linestyle = "dashed" )
axs[0,0].xticks( stats_df[ "1_d2" ].iloc[::5, :], stats_df[ "d" ].iloc[::5, :] )
axs[0,0].plot( stats_df[ "1_d" ], cc_tanh, color="tab:grey", linestyle = "dashed" )
sax1 = axs[0,0].secondary_xaxis( 'top', functions=( dto1_d, dto1_d ) )
sax1.set_xlabel('d (A)')
axs[0,0].tick_params( axis="y", labelcolor=color )
axs[0,0].text( 0.1, 0.1, "CC @ 0.2 = {0}".format( cc_cut ), fontsize = 8 )
# cc* plot
color = "tab:blue"
@@ -224,15 +234,19 @@ def summary_fig( stats_df, cc_tanh, ccstar_tanh ):
axs[0,1].axhline( y = 0.7, color="black", linestyle = "dashed" )
axs[0,1].plot( stats_df[ "1_d" ], stats_df.ccstar, color=color )
# plot fit
axs[0,0].plot( stats_df[ "1_d2" ], ccstar_tanh, color="tab:grey", linestyle = "dashed" )
axs[0,0].xticks( stats_df[ "1_d2" ].iloc[::5, :], stats_df[ "d" ].iloc[::5, :] )
axs[0,1].plot( stats_df[ "1_d" ], ccstar_tanh, color="tab:grey", linestyle = "dashed" )
sax2 = axs[0,1].secondary_xaxis( 'top', functions=( dto1_d, dto1_d ) )
sax2.set_xlabel('d (A)')
axs[0,1].tick_params( axis='y', labelcolor=color )
axs[0,1].text( 0.1, 0.1, "CC* @ 0.7 = {0}".format( ccstar_cut ) , fontsize = 8 )
# rsplit plot
color = "tab:green"
axs[1,0].set_xlabel( "1/d (1/A)" )
axs[1,0].set_ylabel( "Rsplit", color=color )
axs[1,0].plot( stats_df[ "1_d" ], stats_df.rsplit, color=color )
sax3 = axs[1,0].secondary_xaxis( 'top', functions=( dto1_d, dto1_d ) )
sax3.set_xlabel('d (A)')
axs[1,0].tick_params( axis='y', labelcolor=color )
@@ -241,37 +255,14 @@ def summary_fig( stats_df, cc_tanh, ccstar_tanh ):
axs[1,1].set_xlabel( "d (A)" )
axs[1,1].set_ylabel( "lnI", color=color )
axs[1,1].plot( stats_df[ "1_d2" ], stats_df.lnI, color=color )
# axs[1,1].invert_xaxis()
sax4 = axs[1,1].secondary_xaxis( 'top', functions=( dto1_d2, dto1_d2 ) )
sax4.set_xlabel('d (A)')
axs[1,1].tick_params( axis='y', labelcolor=color )
# save figure
plt.tight_layout()
plt.savefig( "plots.png" )
def get_mean_cell( stream ):
# get uc values from stream file
# example - Cell parameters 7.71784 7.78870 3.75250 nm, 90.19135 90.77553 90.19243 deg
# scrub clen and return - else nan
try:
pattern = r"Cell\sparameters\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\snm,\s(\d+\.\d+)\s(\d+\.\d+)\s(\d+\.\d+)\sdeg"
cell_lst = re.findall( pattern, stream )
xtals = len( cell_lst )
except AttributeError:
return np.nan
cols = [ "a", "b", "c", "alpha", "beta", "gamma" ]
cell_df = pd.DataFrame( cell_lst, columns=cols )
mean_a = round( cell_df.a.mean()*10, 3 )
mean_b = round( cell_df.b.mean()*10, 3 )
mean_c = round( cell_df.c.mean()*10, 3 )
mean_alpha = round( cell_df.alpha.mean(), 3 )
mean_beta = round( cell_df.beta.mean(), 3 )
mean_gamma = round( cell_df.gamma.mean(), 3 )
return mean_a, mean_b, mean_c, mean_alpha, mean_beta, mean_gamma
def main( cwd, name, stream, pointgroup, model, iterations, cell, shells, part_h_res, adu, reservation ):
# submitted job set
@@ -307,22 +298,23 @@ def main( cwd, name, stream, pointgroup, model, iterations, cell, shells, part_h
# make summary data table
stats_df = summary_stats( cc_dat, ccstar_dat, mult_dat, rsplit_dat, wilson_dat )
logger.info( "stats table from .dat file =\n{0}".format( stats_df ) )
logger.info( "stats table from .dat file =\n{0}".format( stats_df.to_string() ) )
print_df = stats_df[ [ "1_d", "d", "min",
"max", "nref", "poss",
"comp", "obs", "mult",
"snr", "I", "rsplit", "cc", "ccstar" ] ]
print_df.to_csv( "summary_table.csv", sep="\t", index=False )
# calculate cc metrics
cc_cut, cc_tanh = get_metric( stats_df[ "1_d2" ], stats_df.cc, 0.3 )
ccstar_cut, ccstar_tanh = get_metric( stats_df[ "1_d2" ], stats_df.ccstar, 0.7 )
print( "resolution at CC0.5 at 0.3 = {0}".format( cc_cut ) )
print( "resolution at CC* at 0.7 = {0}".format( ccstar_cut ) )
logger.info( "resolution at CC0.5 at 0.3 = {0}".format( cc_cut ) )
logger.info( "resolution at CC* at 0.7 = {0}".format( ccstar_cut ) )
# show plots
summary_fig( stats_df, cc_tanh, ccstar_tanh )
summary_fig( stats_df, cc_tanh, ccstar_tanh, cc_cut, ccstar_cut )
# move back to top dir
os.chdir( cwd )