pyhealth.interpret.methods.base_interpreter#
Overview#
pyhealth.interpret.methods.BaseInterpreter is the abstract base class
shared by every attribution method in pyhealth.interpret.methods. Subclasses
implement attribute(**batch), which returns a dictionary of attribution
tensors keyed by the task’s feature keys. Subclass it to add a new
interpretability method.
API Reference#
- class pyhealth.interpret.methods.BaseInterpreter(model)[source]#
Bases:
ABCAbstract base class for interpretability methods.
This class defines the interface that all attribution methods must implement. Attribution methods compute importance scores for input features, explaining which features contributed most to the model’s prediction.
API Contract:
All interpretability methods should:
Take a trained model in their constructor
Implement the
attribute()methodReturn attributions as a dictionary keyed by the model’s feature keys (as defined by the task’s
input_schema)Work with any PyHealth model (or clearly document compatibility)
The
attribute()method returns a dictionary that mirrors the task schema:For EHR tasks with
input_schema={"conditions": "sequence", "procedures": "sequence"}, returns{"conditions": attr_tensor, "procedures": attr_tensor}For image tasks with
input_schema={"image": "image"}, returns{"image": attr_tensor}
This design ensures attributions are dynamically tied to dataset feature keys, making the API consistent across CXR datasets, EHR datasets, or any custom task schema.
- Subclasses should implement:
__init__(self, model, **kwargs): Initialize with model and method-specific parametersattribute(self, **data): Compute attributions for given inputs
- Parameters:
model (BaseModel or nn.Module) – A trained PyHealth model to interpret. The model must have
feature_keys(list of input feature names) derived from the dataset’s task schema. Should be in evaluation mode during attribution computation.
Example
>>> # Example 1: EHR data with multiple feature keys >>> from pyhealth.datasets import create_sample_dataset, get_dataloader >>> from pyhealth.models import Transformer >>> >>> samples = [ ... { ... "patient_id": "p0", ... "visit_id": "v0", ... "conditions": ["A05B", "A05C", "A06A"], ... "procedures": ["P01", "P02"], ... "label": 1, ... }, ... ] >>> dataset = create_sample_dataset( ... samples=samples, ... input_schema={"conditions": "sequence", "procedures": "sequence"}, ... output_schema={"label": "binary"}, ... dataset_name="ehr_example", ... ) >>> model = Transformer(dataset=dataset) >>> # model.feature_keys == ["conditions", "procedures"] >>> >>> interpreter = CheferRelevance(model) >>> batch = next(iter(get_dataloader(dataset, batch_size=1))) >>> attributions = interpreter.attribute(**batch) >>> # Returns: {"conditions": tensor(...), "procedures": tensor(...)} >>> print(attributions.keys()) # dict_keys(['conditions', 'procedures']) >>> >>> # Example 2: Image data (CXR) with single feature key >>> # Given task schema: input_schema={"image": "image"} >>> # model.feature_keys == ["image"] (or model.feature_key == "image") >>> >>> interpreter = CheferRelevance(vit_model) >>> attributions = interpreter.attribute(**batch) >>> # Returns: {"image": tensor(...)} - keyed by the task's feature key >>> print(attributions["image"].shape) # [batch, 1, H, W]
Initialize the base interpreter.
- Parameters:
model (
BaseModel) – A trained PyHealth model to interpret. The model should be in evaluation mode during interpretation.
Note
Subclasses should call super().__init__(model) and then perform any method-specific initialization.
- __init__(model)[source]#
Initialize the base interpreter.
- Parameters:
model (
BaseModel) – A trained PyHealth model to interpret. The model should be in evaluation mode during interpretation.
Note
Subclasses should call super().__init__(model) and then perform any method-specific initialization.
- abstract attribute(**data)[source]#
Compute attribution scores for input features.
This is the main method that all interpretability methods must implement. It takes model inputs and returns attribution scores indicating which features were most important for the model’s prediction.
Important: The returned dictionary must be keyed by the model’s feature keys (from
model.feature_keysormodel.feature_key), which are derived from the task’sinput_schema. This ensures attributions map directly to the input modalities defined in the task.- Parameters:
**data –
Input data dictionary from a dataloader batch. Should contain at minimum:
Feature keys (e.g.,
"conditions","procedures","image"): Input tensors for each modality as defined by the task’sinput_schema.Label key (optional): Ground truth labels, may be needed by some methods for loss computation.
target_class_idx(Optional[int]): Target class for attribution. For binary classification (single logit output), this is a no-op because there is only one output. For multi-class or multi-label classification, specifies which class index to explain. If not provided, uses the argmax of logits.Additional method-specific parameters (e.g.,
baseline,steps,interpolate).
The data dictionary should match what would be passed to the model’s
forward()method.- Returns:
- Dictionary mapping each feature key
to its attribution tensor. The keys must match the model’s feature keys from the task schema.
For EHR tasks:
{"conditions": tensor, "procedures": tensor, ...}
For image tasks:
{"image": tensor} # Shape: [batch, 1, H, W] for spatial
Each attribution tensor:
Contains real-valued importance scores
Higher absolute values = more important features
Can be positive or negative depending on the method
Should be on the same device as the input
- Return type:
Dict[str, torch.Tensor]
- Raises:
NotImplementedError – If subclass doesn’t implement this method.
Note
Attribution Properties:
Different attribution methods produce scores with different properties:
Sign: Some methods produce only positive scores (e.g., attention weights), while others produce both positive and negative scores (e.g., Integrated Gradients, DeepLift).
Magnitude: Scores may be normalized (sum to 1) or unnormalized (raw gradients/relevance).
Shape: For sequential data, shape matches input tokens. For images, shape is typically
[batch, 1, H, W]for spatial attribution maps.
Common Patterns:
Gradient-based (IG, Saliency): +/- scores, contribution to output
Attention-based (Chefer): Usually positive, relevance/importance
Perturbation-based (LIME, SHAP): +/- scores, feature contribution
Example
>>> # EHR model with multiple feature keys >>> interpreter = DeepLift(model) >>> batch = next(iter(test_loader)) >>> attributions = interpreter.attribute(**batch) >>> print(attributions.keys()) # ['conditions', 'procedures'] >>> >>> # Image model (CXR) with single feature key >>> interpreter = CheferRelevance(vit_model) >>> attributions = interpreter.attribute(**batch) >>> print(attributions.keys()) # ['image'] >>> print(attributions['image'].shape) # [1, 1, 224, 224]