|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +from dython.model_utils import ks_abc |
| 5 | + |
| 6 | + |
| 7 | +def test_ks_abc_basic(): |
| 8 | + """Test basic ks_abc functionality""" |
| 9 | + y_true = [0, 1, 0, 1, 1, 0] |
| 10 | + y_pred = [0.1, 0.9, 0.3, 0.8, 0.7, 0.2] |
| 11 | + |
| 12 | + result = ks_abc(y_true, y_pred, plot=False) |
| 13 | + assert 'abc' in result |
| 14 | + assert 'ks_stat' in result |
| 15 | + assert 'eopt' in result |
| 16 | + assert 'ax' in result |
| 17 | + |
| 18 | + |
| 19 | +def test_ks_abc_mismatched_shapes(): |
| 20 | + """Test ks_abc with mismatched shapes""" |
| 21 | + y_true = [0, 1, 0] |
| 22 | + y_pred = [0.1, 0.9, 0.3, 0.8] |
| 23 | + |
| 24 | + with pytest.raises(ValueError): |
| 25 | + ks_abc(y_true, y_pred, plot=False) |
| 26 | + |
| 27 | + |
| 28 | +def test_ks_abc_2d_binary(): |
| 29 | + """Test ks_abc with 2D binary array""" |
| 30 | + y_true = np.array([[1, 0], [0, 1], [1, 0], [0, 1]]) |
| 31 | + y_pred = np.array([[0.7, 0.3], [0.2, 0.8], [0.6, 0.4], [0.3, 0.7]]) |
| 32 | + |
| 33 | + result = ks_abc(y_true, y_pred, plot=False) |
| 34 | + assert 'abc' in result |
| 35 | + |
| 36 | + |
| 37 | +def test_ks_abc_single_column(): |
| 38 | + """Test ks_abc with single column""" |
| 39 | + y_true = np.array([[1], [0], [1], [0]]) |
| 40 | + y_pred = np.array([[0.9], [0.2], [0.7], [0.3]]) |
| 41 | + |
| 42 | + result = ks_abc(y_true, y_pred, plot=False) |
| 43 | + assert 'abc' in result |
| 44 | + |
| 45 | + |
| 46 | +def test_ks_abc_multiclass_error(): |
| 47 | + """Test ks_abc with multiclass (should raise error)""" |
| 48 | + y_true = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 49 | + y_pred = np.array([[0.7, 0.2, 0.1], [0.1, 0.8, 0.1], [0.2, 0.1, 0.7]]) |
| 50 | + |
| 51 | + with pytest.raises(ValueError): |
| 52 | + ks_abc(y_true, y_pred, plot=False) |
| 53 | + |
| 54 | + |
| 55 | +def test_ks_abc_with_ax(): |
| 56 | + """Test ks_abc with provided ax""" |
| 57 | + y_true = [0, 1, 0, 1] |
| 58 | + y_pred = [0.1, 0.9, 0.3, 0.8] |
| 59 | + |
| 60 | + fig, ax = plt.subplots() |
| 61 | + result = ks_abc(y_true, y_pred, ax=ax, plot=False) |
| 62 | + assert result['ax'] == ax |
| 63 | + plt.close(fig) |
| 64 | + |
| 65 | + |
| 66 | +def test_ks_abc_with_custom_params(): |
| 67 | + """Test ks_abc with custom visualization parameters""" |
| 68 | + y_true = [0, 1, 0, 1, 1, 0] |
| 69 | + y_pred = [0.1, 0.9, 0.3, 0.8, 0.7, 0.2] |
| 70 | + |
| 71 | + result = ks_abc( |
| 72 | + y_true, y_pred, |
| 73 | + colors=('red', 'blue'), |
| 74 | + title='Custom KS Title', |
| 75 | + xlim=(0, 0.5), |
| 76 | + ylim=(0, 0.5), |
| 77 | + fmt='.3f', |
| 78 | + lw=3, |
| 79 | + legend='upper left', |
| 80 | + plot=False |
| 81 | + ) |
| 82 | + assert 'abc' in result |
| 83 | + |
| 84 | + |
| 85 | +def test_ks_abc_no_legend(): |
| 86 | + """Test ks_abc without legend""" |
| 87 | + y_true = [0, 1, 0, 1] |
| 88 | + y_pred = [0.1, 0.9, 0.3, 0.8] |
| 89 | + |
| 90 | + result = ks_abc(y_true, y_pred, legend=None, plot=False) |
| 91 | + assert 'ax' in result |
| 92 | + |
| 93 | + |
| 94 | +def test_ks_abc_with_filename(tmp_path): |
| 95 | + """Test ks_abc with filename""" |
| 96 | + y_true = [0, 1, 0, 1] |
| 97 | + y_pred = [0.1, 0.9, 0.3, 0.8] |
| 98 | + |
| 99 | + filename = tmp_path / "ks_plot.png" |
| 100 | + result = ks_abc(y_true, y_pred, filename=str(filename), plot=False) |
| 101 | + assert filename.exists() |
| 102 | + plt.close('all') |
| 103 | + |
| 104 | + |
| 105 | +def test_ks_abc_abc_value_range(): |
| 106 | + """Test that ABC value is in valid range""" |
| 107 | + y_true = [0, 1, 0, 1, 1, 0, 0, 1] |
| 108 | + y_pred = [0.1, 0.9, 0.2, 0.8, 0.7, 0.3, 0.15, 0.85] |
| 109 | + |
| 110 | + result = ks_abc(y_true, y_pred, plot=False) |
| 111 | + assert 0 <= result['abc'] <= 1 |
| 112 | + |
| 113 | + |
| 114 | +def test_ks_abc_ks_stat_value_range(): |
| 115 | + """Test that KS statistic is in valid range""" |
| 116 | + y_true = [0, 1, 0, 1, 1, 0, 0, 1] |
| 117 | + y_pred = [0.1, 0.9, 0.2, 0.8, 0.7, 0.3, 0.15, 0.85] |
| 118 | + |
| 119 | + result = ks_abc(y_true, y_pred, plot=False) |
| 120 | + assert 0 <= result['ks_stat'] <= 1 |
| 121 | + |
0 commit comments