|
| 1 | +import argparse |
| 2 | +import base64 |
| 3 | +import io |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import pandas as pd |
| 8 | +import petab.v1 |
| 9 | +import petab.v1.visualize.plotter |
| 10 | +from jinja2 import Environment, FileSystemLoader |
| 11 | +from petab.visualize import plot_problem |
| 12 | + |
| 13 | +import benchmark_models_petab as bmp |
| 14 | + |
| 15 | +# plot simulation line without markers |
| 16 | +petab.v1.visualize.plotter.simulation_line_kwargs["marker"] = "" |
| 17 | + |
| 18 | +def _plot_problem(problem: petab.v1.Problem, sim_df: pd.DataFrame) -> str: |
| 19 | + """Plot measurement points and simulation line for one observable and return base64 png.""" |
| 20 | + plot_problem(problem, simulations_df=sim_df) |
| 21 | + fig = plt.gcf() |
| 22 | + |
| 23 | + buf = io.BytesIO() |
| 24 | + fig.savefig(buf, format="png", dpi=150) |
| 25 | + plt.close(fig) |
| 26 | + buf.seek(0) |
| 27 | + img_b64 = base64.b64encode(buf.read()).decode("ascii") |
| 28 | + return img_b64 |
| 29 | + |
| 30 | + |
| 31 | +def generate_report_for_model(problem_id: str, template_env: Environment, out_dir: Path): |
| 32 | + problem = bmp.get_problem(problem_id) |
| 33 | + if problem is None: |
| 34 | + return |
| 35 | + |
| 36 | + sim_df = bmp.get_simulation_df(problem_id) |
| 37 | + |
| 38 | + images: list[dict[str, str]] = [] |
| 39 | + img = _plot_problem(problem, sim_df) |
| 40 | + images.append({"id": problem_id, "img": img}) |
| 41 | + |
| 42 | + template = template_env.get_template("problem_report.html.jinja") |
| 43 | + rendered = template.render(model_name=problem_id, images=images) |
| 44 | + |
| 45 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 46 | + out_path = out_dir / f"{problem_id}.html" |
| 47 | + with open(out_path, "w", encoding="utf-8") as fh: |
| 48 | + fh.write(rendered) |
| 49 | + print(f"Wrote {out_path}") |
| 50 | + |
| 51 | + |
| 52 | +def generate_index(problem_id_list: list[str], template_env: Environment, out_dir: Path): |
| 53 | + """Render an index page with links to per-problem reports.""" |
| 54 | + template = template_env.get_template("index.html.jinja") |
| 55 | + rendered = template.render(models=problem_id_list, count=len(problem_id_list)) |
| 56 | + |
| 57 | + out_dir.mkdir(parents=True, exist_ok=True) |
| 58 | + out_path = out_dir / "index.html" |
| 59 | + with open(out_path, "w", encoding="utf-8") as fh: |
| 60 | + fh.write(rendered) |
| 61 | + print(f"Wrote {out_path}") |
| 62 | + |
| 63 | + |
| 64 | +def main(output_dir: Path = None): |
| 65 | + if output_dir is None: |
| 66 | + output_dir = Path(__file__).parent / "reports" |
| 67 | + templates_dir = Path(__file__).parent / "templates" |
| 68 | + |
| 69 | + env = Environment(loader=FileSystemLoader(templates_dir), autoescape=True) |
| 70 | + problem_id_list = list(bmp.MODELS) |
| 71 | + |
| 72 | + for problem_id in problem_id_list: |
| 73 | + try: |
| 74 | + generate_report_for_model(problem_id, env, output_dir) |
| 75 | + except Exception as e: |
| 76 | + print(f"Skipping {problem_id} due to error: {e}") |
| 77 | + |
| 78 | + try: |
| 79 | + generate_index(problem_id_list, env, output_dir) |
| 80 | + except Exception as e: |
| 81 | + print(f"Failed to write index: {e}") |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + parser = argparse.ArgumentParser(description="Generate PETab problem reports") |
| 85 | + parser.add_argument( |
| 86 | + "--output-dir", |
| 87 | + "-o", |
| 88 | + type=Path, |
| 89 | + default=None, |
| 90 | + help="Directory to write reports to (default: vis/reports)", |
| 91 | + ) |
| 92 | + args = parser.parse_args() |
| 93 | + |
| 94 | + main(args.output_dir) |
0 commit comments