|
| 1 | +from typing import TYPE_CHECKING, Any |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from benchmarks.benchmark import Benchmark |
| 8 | + |
| 9 | + |
| 10 | +################################################################################### |
| 11 | +### Conversion Functions ########################################################## |
| 12 | +################################################################################### |
| 13 | +def convert_to_dataframe_from_benchmark(benchmark: "Benchmark") -> tuple: |
| 14 | + data = { |
| 15 | + "cache_hit_list": benchmark.cache_hit_list, |
| 16 | + "cache_miss_list": benchmark.cache_miss_list, |
| 17 | + "tp_list": benchmark.tp_list, |
| 18 | + "fp_list": benchmark.fp_list, |
| 19 | + "tn_list": benchmark.tn_list, |
| 20 | + "fn_list": benchmark.fn_list, |
| 21 | + "latency_direct_list": benchmark.latency_direct_list, |
| 22 | + "latency_vectorq_list": benchmark.latency_vectorq_list, |
| 23 | + } |
| 24 | + df = pd.DataFrame(data) |
| 25 | + |
| 26 | + metadata = { |
| 27 | + "observations_dict": benchmark.observations_dict, |
| 28 | + "gammas_dict": benchmark.gammas_dict, |
| 29 | + } |
| 30 | + |
| 31 | + return df, metadata |
| 32 | + |
| 33 | + |
| 34 | +def convert_to_dataframe_from_json_file(json_data: Any) -> tuple: |
| 35 | + data = { |
| 36 | + "cache_hit_list": json_data["cache_hit_list"], |
| 37 | + "cache_miss_list": json_data["cache_miss_list"], |
| 38 | + "tp_list": json_data["tp_list"], |
| 39 | + "fp_list": json_data["fp_list"], |
| 40 | + "tn_list": json_data["tn_list"], |
| 41 | + "fn_list": json_data["fn_list"], |
| 42 | + "latency_direct_list": json_data["latency_direct_list"], |
| 43 | + "latency_vectorq_list": json_data["latency_vectorq_list"], |
| 44 | + } |
| 45 | + df = pd.DataFrame(data) |
| 46 | + |
| 47 | + metadata = { |
| 48 | + "observations_dict": json_data["observations_dict"], |
| 49 | + "gammas_dict": json_data["gammas_dict"], |
| 50 | + } |
| 51 | + |
| 52 | + return df, metadata |
| 53 | + |
| 54 | + |
| 55 | +################################################################################### |
| 56 | +### Stat Functions ################################################################ |
| 57 | +################################################################################### |
| 58 | +def __cumulative_average_stats(data: pd.DataFrame) -> pd.DataFrame: |
| 59 | + """ |
| 60 | + Compute the cumulative average stats of <data>. |
| 61 | + Args: |
| 62 | + data: pd.DataFrame - Data [0, 1, 2, 3, 4, 5, ...] |
| 63 | + Returns: |
| 64 | + cumulative_data: pd.DataFrame - Cumulative Data [0/1, 1/2, 3/3, 6/4, 10/5, 15/6, ...] |
| 65 | + Example: |
| 66 | + data = [0.5, 1.0, 1.0, 0.0, ...] # Accuracy |
| 67 | + cumulative_data = [0.5/1, 1.5/2, 2.5/3, 2.5/4, 2.5/5, 3.5/6, ...] |
| 68 | + """ |
| 69 | + return data.cumsum() / np.arange(1, len(data) + 1) |
| 70 | + |
| 71 | + |
| 72 | +def compute_accuracy_cumulative_list( |
| 73 | + tp: pd.DataFrame, fp: pd.DataFrame, tn: pd.DataFrame, fn: pd.DataFrame |
| 74 | +) -> pd.DataFrame: |
| 75 | + """ |
| 76 | + Compute the entry-wise accuracy. The function accumulates the values of the true positives, |
| 77 | + true negatives, false positives, and false negatives. Afterwards, it computes the accuracy. |
| 78 | + Args: |
| 79 | + tp: pd.DataFrame - True Positives [0, 1, 0, 0, ...] |
| 80 | + fp: pd.DataFrame - False Positives [1, 0, 0, 0, ...] |
| 81 | + tn: pd.DataFrame - True Negatives [1, 0, 1, 0, ...] |
| 82 | + fn: pd.DataFrame - False Negatives [0, 0, 0, 0, ...] |
| 83 | + Returns: |
| 84 | + accuracy: pd.DataFrame - Accuracy [0.xx, 0.xx, 0.xx, 0.xx, ...] |
| 85 | + """ |
| 86 | + tp = tp.cumsum() |
| 87 | + tn = tn.cumsum() |
| 88 | + fp = fp.cumsum() |
| 89 | + fn = fn.cumsum() |
| 90 | + numerator = tp + tn |
| 91 | + denominator = tp + tn + fp + fn |
| 92 | + accuracy = numerator / denominator |
| 93 | + return accuracy |
| 94 | + |
| 95 | + |
| 96 | +def compute_accuracy_score( |
| 97 | + tp: pd.DataFrame, fp: pd.DataFrame, tn: pd.DataFrame, fn: pd.DataFrame |
| 98 | +) -> float: |
| 99 | + """ |
| 100 | + Compute the final accuracy score. The function accumulates the values of the true positives, |
| 101 | + true negatives, false positives, and false negatives. Afterwards, it computes the accuracy and |
| 102 | + returns the last value of the accuracy. |
| 103 | + Args: |
| 104 | + tp: pd.DataFrame - True Positives [0, 1, 0, 0, ...] |
| 105 | + fp: pd.DataFrame - False Positives [1, 0, 0, 0, ...] |
| 106 | + tn: pd.DataFrame - True Negatives [1, 0, 1, 0, ...] |
| 107 | + fn: pd.DataFrame - False Negatives [0, 0, 0, 0, ...] |
| 108 | + Returns: |
| 109 | + accuracy: float - Accuracy 0.xx |
| 110 | + """ |
| 111 | + accuracy = compute_accuracy_cumulative_list(tp=tp, fp=fp, tn=tn, fn=fn) |
| 112 | + return accuracy.iloc[-1] |
| 113 | + |
| 114 | + |
| 115 | +def compute_precision_cumulative_list( |
| 116 | + tp: pd.DataFrame, fp: pd.DataFrame |
| 117 | +) -> pd.DataFrame: |
| 118 | + """ |
| 119 | + Compute the entry-wise precision. The function accumulates the values of the true positives and |
| 120 | + false positives. Afterwards, it computes the precision. |
| 121 | + Args: |
| 122 | + tp: pd.DataFrame - True Positives [0, 1, 1, 0, ...] |
| 123 | + fp: pd.DataFrame - False Positives [1, 0, 1, 0, ...] |
| 124 | + Returns: |
| 125 | + precision: pd.DataFrame - Precision [0.xx, 0.xx, 0.xx, 0.xx, ...] |
| 126 | + """ |
| 127 | + tp = tp.cumsum() |
| 128 | + fp = fp.cumsum() |
| 129 | + denominator = tp + fp |
| 130 | + precision = tp / denominator |
| 131 | + return precision |
| 132 | + |
| 133 | + |
| 134 | +def compute_precision_score(tp: pd.DataFrame, fp: pd.DataFrame) -> float: |
| 135 | + """ |
| 136 | + Compute the final precision score. The function accumulates the values of the true positives and |
| 137 | + false positives. Afterwards, it computes the precision and returns the last value of the precision. |
| 138 | + Args: |
| 139 | + tp: pd.DataFrame - True Positives [0, 1, 0, 0, ...] |
| 140 | + fp: pd.DataFrame - False Positives [1, 0, 0, 0, ...] |
| 141 | + Returns: |
| 142 | + precision: float - Precision 0.xx |
| 143 | + """ |
| 144 | + precision = compute_precision_cumulative_list(tp=tp, fp=fp) |
| 145 | + return precision.iloc[-1] |
| 146 | + |
| 147 | + |
| 148 | +def compute_recall_cumulative_list(tp: pd.DataFrame, fn: pd.DataFrame) -> pd.DataFrame: |
| 149 | + """ |
| 150 | + Compute the entry-wise recall. The function accumulates the values of the true positives and |
| 151 | + false negatives. Afterwards, it computes the recall. |
| 152 | + Args: |
| 153 | + tp: pd.DataFrame - True Positives [0, 1, 1, 0, ...] |
| 154 | + fn: pd.DataFrame - False Negatives [1, 0, 1, 0, ...] |
| 155 | + Returns: |
| 156 | + recall: pd.DataFrame - Recall [0.xx, 0.xx, 0.xx, 0.xx, ...] |
| 157 | + """ |
| 158 | + tp = tp.cumsum() |
| 159 | + fn = fn.cumsum() |
| 160 | + denominator = tp + fn |
| 161 | + recall = tp / denominator |
| 162 | + return recall |
| 163 | + |
| 164 | + |
| 165 | +def compute_recall_score(tp: pd.DataFrame, fn: pd.DataFrame) -> float: |
| 166 | + """ |
| 167 | + Compute the final recall score. The function accumulates the values of the true positives and |
| 168 | + false negatives. Afterwards, it computes the recall and returns the last value of the recall. |
| 169 | + Args: |
| 170 | + tp: pd.DataFrame - True Positives [0, 1, 0, 0, ...] |
| 171 | + fn: pd.DataFrame - False Negatives [1, 0, 1, 0, ...] |
| 172 | + Returns: |
| 173 | + recall: float - Recall 0.xx |
| 174 | + """ |
| 175 | + recall = compute_recall_cumulative_list(tp=tp, fn=fn) |
| 176 | + return recall.iloc[-1] |
| 177 | + |
| 178 | + |
| 179 | +def compute_false_positive_rate_cumulative_list( |
| 180 | + fp: pd.DataFrame, tn: pd.DataFrame |
| 181 | +) -> pd.DataFrame: |
| 182 | + """ |
| 183 | + Compute the entry-wise false positive rate. The function accumulates the values of the false positives and |
| 184 | + true negatives. Afterwards, it computes the false positive rate. |
| 185 | + Args: |
| 186 | + fp: pd.DataFrame - False Positives [0, 1, 1, 0, ...] |
| 187 | + tn: pd.DataFrame - True Negatives [1, 0, 1, 0, ...] |
| 188 | + Returns: |
| 189 | + false_positive_rate: pd.DataFrame - False Positive Rate [0.xx, 0.xx, 0.xx, 0.xx, ...] |
| 190 | + """ |
| 191 | + fp = fp.cumsum() |
| 192 | + tn = tn.cumsum() |
| 193 | + denominator = fp + tn |
| 194 | + false_positive_rate = fp / denominator |
| 195 | + return false_positive_rate |
| 196 | + |
| 197 | + |
| 198 | +def compute_false_positive_rate_score(fp: pd.DataFrame, tn: pd.DataFrame) -> float: |
| 199 | + """ |
| 200 | + Compute the final false positive rate score. The function accumulates the values of the false positives and |
| 201 | + true negatives. Afterwards, it computes the false positive rate and returns the last value of the false positive rate. |
| 202 | + Args: |
| 203 | + fp: pd.DataFrame - False Positives [0, 1, 1, 0, ...] |
| 204 | + tn: pd.DataFrame - True Negatives [1, 0, 1, 0, ...] |
| 205 | + Returns: |
| 206 | + false_positive_rate: float - False Positive Rate 0.xx |
| 207 | + """ |
| 208 | + false_positive_rate = compute_false_positive_rate_cumulative_list(fp=fp, tn=tn) |
| 209 | + return false_positive_rate.iloc[-1] |
| 210 | + |
| 211 | + |
| 212 | +def compute_f1_score_cumulative_list( |
| 213 | + tp: pd.DataFrame, fp: pd.DataFrame, fn: pd.DataFrame |
| 214 | +) -> pd.DataFrame: |
| 215 | + """ |
| 216 | + Compute the entry-wise F1 score. The function accumulates the values of the true positives, |
| 217 | + false positives, and false negatives. Afterwards, it computes the F1 score. |
| 218 | + Args: |
| 219 | + tp: pd.DataFrame - True Positives [0, 1, 1, 0, ...] |
| 220 | + fp: pd.DataFrame - False Positives [0, 1, 1, 0, ...] |
| 221 | + fn: pd.DataFrame - False Negatives [1, 0, 1, 0, ...] |
| 222 | + Returns: |
| 223 | + f1_score: pd.DataFrame - F1 Score [0.xx, 0.xx, 0.xx, 0.xx, ...] |
| 224 | + """ |
| 225 | + precision = compute_precision_cumulative_list(tp=tp, fp=fp) |
| 226 | + recall = compute_recall_cumulative_list(tp=tp, fn=fn) |
| 227 | + |
| 228 | + numerator = 2 * precision * recall |
| 229 | + denominator = precision + recall |
| 230 | + f1_score = numerator / denominator |
| 231 | + return f1_score |
| 232 | + |
| 233 | + |
| 234 | +def compute_f1_score_score( |
| 235 | + tp: pd.DataFrame, fp: pd.DataFrame, fn: pd.DataFrame |
| 236 | +) -> float: |
| 237 | + """ |
| 238 | + Compute the final F1 score. The function accumulates the values of the true positives, |
| 239 | + false positives, and false negatives. Afterwards, it computes the F1 score and returns the last value of the F1 score. |
| 240 | + Args: |
| 241 | + tp: pd.DataFrame - True Positives [0, 1, 1, 0, ...] |
| 242 | + fp: pd.DataFrame - False Positives [0, 1, 1, 0, ...] |
| 243 | + fn: pd.DataFrame - False Negatives [1, 0, 1, 0, ...] |
| 244 | + Returns: |
| 245 | + f1_score: float - F1 Score 0.xx |
| 246 | + """ |
| 247 | + f1_score = compute_f1_score_cumulative_list(tp=tp, fp=fp, fn=fn) |
| 248 | + return f1_score.iloc[-1] |
| 249 | + |
| 250 | + |
| 251 | +def compute_error_rate_cumulative_list(fp: pd.DataFrame) -> pd.DataFrame: |
| 252 | + """ |
| 253 | + Compute the cumulative error rate. |
| 254 | + Args: |
| 255 | + fp: pd.DataFrame - False Positives [0, 1, 0, 0, 0, 1, ...] |
| 256 | + Returns: |
| 257 | + error_rate: pd.DataFrame - Error Rate [0/1, 1/2, 1/3, 1/4, 1/5, 2/6, ...] |
| 258 | + """ |
| 259 | + error_rate = __cumulative_average_stats(data=fp) |
| 260 | + return error_rate |
| 261 | + |
| 262 | + |
| 263 | +def compute_error_rate_score(fp: pd.DataFrame) -> float: |
| 264 | + """ |
| 265 | + Compute the final error rate score. |
| 266 | + Args: |
| 267 | + fp: pd.DataFrame - False Positives [0, 1, 0, 0, 0, 1, ...] |
| 268 | + Returns: |
| 269 | + error_rate: float - Error Rate 0.xx |
| 270 | + """ |
| 271 | + error_rate = compute_error_rate_cumulative_list(fp=fp) |
| 272 | + return error_rate.iloc[-1] |
| 273 | + |
| 274 | + |
| 275 | +def compute_cache_hit_rate_cumulative_list( |
| 276 | + cache_hit_list: pd.DataFrame, |
| 277 | +) -> pd.DataFrame: |
| 278 | + """ |
| 279 | + Compute the cumulative cache hit rate. |
| 280 | + Args: |
| 281 | + cache_hit_list: pd.DataFrame - Cache Hits [0, 1, 0, 0, 0, 1, ...] |
| 282 | + Returns: |
| 283 | + cache_hit_rate: pd.DataFrame - Cache Hit Rate [0/1, 1/2, 1/3, 1/4, 1/5, 2/6, ...] |
| 284 | + """ |
| 285 | + cache_hit_rate = __cumulative_average_stats(data=cache_hit_list) |
| 286 | + return cache_hit_rate |
| 287 | + |
| 288 | + |
| 289 | +def compute_cache_hit_rate_score(cache_hit_list: pd.DataFrame) -> float: |
| 290 | + """ |
| 291 | + Compute the final cache hit rate score. |
| 292 | + Args: |
| 293 | + cache_hit_list: pd.DataFrame - Cache Hits [0, 1, 0, 0, 0, 1, ...] |
| 294 | + Returns: |
| 295 | + cache_hit_rate: float - Cache Hit Rate 0.xx |
| 296 | + """ |
| 297 | + cache_hit_rate = compute_cache_hit_rate_cumulative_list( |
| 298 | + cache_hit_list=cache_hit_list |
| 299 | + ) |
| 300 | + return cache_hit_rate.iloc[-1] |
| 301 | + |
| 302 | + |
| 303 | +def compute_duration_cumulative_list(latency_list: pd.DataFrame) -> pd.DataFrame: |
| 304 | + """ |
| 305 | + Compute the cumulative duration. |
| 306 | + Args: |
| 307 | + latency_list: pd.DataFrame - Latency [0, 1, 2, 3, 4, 5, ...] |
| 308 | + Returns: |
| 309 | + duration: pd.DataFrame - Duration [0, 1, 3, 6, 10, 15, ...] |
| 310 | + """ |
| 311 | + return latency_list.cumsum() |
| 312 | + |
| 313 | + |
| 314 | +def compute_duration_score(latency_list: pd.DataFrame) -> float: |
| 315 | + """ |
| 316 | + Compute the final duration score. |
| 317 | + Args: |
| 318 | + latency_list: pd.DataFrame - Latency [0, 1, 2, 3, 4, 5, ...] |
| 319 | + Returns: |
| 320 | + duration: float - Duration 0.xx |
| 321 | + """ |
| 322 | + return latency_list.sum() |
| 323 | + |
| 324 | + |
| 325 | +def compute_avg_latency_score(latency_list: pd.DataFrame) -> float: |
| 326 | + """ |
| 327 | + Compute the final average latency score. |
| 328 | + Args: |
| 329 | + latency_list: pd.DataFrame - Latency [0, 1, 0.5, 2, 1.5, 0.3, ...] |
| 330 | + Returns: |
| 331 | + avg_latency: float - Average Latency 0.xx |
| 332 | + """ |
| 333 | + return latency_list.mean() |
0 commit comments