epc_ml_data / 3_epc /epc_distribution_slides.py
Koulb's picture
Add files using upload-large-folder tool
e9e349d verified
#!/usr/bin/env python
"""EPC relative error distribution for slides - APS style.
Two separate figures:
1. AO vs ML: deep_h_epc_distribution.png
2. DFT vs ML: deep_h_epc_distribution_dft_ml.png
x-axis: reduced index (sorted by |g_ref| descending)
y-axis: relative error |g_ml - g_ref| / |g_ref|
Points colored by transition type: occ-occ, occ-cond, cond-cond
"""
import os
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DISP_DIR = os.path.join(SCRIPT_DIR, 'displacements')
OUT_AO_ML = '/home/apolyukhin/git/aps_slides/random_slides/pictures_ml/deep_h_epc_distribution.png'
OUT_DFT_ML = '/home/apolyukhin/git/aps_slides/random_slides/pictures_ml/deep_h_epc_distribution_dft_ml.png'
N_OCC = 4
NK = 216
# =============================================================================
# APS slides style
# =============================================================================
marp_text_color = "#575279"
color_vv = "mediumseagreen"
color_vc = "#b4637a"
color_cc = "#ea9d34"
alpha = 0.3
legend_alpha = 0.5
fontsize = 22
plt.rcParams.update({
'font.size': fontsize,
'mathtext.fontset': 'cm',
'text.color': marp_text_color,
'axes.labelcolor': marp_text_color,
'xtick.color': marp_text_color,
'ytick.color': marp_text_color,
'axes.edgecolor': marp_text_color,
'axes.labelpad': 10,
})
# =============================================================================
# Data loading
# =============================================================================
def parse_epc_dir(dir_path, nk=NK):
g = {}
for ik in range(1, nk + 1):
fn = os.path.join(dir_path, f'comparison_{ik}_1.txt')
if not os.path.isfile(fn):
continue
with open(fn) as f:
for line in f:
cols = line.split()
if len(cols) < 8:
continue
i, j, nu = int(cols[0]), int(cols[1]), int(cols[2])
g[(ik, i, j, nu)] = float(cols[7])
return g
print('Loading EPC data...')
g_dft = parse_epc_dir(os.path.join(DISP_DIR, 'out_dft'))
g_hpro = parse_epc_dir(os.path.join(DISP_DIR, 'out_hpro_ao'))
g_e3 = parse_epc_dir(os.path.join(DISP_DIR, 'out_e3_ao'))
# Optical modes with non-negligible DFT value, up to first conduction band
N_OCC1 = N_OCC + 1
optical_keys = [k for k in g_dft if k[3] >= 4 and abs(g_dft[k]) > 1e-4
and k[1] <= N_OCC1 and k[2] <= N_OCC1]
g_dft_arr = np.array([g_dft[k] for k in optical_keys]) * 1000
g_hpro_arr = np.array([g_hpro.get(k, 0.0) for k in optical_keys]) * 1000
g_e3_arr = np.array([g_e3.get(k, 0.0) for k in optical_keys]) * 1000
is_vv = np.array([k[1] <= N_OCC and k[2] <= N_OCC for k in optical_keys])
is_cc = np.array([k[1] > N_OCC and k[2] > N_OCC for k in optical_keys])
is_vc = ~is_vv & ~is_cc
cats = [
('occ-occ', is_vv, color_vv),
('occ-cond', is_vc, color_vc),
('cond-cond', is_cc, color_cc),
]
# =============================================================================
# Plot
# =============================================================================
def make_plot(g_ref, g_ml, label_ref, label_ml, out_path,
clip_pct=99, rect_x_min=50.0, rect_face_alpha=0.15, rect_edge_lw=1.5):
abs_err = np.abs(g_ml - g_ref) / np.abs(g_ref) * 100
clip_val = np.percentile(abs_err, clip_pct)
x = np.abs(g_ref)
y = np.minimum(abs_err, clip_val)
fig, ax = plt.subplots(figsize=(10, 5.5), facecolor='none')
ax.set_facecolor('none')
ax.scatter(x, y, s=2, alpha=alpha, color=marp_text_color, rasterized=True)
# Colored rectangle: x >= rect_x_min, height = max error for those points
x_max = x.max() * 1.02
mask_sig = x >= rect_x_min
P = abs_err[mask_sig].max()
from matplotlib.patches import Rectangle
import matplotlib.colors as mcolors
rgb = mcolors.to_rgb(color_vv)
rect = Rectangle((rect_x_min, 0), x_max - rect_x_min, P,
linewidth=rect_edge_lw,
edgecolor=(*rgb, 1.0),
facecolor=(*rgb, rect_face_alpha),
zorder=0,
label=f'$\\forall\\,|g|>{rect_x_min:.0f}$ meV, $|\\delta g|<{P:.1f}\\%$')
ax.add_patch(rect)
ax.set_xlabel(f'$|g_{{\\rm {label_ref}}}|$ (meV)')
ax.set_ylabel(f'$|g_{{\\rm {label_ml}}}-g_{{\\rm {label_ref}}}|/|g_{{\\rm {label_ref}}}|$ (%)')
ax.set_xlim(0, x_max)
ax.set_ylim(0, clip_val * 1.05)
ax.legend(loc='upper right', framealpha=legend_alpha, fontsize=0.7*fontsize)
plt.tight_layout()
plt.savefig(out_path, dpi=300, transparent=True, bbox_inches='tight')
plt.close(fig)
print(f'Saved: {out_path}')
make_plot(g_hpro_arr, g_e3_arr, 'AO', 'ML', OUT_AO_ML)
make_plot(g_dft_arr, g_e3_arr, 'DFT', 'ML', OUT_DFT_ML,
rect_face_alpha=0.18, rect_edge_lw=2.5)