Delete heatmap.py
Browse files- heatmap.py +0 -73
heatmap.py
DELETED
@@ -1,73 +0,0 @@
|
|
1 |
-
import transformers
|
2 |
-
from transformers import AutoTokenizer, AutoModelForMaskedLM
|
3 |
-
import logging
|
4 |
-
import torch
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
-
import seaborn as sns
|
7 |
-
import numpy as np
|
8 |
-
import gradio as gr
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
logging.getLogger("transformers.modeling_utils").setLevel(logging.ERROR)
|
13 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
-
print(f"Using device: {device}")
|
15 |
-
|
16 |
-
# Load the tokenizer and model
|
17 |
-
model_name = "ChatterjeeLab/FusOn-pLM"
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
19 |
-
model = AutoModelForMaskedLM.from_pretrained(model_name, trust_remote_code=True)
|
20 |
-
model.to(device)
|
21 |
-
model.eval()
|
22 |
-
|
23 |
-
|
24 |
-
# fix this to take dynamic input
|
25 |
-
sequence = 'MCNTNMS'
|
26 |
-
all_logits = []
|
27 |
-
for i in range(len(sequence)):
|
28 |
-
# add a masked token
|
29 |
-
masked_seq = sequence[:i] + '<mask>' + sequence[i+1:]
|
30 |
-
|
31 |
-
# tokenize masked sequence
|
32 |
-
inputs = tokenizer(masked_seq, return_tensors="pt", padding=True, truncation=True,max_length=2000)
|
33 |
-
inputs = {k: v.to(device) for k, v in inputs.items()}
|
34 |
-
|
35 |
-
# predict logits for the masked token
|
36 |
-
|
37 |
-
with torch.no_grad():
|
38 |
-
logits = model(**inputs).logits
|
39 |
-
mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
|
40 |
-
mask_token_logits = logits[0, mask_token_index, :]
|
41 |
-
top_1_tokens = torch.topk(mask_token_logits, 1, dim=1).indices[0].item()
|
42 |
-
logits_array = mask_token_logits.cpu().numpy()
|
43 |
-
|
44 |
-
# filter out non-amino acid tokens
|
45 |
-
filtered_indices = list(range(4, 23 + 1))
|
46 |
-
filtered_logits = logits_array[:, filtered_indices]
|
47 |
-
all_logits.append(filtered_logits)
|
48 |
-
|
49 |
-
token_indices = torch.arange(logits.size(-1))
|
50 |
-
tokens = [tokenizer.decode([idx]) for idx in token_indices]
|
51 |
-
filtered_tokens = [tokens[i] for i in filtered_indices]
|
52 |
-
|
53 |
-
all_logits_array = np.vstack(all_logits)
|
54 |
-
normalized_logits_array = (all_logits_array - all_logits_array.min()) / (all_logits_array.max() - all_logits_array.min())
|
55 |
-
transposed_logits_array = normalized_logits_array.T
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
# Plotting the heatmap
|
60 |
-
step = 50
|
61 |
-
y_tick_positions = np.arange(0, len(sequence), step)
|
62 |
-
y_tick_labels = [str(pos) for pos in y_tick_positions]
|
63 |
-
|
64 |
-
plt.figure(figsize=(15, 8))
|
65 |
-
sns.heatmap(transposed_logits_array, cmap='plasma', xticklabels=y_tick_labels, yticklabels=filtered_tokens)
|
66 |
-
plt.title('Logits for masked per residue tokens')
|
67 |
-
plt.ylabel('Token')
|
68 |
-
plt.xlabel('Residue Index')
|
69 |
-
plt.yticks(rotation=0)
|
70 |
-
plt.xticks(y_tick_positions, y_tick_labels, rotation = 0)
|
71 |
-
plt.show()
|
72 |
-
plt.savefig(f'heatmap_{i}.png', dpi=300, bbox_inches='tight')
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|