epc_ml_data / 3_epc /epc_comparison_slides.py
Koulb's picture
Add files using upload-large-folder tool
e9e349d verified
#!/usr/bin/env python
"""EPC comparison plot for slides - APS style.
2 subplots:
1. AO (HPRO) vs ML (E3_AO)
2. DFT vs ML (E3_AO)
Each subplot colors points by transition type:
occ-occ, occ-cond (mixed), cond-cond
xlim/ylim start from 0 (plotting |g| magnitudes).
Output: pictures_ml/deep_h_epc.png
"""
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_PATH = '/home/apolyukhin/git/aps_slides/random_slides/pictures_ml/deep_h_epc.png'
N_OCC = 4
NK = 216
# =============================================================================
# APS slides style
# =============================================================================
marp_text_color = "#575279"
color_vv = "mediumseagreen" # occ-occ (pbe)
color_vc = "#b4637a" # occ-cond (hse)
color_cc = "#ea9d34" # cond-cond (kcw)
alpha = 0.4
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
out_dft_dir = os.path.join(DISP_DIR, 'out_dft')
out_hpro_dir = os.path.join(DISP_DIR, 'out_hpro_ao')
out_e3_dir = os.path.join(DISP_DIR, 'out_e3_ao')
print('Loading EPC data...')
g_dft = parse_epc_dir(out_dft_dir)
g_hpro = parse_epc_dir(out_hpro_dir)
g_e3 = parse_epc_dir(out_e3_dir)
print(f' DFT: {len(g_dft)} HPRO: {len(g_hpro)} E3: {len(g_e3)}')
# Use optical modes (nu >= 4) with non-negligible DFT value
optical_keys = [k for k in g_dft if k[3] >= 4 and abs(g_dft[k]) > 1e-4]
g_dft_arr = np.array([g_dft[k] for k in optical_keys]) * 1000 # meV
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
# Transition type masks — standard (for AO vs ML panel)
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_ao_ml = [
('occ-occ', is_vv, color_vv),
('occ-cond', is_vc, color_vc),
('cond-cond',is_cc, color_cc),
]
# Transition type masks — DFT vs ML: cond-cond restricted to 1st cond band only
N_OCC1 = N_OCC + 1
is_cc1 = np.array([k[1] == N_OCC1 and k[2] == N_OCC1 for k in optical_keys])
cats_dft_ml = [
('occ-occ', is_vv, color_vv),
('occ-cond', is_vc, color_vc),
('1st cond-cond', is_cc1, color_cc),
]
# =============================================================================
# Plot
# =============================================================================
def plot_panel(ax, g_x, g_y, label_x, label_y, cats):
x_abs = np.abs(g_x)
y_abs = np.abs(g_y)
lim = max(x_abs.max(), y_abs.max()) * 1.05
for cat_label, mask, color in cats:
if not mask.any():
continue
mae = np.mean(np.abs(g_y[mask] - g_x[mask]))
ax.scatter(x_abs[mask], y_abs[mask], s=4, alpha=alpha, color=color,
label=f'{cat_label} (MAE={mae:.1f} meV)', rasterized=True)
ax.plot([0, lim], [0, lim], '--', color=marp_text_color, lw=1.0, alpha=0.6)
ax.set_xlim(0, lim)
ax.set_ylim(0, lim)
ax.set_aspect('equal')
ax.set_xlabel(f'|g| {label_x} (meV)')
ax.set_ylabel(f'|g| {label_y} (meV)')
ax.legend(loc='upper left', framealpha=legend_alpha, fontsize=0.65*fontsize)
fig, axes = plt.subplots(1, 2, figsize=(14, 6), facecolor='none')
for ax in axes:
ax.set_facecolor('none')
plot_panel(axes[0], g_hpro_arr, g_e3_arr, 'AO', 'ML', cats_ao_ml)
plot_panel(axes[1], g_dft_arr, g_e3_arr, 'DFT', 'ML', cats_dft_ml)
plt.tight_layout()
plt.savefig(OUT_PATH, dpi=300, transparent=True, bbox_inches='tight')
print(f'Saved: {OUT_PATH}')