saef-api / ai /make_sensor_notebook.py
m4az19's picture
Deploy SAHAB-1 backend and pretrained AI
124816b verified
Raw History Blame Contribute Delete
3.89 kB
"""Package the exact Python training/export pipeline into a Kaggle notebook."""
import json
from pathlib import Path
def main():
base=Path(__file__).parent;cells=[]
def md(value):cells.append({'cell_type':'markdown','metadata':{},'source':value.splitlines(True)})
def code(value):cells.append({'cell_type':'code','metadata':{},'source':value.splitlines(True),'outputs':[],'execution_count':None})
md('''# SAAMurais: onboard sensor anomaly model
This compact learned **PCA linear autoencoder** runs directly in the Freenove S3 firmware as float32 matrix operations. It is separate from the ground-only water-image network. No GPU or TensorFlow runtime is needed for this 8 → 3 → 8 model.
The default dataset is an explicitly **synthetic engineering bootstrap**, not real sensor validation. It models correlated light/solar/temperature/power traces and a stationary IMU. Test statistics on these synthetic sessions are not operational accuracy. For real training, attach your labelled recording CSV and set `CSV_PATH` below.
Required CSV: `timestamp,session_id,label,battery_voltage_v,solar_voltage_v,solar_current_a,temperature_c,ldr_fraction,accel_norm_g,gyro_norm_dps,battery_current_a`. Units: V, V, A, °C, 0–1 ADC ratio, g, degrees/second, A. Label verified nominal rows `normal`. Use independent chronological recording sessions covering temperature, light, loading, charging and motion modes. Training uses nominal sessions only; threshold selection uses validation sessions; final nominal false-positive fraction uses separate test sessions. Keep anomalous recordings for independent fault evaluation. Never split overlapping traces across sessions.
''')
code('%pip install -q numpy==2.1.3\n')
code("from pathlib import Path\nimport os, sys, subprocess, json\nWORK=Path('/kaggle/working/saamurais')\n(WORK/'ai').mkdir(parents=True,exist_ok=True)\nos.chdir(WORK)\n(WORK/'ai/__init__.py').write_text('')\n")
for name in ('sensor_model.py','train_sensor_model.py'):
code(f"(WORK/'ai/{name}').write_text("+repr((base/name).read_text(encoding='utf-8'))+",encoding='utf-8')\n")
code("CSV_PATH = None # e.g. '/kaggle/input/my-telemetry/recordings.csv'\nargs=['--csv',CSV_PATH] if CSV_PATH else ['--synthetic']\nprint('REAL RECORDINGS' if CSV_PATH else 'SYNTHETIC BOOTSTRAP: NOT HARDWARE VALIDATED')\nsubprocess.run([sys.executable,'-m','ai.train_sensor_model',*args],check=True)\n")
code("model=json.loads((WORK/'models/onboard/sensor_model.json').read_text())\nprint(json.dumps(model,indent=2))\nimport shutil\nEXPORT=Path('/kaggle/working/onboard-export');EXPORT.mkdir(exist_ok=True)\nfor p in (WORK/'models/onboard').glob('*.json'):shutil.copy2(p,EXPORT/p.name)\nshutil.copy2(WORK/'firmware/include/sensor_model_data.h',EXPORT/'sensor_model_data.h')\nshutil.make_archive('/kaggle/working/saamurais-onboard','zip',EXPORT)\n")
md('''## Deploy and verify
Download `saamurais-onboard.zip`. Put its JSON files in local `models/onboard/` and its header in `firmware/include/`. Run `python -m scripts.verify_onboard`, the backend tests, and the firmware build before flashing. Confirm the pin map and runtime flash/PSRAM logs on your actual board. Firmware performs a known reconstruction self-test, skips incomplete samples, and transmits the model version, domain, score, category and measured inference time. Host timing is not ESP32 timing. Threshold safety checks remain separate. Do not deploy to a new operating mode without recorded validation.
''')
notebook={'cells':cells,'metadata':{'kernelspec':{'display_name':'Python 3','language':'python','name':'python3'},'language_info':{'name':'python'},'kaggle':{'isInternetEnabled':True}},'nbformat':4,'nbformat_minor':5}
target=base/'kaggle_sensor_anomaly.ipynb';target.write_text(json.dumps(notebook,indent=1),encoding='utf-8');print(target)
if __name__=='__main__':main()