Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporting #128

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions curifactory/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ def html(self) -> Union[str, list[str]]:
return self.html_string


class LatexTableReporter(Reportable):
"""Output a latex string for a pandas dataframe, useful for copying a table from a report
into a latex paper.

Args:
df (pd.DataFrame): The pandas dataframe to render in latex in the report.
kwargs: Any arguments to pass to `pandas.io.formats.style.Styler.to_latex <https://pandas.pydata.org/docs/reference/api/pandas.io.formats.style.Styler.to_latex.html#pandas.io.formats.style.Styler.to_latex>`_
"""

def __init__(self, df: pd.DataFrame, name: str = None, group: str = None, **kwargs):
self.df = df
self.kwargs = kwargs
super().__init__(name=name, group=group)

def html(self) -> list[str]:
return [
"<pre>",
self.df.style.to_latex(**self.kwargs),
"</pre>",
]


class DFReporter(Reportable):
"""Adds an HTML table to the report for the given pandas dataframe.

Expand Down Expand Up @@ -186,11 +208,7 @@ def html(self) -> list[str]:
output.append(f"<th>{index}</th>")

for item in row:
if (
type(item) == float
or type(item) == np.float64
or type(item) == np.float32
):
if isinstance(item, (float, np.float64, np.float32)):
output.append(
"<td align='right'><pre>{0:.{1}f}</pre></td>".format(
item, self.float_prec
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
numpy
pandas
sphinx~=3.5.4
jinja2<3.1
jinja2
autodocsumm
sphinx-rtd-theme
ipynb-py-convert
Expand Down
20 changes: 19 additions & 1 deletion test/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
from dataclasses import dataclass
from enum import IntEnum

import pandas as pd
from graphviz import Digraph

import curifactory as cf
from curifactory import reporting
from curifactory.caching import JsonCacher, PickleCacher
from curifactory.experiment import run_experiment
from curifactory.reporting import JsonReporter, _add_record_subgraph, render_reportable
from curifactory.reporting import (
JsonReporter,
LatexTableReporter,
_add_record_subgraph,
render_reportable,
)


def test_reportables_cached(configured_test_manager):
Expand Down Expand Up @@ -208,3 +214,15 @@ def test_image_reporter_persists_after_original_deletion(configured_test_manager
assert os.path.exists(
f"{configured_test_manager.reports_path}/{configured_test_manager.get_reference_name()}/reportables/test_save_manual_image_0.png"
)


def test_latex_table_reporter():
"""The LatexTableReporter should correctly...output a latex table? This is just
a basic functionality test."""
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
r = LatexTableReporter(df)
assert r.html() == [
"<pre>",
"\\begin{tabular}{lrr}\n & a & b \\\\\n0 & 1 & 4 \\\\\n1 & 2 & 5 \\\\\n2 & 3 & 6 \\\\\n\\end{tabular}\n",
"</pre>",
]
Loading