"""Usage : python tclab-transfert.py tclab-data.csv jumeau-tclab.json [sortie.csv].""" import csv import hashlib import json import math import statistics import sys from pathlib import Path source = Path(sys.argv[1]) assert hashlib.sha256(source.read_bytes()).hexdigest() == 'fcbca9d3ce49f22f7f9edb2ef8c8d4afa7cf71fbc10c00630a624ba3dc7cd20a' with source.open(newline='') as f: rows = list(csv.DictReader(f)) assert len(rows) == 800 and {float(r['Q1']) for r in rows} == {50} and {float(r['Q2']) for r in rows} == {0} params = json.loads(Path(sys.argv[2]).read_text())['params'] initial, t0 = float(rows[0]['T1']), float(rows[0]['Time']) times = [float(r['Time']) - t0 for r in rows] assert all(a < b for a,b in zip(times,times[1:])) predictions = {} for k in ('mono','cascade','persistance'): if k == 'persistance': p = [initial] * len(times) else: d = params[k] tau, s, gain = d['tau'], d['tau_s'], d['gain'] / 2 p = [initial + gain * (1-math.exp(-t/tau)) if not s else initial + gain * (1-(tau*math.exp(-t/tau)-s*math.exp(-t/s))/(tau-s)) for t in times] predictions[k] = p # Le calcul des prédictions ci-dessus ne lit aucune température après le premier point. observed = [float(r['T1']) for r in rows] for k, p in predictions.items(): print(k, f'MAE {statistics.mean(abs(a-b) for a,b in zip(p[1:],observed[1:])):.4f} °C, fin {p[-1]:.4f} °C') if len(sys.argv) > 3: with Path(sys.argv[3]).open('w', newline='') as f: writer=csv.writer(f) writer.writerow(['temps_releve_s','mesure_C','un_etat_C','deux_etats_C','persistance_C']) writer.writerows(zip([r['Time'] for r in rows], observed, *(map(lambda x: round(x,6),p) for p in predictions.values())))