def test_with_headers_and_footer(self): two_rows = [ { "stage": "forward", "total": "0.011" }, { "stage": "comput_loss", "total": "0.000" }, ] headers = {"stage": "Stage", "total": "Total Time"} footers = { "stage": "This very long footer is necessary.", "total": "0.055" } in_table_form = ascii_table(two_rows, headers, footers) output = ("+-------------------------------------+------------+\n" "| Stage | Total Time |\n" "+-------------------------------------+------------+\n" "| forward | 0.011 |\n" "| comput_loss | 0.000 |\n" "+-------------------------------------+------------+\n" "| This very long footer is necessary. | 0.055 |\n" "+-------------------------------------+------------+") self.assertEqual(in_table_form, output)
def report(self, header): total = timeit.default_timer() - self.start print(f"\n\t {header}") print( ascii_table( [{ "stage": stage, "total": f"{stage_time.total:.3f}", "average": f"{stage_time.average:.3f}", "count": f"{stage_time.count}", } for stage, stage_time in self.per_stage_time.items()], human_column_names={ "stage": "Stage", "total": "Total Time", "average": "Average Time", "count": "Count", }, footer={ "stage": "Overall training", "total": f"{total:.3f}", "average": f"{total:.3f}", "count": "1", }, indentation="\t", ))
def report(self): snapshot_total = timeit.default_timer() - self.start def path(key): return " -> ".join(label for label, _ in key) results = [{ "name": path(key), "total": format_time(times.sum), "avg": format_time(times.average), "max": format_time(times.max), "count": times.count, } for key, times in sorted(self.times.items())] print( ascii_table( results, human_column_names={ "name": "Stage", "total": "Total", "avg": "Average", "max": "Max", "count": "Count", }, footer={ "name": "Total time", "total": format_time(snapshot_total) }, alignments={"name": "<"}, ))
def print_metrics(self, indentation="") -> None: print( ascii_table( [{ "label": label, "precision": f"{metrics.precision:.2f}", "recall": f"{metrics.recall:.2f}", "f1": f"{metrics.f1:.2f}", "support": metrics.true_positives + metrics.false_negatives, } for label, metrics in self.per_label_scores.items()], human_column_names={ "label": "Label", "precision": "Precision", "recall": "Recall", "f1": "F1", "support": "Support", }, footer={ "label": "Overall macro scores", "precision": f"{self.macro_scores.precision:.2f}", "recall": f"{self.macro_scores.recall:.2f}", "f1": f"{self.macro_scores.f1:.2f}", }, indentation=indentation, ))
def test_simple(self): two_rows = [ { "stage": "forward", "total": "0.011" }, { "stage": "comput_loss", "total": "0.000" }, ] in_table_form = ascii_table(two_rows) output = ("+-------------+-------+\n" "| forward | 0.011 |\n" "| comput_loss | 0.000 |\n" "+-------------+-------+") self.assertEqual(in_table_form, output)
def print_metrics(self) -> None: print(f"Accuracy: {self.accuracy * 100:.2f}\n") print("Macro P/R/F1 Scores:") self.macro_prf1_metrics.print_metrics(indentation="\t") print("\nSoft Metrics:") if self.per_label_soft_scores: soft_scores = { label: f"{metrics.average_precision * 100:.2f}" for label, metrics in self.per_label_soft_scores.items() } print( ascii_table_from_dict(soft_scores, "Label", "Average precision", indentation="\t")) all_thresholds = set( itertools.chain.from_iterable( metrics.recall_at_precision for metrics in self.per_label_soft_scores.values())) print("\n\t Precision at Recall") print( ascii_table( (dict( {"label": label}, **{ str(p): f"{r:.2f}" for p, r in metrics.recall_at_precision.items() }, ) for label, metrics in self.per_label_soft_scores.items()), dict( {"label": "Label"}, **{str(t): f"P@R {t}" for t in all_thresholds}, ), indentation="\t", )) if self.mcc: print(f"\nMatthews correlation coefficient: {self.mcc :.2f}") if self.roc_auc: print(f"\nROC AUC: {self.roc_auc:.3f}")
def test_with_headers(self): two_rows = [ { "stage": "forward", "total": "0.011" }, { "stage": "comput_loss", "total": "0.000" }, ] headers = {"stage": "Stage", "total": "Total Time"} in_table_form = ascii_table(two_rows, headers) output = ("+-------------+------------+\n" "| Stage | Total Time |\n" "+-------------+------------+\n" "| forward | 0.011 |\n" "| comput_loss | 0.000 |\n" "+-------------+------------+") self.assertEqual(in_table_form, output)