Skip to content

Add GPR loader and unit tests#5

Open
vtommasini wants to merge 1 commit into
sxs-collaboration:mainfrom
vtommasini:gpr-loader-and-tests
Open

Add GPR loader and unit tests#5
vtommasini wants to merge 1 commit into
sxs-collaboration:mainfrom
vtommasini:gpr-loader-and-tests

Conversation

@vtommasini
Copy link
Copy Markdown

Loads the saved GPR models and runs inference on new binary black hole simulations. No training is done here.

How to use:

  1. for a single simulation (metadata string):
    python gpr_loader.py --metadata "RunID=0111 ZwickyDays=176 q=8.0 chiA=-0.555366763183,-0.575801728447,-0.00249659459858 chiB=-0.0516668737278,0.102310708412,0.791746644364 D0=14.5189208984 Omega0=0.0164280601103 adot0=-2.39687233838e-05"

  2. multiple simulations (one metadata string per line from a text file)
    python gpr_loader.py --file my_runs.txt

  3. specify custom model paths (optional)
    python gpr_loader.py --file my_runs.txt --model_omega path/to/gpr_model_omega.pth --model_adot path/to/gpr_model.adot.pth

Copilot AI review requested due to automatic review settings April 24, 2026 22:36
@vtommasini
Copy link
Copy Markdown
Author

some tests might fail as of 4/24, as this file depends on the rest of GPR_library.py, which hasn't fully been pushed yet

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a standalone GPR checkpoint loader + inference CLI for BBH simulation metadata, along with unit tests intended to validate checkpoint loading, inference output columns, and CLI argument parsing.

Changes:

  • Introduce gpr_loader.py to load saved GPR checkpoints (omega/adot) and run inference over one or more metadata strings.
  • Add run_inference() that parses metadata via GPR_library, runs both models, and appends corrected outputs + uncertainties to the resulting table.
  • Add test_gpr_loader.py with mocked heavy dependencies and tests for checkpoint loading, inference behavior, and CLI parsing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 9 comments.

File Description
gpr_loader.py New CLI + helpers to load GPR checkpoints and run inference over metadata strings.
test_gpr_loader.py New unit tests for loader/inference/CLI, with heavy dependencies mocked.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gpr_loader.py
Comment on lines +15 to +27
import os
import argparse
import numpy as np

# Import torch and related libraries
import torch
import gpytorch # GP library built on PyTorch

# Import GPR library
import importlib
import GPR_library as gpr
from GPR_library import GPRegressionModel

Comment thread test_gpr_loader.py

import unittest
import numpy as np
import pandas as pd
Comment thread test_gpr_loader.py
Comment on lines +21 to +34
# during the test

sys.modules["qgrid"] = MagicMock()
sys.modules["qgridnext"] = MagicMock()
sys.modules["sxs"] = MagicMock()
sys.modules["sxs.julia"] = MagicMock()
sys.modules["sxs.julia.PostNewtonian"] = MagicMock()
sys.modules["GPR_library"] = MagicMock()
sys.modules["plotly"] = MagicMock()
sys.modules["plotly.graph_objects"] = MagicMock()
sys.modules["torch"] = MagicMock()
sys.modules["gpytorch"] = MagicMock()

import gpr_loader
Comment thread test_gpr_loader.py
self.assertEqual(len(df), len(self.sample_metadata_strings))

def test_uncertainties_are_nonneg(self):
"""Test that the ncertainty values are always non-negative."""
Comment thread gpr_loader.py
Comment on lines +4 to +14
# Loads saved GPR models and runs inference on new binary black hole simulations. No training is done here.
# How to use:
# 1.for a single simulation (metadata string):
# python gpr_loader.py --metadata "RunID=0111 ZwickyDays=176 q=8.0 chiA=-0.555366763183,-0.575801728447,-0.00249659459858
# chiB=-0.0516668737278,0.102310708412,0.791746644364 D0=14.5189208984 Omega0=0.0164280601103
# adot0=-2.39687233838e-05",
# 2. multiple simulations (one metadata string per line from a text file)
# python gpr_loader.py --file my_runs.txt
# 3. specify custom model paths (optional)
# python gpr_loader.py --file my_runs.txt --model_omega path/to/gpr_model_omega.pth --model_adot path/to/gpr_model.adot.pth

Comment thread gpr_loader.py
Comment on lines +47 to +50
# Open, read, and load the saved checkpoint file into memory as a
# Python dictionary
ckpt = torch.load(ckpt_path, map_location="cpu")

Comment thread gpr_loader.py
Comment on lines +113 to +122
features = meta_omega["input_features"]

# Extract input features
X_test = df_test[features].values

# Predict corrections (deltas) and uncertainties
delta_omega_pred, omega_unc = gpr.predict_with_gpr_model(
X_test, model_omega, likelihood_omega)
delta_adot_pred, adot_unc = gpr.predict_with_gpr_model(
X_test, model_adot, likelihood_adot)
Comment thread test_gpr_loader.py
Comment on lines +2 to +7
test_gpr_loader.py

Unit tests for gpr_loader.py using Python built-in unittest.

Run with:
python -m unittest test_gpr_loader.py -v
Comment thread test_gpr_loader.py

with patch("torch.load", return_value = self.mock_checkpoint), \
patch("gpr_loader.gpr.GPRegressionModel", return_value=mock_model), \
patch("gpytorch.likelihoods.GaussianLikelihood", return_value=mock_likelihood):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants