|
1 | | -""" |
2 | | -A module for Summary Report |
3 | | -""" |
| 1 | +"""Public summary-report entry points.""" |
4 | 2 |
|
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | +from typing import Any, Dict, Literal, Union, cast |
| 8 | + |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +from rtichoke._calibration_viz_spec_v2 import _calibration_v2_spec_from_curve_list |
| 12 | +from rtichoke._performance_table_spec import ( |
| 13 | + _performance_table_spec_from_performance_data, |
| 14 | +) |
| 15 | +from rtichoke._report_browser import RtichokeBrowserReport |
| 16 | +from rtichoke._report_spec import _report_spec_from_components |
| 17 | +from rtichoke._viz_spec_v2 import _roc_v2_spec_from_performance_data |
| 18 | +from rtichoke.calibration.calibration import _create_calibration_curve_list |
| 19 | +from rtichoke.performance_data.performance_data import prepare_performance_data |
| 20 | +from rtichoke.processing.evaluation_semantics import _build_evaluation_metadata |
5 | 21 | from rtichoke.processing.send_post_request_to_r_rtichoke import ( |
6 | 22 | send_requests_to_rtichoke_r, |
7 | 23 | ) |
8 | | -from rtichoke.processing.transforms import ( |
9 | | - _create_list_data_to_adjust, |
10 | | -) |
11 | | -import subprocess |
| 24 | +from rtichoke.processing.transforms import _create_list_data_to_adjust |
| 25 | + |
| 26 | +SummaryReportRenderer = Literal["r", "browser"] |
| 27 | + |
| 28 | + |
| 29 | +def create_summary_report( |
| 30 | + probs: Dict[str, np.ndarray], |
| 31 | + reals: Union[np.ndarray, Dict[str, np.ndarray]], |
| 32 | + url_api: str = "http://localhost:4242/", |
| 33 | + *, |
| 34 | + renderer: SummaryReportRenderer = "r", |
| 35 | + output_file: str | Path = "summary_report.html", |
| 36 | +) -> Path | None: |
| 37 | + """Create an rtichoke model-performance summary report. |
12 | 38 |
|
| 39 | + The default ``renderer="r"`` preserves the historical public behavior and |
| 40 | + delegates to the R rtichoke backend at ``url_api``. ``renderer="browser"`` |
| 41 | + is an explicit opt-in path that uses Python's existing production |
| 42 | + calculations, canonical standalone component builders, canonical ReportSpec |
| 43 | + assembly, and the vendored ``rtichoke_viz`` ``renderReport()`` composer. |
13 | 44 |
|
14 | | -def create_summary_report(probs, reals, url_api="http://localhost:4242/"): |
15 | | - """Creates a summary report for rtichoke model performance. |
| 45 | + The first browser report contains a canonical PerformanceTable, ROC-v2, and |
| 46 | + calibration-v2 component, in that order. The browser renderer writes an HTML |
| 47 | + file plus the vendored ``rtichoke-viz.js`` and ``rtichoke-viz.css`` assets |
| 48 | + beside it, and returns the written HTML path. The historical R path retains |
| 49 | + its existing return behavior (``None``). |
16 | 50 |
|
17 | 51 | Parameters |
18 | 52 | ---------- |
19 | 53 | probs : Dict[str, np.ndarray] |
20 | | - A dictionary mapping model names to predicted probabilities. |
| 54 | + A dictionary mapping model or population names to predicted probabilities. |
21 | 55 | reals : Union[np.ndarray, Dict[str, np.ndarray]] |
22 | | - The true outcome labels (0 or 1). |
| 56 | + The true binary outcome labels. |
23 | 57 | url_api : str, optional |
24 | | - The API endpoint URL of the R rtichoke backend. |
25 | | - Defaults to ``"http://localhost:4242/"``. |
| 58 | + The API endpoint URL of the historical R rtichoke backend. Used only by |
| 59 | + ``renderer="r"``. Defaults to ``"http://localhost:4242/"``. |
| 60 | + renderer : {"r", "browser"}, optional |
| 61 | + Summary-report backend. Defaults to ``"r"`` for backward compatibility. |
| 62 | + output_file : str or pathlib.Path, optional |
| 63 | + HTML destination for ``renderer="browser"``. Defaults to |
| 64 | + ``"summary_report.html"``. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + pathlib.Path or None |
| 69 | + The generated HTML path for ``renderer="browser"``; ``None`` for the |
| 70 | + historical R backend. |
26 | 71 | """ |
| 72 | + if renderer == "browser": |
| 73 | + return _create_browser_summary_report(probs, reals, output_file=output_file) |
| 74 | + if renderer != "r": |
| 75 | + raise ValueError("renderer must be either 'r' or 'browser'") |
| 76 | + |
27 | 77 | rtichoke_response = send_requests_to_rtichoke_r( |
28 | 78 | dictionary_to_send={"probs": probs, "reals": reals}, |
29 | 79 | url_api=url_api, |
30 | 80 | endpoint="create_summary_report", |
31 | 81 | ) |
32 | 82 | print(rtichoke_response.json()[0].keys()) |
| 83 | + return None |
33 | 84 |
|
34 | 85 |
|
35 | | -def render_summary_report(): |
36 | | - """ |
37 | | - Render the rtichoke Summary Report using Quarto. |
| 86 | +def _create_browser_summary_report( |
| 87 | + probs: Dict[str, np.ndarray], |
| 88 | + reals: Union[np.ndarray, Dict[str, np.ndarray]], |
| 89 | + *, |
| 90 | + output_file: str | Path, |
| 91 | +) -> Path: |
| 92 | + """Build the first public canonical browser report from production outputs.""" |
| 93 | + performance_data = prepare_performance_data(probs, reals) |
| 94 | + metadata = _build_evaluation_metadata(probs, reals, np.array([])) |
| 95 | + calibration_curve_list = _create_calibration_curve_list(probs, reals) |
| 96 | + |
| 97 | + performance_table = _performance_table_spec_from_performance_data( |
| 98 | + performance_data, metadata |
| 99 | + ) |
| 100 | + roc = _roc_v2_spec_from_performance_data(performance_data, metadata) |
| 101 | + calibration = _calibration_v2_spec_from_curve_list(calibration_curve_list, metadata) |
38 | 102 |
|
39 | | - Args: |
40 | | - probs (list): A list of probabilities. |
41 | | - reals (list): A list of real values. |
42 | | - times (list): A list of absolute numbers representing timestamps. |
| 103 | + report = _report_spec_from_components( |
| 104 | + [ |
| 105 | + {"title": "Performance", "spec": performance_table}, |
| 106 | + {"title": "ROC", "spec": roc}, |
| 107 | + {"title": "Calibration", "spec": calibration}, |
| 108 | + ], |
| 109 | + title="rtichoke summary report", |
| 110 | + ) |
| 111 | + return RtichokeBrowserReport(cast(dict[str, Any], report)).write_html(output_file) |
43 | 112 |
|
44 | | - Example: |
45 | | - probs = [0.1, 0.4, 0.8] |
46 | | - reals = [0, 1, 1] |
47 | | - times = [1, 3, 5] |
48 | | - render_summary_report(probs, reals, times) |
49 | 113 |
|
50 | | - This will generate a `summary_report.html` file based on the `summary_report_template.qmd`. |
| 114 | +def render_summary_report(): |
| 115 | + """Render the historical rtichoke Summary Report using Quarto. |
| 116 | +
|
| 117 | + This function is unchanged by the canonical browser-report opt-in path. It |
| 118 | + renders ``aj_estimate_summary_report.qmd`` to ``summary_report.html`` using |
| 119 | + the local Quarto executable. |
51 | 120 | """ |
52 | | - # Define the path to the template and output file |
53 | 121 | template_path = "aj_estimate_summary_report.qmd" |
54 | 122 | output_path = "summary_report.html" |
55 | 123 |
|
56 | | - # Prepare the command to render the Quarto document |
57 | 124 | command = [ |
58 | 125 | "quarto", |
59 | 126 | "render", |
60 | 127 | template_path, |
61 | 128 | "--to", |
62 | 129 | "html", |
63 | 130 | "--output", |
64 | | - output_path, # , |
65 | | - # "--execute-params", |
66 | | - # f"probs={probs},reals={reals},times={times}", |
| 131 | + output_path, |
67 | 132 | ] |
68 | | - |
69 | | - # Execute the command |
70 | 133 | subprocess.run(command, check=True) |
71 | 134 |
|
72 | 135 |
|
|
0 commit comments