Coverage for source/plotting/classification_testing_plot_responsibility_chain.py: 97%
109 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-23 15:55 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-23 15:55 +0000
1# plotting/classification_testing_plot_responsibility_chain.py
3# global imports
4import logging
5import matplotlib.pyplot as plt
6import numpy as np
7from matplotlib.gridspec import GridSpec
8from sklearn.metrics import RocCurveDisplay
10# local imports
11from source.agent import ClassificationTestingStrategyHandler
12from source.plotting import PlotResponsibilityChainBase
14class ClassificationTestingPlotResponsibilityChain(PlotResponsibilityChainBase):
15 """
16 Implements a plotting responsibility chain for classification testing results.
17 It implements the _can_plot and _plot methods to visualize confusion matrices,
18 classification reports, and ROC curves.
19 """
21 # local constants
22 __ADDITIONAL_REPORT_LABELS = ["accuracy", "macro avg", "weighted avg"]
24 def _can_plot(self, key: str) -> bool:
25 """
26 Checks if the plot can be generated for the given key.
28 Parameters:
29 key (str): The key to check.
31 Returns:
32 (bool): True if the plot can be generated, False otherwise.
33 """
35 return key == ClassificationTestingStrategyHandler.PLOTTING_KEY
37 def _plot(self, plot_data: dict) -> plt.Axes:
38 """
39 Generates the classification testing plot based on the provided data.
41 Parameters:
42 plot_data (dict): The data to be plotted.
44 Returns:
45 (plt.Axes): The axes object containing the plot.
46 """
48 conf_matrix = plot_data.get("confusion_matrix", None)
49 class_report = plot_data.get("classification_report", None)
50 prediction_probabilities = plot_data.get("prediction_probabilities", None)
51 true_labels = plot_data.get("true_labels", None)
53 if conf_matrix is None or class_report is None or prediction_probabilities is None or true_labels is None:
54 logging.warning(f"Insufficient data for plotting results under key: {ClassificationTestingStrategyHandler.PLOTTING_KEY}.")
55 plt.text(0.5, 0.5, "Insufficient data for plotting",
56 ha = 'center', va = 'center', fontsize = 12)
57 return plt.gca()
59 additional_report = {}
60 for additional_label in self.__ADDITIONAL_REPORT_LABELS:
61 if additional_label in class_report:
62 additional_report[additional_label] = class_report.pop(additional_label)
64 fig = plt.figure(figsize = self._EXPECTED_FIGURE_SIZE)
65 gs = GridSpec(2, 2, figure = fig)
66 classes = list(class_report.keys())
67 shortened_classes_names = [class_name[:3] for class_name in classes]
69 # Plot 1: Confusion Matrix as a heatmap
70 ax1 = plt.subplot(gs[0, 0])
71 ax1.set_title(f"Confusion Matrix (Accuracy: {additional_report['accuracy']:.2%})")
73 normalized_conf_matrix = conf_matrix.astype('float') / conf_matrix.sum(axis = 1, keepdims = True)
74 normalized_conf_matrix = np.round(np.nan_to_num(normalized_conf_matrix, nan = 0.0), 2)
75 row_max = normalized_conf_matrix.max(axis = 1)
76 row_min = normalized_conf_matrix.min(axis = 1)
77 color_matrix = (normalized_conf_matrix - row_min[:, np.newaxis]) / (row_max - row_min)[:, np.newaxis]
78 ax1.imshow(color_matrix, interpolation = 'nearest', cmap = plt.cm.GnBu)
80 tick_marks = np.arange(len(classes))
81 ax1.set_xticks(tick_marks)
82 ax1.set_yticks(tick_marks)
83 ax1.set_xticklabels(shortened_classes_names)
84 ax1.set_yticklabels(shortened_classes_names)
85 ax1.set_xlabel('Predicted label')
86 ax1.set_ylabel('True label')
88 for i in range(conf_matrix.shape[0]):
89 for j in range(conf_matrix.shape[1]):
90 color = "white" if color_matrix[i, j] > 0.5 else "black"
91 ax1.text(j, i - 0.1, format(conf_matrix[i, j], 'd'),
92 ha = "center", va = "center", fontsize = 10, weight = 'bold', color = color)
93 ax1.text(j, i + 0.15, f'{normalized_conf_matrix[i, j]:.2f}',
94 ha = "center", va = "center", fontsize = 8, color = color)
96 # Plot 2: Precision, Recall, F1 Score Bar Chart
97 ax2 = plt.subplot(gs[1, 0])
98 precision_scores = []
99 recall_scores = []
100 f1_scores = []
102 for metrics_dict in class_report.values():
103 precision_scores.append(metrics_dict["precision"])
104 recall_scores.append(metrics_dict["recall"])
105 f1_scores.append(metrics_dict["f1-score"])
107 shift = 0.2
108 precision_bars = ax2.bar(tick_marks - shift, precision_scores, shift, label = 'Precision')
109 recall_bars = ax2.bar(tick_marks, recall_scores, shift, label = 'Recall')
110 f1_bars = ax2.bar(tick_marks + shift, f1_scores, shift, label = 'F1-score')
112 for i, (precision_bar, recall_bar, f1_bar) in enumerate(zip(precision_bars, recall_bars, f1_bars)):
113 ax2.text(precision_bar.get_x() + (precision_bar.get_width() / 2),
114 precision_bar.get_height() + 0.01 if precision_bar.get_height() < 0.9 else \
115 precision_bar.get_height() - 0.01, f'{precision_scores[i]:.3f}',
116 ha = 'center', va = 'bottom' if precision_bar.get_height() < 0.9 else 'top', rotation = 90,
117 fontsize = 8, weight = 'bold')
118 ax2.text(recall_bar.get_x() + (recall_bar.get_width() / 2),
119 recall_bar.get_height() + 0.01 if recall_bar.get_height() < 0.9 else \
120 recall_bar.get_height() - 0.01, f'{recall_scores[i]:.3f}',
121 ha = 'center', va = 'bottom' if recall_bar.get_height() < 0.9 else 'top', rotation = 90,
122 fontsize = 8, weight = 'bold')
123 ax2.text(f1_bar.get_x() + (f1_bar.get_width() / 2),
124 f1_bar.get_height() + 0.01 if f1_bar.get_height() < 0.9 else \
125 f1_bar.get_height() - 0.01, f'{f1_scores[i]:.3f}',
126 ha = 'center', va = 'bottom' if f1_bar.get_height() < 0.9 else 'top', rotation = 90,
127 fontsize = 8, weight = 'bold')
129 ax2.set_title('Classification metrics by class')
130 ax2.set_xticks(tick_marks)
131 ax2.set_xticklabels(shortened_classes_names)
132 ax2.set_xlabel('Classes')
133 ax2.set_ylabel('Score')
134 ax2.set_ylim([0, 1])
135 ax2.legend(fontsize = 'x-small')
137 # Plot 3: OvR-ROC curves
138 ax3 = plt.subplot(gs[0, 1])
140 for i, class_name in enumerate(classes):
141 y_true_class_binary = (true_labels == i).astype(int)
142 y_score = prediction_probabilities[:, i]
143 RocCurveDisplay.from_predictions(y_true_class_binary, y_score, name = f"{class_name}",
144 ax = ax3, plot_chance_level = (i == len(classes) - 1))
146 ax3.set_title('One-vs-Rest ROC curves')
147 ax3.set_xlabel('False positive rate')
148 ax3.set_ylabel('True positive rate')
149 ax3.grid(alpha = 0.3)
150 ax3.legend(loc = "lower right", fontsize = 'x-small')
151 plt.tight_layout()
153 # Plot 4: Macro avg and weighted avg
154 ax4 = plt.subplot(gs[1, 1])
155 additional_labels = list(additional_report.keys())[1:]
156 precision_scores = []
157 recall_scores = []
158 f1_scores = []
160 for metrics in additional_report.values():
161 if isinstance(metrics, dict):
162 precision_scores.append(metrics['precision'])
163 recall_scores.append(metrics['recall'])
164 f1_scores.append(metrics['f1-score'])
166 x = np.arange(len(additional_labels))
167 precision_bars = ax4.bar(x - shift, precision_scores, shift, label = 'Precision')
168 recall_bars = ax4.bar(x, recall_scores, shift, label = 'Recall')
169 f1_bars = ax4.bar(x + shift, f1_scores, shift, label = 'F1-score')
171 for i, (precision_bar, recall_bar, f1_bar) in enumerate(zip(precision_bars, recall_bars, f1_bars)):
172 ax4.text(precision_bar.get_x() + (precision_bar.get_width() / 2),
173 precision_bar.get_height() + 0.01 if precision_bar.get_height() < 0.9 else \
174 precision_bar.get_height() - 0.01, f'{precision_scores[i]:.3f}',
175 ha = 'center', va = 'bottom' if precision_bar.get_height() < 0.9 else 'top', rotation = 90,
176 fontsize = 8, weight = 'bold')
177 ax4.text(recall_bar.get_x() + (recall_bar.get_width() / 2),
178 recall_bar.get_height() + 0.01 if recall_bar.get_height() < 0.9 else \
179 recall_bar.get_height() - 0.01, f'{recall_scores[i]:.3f}',
180 ha = 'center', va = 'bottom' if recall_bar.get_height() < 0.9 else 'top', rotation = 90,
181 fontsize = 8, weight = 'bold')
182 ax4.text(f1_bar.get_x() + (f1_bar.get_width() / 2),
183 f1_bar.get_height() + 0.01 if f1_bar.get_height() < 0.9 else \
184 f1_bar.get_height() - 0.01, f'{f1_scores[i]:.3f}',
185 ha = 'center', va = 'bottom' if f1_bar.get_height() < 0.9 else 'top', rotation = 90,
186 fontsize = 8, weight = 'bold')
188 ax4.set_title('Macro avg and weighted avg')
189 ax4.set_xticks(x)
190 ax4.set_xticklabels(additional_labels)
191 ax4.set_xlabel('Metrics')
192 ax4.set_ylabel('Score')
193 ax4.set_ylim([0, 1])
194 ax4.legend(fontsize = 'x-small')
195 plt.tight_layout()
197 return plt.gca()