-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_umap_models_grid.py
More file actions
272 lines (224 loc) · 10.6 KB
/
plot_umap_models_grid.py
File metadata and controls
272 lines (224 loc) · 10.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import mowl
mowl.init_jvm("10g")
from mowl.projection import Edge
from mowl.utils.random import seed_everything
from pykeen.models import TransE, TransH, TransD
import pandas as pd
import torch as th
import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import umap
import numpy as np
from data import create_train_val_split
import click as ck
@ck.command()
@ck.option("--edges_file", type=str, default="data/upheno_owl2vecstar_edges.tsv", help="Path to edges file")
@ck.option("--n_neighbors", type=int, default=15, help="Number of neighbors for UMAP")
@ck.option("--max_samples", type=int, default=5000, help="Maximum number of samples per phenotype type")
def main(edges_file, n_neighbors, max_samples):
"""
Generate a 10x3 grid of UMAP plots: 10 folds × 3 models (TransE, TransH, TransD).
Shows MP Phenotypes, HP Phenotypes, Genes, and Diseases.
"""
random_seed = 0
seed_everything(random_seed)
models = ["transe", "transh", "transd"]
model_classes = {"transe": TransE, "transh": TransH, "transd": TransD}
model_display_names = {"transe": "TransE", "transh": "TransH", "transd": "TransD"}
folds = [str(i) for i in range(10)]
transparent = 0.5
# Colorblind-friendly palette (Wong palette, softer tones)
colors = {
'mp': '#56B4E9', # Sky blue
'hp': '#E69F00', # Orange
'genes': '#009E73', # Bluish green
'diseases': '#CC79A7', # Reddish purple
'other': '#999999' # Gray
}
# Create figure with subplots (10 folds x 3 models)
fig, axes = plt.subplots(10, 3, figsize=(15, 50))
for fold_idx, fold in enumerate(folds):
print(f"\n{'='*60}")
print(f"Processing fold {fold}...")
print(f"{'='*60}")
# Load data for this fold
train_disease_genes = pd.read_csv(f"data/gene_disease_folds/fold_{fold}/train.csv")
train_disease_genes, val_disease_genes = create_train_val_split(train_disease_genes, val_ratio=0.1, random_seed=0)
test_disease_genes = pd.read_csv(f"data/gene_disease_folds/fold_{fold}/test.csv")
test_diseases = set(test_disease_genes['Disease'].values)
gene_phenotypes = pd.read_csv("data/gene_phenotypes.csv")
disease_phenotypes = pd.read_csv("data/disease_phenotypes.csv")
test_disease_phenotypes = set()
for _, row in disease_phenotypes.iterrows():
if row['Disease'] in test_diseases:
test_disease_phenotypes.add(row['Phenotype'])
# Build graph4 triples and entities
triples = []
entities = set()
relations = set()
with open(edges_file, "r") as f:
for line in f:
src, rel, dst = line.strip().split("\t")
triples.append((src, rel, dst))
entities.add(src)
entities.add(dst)
relations.add(rel)
# Add gene-phenotype edges (graph2)
for _, row in gene_phenotypes.iterrows():
gene = row['Gene']
phenotype = row['Phenotype']
assert phenotype in entities, f"Phenotype {phenotype} not in entities"
triples.append((gene, 'has_phenotype', phenotype))
entities.add(gene)
# Add disease-phenotype edges (graph3)
for _, row in disease_phenotypes.iterrows():
disease = row['Disease']
phenotype = row['Phenotype']
assert phenotype in entities, f"Phenotype {phenotype} not in entities"
triples.append((disease, 'has_symptom', phenotype))
entities.add(disease)
# Add gene-disease edges (graph4)
for _, row in train_disease_genes.iterrows():
disease = row['Disease']
gene = row['Gene']
triples.append((gene, 'associated_with', disease))
assert gene in entities, f"Gene {gene} not in entities"
assert disease in entities, f"Disease {disease} not in entities"
entities = sorted(list(entities))
relations = sorted(list(relations))
triples = sorted(triples)
# Create triples factory
mowl_triples = [Edge(src, rel, dst) for src, rel, dst in triples]
triples_factory = Edge.as_pykeen(mowl_triples)
entity_to_id = triples_factory.entity_to_id
# Classify entities
mp_entities = []
hp_entities = []
gene_entities = []
disease_entities = []
other_entities = []
for entity in entities:
if "MP_" in entity:
mp_entities.append(entity)
elif "HP_" in entity:
if entity in test_disease_phenotypes:
hp_entities.append(entity)
else:
other_entities.append(entity)
elif "MGI_" in entity:
gene_entities.append(entity)
elif "OMIM_" in entity:
disease_entities.append(entity)
else:
other_entities.append(entity)
# Sample if needed
if len(mp_entities) > max_samples:
np.random.seed(random_seed)
mp_entities = list(np.random.choice(mp_entities, max_samples, replace=False))
if len(hp_entities) > max_samples:
np.random.seed(random_seed + 1)
hp_entities = list(np.random.choice(hp_entities, max_samples, replace=False))
if len(gene_entities) > max_samples:
np.random.seed(random_seed + 2)
gene_entities = list(np.random.choice(gene_entities, max_samples, replace=False))
if len(disease_entities) > max_samples:
np.random.seed(random_seed + 3)
disease_entities = list(np.random.choice(disease_entities, max_samples, replace=False))
if len(other_entities) > max_samples:
np.random.seed(random_seed + 4)
other_entities = list(np.random.choice(other_entities, max_samples, replace=False))
print(f"MP phenotypes: {len(mp_entities)}")
print(f"HP phenotypes: {len(hp_entities)}")
print(f"Genes: {len(gene_entities)}")
print(f"Diseases: {len(disease_entities)}")
print(f"Other entities: {len(other_entities)}")
mp_count = len(mp_entities)
hp_count = len(hp_entities)
gene_count = len(gene_entities)
disease_count = len(disease_entities)
other_count = len(other_entities)
all_entities = mp_entities + hp_entities + gene_entities + disease_entities + other_entities
all_ids = th.tensor([entity_to_id[entity] for entity in all_entities])
total_count = len(all_entities)
for model_idx, model_name in enumerate(models):
print(f"Processing {model_display_names[model_name]}...")
ax = axes[fold_idx, model_idx]
if model_name == "transe":
dim = 100
model_path = f"data/models/transe_transductive_fold_{fold}_seed_0_dim_100_bs_2048_lr_0.001_norm_2_graph4.pt"
elif model_name == "transh":
dim = 200
model_path = f"data/models/transh_transductive_fold_{fold}_seed_0_dim_200_bs_1024_lr_0.001_graph4.pt"
elif model_name == "transd":
dim = 100
model_path = f"data/models/transd_transductive_fold_{fold}_seed_0_dim_100_bs_2048_lr_0.001_graph4.pt"
model_class = model_classes[model_name]
if model_name == "transd":
model = model_class(
triples_factory=triples_factory,
embedding_dim=dim,
relation_dim=dim,
random_seed=random_seed,
)
else:
model = model_class(
triples_factory=triples_factory,
embedding_dim=dim,
random_seed=random_seed,
)
print(f"Loading model from {model_path}...")
model.load_state_dict(th.load(model_path, weights_only=True, map_location=th.device('cpu')))
model.eval()
# Get embeddings
with th.no_grad():
embeddings = model.entity_representations[0](indices=all_ids).cpu().numpy()
# Apply UMAP
print(f"Applying UMAP...")
reducer = umap.UMAP(n_components=2, n_neighbors=min(n_neighbors, total_count - 1))
embeddings_2d = reducer.fit_transform(embeddings)
# Plot MP phenotypes
offset = 0
ax.scatter(embeddings_2d[offset:offset+mp_count, 0], embeddings_2d[offset:offset+mp_count, 1],
c=colors['mp'], alpha=transparent, s=3)
# Plot HP phenotypes
offset += mp_count
ax.scatter(embeddings_2d[offset:offset+hp_count, 0], embeddings_2d[offset:offset+hp_count, 1],
c=colors['hp'], alpha=transparent, s=3)
# Plot Genes
if gene_count > 0:
offset += hp_count
ax.scatter(embeddings_2d[offset:offset+gene_count, 0], embeddings_2d[offset:offset+gene_count, 1],
c=colors['genes'], alpha=transparent, s=3)
# Plot Diseases
if disease_count > 0:
offset += gene_count
ax.scatter(embeddings_2d[offset:offset+disease_count, 0], embeddings_2d[offset:offset+disease_count, 1],
c=colors['diseases'], alpha=transparent, s=3)
# Plot Other entities
if other_count > 0:
offset += disease_count
ax.scatter(embeddings_2d[offset:offset+other_count, 0], embeddings_2d[offset:offset+other_count, 1],
c=colors['other'], alpha=0.1, s=2)
ax.set_title(f'Fold {fold} - {model_display_names[model_name]}', fontsize=10)
ax.grid(True, alpha=0.3)
ax.set_xticks([])
ax.set_yticks([])
# Create legend
legend_elements = [
Patch(facecolor=colors['mp'], alpha=1, label='MP Phenotypes'),
Patch(facecolor=colors['hp'], alpha=1, label='HP Phenotypes'),
Patch(facecolor=colors['genes'], alpha=1, label='Genes'),
Patch(facecolor=colors['diseases'], alpha=1, label='Diseases'),
Patch(facecolor=colors['other'], alpha=1, label='Other Entities'),
]
fig.legend(handles=legend_elements, loc='upper center', bbox_to_anchor=(0.5, 0.995),
ncol=5, fontsize=11, frameon=True, fancybox=True, shadow=True)
plt.tight_layout(rect=(0.0, 0.0, 1.0, 0.99))
output = "umap/graph4_all_folds_models.png"
plt.savefig(output, dpi=300, bbox_inches='tight')
plt.close()
print(f"\n{'='*60}")
print(f"Plot saved to {output}")
print(f"{'='*60}")
if __name__ == "__main__":
main()