Source code for datapipe_testbench.report

"""
Tools to help write a summary report.
"""

import os

import numpy as np
import pylatex
from pylatex import Command, Document, section
from pylatex.utils import NoEscape

__all__ = ["Report"]


[docs] class Report(Document): """Class to write a summary of a benchmark to .pdf.""" def __init__(self, title): super().__init__( documentclass="article", document_options="twoside", fontenc="T1", inputenc="utf8", font_size="normalsize", lmodern=True, textcomp=True, microtype=None, page_numbers=True, geometry_options=dict(margin="2.5cm"), ) self.packages.append(pylatex.Package("pstricks")) self.preamble.append( Command("newrgbcolor", ["linkcolor", "0.71372549 0.145098039 0.109803922"]) ) self.packages.append(pylatex.Package("fancyhdr")) self.preamble.append(Command("pagestyle", "fancy")) self.preamble.append(Command("fancyhf", "")) self.preamble.append( Command("fancyhead", NoEscape(r"\footnotesize\textbf{\thepage}"), "RO,LE") ) self.preamble.append( Command("fancyhead", NoEscape(r"\footnotesize\scshape\rightmark"), "RE") ) self.preamble.append( Command("fancyhead", NoEscape(r"\footnotesize\leftmark"), "LO") ) self.packages.append(pylatex.Package("hyperref", options=["pdftex"])) self.preamble.append( Command( "hypersetup", NoEscape( """colorlinks=true, breaklinks=true, urlcolor=blue, linkcolor=linkcolor, plainpages=false, citecolor=refcolor""" ), ) ) self.packages.append(pylatex.Package("float")) self.preamble.append(Command("title", title)) self.preamble.append(Command("author", "datapipe-benchmark")) self.preamble.append(Command("date", NoEscape(r"\today"))) self.append(NoEscape(r"\maketitle")) self.append(Command("tableofcontents")) self.append(NoEscape(r"\clearpage")) self.append(Command("listoffigures")) self.append(Command("listoftables")) self.append(NoEscape(r"\clearpage"))
[docs] def add_header(self, level, title): """Add new section to document. Parameters ---------- level : int Level 1-5 of the header to add. title : str Title for the header. """ klasses = { 1: section.Section, 2: section.Subsection, 3: section.Subsubsection, 4: section.Paragraph, 5: section.Subparagraph, } if level < 0 or level > 5: msg = f"level need to be [1-5], got {level} instead." raise ValueError(msg) klass = klasses[level] with self.create(klass(title)): pass
[docs] def add_image(self, path, caption="", width=0.95): """Add image to document. Parameters ---------- path : str Relative or absolute path to image. width : float In fraction of linewidth (0 to 1). By default, 0.95. caption : str Image caption. By default, "". """ # Path.resolve() exist, but we still need a string in the end, so it's just simpler this way. abs_path = os.path.abspath(str(path)) with self.create(pylatex.Figure(position="H")) as fig: fig.add_image(abs_path, width=NoEscape(rf"{width}\linewidth")) fig.add_caption(caption)
[docs] def add_table(self, lines): """Add table to document. Parameters ---------- lines : List of lines. Each line is a list of string, one per cell. First line is header. """ tmp = [len(line) for line in lines] n_cols = np.max(tmp) # All columns will be centered by default. col_def = " ".join(["c"] * n_cols) with self.create(pylatex.Tabu(col_def, pos="H", row_height=1.2)) as table: table.add_hline() table.add_row(lines[0], mapper=[pylatex.utils.bold]) table.add_hline() for line in lines[1:]: table.add_row(line) table.add_hline()