Skip to main content

trustformers_debug/interpretability/
attribution.rs

1//! Feature attribution analysis
2//!
3//! This module provides feature attribution capabilities using various methods
4//! like Integrated Gradients, GradCAM, SHAP, and others.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::collections::HashMap;
9
10use super::config::AttributionMethod;
11
12/// Feature attribution analysis result
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct FeatureAttributionResult {
15    /// Analysis timestamp
16    pub timestamp: DateTime<Utc>,
17    /// Attribution results by method
18    pub attribution_by_method: HashMap<AttributionMethod, AttributionMethodResult>,
19    /// Consensus attribution (averaged across methods)
20    pub consensus_attribution: Vec<FeatureAttribution>,
21    /// Attribution agreement analysis
22    pub method_agreement: MethodAgreementAnalysis,
23    /// Top attributed features
24    pub top_features: Vec<TopFeature>,
25    /// Attribution visualization data
26    pub visualization_data: AttributionVisualizationData,
27}
28
29/// Attribution result for a specific method
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct AttributionMethodResult {
32    /// Attribution method used
33    pub method: AttributionMethod,
34    /// Feature attributions
35    pub attributions: Vec<FeatureAttribution>,
36    /// Method-specific parameters
37    pub method_parameters: HashMap<String, f64>,
38    /// Method reliability score
39    pub reliability_score: f64,
40    /// Computation time (milliseconds)
41    pub computation_time_ms: f64,
42}
43
44/// Individual feature attribution
45#[derive(Debug, Clone, Serialize, Deserialize)]
46pub struct FeatureAttribution {
47    /// Feature identifier
48    pub feature_id: String,
49    /// Feature name
50    pub feature_name: String,
51    /// Attribution value
52    pub attribution_value: f64,
53    /// Attribution confidence
54    pub confidence: f64,
55    /// Feature value
56    pub feature_value: f64,
57    /// Normalized attribution (0-1)
58    pub normalized_attribution: f64,
59}
60
61/// Analysis of agreement between attribution methods
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub struct MethodAgreementAnalysis {
64    /// Pairwise correlation between methods
65    pub method_correlations: HashMap<(AttributionMethod, AttributionMethod), f64>,
66    /// Overall agreement score
67    pub overall_agreement: f64,
68    /// Most consistent features across methods
69    pub consistent_features: Vec<String>,
70    /// Most divergent features across methods
71    pub divergent_features: Vec<String>,
72    /// Method reliability ranking
73    pub method_reliability: Vec<(AttributionMethod, f64)>,
74}
75
76/// Top attributed feature with additional information
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct TopFeature {
79    /// Feature information
80    pub feature: FeatureAttribution,
81    /// Rank across all methods
82    pub overall_rank: usize,
83    /// Method-specific ranks
84    pub method_ranks: HashMap<AttributionMethod, usize>,
85    /// Feature stability score
86    pub stability: f64,
87    /// Interpretation guidance
88    pub interpretation: String,
89}
90
91/// Data for visualizing attributions
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct AttributionVisualizationData {
94    /// Attribution heatmap data
95    pub heatmap_data: Vec<Vec<f64>>,
96    /// Feature names for heatmap
97    pub feature_names: Vec<String>,
98    /// Method names for heatmap
99    pub method_names: Vec<String>,
100    /// Attribution timeline (if applicable)
101    pub timeline_data: Option<Vec<TimelinePoint>>,
102    /// Feature interaction data
103    pub interaction_data: Vec<FeatureInteraction>,
104}
105
106/// Point in attribution timeline
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct TimelinePoint {
109    /// Time point (sequence position, time step, etc.)
110    pub time_point: usize,
111    /// Feature attributions at this time point
112    pub attributions: Vec<FeatureAttribution>,
113}
114
115/// Feature interaction information
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct FeatureInteraction {
118    /// First feature
119    pub feature1: String,
120    /// Second feature
121    pub feature2: String,
122    /// Interaction strength
123    pub interaction_strength: f64,
124    /// Interaction type
125    pub interaction_type: InteractionType,
126}
127
128/// Type of feature interaction
129#[derive(Debug, Clone, Serialize, Deserialize)]
130pub enum InteractionType {
131    /// Positive interaction (features reinforce each other)
132    Positive,
133    /// Negative interaction (features oppose each other)
134    Negative,
135    /// Conditional interaction (effect depends on context)
136    Conditional,
137    /// Independent (no significant interaction)
138    Independent,
139}