|
| 1 | +import pytest |
| 2 | +import numpy as np |
| 3 | +from sklearn.datasets import make_classification |
| 4 | +from sklearn.model_selection import train_test_split |
| 5 | +from sklearn.ensemble import RandomForestClassifier |
| 6 | +from matplotlib import pyplot as plt |
| 7 | + |
| 8 | +from random_forest_package.visualizer import ModelVisualizer |
| 9 | +from random_forest_package.exceptions import VisualizationError |
| 10 | + |
| 11 | + |
| 12 | +# Fixture to create a simple classification dataset |
| 13 | +@pytest.fixture(scope='module') |
| 14 | +def classification_data(): |
| 15 | + X, y = make_classification(n_samples=100, n_features=20, n_classes=2, random_state=42) |
| 16 | + return train_test_split(X, y, test_size=0.3, random_state=42) |
| 17 | + |
| 18 | + |
| 19 | +# Fixture to create a trained RandomForestClassifierModel |
| 20 | +@pytest.fixture(scope='module') |
| 21 | +def trained_classifier(classification_data): |
| 22 | + X_train, X_test, y_train, y_test = classification_data |
| 23 | + model = RandomForestClassifier(random_state=42) |
| 24 | + model.fit(X_train, y_train) |
| 25 | + return model, X_test, y_test |
| 26 | + |
| 27 | + |
| 28 | +# Tests for plot_confusion_matrix |
| 29 | +def test_plot_confusion_matrix_normal(trained_classifier): |
| 30 | + model, X_test, y_test = trained_classifier |
| 31 | + visualizer = ModelVisualizer(model) |
| 32 | + |
| 33 | + try: |
| 34 | + visualizer.plot_confusion_matrix(X_test, y_test) |
| 35 | + plt.close() |
| 36 | + except Exception as e: |
| 37 | + pytest.fail(f"Unexpected error: {e}") |
| 38 | + |
| 39 | + |
| 40 | +def test_plot_confusion_matrix_with_normalization(trained_classifier): |
| 41 | + model, X_test, y_test = trained_classifier |
| 42 | + visualizer = ModelVisualizer(model) |
| 43 | + |
| 44 | + try: |
| 45 | + visualizer.plot_confusion_matrix(X_test, y_test, normalize=True) |
| 46 | + plt.close() |
| 47 | + except Exception as e: |
| 48 | + pytest.fail(f"Unexpected error: {e}") |
| 49 | + |
| 50 | + |
| 51 | +def test_plot_confusion_matrix_with_invalid_input(trained_classifier): |
| 52 | + model, _, _ = trained_classifier |
| 53 | + visualizer = ModelVisualizer(model) |
| 54 | + |
| 55 | + with pytest.raises(VisualizationError): |
| 56 | + visualizer.plot_confusion_matrix(None, None) |
| 57 | + |
| 58 | + |
| 59 | +# Tests for plot_roc_curve |
| 60 | +def test_plot_roc_curve_normal(trained_classifier): |
| 61 | + model, X_test, y_test = trained_classifier |
| 62 | + visualizer = ModelVisualizer(model) |
| 63 | + |
| 64 | + try: |
| 65 | + visualizer.plot_roc_curve(X_test, y_test) |
| 66 | + plt.close() |
| 67 | + except Exception as e: |
| 68 | + pytest.fail(f"Unexpected error: {e}") |
| 69 | + |
| 70 | + |
| 71 | +def test_plot_roc_curve_with_invalid_input(trained_classifier): |
| 72 | + model, _, _ = trained_classifier |
| 73 | + visualizer = ModelVisualizer(model) |
| 74 | + |
| 75 | + with pytest.raises(VisualizationError): |
| 76 | + visualizer.plot_roc_curve(None, None) |
| 77 | + |
| 78 | + |
| 79 | +# Tests for plot_precision_recall_curve |
| 80 | +def test_plot_precision_recall_curve_normal(trained_classifier): |
| 81 | + model, X_test, y_test = trained_classifier |
| 82 | + visualizer = ModelVisualizer(model) |
| 83 | + |
| 84 | + try: |
| 85 | + visualizer.plot_precision_recall_curve(X_test, y_test) |
| 86 | + plt.close() |
| 87 | + except Exception as e: |
| 88 | + pytest.fail(f"Unexpected error: {e}") |
| 89 | + |
| 90 | + |
| 91 | +def test_plot_precision_recall_curve_with_invalid_input(trained_classifier): |
| 92 | + model, _, _ = trained_classifier |
| 93 | + visualizer = ModelVisualizer(model) |
| 94 | + |
| 95 | + with pytest.raises(VisualizationError): |
| 96 | + visualizer.plot_precision_recall_curve(None, None) |
| 97 | + |
| 98 | + |
| 99 | +def test_plot_precision_recall_curve_with_single_class(classification_data): |
| 100 | + X_train, X_test, y_train, y_test = classification_data |
| 101 | + y_train_single_class = np.zeros_like(y_train) |
| 102 | + |
| 103 | + model = RandomForestClassifier(random_state=42) |
| 104 | + model.fit(X_train, y_train_single_class) |
| 105 | + |
| 106 | + visualizer = ModelVisualizer(model) |
| 107 | + |
| 108 | + try: |
| 109 | + visualizer.plot_precision_recall_curve(X_test, y_test) |
| 110 | + plt.close() |
| 111 | + except VisualizationError: |
| 112 | + pass # Expected outcome |
| 113 | + except Exception as e: |
| 114 | + pytest.fail(f"Unexpected error: {e}") |
0 commit comments