fixed calibration data not loading due to path

This commit is contained in:
ykcheungab
2026-09-20 11:00:33 +08:00
parent 14fbfec62e
commit 1cf78c9d71
5 changed files with 448 additions and 25 deletions
+60 -4
View File
@@ -217,6 +217,9 @@ class Sensor:
self._force_raw = (0.0, 0.0, 0.0)
self.calibration: Optional[cal_mod.Calibration] = None
self._force_cal_sensor_name: Optional[str] = None
#: Why no calibration was applied, when a bundle existed but carried no
#: usable coefficients. ``None`` means the question never came up.
self._force_cal_attempts: Optional[list[dict]] = None
self._init_force_calibration()
# ---------------------------------------------------------------
@@ -330,10 +333,11 @@ class Sensor:
if self.verbose:
if self.calibration is not None and self.calibration.source_path is not None:
print(f"Force calibration: {self.calibration.source_path}")
elif self._force_cal_sensor_name:
print(
f"Force calibration: sensors/{self._force_cal_sensor_name}/force_calibration.json"
)
elif self._force_cal_attempts is not None:
# A bundle was resolved for this sensor type, so "file missing"
# would be the wrong story: name the bundle and the roots that
# were searched instead.
print(self._force_calibration_note())
else:
print("Force calibration: raw (no force_calibration.json)")
@@ -1258,22 +1262,71 @@ class Sensor:
ordered.append(name)
return ordered
def _force_calibration_note(self) -> str:
"""Explain a bundle that resolved but carried no usable coefficients.
The old message ("raw (no force_calibration.json)") reads as "the file is
missing", which is exactly the wrong story when the lookup found a
bundle from another root and stopped there without ever asking whether
that root had coefficients.
"""
attempts = self._force_cal_attempts or []
found = [entry for entry in attempts if entry.get("found_root") is not None]
with_cal = [entry for entry in found if entry.get("force_calibration_json")]
chosen = (with_cal or found or attempts or [None])[0]
searched: list = []
for entry in attempts:
for root in entry.get("searched") or ():
if root not in searched:
searched.append(root)
lines: list[str] = []
if chosen is None:
lines.append("Force calibration: raw (no sensor bundle resolved)")
else:
root = chosen["found_root"]
if chosen.get("force_calibration_json"):
lines.append(
f"Force calibration: raw (a bundle was found at {root}, but its "
"force_calibration.json could not be loaded)"
)
else:
lines.append(
f"Force calibration: raw (bundle found at {root} has no "
"force_calibration.json)"
)
if searched:
lines.append(
" roots searched: " + "; ".join(str(root) for root in searched)
)
return "\n".join(lines)
def _init_force_calibration(self) -> None:
"""Load force calibration from ``cal_path`` or the product bundle."""
self.calibration = None
self._force_cal_sensor_name = None
self._force_cal_attempts = None
if self.cal_path is not None and str(self.cal_path).strip():
# Explicit path: missing/invalid raises (no silent fallback).
self.calibration = cal_mod.load(self.cal_path, strict=True)
return
attempts: list[dict] = []
for name in self._bundle_name_candidates():
cal = cal_mod.load(sensor_type=name)
if cal is not None:
self.calibration = cal
self._force_cal_sensor_name = name
self._force_cal_attempts = None
return
try:
attempts.append(configs.describe_bundle_resolution(name))
except Exception: # noqa: BLE001 - diagnostics must never break loading
continue
# Deliberately NOT setting ``_force_cal_sensor_name``: callers read a
# non-None value there as "this sensor is calibrated", and a bundle
# without coefficients is not that.
self._force_cal_attempts = attempts or None
@property
def force_calibrated(self) -> bool:
@@ -1292,11 +1345,14 @@ class Sensor:
if cal is None:
self.calibration = None
self._force_cal_sensor_name = None
self._force_cal_attempts = None
return
if isinstance(cal, cal_mod.Calibration):
self.calibration = cal
self._force_cal_attempts = None
return
self.calibration = cal_mod.load(cal, strict=True)
self._force_cal_attempts = None
def _apply_force_filter(self):
pre_filter = np.asarray(self._force_raw, dtype=np.float64)