def test_Struct(): s = Struct(a=1, b=2) assert set(s.keys()) == {'a', 'b'} assert s.a == 1 assert s.b == 2 s.a = 3 assert s.a == 3 with pytest.raises(AttributeError): s.c with pytest.raises(KeyError): s['c'] assert s == Struct(a=3, b=2)
def minos_style(smerr): """Convert minos error to style""" return Struct( is_valid=good(smerr.is_valid, True), lower_valid=good(smerr.lower_valid, True), upper_valid=good(smerr.upper_valid, True), at_lower_limit=good(smerr.at_lower_limit, False), at_upper_limit=good(smerr.at_upper_limit, False), at_lower_max_fcn=good(smerr.at_lower_max_fcn, False), at_upper_max_fcn=good(smerr.at_upper_max_fcn, False), lower_new_min=good(smerr.lower_new_min, False), upper_new_min=good(smerr.upper_new_min, False), )
def fmin_style(sfmin): """convert sfmin to style""" return Struct( is_valid=good(sfmin.is_valid, True), has_valid_parameters=good(sfmin.has_valid_parameters, True), has_accurate_covar=good(sfmin.has_accurate_covar, True), has_posdef_covar=good(sfmin.has_posdef_covar, True), has_made_posdef_covar=good(sfmin.has_made_posdef_covar, False), hesse_failed=good(sfmin.hesse_failed, False), has_covariance=good(sfmin.has_covariance, True), is_above_max_edm=good(sfmin.is_above_max_edm, False), has_reached_call_limit=caution(sfmin.has_reached_call_limit, False), )
def test_console_frontend_with_difficult_values(capsys): def out(): return "\n" + capsys.readouterr()[0] frontend = console.ConsoleFrontend() matrix = ((-1.23456, 0), (0, 0)) vnames = ("x", "y") frontend.print_matrix(vnames, matrix) assert r""" ------------------- Correlation ------------------- | | x y | ------------------- | x | -1.23 0.00 | | y | 0.00 0.00 | ------------------- """ == out() mps = [ Struct(name="x", value=-1.234567e-11, error=1.234567e11, lower_limit=None, upper_limit=None, is_fixed=False, is_const=True) ] frontend.print_param(mps) assert r""" --------------------------------------------------------------------------------------- | No | Name | Value | Sym. Err | Err- | Err+ | Limit- | Limit+ | Fixed | --------------------------------------------------------------------------------------- | 0 | x | -1.23E-11| 1.23E+11 | | | | | CONST | --------------------------------------------------------------------------------------- """ == out()
def test_console_frontend_with_long_names(capsys): def out(): return "\n" + capsys.readouterr()[0] frontend = console.ConsoleFrontend() matrix = ((1.0, 0.1), (0.1, 1.0)) vnames = ["super-long-name", "x"] frontend.print_matrix(vnames, matrix) assert r""" ----------------------------------------------------- Correlation ----------------------------------------------------- | | super-long-name x | ----------------------------------------------------- | super-long-name | 1.00 0.10 | | x | 0.10 1.00 | ----------------------------------------------------- """ == out() mps = [ Struct(name="super-long-name", value=0, error=0, lower_limit=None, upper_limit=None, is_fixed=False, is_const=False) ] frontend.print_param(mps) assert r""" -------------------------------------------------------------------------------------------------- | No | Name | Value | Sym. Err | Err- | Err+ | Limit- | Limit+ | Fixed | -------------------------------------------------------------------------------------------------- | 0 | super-long-name | 0 | 0 | | | | | | -------------------------------------------------------------------------------------------------- """ == out()
def test_get_param_states(): m = Minuit(func3, x=1, y=2, error_x=3, error_y=4, fix_x=True, limit_y=(None, 10), pedantic=False, errordef=1, print_level=0) # these are the initial param states expected = [ Struct(name='x', number=0, value=1, error=3, is_fixed=True, is_const=False, has_limits=False, has_lower_limit=False, has_upper_limit=False, lower_limit=None, upper_limit=None), Struct(name='y', number=1, value=2, error=4, is_fixed=False, is_const=False, has_limits=True, has_lower_limit=False, has_upper_limit=True, lower_limit=None, upper_limit=10) ] assert m.get_param_states() == expected m.migrad() assert m.get_initial_param_states() == expected expected = [ Struct(name='x', number=0, value=1.0, error=3.0, is_fixed=True, is_const=False, has_limits=False, has_lower_limit=False, has_upper_limit=False, lower_limit=None, upper_limit=None), Struct(name='y', number=1, value=5.0, error=1.0, is_fixed=False, is_const=False, has_limits=True, has_lower_limit=False, has_upper_limit=True, lower_limit=None, upper_limit=10) ] param = m.get_param_states() for i, exp in enumerate(expected): p = param[i] assert set(p.keys()) == set(exp.keys()) for key in exp: if key in ('value', 'error'): assert_allclose(p[key], exp[key], rtol=1e-2) else: assert p[key] == exp[key]