File size: 2,204 Bytes
fdcfe30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Portable CPU inference from Proxima's structured UTC schedule inputs."""
import json
from pathlib import Path
from datetime import datetime
import lightgbm as lgb
import pandas as pd
from safetensors import safe_open
from data import features


class Proxima:
    def __init__(self,directory=None):
        self.directory=Path(directory) if directory else Path(__file__).parent
        self.config=json.loads((self.directory/'config.json').read_text(encoding='utf-8'))
        with safe_open(str(self.directory/'model.safetensors'),framework='numpy') as sf:
            self.models={t:lgb.Booster(model_str=sf.get_tensor(t+'_lightgbm_utf8').tobytes().decode('utf-8')) for t in ['arr','dep']}

    def predict_rows(self,rows):
        rows=rows.copy()
        for c in ['sd','sa']:
            values=[pd.Timestamp(v) for v in rows[c]]
            if any(v.tzinfo is None for v in values):raise ValueError('Schedules require timezone-aware timestamps')
            rows[c]=pd.to_datetime(rows[c],utc=True)
        duration=(rows.sa-rows.sd).dt.total_seconds()/60
        if not duration.between(15,1440).all():raise ValueError('Schedule durations must be 15–1440 minutes')
        for c in ['origin','destination']:
            if not rows[c].isin(self.config['airports']).all():raise ValueError('Airport outside released metadata coverage')
        if not rows.airline.astype(str).str.fullmatch('[A-Z0-9]{3}').all():raise ValueError('Use three-character airline ICAO codes')
        X,_=features(rows,self.config)
        return {t:self.models[t].predict(X,num_threads=1) for t in ['arr','dep']}


if __name__=='__main__':
    import argparse
    parser=argparse.ArgumentParser(description='Proxima research flight-delay estimates; input and output CSV')
    parser.add_argument('input',help='CSV columns airline,number,origin,destination,sd,sa; ICAO codes and offset-aware timestamps')
    parser.add_argument('output')
    args=parser.parse_args()
    rows=pd.read_csv(args.input)
    pred=Proxima().predict_rows(rows)
    rows['arrival_delay_minutes']=pred['arr'];rows['departure_delay_minutes']=pred['dep']
    rows.to_csv(args.output,index=False)