FOXES / docs /python_api.md
griffingoodwin04's picture
Fixed dependency issues and notebook errors.
57e33cc
|
Raw History Blame Contribute Delete
10.5 kB

Getting Started

This page takes gives you a nice overview on how to get started with the FOXES package. Every method and their default kwargs is listed in the API Reference.

How the Pieces Fit

Every workflow follows the same four steps:

ObservationFetcher().fetch(time)   ──►  Observation
                                             │
FOXESModel().predict(observation)  ◄─────────┘
                                             │
                                             ▼
                                         Prediction  ──►  .total_flux, .ground_truth, .flux_map, .harp_contributions
                                                          .plot_flux_map(), .plot_uncertainty_map()
  1. Get data. An {py:class}~helio_foxes.Observation is one processed AIA stack plus any HARP and GOES data. Get it from {py:class}~helio_foxes.ObservationFetcher, load saved ones with {py:func}~helio_foxes.load_observations, or wrap your own array with {py:meth}Observation.from_array <helio_foxes.Observation.from_array>.
  2. Load a model. Create a {py:class}~helio_foxes.FOXESModel. It downloads the current checkpoint the first time. Load it once and reuse it.
  3. Predict. {py:meth}FOXESModel.predict <helio_foxes.FOXESModel.predict> turns an Observation into a {py:class}~helio_foxes.Prediction. For a time range, {py:meth}~helio_foxes.FOXESModel.predict_time_series turns many observations into a {py:class}~helio_foxes.PredictionTimeSeries.
  4. Look at the results. Read the numbers from the Prediction and call plot_flux_map or plot_uncertainty_map. For a PredictionTimeSeries, get the light curves as DataFrames and call plot_light_curves.

Install

Use standard (GIL-enabled) CPython in a fresh virtual environment. Python 3.14 is a supported choice on Apple Silicon, Linux, and Windows x86-64.

Platform Python Wheel availability
Apple Silicon macOS 14+ 3.11–3.14 Use an arm64 Python interpreter.
Intel macOS 3.11–3.12 PyPI's Intel PyTorch wheels end at 2.2.2 and require NumPy 1.x. FOXES selects the compatible NumPy series.
glibc-based Linux x86-64 or ARM64 3.11–3.14 Use x86_64 or aarch64 Python to match the host. Current PyTorch Linux wheels require glibc 2.28 or newer.
Windows x86-64 3.11–3.14 Use an AMD64 Python interpreter.

Python 3.15 is pending wheel availability for the dependency stack. Native Windows ARM64 also has upstream wheel gaps, including PyERFA.

On Apple Silicon, platform.machine() below should return arm64. If it returns x86_64, switch to a native ARM interpreter in a terminal running without Rosetta and create a new environment. Pip selects wheels for the running interpreter. In a notebook, run the same diagnostic in the kernel with import platform, sys; print(sys.executable, platform.machine()).

python -c "import platform, sys; print(sys.executable, platform.machine())"
python -m venv .venv

Activate it with source .venv/bin/activate on macOS or Linux, or .\.venv\Scripts\Activate.ps1 in Windows PowerShell. Then install FOXES.

python -m pip install --upgrade pip
python -m pip install helio-foxes
python -m pip check

The model weights aren't bundled with the package. FOXESModel() downloads the current checkpoint the first time you use it and caches it in ~/.cache/helio_foxes (set HELIO_FOXES_CACHE to use another folder). Each new FOXESModel("uncertainty") checks for updated published weights and reuses its cache when unchanged. If the check cannot reach the server, it logs a warning and uses the cache. Importing the package does not download weights. To use a specific local checkpoint, pass its file path instead. The older checkpoints are listed under Models and Checkpoints.

You'll also need an email address registered with JSOC to download AIA data. If you don't have one yet, you can register here; it only takes a minute. Set it once in your environment so you don't have to pass it every time:

export JSOC_EMAIL="you@example.com"

Or pass email="you@example.com" to ObservationFetcher.

Predict One Observation

Loading the model is probably the slowest part, so do it once and reuse it.

import matplotlib.pyplot as plt
import helio_foxes

model = helio_foxes.FOXESModel()
fetcher = helio_foxes.ObservationFetcher()
observation = fetcher.fetch("2023-08-01T00:00:00Z")
print("AIA stack shape:", observation.aia_data.shape)
result = model.predict(observation)

print(f"Predicted GOES XRS-B flux: {result.total_flux:.3e} W/m²")
if result.ground_truth is not None:
    print(f"Measured GOES XRS-B flux: {result.ground_truth:.3e} W/m²")
print("68% interval:", result.prediction_intervals["68"])
result.plot_flux_map()
plt.show()

