Skip to content

Commit aa6bb34

Browse files
committed
fix: honor configured phone scoring weights
1 parent 0e5eb02 commit aa6bb34

3 files changed

Lines changed: 36 additions & 5 deletions

File tree

‎.github/workflows/weekly-refresh.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
repository: GetTechAPI/TechAPI
5555
path: techapi
5656
token: ${{ secrets.TECHENGINEBOT_TOKEN || secrets.TECHAPI_TOKEN || secrets.GITHUB_TOKEN }}
57+
fetch-depth: 0
5758

5859
- uses: actions/setup-python@v6
5960
with:

‎app/services/scoring/phones.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,17 @@ def _display(display: dict[str, Any], scales: dict[str, ReferenceScale]) -> floa
124124

125125

126126
def _value(
127-
overall: float | None, msrp_usd: int | None, scales: dict[str, ReferenceScale]
127+
overall: float | None,
128+
msrp_usd: int | None,
129+
scales: dict[str, ReferenceScale],
130+
weights: dict[str, float],
128131
) -> float | None:
129132
if overall is None or not msrp_usd:
130133
return None
131134
affordability = capability(float(msrp_usd), scales["msrp_usd"])
132135
if affordability is None:
133136
return None
134-
return round(overall * 0.5 + affordability * 0.5, 1)
137+
return combine([(overall, weights["overall"]), (affordability, weights["affordability"])])
135138

136139

137140
def score_phone(
@@ -153,9 +156,16 @@ def score_phone(
153156
scales,
154157
)
155158
display = _display(phone.display, scales)
156-
components = [s for s in (perf.index, camera, battery, display) if s is not None]
157-
overall = round(sum(components) / len(components), 1) if components else None
158-
value = _value(overall, phone.msrp_usd, scales)
159+
overall_weights = cfg.weights["phone_overall"]
160+
overall = combine(
161+
[
162+
(perf.index, overall_weights["performance"]),
163+
(camera, overall_weights["camera"]),
164+
(battery, overall_weights["battery"]),
165+
(display, overall_weights["display"]),
166+
]
167+
)
168+
value = _value(overall, phone.msrp_usd, scales, cfg.weights["value"])
159169
return PhoneScore(
160170
algorithm_version=settings.scoring_algorithm_version,
161171
overall=overall,

‎tests/unit/test_scoring_phones.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
from __future__ import annotations
44

5+
from dataclasses import replace
56
from datetime import date
67

78
from app.models.smartphone import Smartphone
89
from app.models.soc import SoC
910
from app.services.scoring import ALGORITHM_VERSION, DatasetStats, score_phone
11+
from app.services.scoring.config import load_config
1012

1113

1214
def _soc(**overrides: object) -> SoC:
@@ -92,6 +94,24 @@ def test_value_requires_msrp() -> None:
9294
assert score_phone(_phone(msrp_usd=None), _soc()).value is None
9395

9496

97+
def test_overall_and_value_use_configured_weights() -> None:
98+
cfg = load_config()
99+
weights = {name: values.copy() for name, values in cfg.weights.items()}
100+
weights["phone_overall"] = {
101+
"performance": 1.0,
102+
"camera": 0.0,
103+
"battery": 0.0,
104+
"display": 0.0,
105+
}
106+
weights["value"] = {"overall": 1.0, "affordability": 0.0}
107+
configured = replace(cfg, weights=weights)
108+
109+
score = score_phone(_phone(), _soc(), config=configured)
110+
111+
assert score.overall == score.performance
112+
assert score.value == score.overall
113+
114+
95115
def test_relative_fields_filled_only_with_stats() -> None:
96116
soc = _soc()
97117
plain = score_phone(_phone(), soc)

0 commit comments

Comments
 (0)