File size: 9,700 Bytes
6de2f28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
SmartCertify ML β€” Visualization Utilities
Matplotlib/Seaborn plotting functions for model evaluation and data analysis.
"""

import matplotlib
matplotlib.use("Agg")  # Non-interactive backend for server use

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from pathlib import Path
from typing import Dict, List, Optional
from sklearn.metrics import confusion_matrix, roc_curve, auc, precision_recall_curve

from app.config.settings import PLOTS_DIR


def _save_plot(fig: plt.Figure, filename: str) -> str:
    """Save a figure to the plots directory and return the path."""
    path = PLOTS_DIR / filename
    fig.savefig(path, dpi=150, bbox_inches="tight", facecolor="white")
    plt.close(fig)
    return str(path)


def plot_class_distribution(labels: np.ndarray, filename: str = "class_distribution.png") -> str:
    """Plot and save class distribution bar chart."""
    fig, ax = plt.subplots(figsize=(8, 5))
    unique, counts = np.unique(labels, return_counts=True)
    colors = ["#2ecc71", "#e74c3c"]
    label_names = ["Authentic", "Fraudulent"]

    bars = ax.bar(label_names[:len(unique)], counts, color=colors[:len(unique)], edgecolor="white", linewidth=1.5)
    for bar, count in zip(bars, counts):
        ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height() + 50,
                f"{count}\n({count/sum(counts)*100:.1f}%)",
                ha="center", va="bottom", fontweight="bold", fontsize=12)

    ax.set_title("Certificate Class Distribution", fontsize=16, fontweight="bold", pad=15)
    ax.set_ylabel("Count", fontsize=13)
    ax.spines[["top", "right"]].set_visible(False)
    ax.set_ylim(0, max(counts) * 1.2)
    return _save_plot(fig, filename)


def plot_correlation_heatmap(df: pd.DataFrame, filename: str = "correlation_heatmap.png") -> str:
    """Plot and save feature correlation heatmap."""
    numeric_df = df.select_dtypes(include=[np.number])
    corr = numeric_df.corr()

    fig, ax = plt.subplots(figsize=(14, 10))
    mask = np.triu(np.ones_like(corr, dtype=bool))
    sns.heatmap(corr, mask=mask, annot=True, fmt=".2f", cmap="RdBu_r",
                center=0, square=True, linewidths=0.5, ax=ax,
                cbar_kws={"shrink": 0.8})
    ax.set_title("Feature Correlation Heatmap", fontsize=16, fontweight="bold", pad=15)
    return _save_plot(fig, filename)


def plot_confusion_matrix(
    y_true: np.ndarray,
    y_pred: np.ndarray,
    model_name: str,
    filename: Optional[str] = None,
) -> str:
    """Plot and save confusion matrix."""
    cm = confusion_matrix(y_true, y_pred)
    fig, ax = plt.subplots(figsize=(7, 6))
    sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", ax=ax,
                xticklabels=["Authentic", "Fraud"],
                yticklabels=["Authentic", "Fraud"],
                linewidths=1, linecolor="white")
    ax.set_xlabel("Predicted", fontsize=13)
    ax.set_ylabel("Actual", fontsize=13)
    ax.set_title(f"Confusion Matrix β€” {model_name}", fontsize=15, fontweight="bold", pad=12)

    fname = filename or f"confusion_matrix_{model_name.lower().replace(' ', '_')}.png"
    return _save_plot(fig, fname)


def plot_roc_curve(
    y_true: np.ndarray,
    y_proba: np.ndarray,
    model_name: str,
    filename: Optional[str] = None,
) -> str:
    """Plot and save ROC-AUC curve."""
    fpr, tpr, _ = roc_curve(y_true, y_proba)
    roc_auc = auc(fpr, tpr)

    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot(fpr, tpr, color="#3498db", lw=2.5, label=f"{model_name} (AUC = {roc_auc:.4f})")
    ax.plot([0, 1], [0, 1], color="#bdc3c7", lw=1.5, linestyle="--", label="Random")
    ax.fill_between(fpr, tpr, alpha=0.15, color="#3498db")
    ax.set_xlabel("False Positive Rate", fontsize=13)
    ax.set_ylabel("True Positive Rate", fontsize=13)
    ax.set_title(f"ROC Curve β€” {model_name}", fontsize=15, fontweight="bold", pad=12)
    ax.legend(loc="lower right", fontsize=11)
    ax.spines[["top", "right"]].set_visible(False)

    fname = filename or f"roc_curve_{model_name.lower().replace(' ', '_')}.png"
    return _save_plot(fig, fname)


def plot_precision_recall_curve(
    y_true: np.ndarray,
    y_proba: np.ndarray,
    model_name: str,
    filename: Optional[str] = None,
) -> str:
    """Plot and save Precision-Recall curve."""
    precision, recall, _ = precision_recall_curve(y_true, y_proba)

    fig, ax = plt.subplots(figsize=(8, 6))
    ax.plot(recall, precision, color="#e74c3c", lw=2.5, label=model_name)
    ax.fill_between(recall, precision, alpha=0.15, color="#e74c3c")
    ax.set_xlabel("Recall", fontsize=13)
    ax.set_ylabel("Precision", fontsize=13)
    ax.set_title(f"Precision-Recall Curve β€” {model_name}", fontsize=15, fontweight="bold", pad=12)
    ax.legend(loc="lower left", fontsize=11)
    ax.spines[["top", "right"]].set_visible(False)

    fname = filename or f"pr_curve_{model_name.lower().replace(' ', '_')}.png"
    return _save_plot(fig, fname)


def plot_multi_roc(
    results: Dict[str, Dict],
    filename: str = "multi_roc_comparison.png",
) -> str:
    """Plot ROC curves for multiple models on the same figure."""
    fig, ax = plt.subplots(figsize=(10, 7))
    colors = sns.color_palette("husl", len(results))

    for (name, data), color in zip(results.items(), colors):
        fpr, tpr, _ = roc_curve(data["y_true"], data["y_proba"])
        roc_auc = auc(fpr, tpr)
        ax.plot(fpr, tpr, color=color, lw=2, label=f"{name} (AUC={roc_auc:.3f})")

    ax.plot([0, 1], [0, 1], "k--", lw=1, alpha=0.5)
    ax.set_xlabel("False Positive Rate", fontsize=13)
    ax.set_ylabel("True Positive Rate", fontsize=13)
    ax.set_title("ROC Curve Comparison β€” All Models", fontsize=15, fontweight="bold", pad=12)
    ax.legend(loc="lower right", fontsize=10)
    ax.spines[["top", "right"]].set_visible(False)
    return _save_plot(fig, filename)


def plot_learning_curves(
    train_losses: List[float],
    val_losses: List[float],
    train_accs: Optional[List[float]] = None,
    val_accs: Optional[List[float]] = None,
    filename: str = "nn_learning_curve.png",
) -> str:
    """Plot neural network learning curves (loss and optional accuracy)."""
    n_plots = 2 if train_accs is not None else 1
    fig, axes = plt.subplots(1, n_plots, figsize=(7 * n_plots, 5))
    if n_plots == 1:
        axes = [axes]

    epochs = range(1, len(train_losses) + 1)

    axes[0].plot(epochs, train_losses, "b-", lw=2, label="Train Loss")
    axes[0].plot(epochs, val_losses, "r-", lw=2, label="Val Loss")
    axes[0].set_xlabel("Epoch", fontsize=12)
    axes[0].set_ylabel("Loss", fontsize=12)
    axes[0].set_title("Training & Validation Loss", fontsize=14, fontweight="bold")
    axes[0].legend(fontsize=11)
    axes[0].spines[["top", "right"]].set_visible(False)

    if train_accs is not None:
        axes[1].plot(epochs, train_accs, "b-", lw=2, label="Train Acc")
        axes[1].plot(epochs, val_accs, "r-", lw=2, label="Val Acc")
        axes[1].set_xlabel("Epoch", fontsize=12)
        axes[1].set_ylabel("Accuracy", fontsize=12)
        axes[1].set_title("Training & Validation Accuracy", fontsize=14, fontweight="bold")
        axes[1].legend(fontsize=11)
        axes[1].spines[["top", "right"]].set_visible(False)

    fig.tight_layout()
    return _save_plot(fig, filename)


def plot_feature_importance(
    importances: np.ndarray,
    feature_names: List[str],
    model_name: str,
    top_n: int = 15,
    filename: Optional[str] = None,
) -> str:
    """Plot and save feature importance bar chart."""
    indices = np.argsort(importances)[-top_n:]
    fig, ax = plt.subplots(figsize=(10, 7))
    ax.barh(range(len(indices)), importances[indices], color="#3498db", edgecolor="white")
    ax.set_yticks(range(len(indices)))
    ax.set_yticklabels([feature_names[i] for i in indices], fontsize=11)
    ax.set_xlabel("Importance", fontsize=13)
    ax.set_title(f"Top {top_n} Feature Importances β€” {model_name}", fontsize=15, fontweight="bold", pad=12)
    ax.spines[["top", "right"]].set_visible(False)

    fname = filename or f"feature_importance_{model_name.lower().replace(' ', '_')}.png"
    return _save_plot(fig, fname)


def plot_anomaly_distribution(
    scores: np.ndarray,
    threshold: float,
    filename: str = "anomaly_distribution.png",
) -> str:
    """Plot anomaly score distribution."""
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.hist(scores, bins=60, color="#3498db", alpha=0.7, edgecolor="white", label="Anomaly Scores")
    ax.axvline(threshold, color="#e74c3c", lw=2, linestyle="--", label=f"Threshold ({threshold:.3f})")
    ax.set_xlabel("Anomaly Score", fontsize=13)
    ax.set_ylabel("Frequency", fontsize=13)
    ax.set_title("Anomaly Score Distribution", fontsize=15, fontweight="bold", pad=12)
    ax.legend(fontsize=11)
    ax.spines[["top", "right"]].set_visible(False)
    return _save_plot(fig, filename)


def plot_pca_variance(
    explained_variance: np.ndarray,
    filename: str = "pca_explained_variance.png",
) -> str:
    """Plot PCA explained variance ratio."""
    cumulative = np.cumsum(explained_variance)
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.bar(range(1, len(explained_variance) + 1), explained_variance,
           alpha=0.7, color="#3498db", label="Individual", edgecolor="white")
    ax.step(range(1, len(explained_variance) + 1), cumulative,
            where="mid", color="#e74c3c", lw=2, label="Cumulative")
    ax.set_xlabel("Principal Component", fontsize=13)
    ax.set_ylabel("Explained Variance Ratio", fontsize=13)
    ax.set_title("PCA Explained Variance", fontsize=15, fontweight="bold", pad=12)
    ax.legend(fontsize=11)
    ax.spines[["top", "right"]].set_visible(False)
    return _save_plot(fig, filename)