The prediction fields are metadata, total_flux, flux_map, prediction_intervals, flux_std_map, harp_contributions, ground_truth, legacy_correction_enabled, and flux_scale_factor.

observation.aia_data holds the processed AIA stack; it is None when AIA fetching is disabled. The observation fields are metadata, aia_data, sxr_data, and harp_regions, in that order. sxr_data contains the measured GOES data and its provenance.

result.flux_map holds the per-patch contribution to the soft x-ray flux... they add up to result.total_flux. The boxes drawn on the map are HARP active regions; the flux inside each HARP is saved in result.harp_contributions. result.ground_truth is the matched measured GOES XRS-B flux in W/m², for comparison (None if no sample is available). GOES XRS-B covers the 1–8 Å band.

Each HARP row contains total_flux and flux_std in W/m², alongside its region ID and bounding box. flux_std is the square root of the summed patch variance contributions inside the box. These are model-assigned uncertainty attributions; regional calibration hasn't been established. flux_std is None for deterministic checkpoints. HARP light-curve tables also include lower_68, upper_68, lower_95, and upper_95 in W/m². These bounds are computed from the raw regional flux and variance when the table is created. Missing uncertainty gives NaN bounds.

The current checkpoint also gives you nominal 68% and 95% full-disk prediction_intervals. The intervals are asymmetric in W/m² because the model’s predictive Gaussian and full-disk variance output are defined in normalized log10(flux + 1e-8) space. To calculate other prediction intervals, first compute mean ± z * sigma, with both quantities expressed in that same normalized space. Then inverse-transform each endpoint to W/m² by undoing the normalization, exponentiating with base 10, and subtracting the 1e-8 offset.

Plot the contributions with result.plot_uncertainty_map(). HARP light-curve bands use an unshifted log-normal with each region's own flux as its median and its assigned raw variance. The regional calculation reuses the model's log-normal variance conversion; the resulting bounds are stored in W/m². See Uncertainties for the calculation and Results for full-disk coverage on the test set.

Predict a Time Range

Using the same model and fetcher:

observations = fetcher.fetch_range(
    "2023-08-01 00:00", "2023-08-01 01:00",
    cadence="12min", save_dir="saved_observations", progress=True,
)
series = model.predict_time_series(observations)
series.plot_light_curves(show_full_disk=True, show_observed=True)
plt.show()

series.full_disk_light_curve().to_csv("predictions.csv")
harp_curves = series.harp_light_curves()

Full-disk light-curve tables use total_flux and ground_truth columns in W/m². When predictions have intervals, the tables also include lower_68, upper_68, lower_95, and upper_95, copied from each prediction. plot_light_curves reads the tables' lower_68 and upper_68 columns for shading, including the full-disk curve when show_full_disk=True. show_uncertainty=False hides all shading.

Observations are downloaded and processed one at a time, so long ranges won't fill up your memory. Anything already in save_dir is reused instead of downloaded again.

fetch_range returns a generator, so it can only be looped over once. After predict_time_series has used it, observations is empty. Call fetch_range again (it reuses save_dir), or use list(observations) if you need them more than once and they fit in memory.

Reuse Saved Observations

Once you've saved a range, you can run the model on it again without downloading anything:

saved = helio_foxes.load_observations("saved_observations")
series = model.predict_time_series(saved)

Use Your Own Data

If you already have a processed AIA stack:

import numpy as np

observation = helio_foxes.Observation.from_array(
    np.load("my_processed_stack.npy"), timestamp="2023-08-01 12:00",
)
result = model.predict(observation)

The current checkpoint expects an array of shape (7, 512, 512) with channels in the order [94, 131, 171, 193, 211, 304, 335] Å. from_array uses the array as is, with no resizing, cropping, or normalization, so it needs to match the training preprocessing already.

For raw FITS files on disk, run them through {py:meth}helio_foxes.AIAProcessor.process_files first. The notebook walkthrough has more plotting and data-loading examples.

FOXES bundles the ITI AIA calibration table, so AIA preprocessing does not need to download it. A valid existing ~/.iti/sdo_autocal_table.csv is preferred; a missing or invalid cache uses the packaged copy without writing to your home folder. The bundled snapshot covers May 2010 through July 2020. ITI's nearest-row correction is unchanged, including use of the nearest endpoint for observations outside that range.

Older Checkpoints

The older deterministic checkpoints need a correction for their outdated training targets. Loading one by name, e.g. FOXESModel("inverted-attention-mask"), turns it on for you; if you load one from a file path, pass legacy_correction_enabled=True. Legacy Models explains what the correction does.