File size: 2,961 Bytes
21419ae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import pathlib,sys,argparse,json,hashlib
sys.path.insert(0,str(pathlib.Path(__file__).resolve().parents[1]/'src'))
from binaryforecast.api import ROOT
from binaryforecast.core.utils.vendor.protocol import specification,annotate,verify_frames,eligible
from binaryforecast.core.utils.vendor.surya_labels import event_targets
import pandas as pd
p=argparse.ArgumentParser(description='Existing aligned HDF5 index + GOES event catalogue -> binary A/B manifests; no second rolling labels.');p.add_argument('--index',required=True);p.add_argument('--events',required=True);p.add_argument('--setting',choices=['A','B'],required=True);p.add_argument('--output',required=True);p.add_argument('--reference-manifest');p.add_argument('--coverage-start',required=True);p.add_argument('--coverage-end',required=True);args=p.parse_args()
f=pd.read_csv(args.index);f['timestamp']=pd.to_datetime(f.timestamp);assert f.timestamp.is_unique
labels,_=event_targets(f.timestamp,pd.read_csv(args.events),24,args.coverage_start,args.coverage_end);f=f.drop(columns=[c for c in labels if c!='timestamp'and c in f]);f=f.merge(labels,on='timestamp',validate='one_to_one');f['target_binary']=(f.max_flare_rank>=2).astype(int)
bounds=[['2011-01-01','2013-07-01'],['2013-07-01','2014-01-01'],['2014-01-01','2015-01-01']]if args.setting=='A'else[['2011-01-01','2014-01-01'],['2014-01-01','2015-01-01'],['2015-01-01','2017-01-01']];spec=specification([-4,-3,-2,-1,0],24);frames={}
for i,(split,(start,end))in enumerate(zip(['train','validation','test'],bounds)):
cutoff=pd.Timestamp(end)-pd.Timedelta(hours=96)if i<2 else pd.Timestamp(end);part=f[(f.timestamp>=start)&(f.timestamp<cutoff)].copy()
for c in ['aia_h5','hmi_h5']:part[c]=part[c].map(lambda x:pathlib.Path(str(x)).name)
frames[split]=annotate(part,spec,start,end)
verify_frames(frames,spec);out=pathlib.Path(args.output);out.mkdir(parents=True,exist_ok=False);files={}
for split,frame in frames.items():
path=out/(split+'_frames.csv');frame.to_csv(path,index=False);files[path.name]=hashlib.sha256(path.read_bytes()).hexdigest()
if args.reference_manifest:
reference=pathlib.Path(args.reference_manifest)
for split,f in frames.items():
expected=pd.read_csv(reference/(split+'_frames.csv'));cols=['sample_id','target_binary','sequence_valid','sequence_indices'];assert f[cols].reset_index(drop=True).equals(expected[cols].reset_index(drop=True)),split
protocol={'input':spec,'bounds':bounds,'tail_buffer_hours':96,'label_col':'target_binary'};ident=hashlib.sha256(json.dumps({'protocol':protocol,'files':files},sort_keys=True).encode()).hexdigest();ident=json.loads((pathlib.Path(args.reference_manifest)/'manifest.json').read_text())['manifest_id']if args.reference_manifest else ident;(out/'manifest.json').write_text(json.dumps({'schema':'portable_binary_v1','manifest_id':ident,'protocol':protocol,'files':files},indent=2));print(json.dumps({'samples':{s:len(eligible(x))for s,x in frames.items()},'manifest_id':ident}))
|