-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_plot_representations.py
169 lines (153 loc) · 5.25 KB
/
make_plot_representations.py
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
from pathlib import Path
from typing import Tuple
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from umap import UMAP
from data import load_dataset
from util.mlflow.constants import (
ESM,
ESM1V,
ESM2,
EVE,
ONE_HOT,
PROTT5,
PSSM,
TRANSFORMER,
)
PROJECT_DIR = Path(__file__).parent.resolve()
def plot_reduced_representations(
dataset: str, representation: str, augmentation: str = None, project_dir=PROJECT_DIR
) -> None:
X, Y = load_dataset(
dataset, representation=representation, augmentation=augmentation
)
reducer = UMAP(transform_seed=42)
emb = reducer.fit_transform(X)
plt.scatter(
emb[:, 0], emb[:, 1], c=Y, cmap="magma", s=30, alpha=0.75, edgecolors="black"
)
plt.title(f"2D UMAP {dataset} {representation}")
output_path = (
PROJECT_DIR
/ "results"
/ "figures"
/ "representations"
/ f"{dataset}_{representation}_UMAP.png"
)
plt.savefig(output_path)
plt.savefig(output_path.with_suffix(".pdf"))
plt.show()
def plot_reduced_representations_all_datasets(
datasets: Tuple[str, ...], representations: Tuple[str, ...], augmentation=None
):
# Dictionary to map representation to name
name_dict = {
ONE_HOT: "One-Hot",
PSSM: "PSSM",
EVE: "EVE",
TRANSFORMER: "ProtBert",
ESM: "ESM-1b",
PROTT5: "ProtT5",
ESM1V: "ESM-1v",
ESM2: "ESM-2",
}
dataset_key_map = {
"1FQG": f"{chr(946)}-lactamase",
"UBQT": "Ubiquitin",
"TIMB": "TIM-Barrel",
"MTH3": "T2-MTH",
"BRCA": "BRCA1",
}
font_kwargs = {"family": "Arial", "fontsize": 30, "weight": "bold"}
font_kwargs_small = {"family": "Arial", "fontsize": 20}
# Exact figure size might need tweaking
fig, ax = plt.subplots(len(representations), len(datasets), figsize=(20, 10))
for i, dataset in enumerate(datasets):
umap_path = Path(
"results",
"cache",
f"{dataset}_representations_{'_'.join(representations)}.csv",
)
# Load data / generate + save UMAP embeddings
if not umap_path.exists():
df = pd.DataFrame(columns=["representation", "x", "y", "target"])
for representation in representations:
X, Y = load_dataset(
dataset, representation=representation, augmentation=augmentation
)
reducer = UMAP(transform_seed=42)
emb = reducer.fit_transform(X)
df_rep = pd.DataFrame(
{
"representation": representation,
"x": emb[:, 0],
"y": emb[:, 1],
"target": Y.squeeze(),
}
)
df = pd.concat((df, df_rep), axis=0)
df.to_csv(umap_path)
else:
df = pd.read_csv(umap_path, index_col=0)
df["median"] = (df["target"] > df["target"].median()).astype(int)
# Plot in 2x2 blocks
for j, representation in enumerate(representations):
ax_ij = ax[j, i]
df_sub = df[df["representation"] == representation]
sns.scatterplot(
data=df_sub,
x="x",
y="y",
hue="median",
ax=ax_ij,
s=50,
alpha=0.75,
edgecolor="none",
palette=["#636EFA", "#FFA15A"],
legend=False,
)
# Clean up axes
ax_ij.tick_params(
bottom=False,
labelbottom=False,
labeltop=False,
labelleft=False,
labelright=False,
left=False,
)
if i == 0:
ax_ij.set_ylabel(name_dict[representation], **font_kwargs)
ax_ij.yaxis.set_label_coords(-0.275, 0.5, transform=ax_ij.transAxes)
ax_ij.text(
-0.2, 0.5, r"$d_2$", transform=ax_ij.transAxes, **font_kwargs_small
)
else:
ax_ij.yaxis.label.set_visible(False)
if j == 0:
ax_ij.set_title(dataset_key_map[dataset], **font_kwargs)
ax_ij.xaxis.label.set_visible(False)
elif j == len(representations) - 1:
ax_ij.set_xlabel(r"$d_1$", **font_kwargs_small)
else:
ax_ij.xaxis.label.set_visible(False)
ax_ij.spines[["right", "top"]].set_visible(False)
plt.tight_layout()
plt.subplots_adjust(wspace=0.15, left=0.075, right=0.975)
plt.savefig(
f"./results/figures/representations/all_datasets_UMAP_{'_'.join(representations)}.pdf"
)
plt.savefig(
f"./results/figures/representations/all_datasets_UMAP_{'_'.join(representations)}.png"
)
plt.show()
if __name__ == "__main__":
datasets = ["1FQG", "UBQT", "TIMB", "MTH3", "BRCA"]
representations = [ONE_HOT, EVE, PROTT5, ESM]
plot_reduced_representations_all_datasets(
datasets, representations, augmentation=None
)
## SI ablation:
plot_reduced_representations_all_datasets(
datasets, [PSSM, ESM1V, ESM2], augmentation=None
)