Skip to content

Commit d4ee581

Browse files
Merge pull request #7 from RaykKretzschmar/copilot/sub-pr-3-yet-again
Align DummySpectralCNN test model with real SpectralCNN tuple return signature
2 parents 5c177aa + c61f0ce commit d4ee581

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

‎spectra2prop/utils/explainability.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,20 @@ def generate_heatmap(self, input_tensor, target_class_idx=None, normalize=True):
7979
# 1. Forward pass
8080
# The hooks will capture self.activations here
8181
output = self.model(input_tensor)
82+
83+
# Handle tuple output (logits, embedding) from SpectralCNN
84+
if isinstance(output, tuple):
85+
logits = output[0]
86+
else:
87+
logits = output
8288

8389
# Determine target class
8490
if target_class_idx is None:
85-
target_class_idx = output.argmax(dim=1).item()
91+
target_class_idx = logits.argmax(dim=1).item()
8692

8793
# 2. Backward pass
8894
# Create a one-hot target tensor to backpropagate specific class score
89-
target_score = output[:, target_class_idx]
95+
target_score = logits[:, target_class_idx]
9096
target_score.backward(retain_graph=True)
9197

9298
# 3. Compute Weights

‎tests/test_explainability.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def forward(self, x):
2525
x = self.relu(self.conv2(x))
2626
x = self.gap(x)
2727
x = x.view(x.size(0), -1)
28-
x = self.fc(x)
29-
return x
28+
embedding = x # Save features before final layer
29+
logits = self.fc(x)
30+
return logits, embedding # Return tuple (logits, features) to match real SpectralCNN model interface
3031

3132
class NoConvModel(nn.Module):
3233
"""A model with no Conv1d layers to test error handling."""
@@ -107,6 +108,8 @@ def test_heatmap_automatic_class_selection(self):
107108
# Get model output to determine expected class
108109
with torch.no_grad():
109110
output = self.model(self.input_tensor)
111+
if isinstance(output, tuple):
112+
output = output[0]
110113
expected_class = output.argmax(dim=1).item()
111114

112115
# Generate heatmap with automatic class selection

0 commit comments

Comments
 (0)