--- language: - en tags: - solar-flare-forecasting - space-weather - knowledge-distillation - pytorch license: mit --- # Binary Solar Flare Forecasting Corrected-data binary solar flare forecasting for chronological Settings A and B. This repository contains HMI-only CNN baselines, an AIA/HMI MDT spatial teacher, an HMI3D-CTL temporal teacher, and single- or dual-teacher knowledge distillation. The task is to predict whether at least one **M1.0+** flare starts during **(t, t + 24 h]**. The distilled HMI2D student needs only the current HMI magnetogram at inference. ## Released models Each setting contains these four models: | Method identifier | Inference input | Description | |---|---|---| | `HMI2D` | Current HMI magnetogram | Undistilled student baseline | | `MDT2D` | Current HMI + AIA 94/193/304 Å | Spatial teacher with corrected squared orthogonality loss | | `HMI3D_CTL` | Five hourly HMI magnetograms | Temporal teacher trained with supervised contrastive learning | | `dual_selected_logit_KD` | Current HMI magnetogram | HMI2D student distilled from MDT2D and HMI3D-CTL | ## Layout ```text configs/ Shared training settings and A/B-specific settings training.json settingA.json settingB.json data/ FITS packing, HDF5 alignment, manifests and KD cache extraction manifests/{A,B}/ Published train/validation/test sample manifests models/{A,B}/ Eight seed42 weight files and their JSON metadata scripts/ run_experiment.py Unified training and evaluation entry predict.py Inference on preprocessed tensors common.py Shared script imports src/binaryforecast/ api.py Configuration generation and checkpoint resolution core/ model/ Network architectures and backbones loss/ Focal, MDT, CTL and KD losses utils/ Datasets, time protocols, labels and metrics training/ baseline/ Supervised 2D/3D CNN training teacher/ MDT and CTL training kd/ Single- and dual-teacher training registry.json Released checkpoint identities and checksums ``` ## Installation Use Python 3.10 or newer with an environment compatible with the pinned dependencies. The supplied requirements target PyTorch 2.5.1 with CUDA 12.1: ```bash python -m pip install --extra-index-url https://download.pytorch.org/whl/cu121 -r requirements.txt python -m pip install -e . --no-deps ``` For raw FITS preprocessing, also install: ```bash python -m pip install -r requirements-preprocess.txt ``` Training uses CUDA. Released checkpoints can be loaded for CPU inference. Exact numerical agreement across different hardware and dependency versions is not guaranteed. ## Data and labels AIA images (94, 193 and 304 Å) and HMI magnetograms are available from [NASA SDO](https://sdo.gsfc.nasa.gov/data/dataaccess.php). GOES flare metadata are provided by [Surya-Bench](https://huggingface.co/datasets/nasa-ibm-ai4science/surya-bench-flare-forecasting). A sample is positive if an **M1.0+ flare starts within `(t, t+24h]`**; otherwise, it is negative. Times use UTC. Invalid samples are excluded, and the final **96 hours** of training and validation intervals are removed. All models use the same five-frame-eligible samples; 2D models read only the current image. | Setting | Training | Validation | Test | Samples: train / val / test | |---|---|---|---|---| | A | 2011-01-01–2013-07-01 | 2013-07-01–2014-01-01 | 2014-01-01–2015-01-01 | 20,192 / 4,050 / 7,672 | | B | 2011-01-01–2014-01-01 | 2014-01-01–2015-01-01 | 2015-01-01–2017-01-01 | 24,342 / 7,600 / 15,884 | Intervals exclude their right endpoints and precede eligibility filtering. ### Data preparation To use the published manifests, provide `--data-root` with matching HDF5 filenames, or `--path-map mapping.json` for custom paths. HDF5 files contain `images` and `times`, with images shaped `(N,C,512,512)`: HMI has one channel; AIA has three channels ordered 94, 193, 304 Å. To rebuild, convert FITS files using `data/pack_*.py`, then run: ```bash python data/prepare.py \ --source 2011_2013 /path/to/aia_2011_2013.h5 /path/to/hmi_2011_2013.h5 \ --source 2014_2016 /path/to/aia_2014_2016.h5 /path/to/hmi_2014_2016.h5 \ --events /path/to/flare_catalog_2010-2024.csv \ --coverage-start 2010-05-01 --coverage-end 2025-01-01 \ --setting all --output runs/prepared_data \ --reference-root manifests ``` Use the [event-level catalogue](https://github.com/NASA-IMPACT/SuryaBench/blob/main/flare/flare_catalog_2010-2024.csv) for `--events`; Hugging Face’s window-level `data.csv` is not a compatible input. Set coverage bounds to match the catalogue. Alignment uses nearest HMI observations within 30 minutes, followed by input-time checks. `--reference-root` verifies agreement with published samples; omit it for a different dataset. Update the setting configurations to use newly generated manifests. ## Load a released model After installation: ```python import torch from binaryforecast import artifact, load_model model = load_model(artifact('A/dual_selected_logit_KD/seed42'), device='cpu') # hmi: preprocessed float32 tensor, shape (batch, 1, 512, 512). with torch.inference_mode(): logits = model(hmi) probability_m1_plus = logits.softmax(dim=1)[:, 1] ``` For the command-line interface, save a dictionary of preprocessed tensors in a PyTorch file. HMI-only input uses key `HMI`; MDT additionally uses key `AIA`. Then run: ```bash python scripts/predict.py \ --artifact A/dual_selected_logit_KD/seed42 \ --input /path/to/preprocessed_batch.pt \ --device cpu --output predictions.npz ``` This writes logits, probabilities and predictions to `runs/predictions.npz`. For HMI3D-CTL, the HMI tensor is `(batch,1,5,512,512)`, ordered from `t-4h` to `t`; 2D HMI and AIA tensors are `(batch,1,512,512)` and `(batch,3,512,512)`. ## Train and evaluate All commands below run from the repository root. Change `--setting A` to `B` to use Setting B, including its CTL and dual-teacher parameters. Preview a run without training: ```bash python scripts/run_experiment.py --setting A --method HMI2D --seed 42 ``` Add `--execute` and a data location to train. For a fresh run of the released model chain, train these methods with the same run tag: ```bash for method in HMI2D AIA2D HMI3D_CTL MDT2D; do python scripts/run_experiment.py \ --setting A --method "$method" --seed 42 --gpu 0 \ --data-root /path/to/hdf5 --run-tag retrain --execute || break done ``` Extract the dual-teacher cache and train the student: ```bash CUDA_VISIBLE_DEVICES=0 python data/extract_cache.py \ --artifact A/dual_selected_logit_KD/seed42 \ --data-root /path/to/hdf5 --run-tag retrain \ --output runs/retrain/cache --device cuda python scripts/run_experiment.py \ --setting A --method dual_selected_logit_KD --seed 42 --gpu 0 \ --data-root /path/to/hdf5 --run-tag retrain \ --cache-dir runs/retrain/cache --execute ``` Caches contain training/validation teacher outputs and must match the checkpoint hashes and manifest identity. Use the same run tag and checkpoint source for extraction and KD training. Both teachers are frozen during extraction; only the student is optimized during KD. Evaluate a newly trained run separately: ```bash python scripts/run_experiment.py \ --setting A --method dual_selected_logit_KD --seed 42 \ --phase evaluate --gpu 0 --data-root /path/to/hdf5 \ --run-tag retrain --execute ``` ### Single-teacher KD ```bash CUDA_VISIBLE_DEVICES=0 python data/extract_cache.py \ --artifact A/MDT2D_to_HMI2D_logits/seed42 \ --data-root /path/to/hdf5 --run-tag retrain \ --output runs/retrain/cache --device cuda python scripts/run_experiment.py \ --setting A --method MDT2D_to_HMI2D_logits --seed 42 --gpu 0 \ --data-root /path/to/hdf5 --run-tag retrain \ --cache-dir runs/retrain/cache --execute ``` ## License Original code and released model weights for ST-DTKD are available under the [MIT License](LICENSE). Copyright (c) 2026 ST-DTKD contributors. Third-party material remains subject to its original license and attribution requirements; this license does not relicense external SDO or Surya-Bench datasets.