Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -84,50 +84,25 @@ def sequence_to_kmer_vector(sequence: str, k: int = 4) -> np.ndarray:
|
|
84 |
###############################################################################
|
85 |
# 3. SHAP-VALUE (ABLATION) CALCULATION
|
86 |
###############################################################################
|
|
|
|
|
87 |
def calculate_shap_values(model, x_tensor):
|
88 |
model.eval()
|
89 |
-
device = next(model.parameters()).device
|
90 |
-
|
91 |
-
try:
|
92 |
-
# Create background as a torch tensor (using zeros may be acceptable for DeepExplainer)
|
93 |
-
background = torch.zeros((300, x_tensor.shape[1]), device=device)
|
94 |
-
explainer = shap.DeepExplainer(model, background)
|
95 |
-
shap_values_all = explainer.shap_values(x_tensor)
|
96 |
-
# For binary classification, get SHAP for class 1 and first sample
|
97 |
-
shap_values = shap_values_all[1][0]
|
98 |
-
except Exception as e:
|
99 |
-
print(f"DeepExplainer failed, falling back to KernelExplainer: {str(e)}")
|
100 |
-
|
101 |
-
# Define a wrapper that ensures proper input shape and conversion to tensor
|
102 |
-
def model_predict(x):
|
103 |
-
if not isinstance(x, np.ndarray):
|
104 |
-
x = np.array(x)
|
105 |
-
if x.ndim == 1:
|
106 |
-
x = np.expand_dims(x, axis=0)
|
107 |
-
with torch.no_grad():
|
108 |
-
tensor_x = torch.tensor(x, dtype=torch.float, device=device)
|
109 |
-
output = model(tensor_x)
|
110 |
-
probs = torch.softmax(output, dim=1)[:, 1] # human probability
|
111 |
-
return probs.cpu().numpy()
|
112 |
-
|
113 |
-
# Instead of using zeros as background, use the input sample repeated 300 times.
|
114 |
-
x_numpy = x_tensor.cpu().numpy()
|
115 |
-
background = np.repeat(x_numpy, 300, axis=0)
|
116 |
-
|
117 |
-
explainer = shap.KernelExplainer(model_predict, background)
|
118 |
-
# Increase nsamples for a more robust estimate.
|
119 |
-
shap_values = explainer.shap_values(x_numpy, nsamples=1000)
|
120 |
-
# If a list is returned, select the first element.
|
121 |
-
if isinstance(shap_values, list):
|
122 |
-
shap_values = shap_values[0]
|
123 |
-
|
124 |
-
# Get the human probability from the model output.
|
125 |
with torch.no_grad():
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
|
133 |
###############################################################################
|
|
|
84 |
###############################################################################
|
85 |
# 3. SHAP-VALUE (ABLATION) CALCULATION
|
86 |
###############################################################################
|
87 |
+
|
88 |
+
|
89 |
def calculate_shap_values(model, x_tensor):
|
90 |
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
with torch.no_grad():
|
92 |
+
baseline_output = model(x_tensor)
|
93 |
+
baseline_probs = torch.softmax(baseline_output, dim=1)
|
94 |
+
baseline_prob = baseline_probs[0, 1].item() # Prob of 'human'
|
95 |
+
shap_values = []
|
96 |
+
x_zeroed = x_tensor.clone()
|
97 |
+
for i in range(x_tensor.shape[1]):
|
98 |
+
original_val = x_zeroed[0, i].item()
|
99 |
+
x_zeroed[0, i] = 0.0
|
100 |
+
output = model(x_zeroed)
|
101 |
+
probs = torch.softmax(output, dim=1)
|
102 |
+
prob = probs[0, 1].item()
|
103 |
+
shap_values.append(baseline_prob - prob)
|
104 |
+
x_zeroed[0, i] = original_val
|
105 |
+
return np.array(shap_values), baseline_prob
|
106 |
|
107 |
|
108 |
###############################################################################
|