-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_heatmap.py
More file actions
183 lines (159 loc) · 7.03 KB
/
plot_heatmap.py
File metadata and controls
183 lines (159 loc) · 7.03 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
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import Normalize, LogNorm
from pathlib import Path
mpl.rcParams.update({
"font.family": "sans-serif",
"font.sans-serif": ["Arial", "Helvetica", "DejaVu Sans"],
"font.size": 8,
"axes.labelsize": 8,
"axes.titlesize": 8,
"xtick.labelsize": 7,
"ytick.labelsize": 7,
"pdf.fonttype": 42,
"ps.fonttype": 42,
})
import seaborn as sns
import pickle
import numpy as np
import os
def main(seed, feature):
"""
"""
addname = f"result_all_everything_seed{seed}_{feature}+hidden300+batch128+angle"
# modulation + unnormalized modulation + weighted modulation (unnormalized)
paths = [
f"modulation_all_clustering_{addname}_normalized.pkl",
f"modulation_all_clustering_{addname}_unnormalized.pkl",
f"modulation_all_weighted_clustering_{addname}_unnormalized.pkl"
]
os.makedirs("multiple_task_heatmaps", exist_ok=True)
for idx, path in enumerate(paths):
print(f"Processing {path}...")
with open("multiple_tasks/" + path, "rb") as f:
data = pickle.load(f)
select = 1
heatmap = data["cell_vars_rules_sorted_norm_all_lst"][select]
boundaries = data["modulation_cluster_boundary"][select]
xboundary, yboundary = boundaries[0], boundaries[1]
plot_data = np.asarray(heatmap, dtype=float)
# normalization rule
if idx >= 1:
# LogNorm requires strictly positive values
vmax = np.nanmax(plot_data)
if vmax <= 0:
raise ValueError("For idx >= 1, heatmap must contain at least one positive value for LogNorm.")
plot_data = np.clip(plot_data, 1e-3, None)
norm = LogNorm(vmin=1e-3, vmax=vmax)
else:
norm = Normalize(vmin=0, vmax=1)
fig, ax = plt.subplots(1,1,figsize=(10,4))
sns.heatmap(
plot_data,
ax=ax,
cmap="coolwarm",
cbar=True,
norm=norm,
linewidths=0,
)
# clearer separators
for x in xboundary:
ax.axhline(x, color="black", linestyle="-", linewidth=1.5, alpha=0.9, zorder=10)
for y in yboundary:
ax.axvline(y, color="black", linestyle="-", linewidth=1.5, alpha=0.9, zorder=10)
ax.set_title(f"Neuron Class Number: {len(yboundary)+1}")
fig.tight_layout()
fig.savefig(
os.path.join("multiple_task_heatmaps", path.replace(".pkl", ".png")),
dpi=300,
bbox_inches="tight"
)
plt.close(fig)
# for idx == 2: grid of block heatmaps + column-cluster sum filtered heatmap
if idx == 2 and len(yboundary) > 0:
col_edges = [0] + list(yboundary) + [plot_data.shape[1]]
row_edges = [0] + list(xboundary) + [plot_data.shape[0]]
n_col_cls = len(col_edges) - 1
n_row_cls = len(row_edges) - 1
# Mean of each (row_cluster × col_cluster) block; shape (n_row_cls, n_col_cls)
block_means = np.array([
[plot_data[row_edges[ri]:row_edges[ri + 1],
col_edges[ci]:col_edges[ci + 1]].mean()
for ci in range(n_col_cls)]
for ri in range(n_row_cls)
])
# Score each column cluster by summing its block means across row clusters
# (size-independent, unlike raw entry sum)
col_sums = block_means.sum(axis=0) # shape (n_col_cls,)
fig_grid, ax_grid = plt.subplots(
1, 1, figsize=(max(4, n_col_cls), max(2, n_row_cls))
)
sns.heatmap(block_means, ax=ax_grid, cmap="coolwarm", cbar=True,
norm=norm, linewidths=0.5, annot=True, fmt=".2g",
xticklabels=[f"C{ci}" for ci in range(n_col_cls)],
yticklabels=[f"R{ri}" for ri in range(n_row_cls)])
ax_grid.set_title(
f"Block means — {n_row_cls} row × {n_col_cls} col clusters\n"
f"col sums: {', '.join(f'C{ci}={col_sums[ci]:.2g}' for ci in range(n_col_cls))}",
fontsize=8
)
fig_grid.tight_layout()
fig_grid.savefig(
os.path.join("multiple_task_heatmaps", path.replace(".pkl", "_block_grid.png")),
dpi=200, bbox_inches="tight"
)
plt.close(fig_grid)
# --- Figure 2: remove column clusters whose sum is below threshold ---
threshold_frac = 0.10 # remove if sum < 10% of max column-cluster sum
threshold_val = threshold_frac * col_sums.max()
kept = np.where(col_sums >= threshold_val)[0]
removed = np.where(col_sums < threshold_val)[0]
if len(kept) == 0:
kept = np.array([int(np.argmax(col_sums))]) # always keep at least one
removed = np.array([i for i in range(n_col_cls) if i not in kept])
kept_blocks = [plot_data[:, col_edges[ci]:col_edges[ci + 1]] for ci in kept]
plot_data_filt = np.concatenate(kept_blocks, axis=1)
kept_sizes = [col_edges[ci + 1] - col_edges[ci] for ci in kept]
yboundary_filt = list(np.cumsum(kept_sizes)[:-1])
kept_desc = ", ".join(
f"C{ci}(size={col_edges[ci+1]-col_edges[ci]}, sum={col_sums[ci]:.2g})"
for ci in kept
)
removed_desc = (
", ".join(
f"C{ci}(size={col_edges[ci+1]-col_edges[ci]}, sum={col_sums[ci]:.2g})"
for ci in removed
)
if len(removed) > 0 else "none"
)
title_filt = (
f"Col clusters kept: {len(kept)}/{n_col_cls} "
f"(threshold={threshold_frac:.0%} × max={col_sums.max():.3g})\n"
f"Kept: {kept_desc}\n"
f"Removed: {removed_desc}"
)
fig_filt, ax_filt = plt.subplots(1, 1, figsize=(10, 4))
sns.heatmap(
plot_data_filt, ax=ax_filt, cmap="coolwarm",
cbar=True, norm=norm, linewidths=0,
)
for x in xboundary:
ax_filt.axhline(x, color="black", linestyle="-", linewidth=1.5,
alpha=0.9, zorder=10)
for y in yboundary_filt:
ax_filt.axvline(y, color="black", linestyle="-", linewidth=1.5,
alpha=0.9, zorder=10)
ax_filt.set_title(title_filt, fontsize=8)
fig_filt.tight_layout()
fig_filt.savefig(
os.path.join("multiple_task_heatmaps", path.replace(".pkl", "_col_filtered.png")),
dpi=300, bbox_inches="tight"
)
plt.close(fig_filt)
if __name__ == "__main__":
# Clean up old output files
for f in Path("multiple_task_heatmaps").glob("*.png"):
f.unlink()
seed = 749
L2 = "L21e4"
main(seed, L2)