Пример #1
0
def test_suboptimisers_get_parameters():
    t = optimise.Optimiser()
    t.add_parameter(
        1,
        True,
    )
    t.add_parameter(0.1, True, 1e-5)
    u = optimise.Optimiser()
    u.add_parameter(1, True)
    u.add_parameter(0.1, True, 1e-5)
    t.add_suboptimiser(u)
    assert u._get_parameters()[0] == [1.0, 0.1]
    assert u._get_parameters()[1] == [1e-4, 1e-5]
    assert t._get_parameters()[0] == [1.0, 0.1, 1.0, 0.1]
    assert t._get_parameters()[1] == [
        1e-4,
        1e-5,
        1e-4,
        1e-5,
    ]
    u = optimise.Optimiser()
    u.add_parameter(1, True)
    u.add_parameter(0.1, True, 1e-5)
    t = optimise.Optimiser('t', u)
    t.add_parameter(1, True)
    t.add_parameter(0.1, True, 1e-5)
    assert len(t._suboptimisers) == 1
    assert len(t._get_parameters()[0]) == 4
Пример #2
0
def test_plot_residual():
    t = optimise.Optimiser('t')
    t.add_construct_function(lambda: tools.randn(30))
    t.add_construct_function(lambda: tools.randn(30) * 2)
    u = optimise.Optimiser('u')
    u.add_construct_function(lambda: tools.randn(30) * 3)
    t.add_suboptimiser(u)
    t.optimise()
    t.plot_residual()
    if show_plots:
        plt.show()
Пример #3
0
def test_optimiser_construct():
    t = optimise.Optimiser()
    t.add_parameter(1)
    t.add_parameter(0.1, True, 1e-5)
    t.add_construct_function(lambda: [1, 2, 3])
    assert len(t._construct_functions) == 1
    assert list(t.construct()) == [1, 2, 3]
    t = optimise.Optimiser()
    x = t.add_parameter(0.1, False, 1e-5)
    t.add_construct_function(lambda: x - 1)
    assert list(t.construct()) == [-0.9]
    x.value = 0.2
    assert x.value == 0.2
    assert list(t.construct()) == [-0.8]
Пример #4
0
def test_get_all_suboptimisers():
    t = optimise.Optimiser()
    t.add_parameter(1)
    u = optimise.Optimiser()
    u.add_parameter(1)
    v = optimise.Optimiser()
    v.add_parameter(1)
    t.add_suboptimiser(u)
    u.add_suboptimiser(v)
    assert len(t._suboptimisers) == 1
    assert len(t._get_all_suboptimisers()) == 3
    assert len(t._get_parameters()[0]) == 0
    t.add_suboptimiser(v)
    assert len(t._get_all_suboptimisers()) == 3
    assert len(t._get_parameters()[0]) == 0
Пример #5
0
def test_optimise():
    t = optimise.Optimiser()
    x = t.add_parameter(0.1, True, 1e-5)
    t.add_construct_function(lambda: x - 1)
    t.optimise(verbose=True)
    assert tools.rms(t.residual) < 1e-2
    assert abs(x.value - 1) < 1e-2
Пример #6
0
def test_optimiser_has_changed():
    t = optimise.Optimiser(x=1, y=(0.1, True, 1e-5))
    assert t.has_changed()
    t.construct()
    assert not t.has_changed()
    t = optimise.Optimiser(x=1, y=(0.1, True, 1e-5))
    u = optimise.Optimiser(z=1, w=(0.1, True, 1e-5))
    t.add_suboptimiser(u)
    assert t.has_changed()
    assert u.has_changed()
    t.construct()
    assert not t.has_changed()
    assert not u.has_changed()
    u['z'] = 5
    assert u.has_changed()
    assert t.has_changed()
    t.construct()
    t['x'] = 2.5
    assert t.has_changed()
    assert not u.has_changed()
Пример #7
0
def test_plot():
    t = optimise.Optimiser('t')

    def f():
        plt.plot([1, 2, 5])
        plt.title('t')

    t.add_plot_function(f)

    def f():
        plt.plot([-3, -2, -1])

    t.add_plot_function(f)
    u = optimise.Optimiser('u')

    def f():
        plt.plot([10, 20, 30])

    t.add_plot_function(f)
    t.add_suboptimiser(u)
    t.plot()
    if show_plots:
        plt.show()
Пример #8
0
def test_optimiser_format_input():
    t = optimise.Optimiser()
    # assert t.format_input() == "from spectr import *\n\no = Optimiser(name='o')"
    t.print_input()
    t.print_input('^from')
    assert t.format_input('^from.*') == "from spectr import *\n"