Example #1
0
def test_sampler_from_cli(Model, name):
    parser = argparse.ArgumentParser()
    option_utils.add_sampler_option_group(parser)
    args = parser.parse_args([
        '--sampler',
        name,
        '--nwalkers',
        '2',  # required for some samplers
        '--ntemps',
        '1',  # required for some samplers
    ])
    sampler = option_utils.sampler_from_cli(args, Model())
    assert isinstance(sampler, SAMPLERS[name])
Example #2
0
def test_add_sampler_option_group(capsys):
    parser = argparse.ArgumentParser()
    option_utils.add_sampler_option_group(parser)

    # check no arguments raises error
    with pytest.raises(SystemExit):
        parser.parse_args()
    assert '--sampler is required' in capsys.readouterr()[1]

    # check type casting
    args = parser.parse_args([
        '--sampler',
        'mcmc',
        '--niterations',
        '1',
        '--n-independent-samples',
        '2',
        '--nwalkers',
        '3',
        '--ntemps',
        '4',
        '--burn-in-function',
        'half_chain',
        '--min-burn-in',
        '5',
        '--update-interval',
        '6',
        '--nprocesses',
        '7',
        '--use-mpi',
    ])
    for arg in ('niterations', 'n_independent_samples', 'nwalkers', 'ntemps',
                'min_burn_in', 'update_interval', 'nprocesses'):
        assert isinstance(getattr(args, arg), int)

    # check choices
    with pytest.raises(SystemExit):
        parser.parse_args(['--sampler', 'foo'])
    assert 'invalid choice: \'foo\'' in capsys.readouterr()[1]
    with pytest.raises(SystemExit):
        parser.parse_args(['--burn-in-function', 'bar'])
    assert 'invalid choice: \'bar\'' in capsys.readouterr()[1]