File size: 7,352 Bytes
124816b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Legacy optional PCA research model. The no-training mission route is documented
in docs/START_HERE.md and firmware/pretrained; it does not deploy this bootstrap.

Eight-feature PCA linear autoencoder, exported as immutable float32 C++ arrays.

PCA is a fitted, rank-constrained reconstruction model, not a hand-written safety
threshold. Its residual threshold is fitted on held-out nominal sessions. No tensor
runtime or PSRAM is needed for 8->3->8 matrix operations.
"""
import json
import hashlib
from pathlib import Path
import numpy as np

ROOT=Path(__file__).resolve().parents[1]
FEATURE_VERSION='sensor-features-1'
FEATURES=['battery_voltage_v','solar_voltage_v','solar_current_a','temperature_c',
          'ldr_fraction','accel_norm_g','gyro_norm_dps','battery_current_a']

def features(sensors):
    values=[sensors.get(k) for k in FEATURES[:5]]
    for key in ('accel_g','gyro_dps'):
        v=sensors.get(key)
        values.append(float(np.linalg.norm(v)) if v is not None and len(v)==3 else None)
    values.append(sensors.get('battery_current_a'))
    if any(v is None for v in values):return None
    x=np.asarray(values,dtype=np.float32)
    return x if np.isfinite(x).all() else None

def load(path=None):
    return json.loads(Path(path or ROOT/'models'/'onboard'/'sensor_model.json').read_text())

def infer(sensors,model=None):
    model=model or load();x=features(sensors)
    base={'model_version':model['version'],'feature_version':FEATURE_VERSION,
          'training_domain':model['training_domain'],'inference_us':None}
    if x is None:return {**base,'status':'insufficient_inputs','score':None,'category':'unavailable'}
    mean=np.asarray(model['mean'],np.float32);scale=np.asarray(model['scale'],np.float32)
    basis=np.asarray(model['basis'],np.float32)
    z=(x-mean)/scale;reconstruction=(z@basis.T)@basis
    score=float(np.mean((z-reconstruction)**2))
    return {**base,'status':'ok','score':score,'category':'anomaly' if score>model['threshold'] else 'nominal'}

def synthetic_sessions():
    """Deterministic engineering assumptions, not recorded or operational sensor data.

    Solar current follows light, panel voltage has a daylight knee, temperature
    lags illumination, battery voltage follows a charge/load cycle. A stationary
    platform sees gravity and low gyro rates. Session variation uses deterministic
    periodic perturbations; faults are separate, interpretable held-out injections.
    """
    rows=[]
    for session in range(12):
        for step in range(240):
            t=step/239;sun=max(0,np.sin(np.pi*t))
            ldr=np.clip(sun+.015*np.sin(step*.71+session),0,1)
            solar_i=.65*sun*(.95+.04*np.sin(session+step*.13))
            load=.18+.05*np.sin(step*.09+session)**2
            battery_i=load-solar_i*.8
            row=dict(zip(FEATURES,[3.65+.45*sun+.03*np.sin(step*.07+session),
                 5.4*sun/(sun+.12) if sun else 0,solar_i,25+12*sun+.8*np.sin(step*.02+session),
                 ldr,1+.008*np.sin(step*.11+session),.35+.15*np.sin(step*.17+session)**2,battery_i]))
            rows.append({'session_id':f'{session:06d}','timestamp':f'{session:02d}:{step:03d}',
                         'label':'normal',**row})
    return rows

def train(rows,domain,rank=3):
    sessions=sorted({r['session_id'] for r in rows})
    if len(sessions)<6:raise ValueError('At least six independent chronological recording sessions required')
    n=len(sessions);train_ids=set(sessions[:int(n*.6)]);val_ids=set(sessions[int(n*.6):int(n*.8)]);test_ids=set(sessions[int(n*.8):])
    def matrix(ids):return np.asarray([[float(r[k]) for k in FEATURES] for r in rows if r['session_id'] in ids and r['label']=='normal'],dtype=np.float64)
    a,b,c=matrix(train_ids),matrix(val_ids),matrix(test_ids)
    if min(map(len,(a,b,c)))<20 or not all(np.isfinite(x).all() for x in (a,b,c)):
        raise ValueError('Each split needs >=20 complete finite normal examples')
    mean=a.mean(0);scale=np.maximum(a.std(0),1e-3)
    _,_,v=np.linalg.svd((a-mean)/scale,full_matrices=False);basis=v[:rank]
    # Canonical component signs make regeneration stable across LAPACK implementations.
    for row in basis:
        if row[np.argmax(abs(row))]<0:row*=-1
    residual=lambda x:np.mean((((x-mean)/scale)-(((x-mean)/scale)@basis.T)@basis)**2,axis=1)
    threshold=float(np.quantile(residual(b),.99))
    checksum=hashlib.sha256(json.dumps(rows,sort_keys=True).encode()).hexdigest()
    return {'version':'sensor-pca-1-'+domain+'-'+checksum[:8],'dataset_sha256':checksum,'feature_version':FEATURE_VERSION,'features':FEATURES,'rank':rank,
        'training_domain':domain,'mean':mean.astype('float32').tolist(),'scale':scale.astype('float32').tolist(),
        'basis':basis.astype('float32').tolist(),'threshold':threshold,
        'evaluation':{'split_unit':'chronological session; no session crosses splits','train_sessions':sorted(train_ids),
            'validation_sessions':sorted(val_ids),'test_sessions':sorted(test_ids),'train_samples':len(a),'validation_samples':len(b),'test_samples':len(c),
            'test_normal_false_positive_fraction':float(np.mean(residual(c)>threshold)),
            'scope':'Synthetic assumptions only; no hardware accuracy established' if domain=='synthetic_bootstrap' else 'Recorded sessions; deployment-domain coverage still requires review'},
        'runtime':'Portable float32 C++ linear autoencoder; no external tensor runtime',
        'parameter_bytes_float32':int((mean.size+scale.size+basis.size+1)*4),
        'scratch_bytes_float32':int((8+rank+8)*4),
        'limitations':['Incomplete inputs skip inference; no zero imputation','Stationary engineering bootstrap is not an orbital dynamics model',
            'Residual is not a probability','Season, component tolerances and operating modes require real recordings',
            'Independent safety limits remain separate from learned anomaly inference']}

def export(model,output,header):
    output=Path(output);header=Path(header);output.parent.mkdir(parents=True,exist_ok=True);header.parent.mkdir(parents=True,exist_ok=True)
    output.write_text(json.dumps(model,indent=2)+'\n')
    literal=lambda x:f'{float(x):.9e}f'
    array=lambda name,x:f'static constexpr float {name}[{len(x)}] = {{'+','.join(map(literal,x))+'};\n'
    text='#pragma once\n// Generated by ai/train_sensor_model.py. Synthetic bootstrap is NOT validated hardware AI.\nnamespace sensor_model {\n'
    text+=f'static constexpr const char* VERSION = "{model["version"]}";\n'
    text+=f'static constexpr const char* DOMAIN = "{model["training_domain"]}";\n'
    text+=f'static constexpr int N=8, R={model["rank"]};\n'
    text+=array('MEAN',model['mean'])+array('SCALE',model['scale'])+array('BASIS',np.asarray(model['basis']).ravel())
    text+=f'static constexpr float THRESHOLD={literal(model["threshold"])};\n}}\n'
    # A nonzero reconstruction witness catches broken parameter/order exports on boot.
    raw=np.asarray(model['mean'],np.float32).copy();raw[3]+=5
    sample={k:float(raw[i]) for i,k in enumerate(FEATURES[:5])}
    sample.update(accel_g=[0.,0.,float(raw[5])],gyro_dps=[0.,0.,float(raw[6])],battery_current_a=float(raw[7]))
    text=text[:-2]+array('SELF_TEST_RAW',raw)+f'static constexpr float SELF_TEST_SCORE={literal(infer(sample,model)["score"])};\n}}\n'
    header.write_text(text)