project / test
sisidhar-cisco
fix(inference): correct ratio-domain pipeline + add theory/status docs
069300f
Raw History Blame Contribute Delete
3.26 kB
import numpy as np
import pandas as pd
# --- replicate the notebook's data prep exactly ---
aqi_breakpoints = {
'pm25': [(0,50,0,50),(51,100,51,100),(101,200,101,200),(201,300,201,300)],
'pm10': [(0,50,0,50),(51,100,51,100),(101,250,101,200),(251,350,201,300)],
'co': [(0,1.0,0,50),(1.1,2.0,51,100),(2.1,10.0,101,200),(10.1,17.0,201,300)],
}
def sub_aqi(c, bps):
for il,ih,cl,ch in bps:
if cl <= c <= ch:
return il if ch==cl else ((ih-il)/(ch-cl))*(c-cl)+il
if c < bps[0][2]: return bps[0][0]
if c > bps[-1][3]: return bps[-1][1]
return np.nan
def overall_aqi(row):
s=[]
for p in ['pm25','pm10','co']:
if p in row and not pd.isna(row[p]):
s.append(sub_aqi(row[p], aqi_breakpoints[p]))
return np.nanmax(s) if s else np.nan
df = pd.read_csv('final.csv')
df['datetime'] = pd.to_datetime(df['datetime'], format="%d-%m-%Y %H:%M", errors='coerce')
df = df.dropna(subset=['datetime']).set_index('datetime').sort_index()
df = df[['temp','pm25','pm10','co']].copy()
df['calculated_aqi'] = df.apply(overall_aqi, axis=1)
df = df[['calculated_aqi','temp','pm25','pm10','co']].copy()
df = df.ffill().bfill().dropna()
n_ahead = 1
scaler_df = df.shift(n_ahead).rolling(24*14).median()
tmp_df = (df / scaler_df).iloc[24*14 + n_ahead:] # this is the RATIO the model was trained on
print("=== absolute df (last 24 rows) ===")
print(df.tail(24).to_string())
print("\n=== rolling-median denominator (last 24 rows) ===")
print(scaler_df.tail(24).to_string())
print("\n=== RATIO tmp_df (last 24 rows) — what the model actually saw ===")
print(tmp_df.tail(24).to_string())
print("\n=== ratio min/max per feature across all training data ===")
print("min:", tmp_df.min().values)
print("max:", tmp_df.max().values)
import json, numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.utils import custom_object_scope
from tkan import TKAN
# reuse df, tmp_df, scaler_df from your script above
class MMS:
def __init__(s): s.min_=s.max_=s.scale_=None; s.r=(0,1)
def load(s,a):
s.min_=np.array(a['min_']); s.max_=np.array(a['max_'])
s.scale_=np.where(np.array(a['scale_'])==0,1,np.array(a['scale_'])); s.r=tuple(a['minmax_range'])
def tf(s,X): return (X-s.min_)/s.scale_*(s.r[1]-s.r[0])+s.r[0]
def inv(s,X): return ((X-s.r[0])/(s.r[1]-s.r[0]))*s.scale_+s.min_
isc=MMS(); isc.load(json.load(open('input_scaler_attributes.json')))
tsc=MMS(); tsc.load(json.load(open('target_scaler_attributes.json')))
with custom_object_scope({"TKAN":TKAN,"tkan>TKAN":TKAN}):
m=load_model('best_model_TKAN_nahead_1.keras',compile=False)
# last 24 ratio rows as model input; the target is the ratio for the NEXT step
window = tmp_df.iloc[-25:-1][['calculated_aqi','temp','pm25','pm10','co']].values.reshape(1,24,5)
Xs = isc.tf(window)
pred_scaled = m.predict(Xs, verbose=0)
pred_ratio = tsc.inv(pred_scaled.reshape(1,1,1)).flatten()[0]
# denominator for the predicted step = scaler_df AQI at that timestamp
denom = scaler_df['calculated_aqi'].iloc[-1]
print("predicted ratio :", pred_ratio)
print("denominator (median) :", denom)
print("recovered AQI :", pred_ratio * denom)
print("actual AQI last row :", df['calculated_aqi'].iloc[-1])