Skip to content

Commit 87cbfda

Browse files
model.explain correction + small conformity tasks
1 parent fa1d6c7 commit 87cbfda

5 files changed

Lines changed: 189 additions & 545 deletions

File tree

customics/model.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -770,18 +770,25 @@ def explain(
770770

771771
expr_df = expr_df.loc[sample_id, :]
772772
background = addToTensor(randomTrainingSample(expr_df, 10), device)
773-
foreground = addToTensor(
774-
splitExprandSample(condition=condition, sample_size=10, expr=expr_df),
775-
device,
776-
)
773+
foreground_df = splitExprandSample(condition=condition, sample_size=10, expr=expr_df)
774+
foreground = addToTensor(foreground_df, device)
777775

776+
class_idx = int(self.label_encoder.transform([subtype])[0])
778777
explainer = shap.DeepExplainer(ModelWrapper(self, source=source), background)
779778
shap_values = explainer.shap_values(foreground, ranked_outputs=None)
780779

781-
tumour_expr = expr_df.head(10)
780+
# SHAP ≥0.46 stacks class outputs into (n_samples, n_features, n_classes);
781+
# older versions return a list indexed [class][sample, feature].
782+
import numpy as np
783+
784+
if isinstance(shap_values, np.ndarray) and shap_values.ndim == 3:
785+
sv = shap_values[..., class_idx]
786+
else:
787+
sv = shap_values[class_idx]
788+
782789
shap.summary_plot(
783-
shap_values[0],
784-
features=tumour_expr,
790+
sv,
791+
features=foreground_df,
785792
feature_names=list(expr_df.columns),
786793
show=False,
787794
plot_type="violin",
@@ -793,6 +800,12 @@ def explain(
793800
plt.show()
794801
plt.clf()
795802

803+
# SHAP registers forward/backward hook tensors as nn.Parameter on each
804+
# module. Remove them so state_dict() stays clean for save/load.
805+
for module in self.modules():
806+
module._parameters.pop("x", None)
807+
module._parameters.pop("y", None)
808+
796809
# ------------------------------------------------------------------ #
797810
# Utilities
798811
# ------------------------------------------------------------------ #

customics/modules/fully_connected.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(
7171
act_cls = _ACTIVATION_MAP[key]
7272
if act_cls is not None:
7373
layers.append(
74-
act_cls(negative_slope=leaky_slope, inplace=True)
74+
act_cls(negative_slope=leaky_slope, inplace=False)
7575
if key == "leakyrelu"
7676
else (act_cls(dim=1) if key == "softmax" else act_cls())
7777
)

docs/tutorials/usage.ipynb

Lines changed: 82 additions & 60 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)