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