File size: 4,998 Bytes
e9e349d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/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)