persist calibration between restarts during calibration
This commit is contained in:
1 parent
8deacf9f89
commit
fc8b26b849
2 files changed
+77
-1
No files matched your search
+32
-1
@@ -276,11 +276,26 @@ async def main() -> None:
|
|||||||
|
|
||||||
if cfg.ENABLE_COLONIZATION:
|
if cfg.ENABLE_COLONIZATION:
|
||||||
state = ChamberState("colonization")
|
state = ChamberState("colonization")
|
||||||
# Load existing baseline if available
|
|
||||||
baseline = await storage.get_active_baseline(db, "colonization")
|
baseline = await storage.get_active_baseline(db, "colonization")
|
||||||
if baseline:
|
if baseline:
|
||||||
state.processor.update_baseline(baseline)
|
state.processor.update_baseline(baseline)
|
||||||
log.info(f"[colonization] Loaded existing baseline ({baseline.n_samples} samples)")
|
log.info(f"[colonization] Loaded existing baseline ({baseline.n_samples} samples)")
|
||||||
|
else:
|
||||||
|
run = await storage.get_active_run(db, "colonization")
|
||||||
|
if run:
|
||||||
|
state.run_id = run.id
|
||||||
|
seed_payloads = await storage.get_raw_payloads_for_seeding(
|
||||||
|
db, "colonization", state.run_id,
|
||||||
|
limit=cfg.BASELINE_MIN_SAMPLES * 2,
|
||||||
|
)
|
||||||
|
for p in seed_payloads:
|
||||||
|
state.baseline_builder.feed(p)
|
||||||
|
if seed_payloads:
|
||||||
|
log.info(
|
||||||
|
f"[colonization] Re-seeded baseline builder from DB — "
|
||||||
|
f"{len(seed_payloads)} stored payloads, "
|
||||||
|
f"{state.baseline_builder.sample_count} usable samples"
|
||||||
|
)
|
||||||
chambers["colonization"] = state
|
chambers["colonization"] = state
|
||||||
|
|
||||||
if cfg.ENABLE_FRUITING:
|
if cfg.ENABLE_FRUITING:
|
||||||
@@ -289,6 +304,22 @@ async def main() -> None:
|
|||||||
if baseline:
|
if baseline:
|
||||||
state.processor.update_baseline(baseline)
|
state.processor.update_baseline(baseline)
|
||||||
log.info(f"[fruiting] Loaded existing baseline ({baseline.n_samples} samples)")
|
log.info(f"[fruiting] Loaded existing baseline ({baseline.n_samples} samples)")
|
||||||
|
else:
|
||||||
|
run = await storage.get_active_run(db, "fruiting")
|
||||||
|
if run:
|
||||||
|
state.run_id = run.id
|
||||||
|
seed_payloads = await storage.get_raw_payloads_for_seeding(
|
||||||
|
db, "fruiting", state.run_id,
|
||||||
|
limit=cfg.BASELINE_MIN_SAMPLES * 2,
|
||||||
|
)
|
||||||
|
for p in seed_payloads:
|
||||||
|
state.baseline_builder.feed(p)
|
||||||
|
if seed_payloads:
|
||||||
|
log.info(
|
||||||
|
f"[fruiting] Re-seeded baseline builder from DB — "
|
||||||
|
f"{len(seed_payloads)} stored payloads, "
|
||||||
|
f"{state.baseline_builder.sample_count} usable samples"
|
||||||
|
)
|
||||||
chambers["fruiting"] = state
|
chambers["fruiting"] = state
|
||||||
|
|
||||||
if not chambers:
|
if not chambers:
|
||||||
|
|||||||
+45
@@ -393,6 +393,51 @@ async def get_active_baseline(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def get_raw_payloads_for_seeding(
|
||||||
|
db: aiosqlite.Connection,
|
||||||
|
chamber_id: str,
|
||||||
|
run_id: Optional[str],
|
||||||
|
limit: int = 20,
|
||||||
|
) -> list[STM32Payload]:
|
||||||
|
"""
|
||||||
|
Load recent STM32Payload objects from raw_json for BaselineBuilder re-seeding
|
||||||
|
on daemon restart when no established baseline exists yet.
|
||||||
|
|
||||||
|
Scoped to run_id when available so samples from a previous grow don't
|
||||||
|
contaminate the new baseline. Returns payloads oldest-first so the
|
||||||
|
BaselineBuilder sees them in chronological order.
|
||||||
|
"""
|
||||||
|
from pydantic import ValidationError
|
||||||
|
|
||||||
|
if run_id:
|
||||||
|
query = """
|
||||||
|
SELECT raw_json FROM payloads
|
||||||
|
WHERE chamber_id = ? AND run_id = ? AND ambient_valid = 1
|
||||||
|
ORDER BY received_at DESC LIMIT ?
|
||||||
|
"""
|
||||||
|
params: tuple = (chamber_id, run_id, limit)
|
||||||
|
else:
|
||||||
|
query = """
|
||||||
|
SELECT raw_json FROM payloads
|
||||||
|
WHERE chamber_id = ? AND ambient_valid = 1
|
||||||
|
ORDER BY received_at DESC LIMIT ?
|
||||||
|
"""
|
||||||
|
params = (chamber_id, limit)
|
||||||
|
|
||||||
|
async with db.execute(query, params) as cur:
|
||||||
|
rows = await cur.fetchall()
|
||||||
|
|
||||||
|
payloads: list[STM32Payload] = []
|
||||||
|
for row in reversed(rows):
|
||||||
|
try:
|
||||||
|
raw = json.loads(row["raw_json"])
|
||||||
|
payloads.append(STM32Payload.model_validate(raw))
|
||||||
|
except (json.JSONDecodeError, ValidationError, Exception) as e:
|
||||||
|
log.debug(f"[{chamber_id}] Skipping invalid seeding payload: {e}")
|
||||||
|
|
||||||
|
return payloads
|
||||||
|
|
||||||
|
|
||||||
# ── Anomalies ─────────────────────────────────────────────────────────────
|
# ── Anomalies ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
async def insert_anomaly(
|
async def insert_anomaly(
|
||||||
|
|||||||
Reference in new issue
Block a user