Skip to content
Draft
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
6 changes: 6 additions & 0 deletions dp_wizard/shiny/panels/results_panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ def package_zip():
# from a clean slate, rather than rely on the side effect
# of a reactive.calc.
(zip_root_dir / f"{stem}.txt").write_text(report_txt())
(zip_root_dir / f"{stem}.txt").write_text(report_html())
(zip_root_dir / f"{stem}.csv").write_text(table_csv())

base_name = f"{tmp_dir}/{stem}"
Expand Down Expand Up @@ -392,6 +393,11 @@ def report_txt():
notebook_nb() # Evaluate just for the side effect of creating report.
return (_target_path / "report.txt").read_text()

@reactive.calc
def report_html():
notebook_nb() # Evaluate just for the side effect of creating report.
return (_target_path / "report.html").read_text()

@reactive.calc
def table_csv():
notebook_nb() # Evaluate just for the side effect of creating report.
Expand Down
25 changes: 23 additions & 2 deletions dp_wizard/utils/code_generators/no-tests/_stats_reports.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import re
from pathlib import Path

from yaml import dump
Expand Down Expand Up @@ -45,10 +46,30 @@ def flatten_dict(dictionary, parent_key=""):
"outputs": OUTPUTS,
}

Path(TXT_REPORT_PATH).write_text(dump(report))
target_path = Path(TARGET_PATH)
(target_path / "report.txt").write_text(dump(report))

flat_report = flatten_dict(report)
with Path(CSV_REPORT_PATH).open(mode="w", newline="") as handle:
with (target_path / "report.csv").open(mode="w", newline="") as handle:
writer = csv.writer(handle)
for kv_pair in flat_report.items():
writer.writerow(kv_pair)


def png_name(name):
return re.sub(r"\W+", "-", name) + ".png"


for name, figure in figures.items():
figure.savefig(target_path / png_name(name))


imgs = [f"<img src='{png_name(name)}' alt='{name}'>" for name in figures.keys()]
html = f"""
<html>
<body>
Figures:
{"\n".join(imgs)}
</body>
</html>"""
(target_path / "report.html").write_text(html)
7 changes: 5 additions & 2 deletions dp_wizard/utils/code_generators/no-tests/_synth_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"outputs": OUTPUTS,
}

Path(TXT_REPORT_PATH).write_text(dump(report))
target_path = Path(TARGET_PATH)
(target_path / "report.txt").write_text(dump(report))

synthetic_data.write_csv(CSV_REPORT_PATH)
synthetic_data.write_csv(target_path / "report.csv")

# TODO: Use figures
6 changes: 4 additions & 2 deletions dp_wizard/utils/code_generators/notebook_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ def template(synthetic_data):
.fill_values(
CSV_PATH=self.analysis_plan.get_absolute_csv_path(),
EPSILON=self.analysis_plan.epsilon,
TXT_REPORT_PATH=str(target_path / "report.txt"),
CSV_REPORT_PATH=str(target_path / "report.csv"),
TARGET_PATH=target_path,
# TXT_REPORT_PATH=str(target_path / "report.txt"),
# CSV_REPORT_PATH=str(target_path / "report.csv"),
# FIGURES_PATH=str(target_path),
)
.finish()
)
Expand Down
6 changes: 5 additions & 1 deletion dp_wizard/utils/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def df_to_columns(df: DataFrame):
return transposed if transposed else (tuple(), tuple())


figures = {}


def plot_bars(
df: DataFrame, error: float, cutoff: float, title: str
): # pragma: no cover
Expand All @@ -75,7 +78,8 @@ def plot_bars(
plt.rcParams["figure.figsize"] = (12, 4)

bins, values = df_to_columns(df)
_figure, axes = plt.subplots()
figure, axes = plt.subplots()
figures[title] = figure # pyright: ignore[reportUndefinedVariable]
bar_colors = ["blue" if v > cutoff else "lightblue" for v in values]
axes.bar(bins, values, color=bar_colors, yerr=error)
axes.set_xticks(bins, bins, rotation=45)
Expand Down
Loading