Skip to content

Commit 808a33c

Browse files
committed
format: make ty pass
1 parent b70237f commit 808a33c

9 files changed

Lines changed: 60 additions & 27 deletions

‎src/rtichoke/_performance_table_spec.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
from __future__ import annotations
88

9+
import math
910
from collections.abc import Mapping
10-
from typing import Any, TypedDict
11+
from typing import Any, TypedDict, cast
1112

12-
import math
1313
import polars as pl
1414

1515
from rtichoke.processing.evaluation_semantics import _EvaluationMetadata
1616

17-
_METRICS = (
17+
_METRICS: tuple[tuple[str, str], ...] = (
1818
("true_positives", "True Positives"),
1919
("true_negatives", "True Negatives"),
2020
("false_positives", "False Positives"),
@@ -114,7 +114,7 @@ def _build_performance_table_spec(
114114
)
115115

116116
metric_definitions: list[_MetricDefinition] = [
117-
{"id": metric_id, "label": label}
117+
cast(_MetricDefinition, {"id": metric_id, "label": label})
118118
for metric_id, label in _METRICS
119119
if metric_id in performance_data.columns
120120
]
@@ -180,10 +180,16 @@ def _build_performance_table_spec(
180180
canonical_row: _PerformanceTableRow = {
181181
"evaluationId": evaluation_ids[group],
182182
"operatingPoint": operating_point,
183-
"values": [
184-
{"metricId": metric_id, "estimate": _nullable_number(row[metric_id])}
185-
for metric_id in metric_ids
186-
],
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+
),
187193
}
188194
if time_dependent:
189195
canonical_row["horizon"] = _number(row["fixed_time_horizon"])

‎src/rtichoke/processing/adjustments.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from collections.abc import Sequence
2+
13
import pandas as pd
24
import polars as pl
3-
from polarstate import predict_aj_estimates
4-
from polarstate import prepare_event_table
5-
from collections.abc import Sequence
5+
from polarstate import predict_aj_estimates, prepare_event_table
6+
67
from rtichoke.processing.transforms import assign_and_explode_polars
78

89

@@ -529,7 +530,7 @@ def _aj_estimates_by_cutoff_per_horizon(
529530
df.filter(pl.col("fixed_time_horizon") == h)
530531
.group_by("strata")
531532
.map_groups(
532-
lambda group: extract_aj_estimate_by_cutoffs(
533+
lambda group, h=h: extract_aj_estimate_by_cutoffs(
533534
group, [h], breaks, stratified_by, full_event_table=False
534535
)
535536
)
@@ -547,7 +548,7 @@ def _aj_estimates_per_horizon(
547548
df.filter(pl.col("fixed_time_horizon") == h)
548549
.group_by("strata")
549550
.map_groups(
550-
lambda group: extract_aj_estimate_for_strata(
551+
lambda group, h=h: extract_aj_estimate_for_strata(
551552
group, [h], full_event_table
552553
)
553554
)
@@ -650,6 +651,8 @@ def _aj_adjusted_events(
650651
adjusted = extract_aj_estimate_by_cutoffs(
651652
non_competing, horizons, breaks, stratified_by, full_event_table
652653
)
654+
else:
655+
raise ValueError(f"Unsupported risk-set scope: {risk_set_scope!r}")
653656

654657
adjusted = adjusted.with_columns(
655658
[
@@ -704,6 +707,8 @@ def _aj_adjusted_events(
704707
adjusted = _aj_estimates_by_cutoff_per_horizon(
705708
base_df, horizons, breaks, stratified_by
706709
)
710+
else:
711+
raise ValueError(f"Unsupported risk-set scope: {risk_set_scope!r}")
707712

708713
adjusted = adjusted.with_columns(
709714
pl.lit(risk_set_scope)
@@ -730,6 +735,8 @@ def _aj_adjusted_events(
730735
adjusted = extract_aj_estimate_by_cutoffs(
731736
base_df, horizons, breaks, stratified_by, full_event_table
732737
)
738+
else:
739+
raise ValueError(f"Unsupported risk-set scope: {risk_set_scope!r}")
733740

734741
adjusted = adjusted.with_columns(
735742
[

‎tests/test_cutoff_grid_endpoint.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import cast
2+
13
import numpy as np
24
import pytest
35

@@ -17,7 +19,7 @@ def test_probability_threshold_breaks_match_r_seq_semantics(by, expected):
1719
breaks = create_breaks_values(None, "probability_threshold", by)
1820

1921
assert breaks.tolist() == expected
20-
assert np.max(breaks) <= 1.0
22+
assert float(np.max(breaks)) <= 1.0
2123

2224

2325
@pytest.mark.parametrize(
@@ -33,7 +35,7 @@ def test_binary_performance_data_matches_r_cutoff_endpoint(by, expected_max):
3335

3436
assert cutoffs.min() == 0.0
3537
assert cutoffs.max() == expected_max
36-
assert cutoffs.max() <= 1.0
38+
assert cast(float, cutoffs.max()) <= 1.0
3739

3840

3941
@pytest.mark.parametrize(
@@ -56,4 +58,4 @@ def test_time_performance_data_matches_r_cutoff_endpoint(by, expected_max):
5658

5759
assert cutoffs.min() == 0.0
5860
assert cutoffs.max() == expected_max
59-
assert cutoffs.max() <= 1.0
61+
assert cast(float, cutoffs.max()) <= 1.0

‎tests/test_performance_table.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
from typing import cast
2+
13
import numpy as np
24
import pytest
35
from great_tables import GT
46
from reactable import Reactable
57

68
import rtichoke.performance_table as performance_table_module
7-
from rtichoke.performance_table_reactable import _bar_style, _net_benefit_style
8-
99
from rtichoke import (
1010
create_performance_table,
1111
create_performance_table_times,
1212
prepare_performance_data,
1313
prepare_performance_data_times,
1414
render_performance_table,
1515
)
16+
from rtichoke.performance_table import PerformanceTableRenderer
17+
from rtichoke.performance_table_reactable import _bar_style, _net_benefit_style
1618

1719

1820
def _example():
@@ -215,7 +217,9 @@ def test_invalid_renderer_is_rejected():
215217
probs, reals = _example()
216218
data = prepare_performance_data(probs, reals, by=0.1)
217219
with pytest.raises(ValueError, match="renderer"):
218-
render_performance_table(data, renderer="unknown")
220+
render_performance_table(
221+
data, renderer=cast(PerformanceTableRenderer, "unknown")
222+
)
219223

220224

221225
def test_reactable_metric_bar_matches_r_colors_and_geometry():

‎tests/test_performance_table_spec.py‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def _static(
2121
by: float = 0.5,
2222
stratified_by: Sequence[str] = ("probability_threshold",),
2323
):
24-
data = prepare_performance_data(
25-
probs, reals, by=by, stratified_by=stratified_by
26-
)
24+
data = prepare_performance_data(probs, reals, by=by, stratified_by=stratified_by)
2725
metadata = _build_evaluation_metadata(probs, reals, np.array([]))
2826
return _performance_table_spec_from_performance_data(data, metadata)
2927

@@ -110,9 +108,7 @@ def test_static_probability_threshold_and_ppcr_operating_points():
110108
"probability_threshold"
111109
}
112110
assert {row["operatingPoint"]["type"] for row in ppcr["rows"]} == {"ppcr"}
113-
assert all(
114-
0 <= row["operatingPoint"]["value"] <= 1 for row in ppcr["rows"]
115-
)
111+
assert all(0 <= row["operatingPoint"]["value"] <= 1 for row in ppcr["rows"])
116112

117113

118114
def test_zero_is_preserved_and_missing_metric_is_null():

‎tests/test_time_gains_v2.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from typing import Any, cast
23

34
import matplotlib.figure
45
import numpy as np
@@ -116,6 +117,7 @@ def test_censoring_and_competing_risk_reference_comes_from_performance_layer():
116117
spec = _gains_times_v2_spec_from_performance_data(
117118
performance, _build_evaluation_metadata(probs, reals, times)
118119
)
120+
references = cast(list[dict[str, Any]], spec["references"])
119121
calculated = {
120122
float(row["fixed_time_horizon"]): float(row["real_positives"] / row["n"])
121123
for row in performance.filter(performance["chosen_cutoff"] == 0)
@@ -126,7 +128,7 @@ def test_censoring_and_competing_risk_reference_comes_from_performance_layer():
126128

127129
assert {
128130
reference["horizon"]: reference["points"][1]["x"]
129-
for reference in spec["references"][1:]
131+
for reference in references[1:]
130132
} == calculated
131133

132134

‎tests/test_time_lift_v2.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast
2+
13
import matplotlib.figure
24
import numpy as np
35
import plotly.graph_objects as go
@@ -143,6 +145,7 @@ def test_time_lift_censoring_and_competing_risk_reference_comes_from_performance
143145
spec = _lift_times_v2_spec_from_performance_data(
144146
performance, _build_evaluation_metadata(probs, reals, times)
145147
)
148+
references = cast(list[dict[str, Any]], spec["references"])
146149
calculated_risks = {
147150
float(row["fixed_time_horizon"]): float(row["real_positives"] / row["n"])
148151
for row in performance.filter(performance["chosen_cutoff"] == 0)
@@ -151,7 +154,7 @@ def test_time_lift_censoring_and_competing_risk_reference_comes_from_performance
151154
.to_dicts()
152155
}
153156

154-
for reference in spec["references"][1:]:
157+
for reference in references[1:]:
155158
horizon = reference["horizon"]
156159
risk = calculated_risks[horizon]
157160
assert reference["points"] == [

‎tests/test_viz_browser.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
from typing import Any, cast
23

34
import numpy as np
45

@@ -35,6 +36,7 @@ def test_real_roc_output_maps_to_canonical_spec():
3536
performance_data = _real_roc_performance_data()
3637

3738
spec = _roc_spec_from_performance_data(performance_data)
39+
spec = cast(dict[str, Any], spec)
3840

3941
assert spec["schemaVersion"] == "1.0"
4042
assert spec["type"] == "roc"
@@ -47,6 +49,7 @@ def test_real_roc_output_maps_to_canonical_spec():
4749

4850
def test_real_calibration_output_maps_to_canonical_spec():
4951
spec = _calibration_spec_from_curve_list(_real_calibration_curve_list())
52+
spec = cast(dict[str, Any], spec)
5053

5154
assert spec["schemaVersion"] == "1.0"
5255
assert spec["type"] == "calibration"

‎tests/test_viz_spec_v2.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast
2+
13
import numpy as np
24

35
from rtichoke._viz_spec_v2 import (
@@ -24,6 +26,7 @@ def test_roc_v2_one_model_one_population():
2426
performance_data,
2527
_static_metadata(probs, reals),
2628
)
29+
spec = cast(dict[str, Any], spec)
2730

2831
assert spec["schemaVersion"] == "2.0"
2932
assert spec["type"] == "roc"
@@ -57,6 +60,7 @@ def test_roc_v2_multiple_models_share_one_population():
5760
performance_data,
5861
_static_metadata(probs, reals),
5962
)
63+
spec = cast(dict[str, Any], spec)
6064

6165
evaluations = spec["evaluations"]
6266
assert [evaluation["id"] for evaluation in evaluations] == [
@@ -87,6 +91,7 @@ def test_roc_v2_keyed_populations_keep_model_identity_unknown():
8791
performance_data,
8892
_static_metadata(probs, reals),
8993
)
94+
spec = cast(dict[str, Any], spec)
9095

9196
assert spec["evaluations"] == [
9297
{"id": "evaluation-1", "population": "Population A"},
@@ -112,6 +117,7 @@ def test_roc_v2_ids_do_not_encode_compatibility_group_labels():
112117
prepare_performance_data(probs, reals, by=0.25),
113118
_static_metadata(probs, reals),
114119
)
120+
spec = cast(dict[str, Any], spec)
115121

116122
renamed_reals = {
117123
"Cohort X": reals["Population A"],
@@ -125,6 +131,7 @@ def test_roc_v2_ids_do_not_encode_compatibility_group_labels():
125131
prepare_performance_data(renamed_probs, renamed_reals, by=0.25),
126132
_static_metadata(renamed_probs, renamed_reals),
127133
)
134+
renamed_spec = cast(dict[str, Any], renamed_spec)
128135

129136
assert [evaluation["id"] for evaluation in spec["evaluations"]] == [
130137
evaluation["id"] for evaluation in renamed_spec["evaluations"]
@@ -142,6 +149,7 @@ def test_gains_v2_uses_production_prevalence_for_perfect_path():
142149
spec = _gains_v2_spec_from_performance_data(
143150
performance_data, _static_metadata(probs, reals)
144151
)
152+
spec = cast(dict[str, Any], spec)
145153

146154
assert spec["type"] == "gains"
147155
assert spec["x"] == "ppcr"
@@ -171,6 +179,7 @@ def test_gains_v2_shares_one_perfect_path_across_models():
171179
prepare_performance_data(probs, reals, by=0.25),
172180
_static_metadata(probs, reals),
173181
)
182+
spec = cast(dict[str, Any], spec)
174183

175184
assert len(spec["series"]) == 2
176185
assert len(spec["references"]) == 2
@@ -191,6 +200,7 @@ def test_gains_v2_keeps_equal_prevalence_populations_distinct():
191200
prepare_performance_data(probs, reals, by=0.25),
192201
_static_metadata(probs, reals),
193202
)
203+
spec = cast(dict[str, Any], spec)
194204

195205
perfect = spec["references"][1:]
196206
assert [reference["population"] for reference in perfect] == [

0 commit comments

Comments
 (0)