Spaces:
Sleeping
Sleeping
File size: 2,369 Bytes
3efedb0 |
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 |
import unittest
import pandas as pd
import numpy as np
import os
import sys
# Add the src directory to the path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from src.models.loan_model import LoanApprovalModel
from src.explainers.shap_explainer import ShapExplainer
class TestShapExplainer(unittest.TestCase):
def setUp(self):
"""Set up test fixtures"""
self.model = LoanApprovalModel()
self.explainer = ShapExplainer(self.model)
# Create sample data
self.sample_data = pd.DataFrame({
"income": [50000],
"credit_score": [720],
"debt_to_income": [25],
"loan_amount": [200000],
"loan_term": [30],
"employment_length": [5],
"home_ownership": ["OWN"],
"loan_purpose": ["HOME"]
})
def test_generate_explanation(self):
"""Test explanation generation"""
explanation = self.explainer.generate_explanation(self.sample_data)
# Check that explanation is a string
self.assertIsInstance(explanation, str)
# Check that explanation contains key features
self.assertTrue(any(feature in explanation.lower() for feature in
["income", "credit score", "debt", "loan"]))
def test_get_feature_importance(self):
"""Test feature importance calculation"""
importance = self.explainer.get_feature_importance(self.sample_data)
# Check that importance is a list of dictionaries
self.assertIsInstance(importance, list)
self.assertIsInstance(importance[0], dict)
# Check that each dictionary has feature and importance keys
self.assertTrue(all("feature" in item and "importance" in item for item in importance))
# Check that importance values are between -1 and 1
self.assertTrue(all(-1 <= item["importance"] <= 1 for item in importance))
def test_generate_plot(self):
"""Test plot generation"""
plot_data = self.explainer.generate_plot(self.sample_data)
# Check that plot data is returned
self.assertIsNotNone(plot_data)
if __name__ == "__main__":
unittest.main() |