fix: record "sample" in op_history instead of "__getitem__" (#4376)

Explanation.sample() delegates to self[list(inds)], which records
"__getitem__" in op_history. This misrepresents the operation the
user actually called. Fix by replacing the __getitem__ entry with
a "sample" entry after the slice, so op_history accurately reflects
the high-level operation.

Closes #4374
This commit is contained in:
Tarun
2026-04-21 21:46:48 +05:30
committed by GitHub
parent 7fd8209709
commit 09b4e7bec2
2 changed files with 7 additions and 2 deletions
+6 -1
View File
@@ -622,7 +622,12 @@ class Explanation(metaclass=MetaExplanation):
length = self.shape[0]
assert length is not None
inds = rng.choice(length, size=min(max_samples, length), replace=replace)
return self[list(inds)]
prev_shape = self.shape
new_self = self[list(inds)]
# Replace the __getitem__ entry with "sample" so op_history
# reflects the high-level operation the user actually called.
new_self.op_history[-1] = OpHistoryItem(name="sample", args=(max_samples,), prev_shape=prev_shape)
return new_self
def hclust(self, metric: str = "sqeuclidean", axis: Literal[0, 1] = 0) -> npt.NDArray[np.int64]:
"""Computes an optimal leaf ordering sort order using hclustering.
+1 -1
View File
@@ -136,7 +136,7 @@ def test_populating_op_history():
exp += 2
expected_op_names = [
"abs",
"__getitem__",
"sample",
"flip",
"__getitem__",
"mean",