-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersection.py
More file actions
135 lines (91 loc) · 4.21 KB
/
intersection.py
File metadata and controls
135 lines (91 loc) · 4.21 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
import torch
from collections import defaultdict, Counter
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from collections import defaultdict
# %%
from rich.console import Console
from rich.table import Table
def display_table(dataframe):
table = Table(show_header=True, header_style="bold magenta")
for column in dataframe.columns:
table.add_column(column)
for index, row in dataframe.iterrows():
table.add_row(*[str(value) for value in row])
console = Console()
console.print(table)
def display_heatmap(x,comp):
# Show the features-presence heatmap
topic_feat_dict = defaultdict(lambda: defaultdict(int))
for topic, topic_dict in x.items():
for eg, features_dict in topic_dict.items():
for pos, feat, layer in features_dict:
if layer == comp:
topic_feat_dict[topic][feat] += 1
unique_features = sorted(set(feat for topic in topic_feat_dict for feat in topic_feat_dict[topic].keys()))
heatmap_data = pd.DataFrame(0, index=topic_feat_dict.keys(), columns=unique_features)
for topic, features in topic_feat_dict.items():
for feat, count in features.items():
heatmap_data.at[topic, feat] = 1 if count > 0 else 0
# Sort the heatmap data by the number of active features
heatmap_data = heatmap_data[heatmap_data.sum(axis=0).sort_values(ascending=False).index]
# Calculate the number of unique features
num_features = heatmap_data.shape[1]
# Adjust the figure size based on the number of features
plt.figure(figsize=((12, 8))) # Width is proportional to the number of features
sns.heatmap(heatmap_data.T, annot=False, cmap="YlGnBu", cbar=False, linewidths=0.5, linecolor='white')
plt.title("Feature Presence Heatmap")
plt.xlabel("Unique Features")
plt.ylabel("Topics")
plt.xticks(rotation=45, ha='right') # Rotate x-axis labels for better readability
plt.show()
# %%
x = torch.load("tuples/all_tuples_dict_top_100_item_pos_logit_diff_all_resid.pt")
# %%
all_tuples_df = []
topic_features = defaultdict(lambda: defaultdict(list))
topic_positions = defaultdict(lambda: defaultdict(list))
for key, val in x.items():
for eg_id, tups in val.items():
for pos, feat, layer in tups:
if pos != 0:
topic_features[key][layer].append(feat)
topic_positions[key][layer].append(pos)
counter_features = {key: {layer: Counter(features) for layer, features in topic_features[key].items()} for key in topic_features.keys()}
# Get the top-10 features for each topic and layer
top_counter_features = {key: {layer: counter.most_common(10) for layer, counter in counter_features[key].items()} for key in counter_features.keys()}
for key, top_feats in top_counter_features.items():
for layer in top_feats.keys():
all_tuples_df.append({"Topic": key, "Layer": layer, "Top-10 feats": [elem[0] for elem in top_feats[layer]], "Top-10 positions": [elem[1] for elem in top_feats[layer]]})
all_tuples_df = pd.DataFrame(all_tuples_df)
all_tuples_df.to_html("tables/all_tuples_df_attrb.html")
display_table(all_tuples_df)
for l in [7]:
display_heatmap(x,f"blocks.{l}.attn.hook_z")
# ======== Expression matrix ======
total_examples = 0
unique_features = []
for key,val in x.items():
for eg_id, tups in val.items():
total_examples += 1
for pos, feat, layer in tups:
unique_features.append(feat)
unique_features = list(set(unique_features))
count_matrix = torch.zeros((len(unique_features), total_examples))
i = 0
for (key, val) in x.items():
for eg_id, tups in val.items():
for pos, feat, layer in tups:
#if layer == "blocks.2.attn.hook_z":
count_matrix[unique_features.index(feat), i] += 1
i+=1
df = pd.DataFrame(count_matrix/50, columns=range(total_examples), index=unique_features)
# remove nan and inf values
df.fillna(0, inplace=True)
df.replace([float('inf')], 0, inplace=True)
# Create a clustermap with hierarchical clustering
plt.figure(figsize=(14, 10))
sns.clustermap(df, cmap='RdYlGn', standard_scale=1, figsize=(12, 12), cbar_kws={'label': 'Expression Level'})
plt.title('Hierarchical Clustering of Gene Expression Data')
plt.show()