|
| 1 | +"""Internal canonical PerformanceTableSpec builders. |
| 2 | +
|
| 3 | +These helpers translate already-calculated production performance data plus |
| 4 | +semantic evaluation metadata. They do not calculate or render statistics. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import math |
| 10 | +from collections.abc import Mapping |
| 11 | +from typing import Any, TypedDict, cast |
| 12 | + |
| 13 | +import polars as pl |
| 14 | + |
| 15 | +from rtichoke.processing.evaluation_semantics import _EvaluationMetadata |
| 16 | + |
| 17 | +_METRICS: tuple[tuple[str, str], ...] = ( |
| 18 | + ("true_positives", "True Positives"), |
| 19 | + ("true_negatives", "True Negatives"), |
| 20 | + ("false_positives", "False Positives"), |
| 21 | + ("false_negatives", "False Negatives"), |
| 22 | + ("sensitivity", "Sensitivity"), |
| 23 | + ("specificity", "Specificity"), |
| 24 | + ("false_positive_rate", "False Positive Rate"), |
| 25 | + ("ppv", "PPV"), |
| 26 | + ("npv", "NPV"), |
| 27 | + ("lift", "Lift"), |
| 28 | + ("predicted_positives", "Predicted Positives"), |
| 29 | + ("ppcr", "PPCR"), |
| 30 | + ("net_benefit", "Net Benefit"), |
| 31 | + ("net_benefit_interventions_avoided", "Interventions Avoided"), |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +class _EvaluationSpec(TypedDict, total=False): |
| 36 | + id: str |
| 37 | + model: str |
| 38 | + population: str |
| 39 | + |
| 40 | + |
| 41 | +class _MetricDefinition(TypedDict): |
| 42 | + id: str |
| 43 | + label: str |
| 44 | + |
| 45 | + |
| 46 | +class _OperatingPoint(TypedDict): |
| 47 | + type: str |
| 48 | + value: float |
| 49 | + |
| 50 | + |
| 51 | +class _MetricValue(TypedDict): |
| 52 | + metricId: str |
| 53 | + estimate: float | int | None |
| 54 | + |
| 55 | + |
| 56 | +class _EvaluationContext(TypedDict): |
| 57 | + censoringHeuristic: str |
| 58 | + competingEventHeuristic: str |
| 59 | + |
| 60 | + |
| 61 | +class _PerformanceTableRow(TypedDict, total=False): |
| 62 | + evaluationId: str |
| 63 | + operatingPoint: _OperatingPoint |
| 64 | + values: list[_MetricValue] |
| 65 | + horizon: float |
| 66 | + context: _EvaluationContext |
| 67 | + |
| 68 | + |
| 69 | +class _PerformanceTableSpec(TypedDict): |
| 70 | + schemaVersion: str |
| 71 | + type: str |
| 72 | + evaluations: list[_EvaluationSpec] |
| 73 | + metrics: list[_MetricDefinition] |
| 74 | + rows: list[_PerformanceTableRow] |
| 75 | + |
| 76 | + |
| 77 | +def _performance_table_spec_from_performance_data( |
| 78 | + performance_data: pl.DataFrame, |
| 79 | + evaluation_metadata: Mapping[str, _EvaluationMetadata], |
| 80 | +) -> _PerformanceTableSpec: |
| 81 | + """Build the canonical static PerformanceTableSpec.""" |
| 82 | + return _build_performance_table_spec( |
| 83 | + performance_data, evaluation_metadata, time_dependent=False |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +def _performance_table_times_spec_from_performance_data( |
| 88 | + performance_data: pl.DataFrame, |
| 89 | + evaluation_metadata: Mapping[str, _EvaluationMetadata], |
| 90 | +) -> _PerformanceTableSpec: |
| 91 | + """Build the canonical time-dependent PerformanceTableSpec.""" |
| 92 | + return _build_performance_table_spec( |
| 93 | + performance_data, evaluation_metadata, time_dependent=True |
| 94 | + ) |
| 95 | + |
| 96 | + |
| 97 | +def _build_performance_table_spec( |
| 98 | + performance_data: pl.DataFrame, |
| 99 | + evaluation_metadata: Mapping[str, _EvaluationMetadata], |
| 100 | + *, |
| 101 | + time_dependent: bool, |
| 102 | +) -> _PerformanceTableSpec: |
| 103 | + required = {"reference_group", "stratified_by", "chosen_cutoff"} |
| 104 | + if time_dependent: |
| 105 | + required |= { |
| 106 | + "fixed_time_horizon", |
| 107 | + "censoring_heuristic", |
| 108 | + "competing_heuristic", |
| 109 | + } |
| 110 | + missing = required.difference(performance_data.columns) |
| 111 | + if missing: |
| 112 | + raise ValueError( |
| 113 | + "Performance-table data is missing columns: " + ", ".join(sorted(missing)) |
| 114 | + ) |
| 115 | + |
| 116 | + metric_definitions: list[_MetricDefinition] = [ |
| 117 | + cast(_MetricDefinition, {"id": metric_id, "label": label}) |
| 118 | + for metric_id, label in _METRICS |
| 119 | + if metric_id in performance_data.columns |
| 120 | + ] |
| 121 | + metric_ids = [definition["id"] for definition in metric_definitions] |
| 122 | + |
| 123 | + rows = performance_data.select( |
| 124 | + [ |
| 125 | + column |
| 126 | + for column in ( |
| 127 | + "reference_group", |
| 128 | + "fixed_time_horizon", |
| 129 | + "censoring_heuristic", |
| 130 | + "competing_heuristic", |
| 131 | + "stratified_by", |
| 132 | + "chosen_cutoff", |
| 133 | + *metric_ids, |
| 134 | + ) |
| 135 | + if column in performance_data.columns |
| 136 | + ] |
| 137 | + ).to_dicts() |
| 138 | + row_groups = {str(row["reference_group"]) for row in rows} |
| 139 | + missing_metadata = row_groups.difference(evaluation_metadata) |
| 140 | + if missing_metadata: |
| 141 | + raise ValueError( |
| 142 | + "Performance-table rows are missing evaluation metadata: " |
| 143 | + + ", ".join(sorted(missing_metadata)) |
| 144 | + ) |
| 145 | + |
| 146 | + ordered_groups = [group for group in evaluation_metadata if group in row_groups] |
| 147 | + evaluation_ids = { |
| 148 | + group: f"evaluation-{index}" |
| 149 | + for index, group in enumerate(ordered_groups, start=1) |
| 150 | + } |
| 151 | + evaluations: list[_EvaluationSpec] = [] |
| 152 | + for group in ordered_groups: |
| 153 | + metadata = evaluation_metadata[group] |
| 154 | + evaluation: _EvaluationSpec = { |
| 155 | + "id": evaluation_ids[group], |
| 156 | + "population": metadata.population, |
| 157 | + } |
| 158 | + if metadata.model is not None: |
| 159 | + evaluation["model"] = metadata.model |
| 160 | + evaluations.append(evaluation) |
| 161 | + |
| 162 | + canonical_rows: list[_PerformanceTableRow] = [] |
| 163 | + for row in rows: |
| 164 | + group = str(row["reference_group"]) |
| 165 | + stratified_by = str(row["stratified_by"]) |
| 166 | + operating_point: _OperatingPoint |
| 167 | + if stratified_by == "probability_threshold": |
| 168 | + operating_point = { |
| 169 | + "type": "probability_threshold", |
| 170 | + "value": _number(row["chosen_cutoff"]), |
| 171 | + } |
| 172 | + elif stratified_by == "ppcr": |
| 173 | + operating_point = {"type": "ppcr", "value": _number(row["ppcr"])} |
| 174 | + else: |
| 175 | + raise ValueError( |
| 176 | + "Canonical PerformanceTableSpec supports probability_threshold " |
| 177 | + f"or ppcr operating points, not {stratified_by!r}" |
| 178 | + ) |
| 179 | + |
| 180 | + canonical_row: _PerformanceTableRow = { |
| 181 | + "evaluationId": evaluation_ids[group], |
| 182 | + "operatingPoint": operating_point, |
| 183 | + "values": cast( |
| 184 | + list[_MetricValue], |
| 185 | + [ |
| 186 | + { |
| 187 | + "metricId": metric_id, |
| 188 | + "estimate": _nullable_number(row[metric_id]), |
| 189 | + } |
| 190 | + for metric_id in metric_ids |
| 191 | + ], |
| 192 | + ), |
| 193 | + } |
| 194 | + if time_dependent: |
| 195 | + canonical_row["horizon"] = _number(row["fixed_time_horizon"]) |
| 196 | + canonical_row["context"] = { |
| 197 | + "censoringHeuristic": str(row["censoring_heuristic"]), |
| 198 | + "competingEventHeuristic": str(row["competing_heuristic"]), |
| 199 | + } |
| 200 | + canonical_rows.append(canonical_row) |
| 201 | + |
| 202 | + return { |
| 203 | + "schemaVersion": "2.0", |
| 204 | + "type": "performance_table", |
| 205 | + "evaluations": evaluations, |
| 206 | + "metrics": metric_definitions, |
| 207 | + "rows": canonical_rows, |
| 208 | + } |
| 209 | + |
| 210 | + |
| 211 | +def _nullable_number(value: Any) -> float | int | None: |
| 212 | + if value is None: |
| 213 | + return None |
| 214 | + if isinstance(value, float) and math.isnan(value): |
| 215 | + return None |
| 216 | + return value |
| 217 | + |
| 218 | + |
| 219 | +def _number(value: Any) -> float: |
| 220 | + if value is None: |
| 221 | + raise ValueError("Operating point and horizon values must not be null") |
| 222 | + return float(value) |
0 commit comments