-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_v7.py
More file actions
220 lines (186 loc) · 9.41 KB
/
visualize_v7.py
File metadata and controls
220 lines (186 loc) · 9.41 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
visualize_v7.py — Final comparison visualization for Hatch v1–v7
4-panel figure:
1. Full performance progression (v1–v7): Mean F1, F1 Folded, F1 Disordered
2. CD threshold shifts (v7 vs midpoints): how much each feature moved
3. Feature importance: global AUC vs window AUC comparison
4. Confusion matrices: v5 (best sliding window) vs v7 (global)
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.gridspec import GridSpec
# ---------------------------------------------------------------------------
# Data
# ---------------------------------------------------------------------------
VERSIONS = ['v1', 'v2', 'v3', 'v4', 'v5', 'v7']
MEAN_F1 = [35.2, 46.9, 65.2, 70.0, 75.4, 89.4]
F1_FOLD = [34.9, 31.8, 64.3, 73.9, 77.0, 90.2]
F1_DIS = [35.5, 61.9, 66.2, 66.1, 73.8, 88.6]
FEATURE_NAMES_SHORT = [
"Hydrophobicity",
"Flexibility",
"H-Bond Potential",
"Net Charge",
"Shannon Entropy",
"Proline Freq",
"Bulky Hydrophobic",
]
MIDPOINTS = [0.4353, 0.8244, 1.1851, 0.0505, 3.7639, 0.0499, 0.2607]
CD_OPT = [0.4030, 0.8345, 1.1277, 0.0465, 3.8419, 0.0532, 0.2607]
SHIFTS = [cd - mp for cd, mp in zip(CD_OPT, MIDPOINTS)]
# Window AUC (from feature_importance_scan.py)
WINDOW_AUC = [0.764, 0.778, 0.598, 0.599, 0.785, 0.617, 0.838]
# v5 confusion matrix (from grid_search_v5 best result, 400-seq test set)
# Approximate from 75.4% mean F1, ~200 per class
CM_V5 = np.array([[148, 52], [46, 154]]) # [[TN, FP], [FN, TP]]
# v7 confusion matrix (from global_cd_optimizer.py, 5680-seq test set)
CM_V7 = np.array([[2341, 339], [433, 2567]])
# ---------------------------------------------------------------------------
# Plot
# ---------------------------------------------------------------------------
fig = plt.figure(figsize=(16, 12))
fig.patch.set_facecolor('#0d1117')
gs = GridSpec(2, 2, figure=fig, hspace=0.38, wspace=0.32)
DARK_BG = '#0d1117'
CARD_BG = '#161b22'
GRID_COL = '#21262d'
TEXT_COL = '#e6edf3'
MUTED = '#8b949e'
C_MEAN = '#58a6ff'
C_FOLD = '#3fb950'
C_DIS = '#f78166'
C_V7 = '#d2a8ff'
def style_ax(ax, title):
ax.set_facecolor(CARD_BG)
for spine in ax.spines.values():
spine.set_color(GRID_COL)
ax.tick_params(colors=MUTED, labelsize=9)
ax.xaxis.label.set_color(MUTED)
ax.yaxis.label.set_color(MUTED)
ax.set_title(title, color=TEXT_COL, fontsize=11, fontweight='bold', pad=10)
ax.grid(axis='y', color=GRID_COL, linewidth=0.5, alpha=0.7)
# ── Panel 1: Full progression ──────────────────────────────────────────────
ax1 = fig.add_subplot(gs[0, 0])
style_ax(ax1, "Performance Progression: v1 → v7")
x = np.arange(len(VERSIONS))
w = 0.26
ax1.bar(x - w, MEAN_F1, width=w, color=C_MEAN, alpha=0.85, label='Mean F1')
ax1.bar(x, F1_FOLD, width=w, color=C_FOLD, alpha=0.85, label='F1 Folded')
ax1.bar(x + w, F1_DIS, width=w, color=C_DIS, alpha=0.85, label='F1 Disordered')
ax1.set_xticks(x)
ax1.set_xticklabels(VERSIONS, color=TEXT_COL)
ax1.set_ylim(0, 100)
ax1.set_ylabel("F1 Score (%)", color=MUTED)
# Annotate v7 bars
for i, (mf, ff, df) in enumerate(zip(MEAN_F1, F1_FOLD, F1_DIS)):
if VERSIONS[i] == 'v7':
for val, offset in [(mf, -w), (ff, 0), (df, w)]:
ax1.text(i + offset, val + 1.5, f'{val:.1f}', ha='center',
va='bottom', fontsize=7.5, color=TEXT_COL, fontweight='bold')
# Annotate v5 mean F1
ax1.text(4 - w, MEAN_F1[4] + 1.5, f'{MEAN_F1[4]:.1f}', ha='center',
va='bottom', fontsize=7.5, color=C_MEAN)
# Architecture change arrow
ax1.annotate('', xy=(4.6, 88), xytext=(4.1, 78),
arrowprops=dict(arrowstyle='->', color=C_V7, lw=1.5))
ax1.text(4.65, 87, 'Global\nArchitecture', color=C_V7, fontsize=7.5, va='top')
ax1.legend(loc='upper left', framealpha=0.3, facecolor=CARD_BG,
labelcolor=TEXT_COL, fontsize=8)
# ── Panel 2: CD threshold shifts ──────────────────────────────────────────
ax2 = fig.add_subplot(gs[0, 1])
style_ax(ax2, "Coordinate Descent Threshold Shifts (v7)")
colors = [C_DIS if s < 0 else C_FOLD if s > 0 else MUTED for s in SHIFTS]
y_pos = np.arange(len(FEATURE_NAMES_SHORT))
bars = ax2.barh(y_pos, SHIFTS, color=colors, alpha=0.85, height=0.6)
ax2.set_yticks(y_pos)
ax2.set_yticklabels(FEATURE_NAMES_SHORT, color=TEXT_COL, fontsize=9)
ax2.axvline(0, color=MUTED, linewidth=0.8, linestyle='--')
ax2.set_xlabel("Threshold Shift (CD − Midpoint)", color=MUTED)
ax2.grid(axis='x', color=GRID_COL, linewidth=0.5, alpha=0.7)
ax2.grid(axis='y', visible=False)
for i, (s, feat) in enumerate(zip(SHIFTS, FEATURE_NAMES_SHORT)):
if abs(s) > 0.001:
ax2.text(s + (0.002 if s > 0 else -0.002), i,
f'{s:+.4f}', va='center',
ha='left' if s > 0 else 'right',
fontsize=8, color=TEXT_COL)
up_patch = mpatches.Patch(color=C_FOLD, alpha=0.85, label='Threshold ↑ (tighter for folded)')
down_patch = mpatches.Patch(color=C_DIS, alpha=0.85, label='Threshold ↓ (tighter for folded)')
ax2.legend(handles=[up_patch, down_patch], loc='lower right',
framealpha=0.3, facecolor=CARD_BG, labelcolor=TEXT_COL, fontsize=8)
# ── Panel 3: Window AUC (feature importance) ──────────────────────────────
ax3 = fig.add_subplot(gs[1, 0])
style_ax(ax3, "Feature Discriminative Power (Window AUC, W=30)")
y_pos = np.arange(len(FEATURE_NAMES_SHORT))
auc_colors = [C_FOLD if a >= 0.76 else C_DIS for a in WINDOW_AUC]
ax3.barh(y_pos, WINDOW_AUC, color=auc_colors, alpha=0.85, height=0.6)
ax3.set_yticks(y_pos)
ax3.set_yticklabels(FEATURE_NAMES_SHORT, color=TEXT_COL, fontsize=9)
ax3.set_xlim(0.5, 0.95)
ax3.axvline(0.76, color=MUTED, linewidth=0.8, linestyle='--', alpha=0.6)
ax3.text(0.762, 6.6, 'Tier 1\nboundary', color=MUTED, fontsize=7.5)
ax3.set_xlabel("AUC (794,870 training windows)", color=MUTED)
ax3.grid(axis='x', color=GRID_COL, linewidth=0.5, alpha=0.7)
ax3.grid(axis='y', visible=False)
for i, a in enumerate(WINDOW_AUC):
ax3.text(a + 0.003, i, f'{a:.3f}', va='center', fontsize=8.5, color=TEXT_COL)
t1_patch = mpatches.Patch(color=C_FOLD, alpha=0.85, label='Tier 1 (AUC ≥ 0.76)')
t2_patch = mpatches.Patch(color=C_DIS, alpha=0.85, label='Tier 2 (AUC < 0.76)')
ax3.legend(handles=[t1_patch, t2_patch], loc='lower right',
framealpha=0.3, facecolor=CARD_BG, labelcolor=TEXT_COL, fontsize=8)
# ── Panel 4: Confusion matrices ────────────────────────────────────────────
ax4 = fig.add_subplot(gs[1, 1])
ax4.set_facecolor(CARD_BG)
for spine in ax4.spines.values():
spine.set_color(GRID_COL)
ax4.set_title("Confusion Matrices: v5 (Sliding Window) vs v7 (Global)",
color=TEXT_COL, fontsize=11, fontweight='bold', pad=10)
ax4.axis('off')
def draw_cm(ax, cm, title, x_offset, y_offset, scale=1.0):
total = cm.sum()
labels = [['TN', 'FP'], ['FN', 'TP']]
colors_cm = [['#1f6feb', '#da3633'], ['#da3633', '#238636']]
cell_size = 0.18 * scale
for r in range(2):
for c in range(2):
val = cm[r, c]
pct = val / total * 100
x = x_offset + c * cell_size
y = y_offset - r * cell_size
rect = plt.Rectangle((x, y - cell_size), cell_size, cell_size,
facecolor=colors_cm[r][c], alpha=0.4,
transform=ax.transAxes)
ax.add_patch(rect)
ax.text(x + cell_size/2, y - cell_size/2,
f'{labels[r][c]}\n{val:,}\n({pct:.1f}%)',
ha='center', va='center', fontsize=7.5 * scale,
color=TEXT_COL, fontweight='bold',
transform=ax.transAxes)
ax.text(x_offset + cell_size, y_offset + 0.04,
title, ha='center', va='bottom', fontsize=9 * scale,
color=TEXT_COL, fontweight='bold', transform=ax.transAxes)
# Axis labels
for c, lbl in enumerate(['Pred\nDisordered', 'Pred\nFolded']):
ax.text(x_offset + c * cell_size + cell_size/2, y_offset - 2*cell_size - 0.02,
lbl, ha='center', va='top', fontsize=7 * scale, color=MUTED,
transform=ax.transAxes)
for r, lbl in enumerate(['Actual\nDisordered', 'Actual\nFolded']):
ax.text(x_offset - 0.03, y_offset - r * cell_size - cell_size/2,
lbl, ha='right', va='center', fontsize=7 * scale, color=MUTED,
transform=ax.transAxes)
draw_cm(ax4, CM_V5, f'v5 (Sliding Window)\n75.4% Mean F1', 0.08, 0.82, scale=0.9)
draw_cm(ax4, CM_V7, f'v7 (Global Classifier)\n89.4% Mean F1', 0.57, 0.82, scale=0.9)
# ── Title & footer ─────────────────────────────────────────────────────────
fig.suptitle(
"Hatch: Biophysical Protein Disorder Classifier · v1 → v7 Experiment Summary",
color=TEXT_COL, fontsize=14, fontweight='bold', y=0.98
)
fig.text(0.5, 0.01,
"v7: Global Feature Classifier + Coordinate Descent | "
"89.47% Accuracy | 89.41% Mean F1 | 5,680-sequence test set",
ha='center', color=MUTED, fontsize=9)
plt.savefig("hatch_v7_final.png", dpi=150, bbox_inches='tight',
facecolor=DARK_BG, edgecolor='none')
print("Saved: hatch_v7_final.png")
plt.close()