From 25691d301439863460cfa399685586d6cc7cfdb2 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Fri, 5 Feb 2021 16:58:43 +0100
Subject: [PATCH] Added a function to parse sklearn classification report to
 Latex.

---
 pysegcnn/core/utils.py | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/pysegcnn/core/utils.py b/pysegcnn/core/utils.py
index 609deda..ee6e7e7 100644
--- a/pysegcnn/core/utils.py
+++ b/pysegcnn/core/utils.py
@@ -31,6 +31,7 @@ import xml.etree.ElementTree as ET
 # externals
 import torch
 import numpy as np
+import pandas as pd
 from osgeo import gdal, ogr, osr
 
 # locals
@@ -2632,3 +2633,29 @@ def _tmp_path(path):
     path = pathlib.Path(path)
     return pathlib.Path(str(path).replace(path.suffix,
                                           '_tmp{}'.format(path.suffix)))
+
+
+def report2latex(classification_report, filename=None):
+    """Convert :py:class:`sklearn.metrics.classification_report` to Latex.
+
+    Parameters
+    ----------
+    classification_report : `dict`
+        The dictionary returned by
+        :py:class:`sklearn.metrics.classification_report`.
+    filename : `str` or :py:class:`pathlib.Path` or `None`, optional
+        The object to write the Latex table to. If `None` the table is
+        returned as string.
+
+    """
+    # convert to pandas DataFrame and export to latex
+    df = pd.DataFrame(classification_report).transpose()
+
+    # check if output filename exists
+    if filename is not None:
+        filename = pathlib.Path(filename)
+        if not filename.exists():
+            filename.parent().mkdir(exist_ok=True, parents=True)
+
+    # export to latex
+    df.to_latex(buf=filename)
-- 
GitLab