Spaces:
Sleeping
Sleeping
File size: 8,307 Bytes
78d786d 696610f 78d786d 696610f 78d786d 696610f 78d786d b7daa57 78d786d b7daa57 78d786d 696610f 78d786d b7daa57 78d786d b7daa57 696610f 78d786d b7daa57 78d786d 7d1331e 78d786d 7d1331e 78d786d 7d1331e 78d786d 7d1331e 78d786d 7d1331e 78d786d 7d1331e 696610f 78d786d 7d1331e 78d786d 7d1331e 78d786d 7d1331e 696610f 78d786d |
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 |
import numpy as np
import matplotlib.pyplot as plt
import os
import logging
from matplotlib.colors import LinearSegmentedColormap
from mpl_toolkits.axes_grid1 import make_axes_locatable
from PIL import Image
import io
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def plot_crossattention_weights(target_mask, drug_mask, target_tokenized, drug_tokenized,
crossattention_weights, target_tokenizer, drug_tokenizer):
"""
Plots the cross-attention weights for a given drug-target pair, only considering unmasked tokens.
Parameters:
target_mask (np.ndarray): Boolean mask for target tokens.
drug_mask (np.ndarray): Boolean mask for drug tokens.
target_tokenized (dict): Tokenized target sequence.
drug_tokenized (dict): Tokenized drug sequence.
crossattention_weights (np.ndarray): The cross-attention weights.
target_tokenizer: Target tokenizer instance.
drug_tokenizer: Drug tokenizer instance.
Returns:
PIL.Image: The generated attention heatmap image.
"""
logger.info("Starting plot_crossattention_weights")
# Convert masks to numpy arrays if they're tensors
if hasattr(target_mask, 'cpu'):
target_mask = target_mask.cpu()
if hasattr(drug_mask, 'cpu'):
drug_mask = drug_mask.cpu()
logger.info(f"Target mask shape: {target_mask.shape}, Drug mask shape: {drug_mask.shape}")
# Get tokens for unmasked positions
tokens_input = target_tokenized["input_ids"][0][target_mask]
target_token_str = target_tokenizer.convert_ids_to_tokens(tokens_input)
tokens_input = drug_tokenized["input_ids"][0][drug_mask]
drug_token_str = drug_tokenizer.convert_ids_to_tokens(tokens_input)
logger.info(f"Drug tokens: {drug_token_str}")
# Extract subset of attention weights
if hasattr(crossattention_weights, 'cpu'):
crossattention_weights = crossattention_weights.cpu()
subset = crossattention_weights[target_mask][:, drug_mask]
subset_np = subset.detach().numpy() # Convert to numpy for matplotlib
logger.info(f"Subset shape: {subset_np.shape}")
height, width = subset_np.shape
fig, ax = plt.subplots(
figsize=(width * 0.2 + 2, height * 0.2 + 3),
dpi=300
)
im = ax.imshow(subset_np, cmap='hot', interpolation='nearest')
plt.colorbar(im, ax=ax, orientation='vertical', fraction=0.05, shrink=0.8)
plt.title("Cross-Attention Weights")
plt.xlabel("Drug Tokens")
plt.ylabel("Target Tokens")
# Create vertical labels for drug tokens
vertical_labels = ['\n'.join(label) for label in drug_token_str]
plt.xticks(ticks=np.arange(width), labels=vertical_labels)
plt.yticks(ticks=np.arange(height), labels=target_token_str)
# Add text annotations
max_val = subset_np.max()
logger.info(f"Max crossattention weight: {max_val}")
for i in range(height):
for j in range(width):
val = subset_np[i, j]
if val > max_val / 2:
# Extract just the digits after the decimal (no leading '0.')
text = f"{val % 1:.2f}"[2:]
plt.text(j, i, text,
ha='center', va='center',
color="black",
fontsize=6)
# Convert to PIL Image
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
buf.seek(0)
img = Image.open(buf)
plt.close()
logger.info("Finished plot_crossattention_weights successfully")
return img
def plot_presum(tokenized_input, affinities, scaler, w, b, target_tokenizer,
raw_affinities=False):
"""
Generates an annotated 1D heatmap of token-level contribution scores.
Args:
tokenized_input (dict): Output of a tokenizer with keys:
- 'input_ids' (torch.Tensor): token ID sequences, shape (1, seq_len)
- 'attention_mask' (torch.Tensor): mask indicating padding tokens
affinities (torch.Tensor): Final layer summation affinity contributions from the model, shape (1, seq_len)
scaler (object): Fitted scaler with `mean_` and `std_` attributes for inverse-transform.
w (float): Weight applied to the summed affinities before bias.
b (float): Bias added to the summed affinities.
target_tokenizer: Target tokenizer instance.
raw_affinities (bool): If True, plot raw (signed) contributions on a blue—white—red scale.
If False, enforce non-negative contributions and use a white—red scale.
Default: False
Returns:
PIL.Image: The generated contribution visualization image.
Raises:
ValueError: If `sum(transformed_affinities) < 0` when `raw_affinities=False`.
"""
colors = [
(1.0, 0.95, 0.95),
(1.0, 0.5, 0.5),
(0.8, 0.0, 0.0)
]
custom_reds = LinearSegmentedColormap.from_list("CustomReds", colors)
# Convert tensors to numpy if needed
if hasattr(affinities, 'cpu'):
affinities = affinities.cpu().numpy()
if hasattr(w, 'cpu'):
w = w.cpu().numpy()
if hasattr(b, 'cpu'):
b = b.cpu().numpy()
# Apply transformations
affinities = w * (affinities[0]) + b / len(affinities[0])
affinities = (affinities * scaler.std_) + scaler.mean_ / len(affinities)
if sum(affinities) < 0 and not raw_affinities:
raise ValueError("Cannot use non-raw affinities with negative binding affinity prediction")
# Get token strings
tokens_input = tokenized_input["input_ids"][0]
if hasattr(tokens_input, 'cpu'):
tokens_input = tokens_input.cpu().numpy()
token_str = target_tokenizer.convert_ids_to_tokens(tokens_input)
# Handle padding
pad_mask = tokenized_input["attention_mask"][0] == 0
if hasattr(pad_mask, 'cpu'):
pad_mask = pad_mask.cpu().numpy()
padding_affinities_sum = affinities[pad_mask].sum()
non_padding_affinities = affinities[~pad_mask]
processed_affinities = non_padding_affinities + padding_affinities_sum/len(non_padding_affinities)
# Make affinities non-negative if requested
if not raw_affinities:
all_negative_non_paddings = processed_affinities[processed_affinities < 0]
while(len(all_negative_non_paddings) > 0):
all_positive_non_paddings = processed_affinities[processed_affinities > 0]
processed_affinities[processed_affinities < 0] = 0
processed_affinities[processed_affinities > 0] = all_positive_non_paddings + all_negative_non_paddings.sum()/len(all_positive_non_paddings)
all_negative_non_paddings = processed_affinities[processed_affinities < 0]
# Create visualization
max_per_row = 20
n = len(processed_affinities)
n_rows = int(np.ceil(n / max_per_row))
grid = np.full((n_rows, max_per_row), np.nan)
grid.flat[:n] = processed_affinities
fig, ax = plt.subplots(
figsize = (max_per_row * 1, n_rows * 1 + 2),
dpi = 300
)
ax.set_xticks([])
ax.set_yticks([])
im = ax.imshow(
grid,
aspect='equal',
cmap='bwr' if raw_affinities else custom_reds,
vmin=np.nanmin(grid) if not raw_affinities else -max(abs(np.nanmin(grid)), abs(np.nanmax(grid))),
vmax=np.nanmax(grid) if not raw_affinities else max(abs(np.nanmin(grid)), abs(np.nanmax(grid))),
)
def wrap_text(text, width=8):
return '\n'.join(text[i:i+width] for i in range(0, len(text), width))
for idx, val in enumerate(processed_affinities):
r, c = divmod(idx, max_per_row)
wrapped_token = wrap_text(token_str[idx], width=8)
ax.text(c, r, f"{val:.2f}\n{wrapped_token}",
ha='center', va='center', fontsize=8)
divider = make_axes_locatable(ax)
cax = divider.append_axes('bottom', size=0.2, pad=0.3)
cbar = fig.colorbar(im, cax=cax, orientation='horizontal')
cbar.set_label("Contribution")
# Convert to PIL Image
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', dpi=300)
buf.seek(0)
img = Image.open(buf)
plt.close()
return img |