Skip to main content

COMPAS LnL Surrogate

Coverage Status

This python package helps make an LnL surrogate for COMPAS SF-params given a set of BBH mergers. The surrogate is trained using active learning/bayesian optimisation techniques. This package acts as a bridge between the COMPAS LnL computer, surrogate modelling packages, and Bayesian optimisation packages.

Installation

pip install lnl_surrogate@git+https://github.com/COMPAS-Surrogate/lnl_surrogate.git

Example

from trieste.acquisition.function import PredictiveVariance, ExpectedImprovement


import numpy as np
from lnl_surrogate.surrogate import train
from scipy.stats import norm
from lnl_surrogate.surrogate.setup_optimizer import McZGrid
from lnl_computer.mock_data import generate_mock_data
from typing import Dict
import matplotlib.pyplot as plt
import os

np.random.seed(0)

MINX, MAXX = 0.005, 0.015
MIDX = (MINX + MAXX) / 2
NORM = norm(MIDX, 0.003)
OUTDIR = 'outdir'
os.makedirs(OUTDIR, exist_ok=True)

def mock_lnl(*args, **kwargs):
    sf_sample: Dict = kwargs.get('sf_sample')
    sf_sample = np.array(list(sf_sample.values()))
    return NORM.logpdf(sf_sample), 0

def plot_res(model, data, search_space):
    x = np.linspace(MINX, MAXX, 100).reshape(-1, 1)
    true_y = NORM.logpdf(x) * -1.0
    model_y, model_yunc = model.predict(x)
    x_obs = data.query_points
    y_obs = data.observations

    tf_to_np = lambda x: x.numpy().flatten() if hasattr(x, 'numpy') else x
    # make new fig
    plt.figure()
    plt.plot(x, true_y, label='True', color='black')
    plt.plot(x, model_y, label='Model', color="tab:orange")
    plt.scatter(x_obs, y_obs, label='Observed', color='black')
    yup, ydown = tf_to_np(model_y + model_yunc), tf_to_np(model_y - model_yunc)
    plt.fill_between(x.flatten(), yup.flatten(), ydown.flatten(), alpha=0.2 , color="tab:orange")
    plt.legend(loc='upper right')
    return plt.gcf()

McZGrid.lnl = mock_lnl
mock_data = generate_mock_data(OUTDIR)

acq_fns = [PredictiveVariance(), ExpectedImprovement()]
for acq_fn in acq_fns:
    res = train(
        model_type='gp',
        mcz_obs=mock_data.observations.mcz,
        compas_h5_filename=mock_data.compas_filename,
        acquisition_fns=[acq_fn],
        params=['aSF'],
        n_init=2,
        n_rounds=10,
        n_pts_per_round=1,
        outdir=f"{OUTDIR}/gp",
        truth=dict(
            aSF=MIDX,
            lnl=mock_lnl(sf_sample={'aSF': MIDX})[0]*-1.0
        ),
        model_plotter=plot_res,
        noise_level=1e-3
    )
Exploratory Acquisition Exploitative Acquisition
Exploratory Exploitative

Regret

Metadata

Release files for compas-surrogate.lnl-surrogate 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for compas-surrogate.lnl-surrogate 0.1.1
File Size Uploaded
compas_surrogate_lnl_surrogate-0.1.1.tar.gz 587.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for compas-surrogate.lnl-surrogate 0.1.1
File Interpreter ABI Platform
compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 611.5 kB

Release files / compas_surrogate_lnl_surrogate-0.1.1.tar.gz

Download URL compas_surrogate_lnl_surrogate-0.1.1.tar.gz
Size 587.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ea91991f19e538e205309407d25ede46d220a00e866b1e9bb05b24ce09fbc001
BLAKE2b-256 checksum
How to use checksums
64480526d302234c18f36414609200f98aa4791dfef8b324a2c8fe726ff5e5c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 12, 2024.

Transparency log

Release files / compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl

Download URL compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe459bc094098561e23dc130c62a1d2c1e7fe9ea42b9ffca02dc586431ef7c93
BLAKE2b-256 checksum
How to use checksums
11a5909f0566353c49100a6139c9a97ada766a6b8634d3daaa3ee1b696ac8e24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.0.1 CPython/3.12.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 12, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page