Spaces:
Sleeping
Sleeping
File size: 23,785 Bytes
59f824e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | """Explainability module for deception detection using SHAP and feature importance.
This module provides interpretable explanations for deception predictions by
combining SHAP values, feature importance, and domain knowledge to produce
human-readable behavioral explanations.
"""
from __future__ import annotations
import warnings
from typing import Any, Dict, List, Optional, Tuple, Union
import numpy as np
import pandas as pd
import shap
from sklearn.inspection import permutation_importance
from sklearn.utils import check_array
from src.model.classifier import DeceptionClassifier
class DeceptionExplainer:
"""Generates interpretable explanations for deception predictions.
Combines SHAP values, feature importance, and domain knowledge
to produce human-readable behavioral explanations.
"""
# Domain knowledge mapping: feature name -> behavioral interpretation
FEATURE_INTERPRETATIONS = {
'eye_left_openness': 'Left eye openness (reduced blinking or gaze aversion may indicate deception)',
'eye_right_openness': 'Right eye openness (asymmetric eye behavior can signal cognitive load)',
'mouth_openness': 'Mouth openness (suppressed or exaggerated expressions may indicate deception)',
'mouth_width': 'Mouth width changes (forced smiles show different width patterns)',
'mouth_asymmetry': 'Mouth asymmetry (asymmetric expressions are harder to fake)',
'left_brow_height': 'Left eyebrow height (raised brows can indicate surprise or concern)',
'right_brow_height': 'Right eyebrow height (asymmetric brow activity linked to deception)',
'brow_asymmetry': 'Eyebrow asymmetry (involuntary micro-expressions often appear asymmetrically)',
'brow_tension': 'Eyebrow tension (furrowed brows indicate cognitive effort or stress)',
'nose_wrinkle': 'Nose wrinkle (disgust micro-expression, hard to fake voluntarily)',
'nostril_flare': 'Nostril flare (stress indicator, autonomic nervous system response)',
'nose_asymmetry': 'Nose asymmetry (subtle asymmetries reveal involuntary expressions)',
'head_pitch': 'Head pitch (nodding patterns differ between truthful and deceptive responses)',
'head_yaw': 'Head yaw (head turning can indicate avoidance behavior)',
'head_roll': 'Head tilt (unusual tilt patterns may indicate discomfort)',
'head_displacement': 'Head movement (excessive stillness or fidgeting both signal stress)',
'pitch_mean': 'Average pitch (elevated pitch is a stress indicator)',
'pitch_std': 'Pitch variability (reduced variability suggests rehearsed responses)',
'pitch_range': 'Pitch range (constricted range indicates tension)',
'pitch_variation_coefficient': 'Pitch variation coefficient (normalized pitch instability)',
'voiced_ratio': 'Voiced speech ratio (reduced voicing suggests hesitation)',
'pause_count': 'Number of pauses (increased pausing indicates cognitive load)',
'avg_pause_duration': 'Average pause length (longer pauses suggest response fabrication)',
'max_pause_duration': 'Longest pause (extended silence before answering is suspicious)',
'pause_rate': 'Pauses per minute (higher rate indicates cognitive effort)',
'speech_ratio': 'Speech ratio (less speech time relative to pauses suggests evasion)',
'energy_mean': 'Average speech energy (reduced energy may indicate withdrawal)',
'energy_std': 'Energy variability (high variability suggests emotional instability)',
'energy_range': 'Energy range (wide range indicates emotional dysregulation)',
'energy_variation': 'Energy variation coefficient (normalized energy instability)',
'spectral_centroid_mean': 'Spectral centroid (brightness of voice, shifts under stress)',
'spectral_bandwidth_mean': 'Spectral bandwidth (voice quality changes under deception)',
'audio_visual_sync': 'Audio-visual synchrony (desynchronization suggests incongruent behavior)',
'pause_gaze_coupling': 'Pause-gaze coupling (looking away while pausing is a deception cue)',
'stress_composite': 'Stress composite (combined physiological stress indicator)',
}
FEATURE_GROUPS = {
'Eye Behavior': ['eye_left_openness', 'eye_right_openness'],
'Mouth Dynamics': ['mouth_openness', 'mouth_width', 'mouth_asymmetry'],
'Eyebrow Activity': ['left_brow_height', 'right_brow_height', 'brow_asymmetry', 'brow_tension'],
'Nose Movement': ['nose_wrinkle', 'nostril_flare', 'nose_asymmetry'],
'Head Dynamics': ['head_pitch', 'head_yaw', 'head_roll', 'head_displacement'],
'Voice Pitch': ['pitch_mean', 'pitch_std', 'pitch_range', 'pitch_variation_coefficient'],
'Speech Timing': ['pause_count', 'avg_pause_duration', 'max_pause_duration', 'pause_rate', 'speech_ratio', 'voiced_ratio'],
'Voice Energy': ['energy_mean', 'energy_std', 'energy_range', 'energy_variation', 'spectral_centroid_mean', 'spectral_bandwidth_mean'],
'Cross-Modal': ['audio_visual_sync', 'pause_gaze_coupling', 'stress_composite'],
}
def __init__(self, classifier: DeceptionClassifier):
"""Initialize with a trained DeceptionClassifier.
Args:
classifier: A trained DeceptionClassifier instance.
Raises:
ValueError: If classifier is not trained.
"""
if not classifier.is_fitted_:
warnings.warn("Classifier must be trained before creating explainer. Explainer will not work until trained.")
self.classifier = classifier
self._shap_explainer = None
self._feature_names = classifier.feature_names_
def _init_shap_explainer(self) -> Optional[shap.TreeExplainer]:
"""Lazy-initialize SHAP TreeExplainer from the trained model.
Returns:
shap.TreeExplainer: Initialized SHAP explainer, or None on failure.
Notes:
Uses feature_perturbation='tree_path_dependent' for tree-based models.
On failure, warns and returns None.
"""
if self._shap_explainer is None:
try:
# Use the underlying GradientBoostingClassifier (not calibrated model)
# Research rationale: TreeExplainer works best with tree-based models
# and provides exact SHAP values for gradient boosting.
self._shap_explainer = shap.TreeExplainer(
model=self.classifier.model,
feature_perturbation='tree_path_dependent'
)
except Exception as e:
warnings.warn(
f"Failed to initialize SHAP TreeExplainer: {e}. "
"Falling back to permutation importance.",
RuntimeWarning
)
self._shap_explainer = None
return self._shap_explainer
def compute_shap_values(self, X: np.ndarray, feature_names: List[str]) -> np.ndarray:
"""Compute SHAP values using TreeExplainer.
Args:
X: Feature matrix of shape (n_samples, n_features).
feature_names: Ordered list of feature names matching columns of X.
Returns:
np.ndarray: SHAP values array of shape (n_samples, n_features).
Notes:
- If SHAP fails, falls back to permutation importance.
- Returns absolute SHAP values (magnitude of contribution).
"""
X = check_array(X, ensure_2d=True, dtype=np.float64)
# Ensure feature names match
if len(feature_names) != X.shape[1]:
raise ValueError(
f"Feature names length ({len(feature_names)}) "
f"does not match X columns ({X.shape[1]})"
)
# Scale features using classifier's scaler
X_scaled = self.classifier.scaler.transform(X)
try:
explainer = self._init_shap_explainer()
if explainer is None:
raise RuntimeError("SHAP explainer could not be initialized.")
shap_values = explainer.shap_values(X_scaled)
# For binary classification, SHAP returns list of arrays for each class
# We take the SHAP values for the positive class (deception)
if isinstance(shap_values, list) and len(shap_values) == 2:
shap_values = shap_values[1] # Positive class (deception)
elif isinstance(shap_values, np.ndarray) and shap_values.ndim == 3:
shap_values = shap_values[:, :, 1] # Positive class for multi-output
except Exception as e:
warnings.warn(
f"SHAP computation failed: {e}. Falling back to permutation importance.",
RuntimeWarning
)
# Fallback: compute permutation importance as proxy for feature contribution
shap_values = self._compute_fallback_importance(X_scaled, feature_names)
return np.asarray(shap_values, dtype=np.float64)
def _compute_fallback_importance(
self, X_scaled: np.ndarray, feature_names: List[str]
) -> np.ndarray:
"""Compute permutation importance as fallback when SHAP fails.
Args:
X_scaled: Scaled feature matrix.
feature_names: Feature names.
Returns:
np.ndarray: Importance values shaped (n_samples, n_features).
"""
# Use permutation importance to get global feature importance
# Research rationale: permutation importance measures decrease in model
# performance when a feature is randomly shuffled, providing a robust
# alternative to SHAP for feature contribution estimation.
try:
result = permutation_importance(
estimator=self.classifier.model,
X=X_scaled,
y=self.classifier.model.predict(X_scaled), # Use predicted labels
n_repeats=10,
random_state=42,
n_jobs=-1
)
# Broadcast global importance to each sample
importance = result.importances_mean
importance = np.clip(importance, 0, None) # Ensure non-negative
importance = importance / (np.sum(importance) + 1e-10) # Normalize
# Create per-sample importance by adding small noise
# This approximates sample-specific contributions
n_samples = X_scaled.shape[0]
noise = np.random.normal(0, 0.01 * importance.std(), (n_samples, len(importance)))
shap_values = importance.reshape(1, -1) + noise
return shap_values
except Exception:
# Ultimate fallback: uniform importance
n_samples, n_features = X_scaled.shape
return np.ones((n_samples, n_features)) / n_features
def get_feature_importance(
self, X: np.ndarray, feature_names: List[str], top_k: int = 10
) -> pd.DataFrame:
"""Get ranked feature importance combining model importance and SHAP.
Args:
X: Feature matrix of shape (n_samples, n_features).
feature_names: Ordered list of feature names.
top_k: Number of top features to return (default: 10).
Returns:
pd.DataFrame: Columns: ['feature', 'importance', 'shap_importance',
'combined_score', 'rank', 'interpretation']
"""
X = check_array(X, ensure_2d=True, dtype=np.float64)
# 1. Get model's built-in feature importance
try:
model_importance = self.classifier.get_feature_importance()
model_importance_dict = dict(
zip(model_importance['feature'], model_importance['importance'])
)
except Exception:
model_importance_dict = {}
# 2. Compute SHAP importance (mean absolute SHAP value per feature)
shap_values = self.compute_shap_values(X, feature_names)
shap_importance = np.mean(np.abs(shap_values), axis=0)
# 3. Combine scores
combined_scores = []
for i, feat in enumerate(feature_names):
model_imp = model_importance_dict.get(feat, 0.0)
shap_imp = shap_importance[i] if i < len(shap_importance) else 0.0
# Research rationale: weighted average gives more weight to SHAP
# because it captures non-linear interactions better than Gini importance.
combined = 0.3 * model_imp + 0.7 * shap_imp
combined_scores.append({
'feature': feat,
'importance': float(model_imp),
'shap_importance': float(shap_imp),
'combined_score': float(combined),
'interpretation': self.FEATURE_INTERPRETATIONS.get(feat, 'No interpretation available.')
})
df = pd.DataFrame(combined_scores)
df = df.sort_values('combined_score', ascending=False).reset_index(drop=True)
df['rank'] = range(1, len(df) + 1)
# Limit to top_k
if top_k > 0:
df = df.head(top_k)
return df
def get_ranked_indicators(
self, X: np.ndarray, feature_names: List[str], top_k: int = 5
) -> List[Dict]:
"""Get ranked deception indicators for a single sample.
Args:
X: Feature matrix of shape (1, n_features) for a single sample.
feature_names: Ordered list of feature names.
top_k: Number of top indicators to return (default: 5).
Returns:
List[Dict]: Each dict contains:
- 'feature': feature name
- 'value': original feature value
- 'shap_value': SHAP contribution value
- 'abs_contribution': absolute SHAP magnitude
- 'direction': 'toward_deception' or 'toward_truth'
- 'interpretation': behavioral interpretation
"""
if X.ndim != 2:
raise ValueError("X must be 2D. Use shape (1, n_features) for single sample.")
if X.shape[0] != 1:
warnings.warn(
"Multiple samples provided. Using first sample for indicator ranking.",
UserWarning
)
X = X[:1]
# Compute SHAP values for the sample
shap_values = self.compute_shap_values(X, feature_names)
shap_sample = shap_values[0]
# Get original feature values
feature_values = X[0]
# Create indicator dictionaries
indicators = []
for i, feat in enumerate(feature_names):
shap_val = shap_sample[i]
feat_val = feature_values[i]
# Determine direction based on SHAP sign
# Positive SHAP pushes prediction toward deception (class 1)
direction = 'toward_deception' if shap_val > 0 else 'toward_truth'
indicators.append({
'feature': feat,
'value': float(feat_val),
'shap_value': float(shap_val),
'abs_contribution': float(np.abs(shap_val)),
'direction': direction,
'interpretation': self.FEATURE_INTERPRETATIONS.get(feat, 'No interpretation available.')
})
# Sort by absolute contribution (most influential first)
indicators.sort(key=lambda x: x['abs_contribution'], reverse=True)
# Return top_k indicators
return indicators[:top_k]
def explain_prediction(
self,
X: np.ndarray,
feature_names: List[str],
prediction: float,
subject_id: Optional[str] = None
) -> Dict:
"""Generate complete explanation for a single prediction.
Args:
X: Feature matrix of shape (1, n_features) for a single sample.
feature_names: Ordered list of feature names.
prediction: Deception probability (0-1).
subject_id: Optional subject identifier for contextualization.
Returns:
Dict: Explanation dictionary with keys:
- 'verdict': 'truthful' or 'deceptive' based on threshold 0.5
- 'confidence': absolute distance from decision boundary
- 'deception_probability': prediction probability
- 'top_indicators': list of top indicators (from get_ranked_indicators)
- 'group_contributions': dict of SHAP contributions per feature group
- 'behavioral_summary': human-readable summary text
- 'feature_values': dict of original feature values
- 'subject_id': subject identifier if provided
"""
if X.ndim != 2 or X.shape[0] != 1:
raise ValueError("X must be a single sample with shape (1, n_features).")
# Compute SHAP values
shap_values = self.compute_shap_values(X, feature_names)
# Determine verdict
verdict = 'deceptive' if prediction >= 0.5 else 'truthful'
confidence = abs(prediction - 0.5) * 2 # Scale to 0-1
# Get top indicators
top_indicators = self.get_ranked_indicators(X, feature_names, top_k=5)
# Compute group contributions
group_contributions = self.explain_group_contributions(shap_values, feature_names)
# Generate behavioral summary
behavioral_summary = self.generate_behavioral_summary(
top_indicators, prediction, group_contributions
)
# Prepare feature values dictionary
feature_values = {
feat: float(val) for feat, val in zip(feature_names, X[0])
}
return {
'verdict': verdict,
'confidence': float(confidence),
'deception_probability': float(prediction),
'top_indicators': top_indicators,
'group_contributions': group_contributions,
'behavioral_summary': behavioral_summary,
'feature_values': feature_values,
'subject_id': subject_id
}
def generate_behavioral_summary(
self,
top_indicators: List[Dict],
prediction: float,
group_contributions: Dict[str, float]
) -> str:
"""Generate human-readable behavioral explanation paragraph.
Args:
top_indicators: Top indicators from get_ranked_indicators.
prediction: Deception probability (0-1).
group_contributions: Normalized SHAP contributions per feature group.
Returns:
str: Human-readable summary paragraph.
Example:
"The subject shows elevated deception probability (0.82). Key indicators include
increased pause duration (SHAP: +0.12), which suggests cognitive load during
response fabrication, and reduced audio-visual synchrony (SHAP: +0.09),
indicating incongruent verbal and non-verbal behavior."
"""
# Determine deception level
if prediction >= 0.7:
level = "strongly elevated"
elif prediction >= 0.6:
level = "elevated"
elif prediction >= 0.5:
level = "slightly elevated"
elif prediction >= 0.4:
level = "slightly reduced"
elif prediction >= 0.3:
level = "reduced"
else:
level = "strongly reduced"
# Build summary opening
summary_parts = [
f"The subject shows {level} deception probability ({prediction:.2f})."
]
# Add top indicators
if top_indicators:
indicator_desc = []
for idx, ind in enumerate(top_indicators[:3]): # top 3
feat = ind['feature']
shap_val = ind['shap_value']
direction = ind['direction']
interp = self.FEATURE_INTERPRETATIONS.get(feat, '')
# Extract the main behavioral insight (first sentence)
if interp:
insight = interp.split('.')[0] + '.'
else:
insight = "This feature contributes to the prediction."
# Format SHAP value with sign
sign = '+' if shap_val >= 0 else ''
indicator_desc.append(
f"{feat.replace('_', ' ')} (SHAP: {sign}{shap_val:.2f}), "
f"which {insight.lower()}"
)
if indicator_desc:
summary_parts.append("Key indicators include " + ", ".join(indicator_desc) + ".")
# Add group contributions
top_group = max(group_contributions.items(), key=lambda x: x[1]) if group_contributions else None
if top_group:
group_name, contrib = top_group
summary_parts.append(
f"The {group_name} feature group contributed the most ({contrib:.1%}) to the prediction, "
f"suggesting that {group_name.lower()} played a dominant role."
)
# Join into a single paragraph
return " ".join(summary_parts)
def explain_group_contributions(
self, shap_values: np.ndarray, feature_names: List[str]
) -> Dict[str, float]:
"""Aggregate SHAP contribution per feature group, normalized to sum to 1.0.
Args:
shap_values: SHAP values array of shape (n_samples, n_features).
feature_names: Ordered list of feature names.
Returns:
Dict[str, float]: Mapping from group name to normalized contribution.
"""
# Ensure single sample (take first if multiple)
if shap_values.ndim == 2 and shap_values.shape[0] > 1:
shap_values = shap_values[0:1]
shap_abs = np.abs(shap_values).mean(axis=0) # average across samples (usually 1)
# Map feature to group
feature_to_group = {}
for group, features in self.FEATURE_GROUPS.items():
for feat in features:
feature_to_group[feat] = group
# Aggregate contributions
group_sums = {}
for i, feat in enumerate(feature_names):
if i >= len(shap_abs):
continue
group = feature_to_group.get(feat)
if group is None:
# Assign to 'Other' group
group = 'Other'
group_sums[group] = group_sums.get(group, 0.0) + shap_abs[i]
# Normalize to sum to 1.0
total = sum(group_sums.values())
if total > 0:
group_contributions = {k: v / total for k, v in group_sums.items()}
else:
group_contributions = {k: 0.0 for k in group_sums.keys()}
return group_contributions
|