Spaces:
Sleeping
Sleeping
File size: 7,457 Bytes
8a08300 |
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 |
"""
Tests for SHAP Explainability Engine.
Integration tests verifying SHAP functionality with trained models.
"""
import base64
import tempfile
from pathlib import Path
import joblib
import numpy as np
import pandas as pd
import pytest
from src.explainability import FraudExplainer
from src.models.pipeline import create_fraud_pipeline
@pytest.fixture
def sample_transaction():
"""Create a valid sample transaction for testing."""
return pd.DataFrame(
[
{
"trans_date_trans_time": "2020-01-01 12:00:00",
"amt": 150.0,
"lat": 40.7128,
"long": -74.0060,
"merch_lat": 40.7200,
"merch_long": -74.0100,
"job": "Engineer, biomedical",
"category": "grocery_pos",
"gender": "M",
"dob": "1990-01-01",
"trans_count_24h": 3,
"amt_to_avg_ratio_24h": 1.2,
"amt_relative_to_all_time": 1.1,
}
]
)
@pytest.fixture
def trained_pipeline():
"""Create a minimal trained pipeline for testing."""
# Create and quickly train a pipeline
params = {"max_depth": 3, "n_estimators": 10, "learning_rate": 0.3}
pipeline = create_fraud_pipeline(params)
# Generate minimal training data
np.random.seed(42)
n_samples = 100
X_train = pd.DataFrame(
{
"trans_date_trans_time": pd.date_range("2019-01-01", periods=n_samples, freq="h"),
"amt": np.random.uniform(10, 500, n_samples),
"lat": np.random.uniform(30, 45, n_samples),
"long": np.random.uniform(-120, -70, n_samples),
"merch_lat": np.random.uniform(30, 45, n_samples),
"merch_long": np.random.uniform(-120, -70, n_samples),
"job": np.random.choice(["Engineer, biomedical", "Data scientist"], n_samples),
"category": np.random.choice(["grocery_pos", "gas_transport"], n_samples),
"gender": np.random.choice(["M", "F"], n_samples),
"dob": ["1990-01-01"] * n_samples,
"trans_count_24h": np.random.randint(1, 10, n_samples),
"amt_to_avg_ratio_24h": np.random.uniform(0.5, 2.0, n_samples),
"amt_relative_to_all_time": np.random.uniform(0.5, 2.0, n_samples),
}
)
y_train = np.random.randint(0, 2, n_samples)
# Train pipeline
pipeline.fit(X_train, y_train)
# Save to temp file
with tempfile.NamedTemporaryFile(delete=False, suffix=".pkl") as f:
joblib.dump(pipeline, f.name)
temp_path = f.name
yield temp_path
# Cleanup
Path(temp_path).unlink()
class TestFraudExplainer:
"""Test suite for FraudExplainer class."""
def test_initialization(self, trained_pipeline):
"""Test that explainer initializes without errors."""
explainer = FraudExplainer(trained_pipeline)
assert explainer.pipeline is not None
assert explainer.model is not None
assert explainer.preprocessor is not None
assert explainer.explainer is not None
assert len(explainer.feature_names) > 0
def test_initialization_invalid_path(self):
"""Test that explainer raises error for invalid path."""
with pytest.raises(FileNotFoundError):
FraudExplainer("/nonexistent/path.pkl")
def test_generate_waterfall(self, trained_pipeline, sample_transaction):
"""Test waterfall plot generation for a single transaction."""
explainer = FraudExplainer(trained_pipeline)
# Generate waterfall (base64)
waterfall_b64 = explainer.generate_waterfall(sample_transaction)
# Verify it's a valid base64 string
assert isinstance(waterfall_b64, str)
assert len(waterfall_b64) > 0
# Verify it decodes to valid bytes
try:
decoded = base64.b64decode(waterfall_b64)
assert len(decoded) > 0
except Exception as e:
pytest.fail(f"Failed to decode base64: {e}")
def test_generate_waterfall_multiple_transactions_fails(
self, trained_pipeline, sample_transaction
):
"""Test that waterfall fails with multiple transactions."""
explainer = FraudExplainer(trained_pipeline)
# Create 2 transactions
two_transactions = pd.concat([sample_transaction, sample_transaction])
with pytest.raises(ValueError, match="Expected 1 transaction"):
explainer.generate_waterfall(two_transactions)
def test_generate_summary(self, trained_pipeline, sample_transaction):
"""Test summary plot generation for multiple transactions."""
explainer = FraudExplainer(trained_pipeline)
# Create sample of 10 transactions
sample = pd.concat([sample_transaction] * 10, ignore_index=True)
# Generate summary plot
summary_b64 = explainer.generate_summary(sample)
# Verify it's a valid base64 string
assert isinstance(summary_b64, str)
assert len(summary_b64) > 0
# Verify it decodes
try:
decoded = base64.b64decode(summary_b64)
assert len(decoded) > 0
except Exception as e:
pytest.fail(f"Failed to decode base64: {e}")
def test_explain_prediction(self, trained_pipeline, sample_transaction):
"""Test comprehensive prediction explanation."""
explainer = FraudExplainer(trained_pipeline)
explanation = explainer.explain_prediction(sample_transaction, threshold=0.5)
# Verify structure
assert "prediction" in explanation
assert "decision" in explanation
assert "shap_values" in explanation
assert "top_features" in explanation
assert "base_value" in explanation
# Verify types
assert isinstance(explanation["prediction"], float)
assert explanation["decision"] in ["BLOCK", "APPROVE"]
assert isinstance(explanation["shap_values"], dict)
assert isinstance(explanation["top_features"], list)
assert len(explanation["top_features"]) == 5
# Verify top_features structure
for feature in explanation["top_features"]:
assert "feature" in feature
assert "impact" in feature
assert "abs_impact" in feature
def test_no_value_error_raised(self, trained_pipeline, sample_transaction):
"""Test that no ValueError is raised during normal operation."""
explainer = FraudExplainer(trained_pipeline)
# This should not raise ValueError
try:
waterfall = explainer.generate_waterfall(sample_transaction)
summary = explainer.generate_summary(sample_transaction)
explanation = explainer.explain_prediction(sample_transaction)
except ValueError as e:
pytest.fail(f"Unexpected ValueError raised: {e}")
def test_shap_values_calculation(self, trained_pipeline, sample_transaction):
"""Test SHAP value calculation."""
explainer = FraudExplainer(trained_pipeline)
shap_values, X_transformed = explainer.calculate_shap_values(sample_transaction)
# Verify shapes
assert shap_values.shape[0] == 1 # 1 transaction
assert shap_values.shape[1] == len(explainer.feature_names)
assert X_transformed.shape[0] == 1
assert X_transformed.shape[1] == len(explainer.feature_names)
|