• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nikhil-sarin / redback / 19121091639

06 Nov 2025 12:49AM UTC coverage: 87.696% (-0.03%) from 87.724%
19121091639

Pull #292

github

web-flow
Merge dc3a4ebb7 into 5813eaa20
Pull Request #292: Create a mechanism to generate and test on reference results

23 of 36 new or added lines in 2 files covered. (63.89%)

2 existing lines in 1 file now uncovered.

14476 of 16507 relevant lines covered (87.7%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.85
/redback/test_utils/ref_data_utils.py
1
"""A helper function to generate reference data for a given model."""
2

3
import numpy as np
1✔
4
from astropy.table import Table
1✔
5
from itertools import product
1✔
6

7

8
def make_ref_data(model, param_grid, other_kwargs, filename):
1✔
9
    """Generate reference data for a given model.
10

11
    Parameters
12
    ----------
13
    model : callable
14
        The model function to generate data for.
15
    required_args : dict
16
        A list of the names of the required arguments for the model function.
17
    param_grid : dict
18
        A dictionary where keys are parameter names and values are lists of parameter values to sample.
19
    other_kwargs : dict
20
        Additional keyword arguments to pass to the model function.
21
    filename : str
22
        The name of the file to save the reference data to (ECSV format).
23
    """
24
    # Create a list of all combinations of parameters
NEW
25
    keys, values = zip(*param_grid.items())
×
NEW
26
    param_combinations = [dict(zip(keys, v)) for v in product(*values)]
×
27

28
    # Generate a table of all inputs to the function and the results.
NEW
29
    all_data = {k: [] for k in keys}
×
NEW
30
    all_data.update({k: [] for k in other_kwargs.keys()})
×
NEW
31
    all_data["results"] = []
×
32

NEW
33
    for current_params in param_combinations:
×
34
        # Save these sets of keyword arguments.
NEW
35
        current_kwargs = {**other_kwargs, **current_params}
×
NEW
36
        for k, v in current_kwargs.items():
×
NEW
37
            all_data[k].append(v)
×
38

39
        # Evaluate the model and save the results.
NEW
40
        result = model(**current_kwargs)
×
NEW
41
        all_data["results"].append(result)
×
42

43
    # Convert to Table and save as an ECSV file.
NEW
44
    table = Table(all_data)
×
NEW
45
    table.write(filename, format='ascii.ecsv', overwrite=True)
×
46

47

48
def assert_matches_ref_data(model, filename):
1✔
49
    """Test a model against reference data.
50

51
    Parameters
52
    ----------
53
    model : callable
54
        The model function to test.
55
    filename : str
56
        The name of the file containing the reference data (ECSV format).
57
    other_kwargs : dict
58
        Additional keyword arguments to pass to the model function.
59
    """
60
    # Load the reference data
61
    table = Table.read(filename, format='ascii.ecsv')
1✔
62

63
    # Extract parameters and results
64
    param_names = [col for col in table.colnames if col != "results"]
1✔
65
    params = table[param_names].as_array()
1✔
66
    reference_results = [np.array(x) for x in table["results"]]
1✔
67

68
    # Test the model against the reference results
69
    for idx in range(len(table)):
1✔
70
        current_params = {name: params[idx][name] for name in param_names}
1✔
71
        ref_result = reference_results[idx]
1✔
72
        model_result = model(**current_params)
1✔
73
        assert np.allclose(model_result, ref_result), f"Model output does not match reference data for parameters: {current_params}"
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc