feat: Add personal reference values management in settings and API
- Introduced new routes and API endpoints for managing personal reference values. - Updated the SettingsPage to include a section for reference values with navigation to manage them. - Enhanced the backend to support reference values in the data layer and versioning. - Added necessary imports and UI components for a seamless user experience.
This commit is contained in:
1 parent
e7dedd527f
commit
f0e6fd04fb
23 files changed
+1719
-2
No files matched your search
@@ -0,0 +1,6 @@
|
||||
"""Example built-in algorithms (threshold bands, linear range mapping)."""
|
||||
|
||||
from .linear_range import linear_range_score
|
||||
from .threshold_band import threshold_band_score
|
||||
|
||||
__all__ = ["linear_range_score", "threshold_band_score"]
|
||||
@@ -0,0 +1,69 @@
|
||||
"""
|
||||
linear_range: map a value linearly from [min_value, max_value] to [0, 1], clamped.
|
||||
|
||||
params:
|
||||
value_key: str
|
||||
min_value: float
|
||||
max_value: float
|
||||
invert: bool (optional) — if True, high values map to 0
|
||||
|
||||
Missing value → score 0 and missing_inputs.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Mapping
|
||||
|
||||
from data_layer.training_profile.models import AlgorithmRunResult
|
||||
from data_layer.utils import safe_float
|
||||
|
||||
|
||||
ALGORITHM_ID = "linear_range"
|
||||
|
||||
|
||||
def linear_range_score(
|
||||
*,
|
||||
inputs: Mapping[str, Any],
|
||||
params: Mapping[str, Any],
|
||||
) -> AlgorithmRunResult:
|
||||
value_key = str(params.get("value_key", ""))
|
||||
if not value_key:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=0.0,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=["__param_value_key__"],
|
||||
detail={"error": "params.value_key required"},
|
||||
)
|
||||
|
||||
if value_key not in inputs or inputs[value_key] is None:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=0.0,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=[value_key],
|
||||
detail={"value_key": value_key},
|
||||
)
|
||||
|
||||
raw = safe_float(inputs[value_key])
|
||||
lo = safe_float(params.get("min_value"))
|
||||
hi = safe_float(params.get("max_value"))
|
||||
invert = bool(params.get("invert", False))
|
||||
|
||||
if hi <= lo:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=raw,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=[],
|
||||
detail={"error": "max_value must be > min_value", "min": lo, "max": hi},
|
||||
)
|
||||
|
||||
t = (raw - lo) / (hi - lo)
|
||||
t = max(0.0, min(1.0, t))
|
||||
if invert:
|
||||
t = 1.0 - t
|
||||
|
||||
return AlgorithmRunResult(
|
||||
raw_score=raw,
|
||||
normalized_score=t,
|
||||
missing_inputs=[],
|
||||
detail={"value_key": value_key, "min": lo, "max": hi, "invert": invert},
|
||||
)
|
||||
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
threshold_band: map a numeric value into a score using ordered upper bounds.
|
||||
|
||||
params:
|
||||
value_key: str — key in inputs to read
|
||||
bands: list of { "max": float | null, "score": float } — ascending max; null = +inf
|
||||
|
||||
Missing value_key → normalized_score 0, missing_inputs lists value_key.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, List, Mapping
|
||||
|
||||
from data_layer.training_profile.models import AlgorithmRunResult
|
||||
from data_layer.utils import safe_float
|
||||
|
||||
|
||||
ALGORITHM_ID = "threshold_band"
|
||||
|
||||
|
||||
def threshold_band_score(
|
||||
*,
|
||||
inputs: Mapping[str, Any],
|
||||
params: Mapping[str, Any],
|
||||
) -> AlgorithmRunResult:
|
||||
value_key = str(params.get("value_key", ""))
|
||||
if not value_key:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=0.0,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=["__param_value_key__"],
|
||||
detail={"error": "params.value_key required"},
|
||||
)
|
||||
|
||||
if value_key not in inputs or inputs[value_key] is None:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=0.0,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=[value_key],
|
||||
detail={"value_key": value_key},
|
||||
)
|
||||
|
||||
raw = safe_float(inputs[value_key])
|
||||
bands = params.get("bands") or []
|
||||
if not isinstance(bands, list) or not bands:
|
||||
return AlgorithmRunResult(
|
||||
raw_score=raw,
|
||||
normalized_score=0.0,
|
||||
missing_inputs=[],
|
||||
detail={"error": "params.bands must be a non-empty list", "raw": raw},
|
||||
)
|
||||
|
||||
score = 0.0
|
||||
for band in bands:
|
||||
if not isinstance(band, dict):
|
||||
continue
|
||||
max_v = band.get("max")
|
||||
s = safe_float(band.get("score"))
|
||||
if max_v is None:
|
||||
score = s
|
||||
break
|
||||
if raw <= safe_float(max_v):
|
||||
score = s
|
||||
break
|
||||
else:
|
||||
score = safe_float(bands[-1].get("score")) if isinstance(bands[-1], dict) else 0.0
|
||||
|
||||
norm = max(0.0, min(1.0, score))
|
||||
return AlgorithmRunResult(
|
||||
raw_score=raw,
|
||||
normalized_score=norm,
|
||||
missing_inputs=[],
|
||||
detail={"value_key": value_key, "bands_applied": True, "band_score": score},
|
||||
)
|
||||
Reference in new issue
Block a user