def test_nplanets_type(): """ Test that read_config raises the correct erors when invalid data types are given. """ parent_path = os.path.join( Path(__file__).parent.absolute(), 'test_examples/51Peg') configfile = os.path.join(parent_path, 'config_51Peg_example.py') with pytest.raises(TypeError): config.read_config(configfile, nplanets=0.5) with pytest.raises(ValueError): config.read_config(configfile, nplanets=-6)
def test_with_planets(): """ Test for the config module """ parent_path = os.path.join( Path(__file__).parent.absolute(), 'test_examples/51Peg') configfile = os.path.join(parent_path, 'config_51Peg_example.py') # Test read config functionalities when adding planets rundict, datadict, priordict, fixeddict = config.read_config(configfile, nplanets=0) parnames = list(priordict.keys()) assert rundict['nplanets'] == 0 for par in parnames: assert 'planet' not in par # Test with 1 planet rundict, datadict, priordict, fixeddict = config.read_config(configfile, nplanets=1) parnames = list(priordict.keys()) assert rundict['nplanets'] == 1 assert 'planet1_period' in parnames # Check that there really is only one planet present nplanets = 0 for par in parnames: if 'k1' in par: nplanets += 1 assert nplanets == 1 # Test with 2 planet rundict, datadict, priordict, fixeddict = config.read_config(configfile, nplanets=2) parnames = list(priordict.keys()) assert rundict['nplanets'] == 2 assert 'planet1_period' in parnames assert 'planet2_period' in parnames # Check that there really are two planet present nplanets = 0 for par in parnames: if 'k1' in par: nplanets += 1 assert nplanets == 2
def test_no_planets(): """ Test for the config module """ parent_path = os.path.join( Path(__file__).parent.absolute(), 'test_examples/51Peg') configfile = os.path.join(parent_path, 'config_51Peg_example.py') # Test basic read config functionalities (without planet information) rundict, datadict, priordict, fixeddict = config.read_config(configfile) parnames = list(priordict.keys()) assert len(parnames) == 7 assert len(fixeddict.keys()) == 1 assert len(datadict['hamilton']['data']) == 256 assert rundict['target'] == '51Peg' assert rundict['star_params']['star_mass'][0] == 1.11 assert rundict['star_params']['star_mass'][1] == 0.02 assert 'nplanets' not in rundict.keys()
def poly_setup(configfile, nplanets=None): """ Sets up a basic polychord run for testing """ parent_path = os.path.join( Path(__file__).parent.absolute(), 'test_examples/51Peg') configfile = os.path.join(parent_path, configfile) # Read dictionaries from configuration file rundict, datadict, priordict, fixedpardict = config.read_config( configfile, nplanets) parnames = list(priordict.keys()) # Import model module modulename = 'model_51Peg_example' sys.path.insert(0, parent_path) mod = importlib.import_module(modulename) # Instantiate model class (pass additional arguments) model = mod.Model(fixedpardict, datadict, parnames) return model, rundict, priordict
def test_gaussian_2d(): """ Checks that multidimensional models work. """ parent_path = os.path.join( Path(__file__).parent.absolute(), 'test_examples/gaussian') configfile = os.path.join(parent_path, 'config_gaussian_2d.py') # Read dictionaries from configuration file rundict, datadict, priordict, fixeddict = config.read_config(configfile) parnames = list(priordict.keys()) # Import model module modulename = 'model_gaussian_example' sys.path.insert(0, parent_path) mod = importlib.import_module(modulename) # Instantiate model class (pass additional arguments) model = mod.Model(fixeddict, datadict, parnames) polysettings = {'nlive': 500} output = polychord.run(model, rundict, priordict, polysettings) print(output.logZ) assert abs(output.logZ + 4.15) < 0.5 # Clean old runs, it keep the newest one so the checks should pass clean_runs() # Check that output files were generated base_dir_parent = Path(output.base_dir).parent.absolute() assert os.path.isfile( os.path.join(base_dir_parent, output.file_root + '.pkl')) assert os.path.isfile(os.path.join(base_dir_parent, 'post_processing.py')) assert os.path.isfile(os.path.join(base_dir_parent, 'results.txt')) assert os.path.isfile( os.path.join(base_dir_parent, 'model_gaussian_example.py')) return
import os import sys import importlib from pathlib import Path from evidence import config, polychord configfile = 'config_51Peg_example.py' parent_path = Path(__file__).parent.absolute() configfile = os.path.join(parent_path, configfile) nplanets = 1 # Read dictionaries from configuration file rundict, datadict, priordict, fixedpardict = config.read_config( configfile, nplanets) parnames = list(priordict.keys()) # Import model module modulename = 'model_51Peg_example' sys.path.insert(0, parent_path) mod = importlib.import_module(modulename) # Instantiate model class (pass additional arguments) model = mod.Model(fixedpardict, datadict, parnames) # Set PolyChord run settings polysettings = {'nlive': 5} output = polychord.run(model, rundict, priordict, polysettings)