Add GPR loader and unit tests#5
Open
vtommasini wants to merge 1 commit into
Open
Conversation
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 |
There was a problem hiding this comment.
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.pyto load saved GPR checkpoints (omega/adot) and run inference over one or more metadata strings. - Add
run_inference()that parses metadata viaGPR_library, runs both models, and appends corrected outputs + uncertainties to the resulting table. - Add
test_gpr_loader.pywith 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 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 | ||
|
|
|
|
||
| import unittest | ||
| import numpy as np | ||
| import pandas as pd |
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 |
| 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 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 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 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 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 |
|
|
||
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Loads the saved GPR models and runs inference on new binary black hole simulations. No training is done here.
How to use:
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"multiple simulations (one metadata string per line from a text file)
python gpr_loader.py --file my_runs.txtspecify 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