Пример #1
0
def test_mixed_vector_length():
    #================================================================
    "Check the definition of scalar nonlinear parameters and vector linear parameters"
    model = Model(gauss2_scaled)
    model.addlinear('gaussian', vec=100)

    assert model.Nparam == 101
Пример #2
0
def test_mixed_vector_names():
    #================================================================
    "Check the definition of scalar nonlinear parameters and vector linear parameters"
    model = Model(gauss2_scaled)
    model.addlinear('gaussian', vec=100)

    assert 'gaussian' in model.__dict__ and 'scale' in model.__dict__
Пример #3
0
def test_freeze_vec_outofbounds():
    #================================================================
    "Check that a parameter cannot be frozen outside of the bounds"
    model = Model(gauss2_identity)
    model.addlinear('gaussian', vec=100, lb=0)
    with pytest.raises(ValueError):
        model.gaussian.freeze(np.full(100, -10))
Пример #4
0
def test_addnonlinear_names():
    #================================================================
    "Check that linear parameters can be properly added"
    model = Model(gauss)
    model.addnonlinear('trivial1', lb=0)
    model.addnonlinear('trivial2', lb=0)

    assert hasattr(model, 'trivial1') and hasattr(model, 'trivial2')
Пример #5
0
def test_addnonlinear_length():
    #================================================================
    "Check that the model is contructed correctly with the appropiate number of parameters"
    model = Model(gauss)
    model.addnonlinear('trivial1', lb=0)
    model.addnonlinear('trivial2', lb=0)

    assert model.Nparam == 4
Пример #6
0
def test_preserve_original():
    "Check that the original model is not changed by the function"
    model = Model(gauss)
    model.mean.par0 = 3
    model.width.par0 = 0.2

    _ = fit(model, mock_data)
    assert model._parameter_list() == ['mean', 'width']
Пример #7
0
def test_addlinear_vector_set():
    #================================================================
    "Check that attributes of the vector linear parameters are editable"
    model = Model(gauss2_identity)
    model.addlinear('gaussian', vec=100)
    model.gaussian.set(lb=np.zeros(100))

    assert np.allclose(getattr(model.gaussian, 'lb'), np.zeros(100))
Пример #8
0
def test_addlinear_set():
    #================================================================
    "Check that attributes of the linear parameters are editable"
    model = Model(gauss2_design)
    model.addlinear('amp1')
    model.amp1.set(lb=0, ub=10)

    assert getattr(model.amp1, 'lb') == 0 and getattr(model.amp1, 'ub') == 10
Пример #9
0
def test_addlinear_names():
    #================================================================
    "Check that linear parameters can be properly added"
    model = Model(gauss2_design)
    model.addlinear('amp1', lb=0)
    model.addlinear('amp2', lb=0)

    assert 'amp1' in model.__dict__ and 'amp2' in model.__dict__
Пример #10
0
def test_addlinear_length():
    #================================================================
    "Check that the model is contructed correctly with the appropiate number of parameters"
    model = Model(gauss2_design)
    model.addlinear('amp1', lb=0)
    model.addlinear('amp2', lb=0)

    assert model.Nparam == 6
Пример #11
0
def test_mixed_vector_call_positional():
    #================================================================
    "Check that calling the model with scalar and vector parameters returns the correct response"
    model = Model(gauss2_scaled)
    model.addlinear('gaussian', vec=len(x))
    reference = 5 * gauss2(3, 4, 0.2, 0.3, 0.5, 0.4)
    response = model(5, gauss2(3, 4, 0.2, 0.3, 0.5, 0.4))

    assert np.allclose(response, reference)
Пример #12
0
def test_addnonlinear_set():
    #================================================================
    "Check that attributes of the linear parameters are editable"
    model = Model(gauss)
    model.addnonlinear('trivial1')
    model.trivial1.set(lb=0, ub=10)

    assert getattr(model.trivial1, 'lb') == 0 and getattr(
        model.trivial1, 'ub') == 10
Пример #13
0
def test_addlinear_call_mixed():
    #================================================================
    "Check that calling the model with parameters returns the correct response"
    model = Model(gauss2_design)
    model.addlinear('amp1')
    model.addlinear('amp2')
    response = model(3, 4, 0.2, width2=0.3, amp1=0.5, amp2=0.4)
    reference = gauss2(3, 4, 0.2, width2=0.3, amp1=0.5, amp2=0.4)

    assert np.allclose(response, reference)
Пример #14
0
def test_addnonlinear_call_positional():
    #================================================================
    "Check that calling the model with parameters returns the correct response"
    model = Model(gauss)
    model.addnonlinear('trivial1')
    model.addnonlinear('trivial2')
    response = model(3, 0.2, 1, 1)
    reference = gauss(3, 0.2)

    assert np.allclose(response, reference)
Пример #15
0
def test_addnonlinear_call_keywords():
    #================================================================
    "Check that calling the model with parameters returns the correct response"
    model = Model(gauss)
    model.addnonlinear('trivial1')
    model.addnonlinear('trivial2')
    response = model(mean=3, width=0.2, trivial1=1, trivial2=1)
    reference = gauss(mean=3, width=0.2)

    assert np.allclose(response, reference)
Пример #16
0
def test_addlinear_vector_call_keywords():
    #================================================================
    "Check that calling the model with scalar and vector parameters returns the correct response"
    model = Model(gauss2_identity)
    model.addlinear('gaussian', vec=len(x))
    reference = gauss2(mean1=3,
                       mean2=4,
                       width1=0.2,
                       width2=0.3,
                       amp1=0.5,
                       amp2=0.4)
    response = model(gaussian=reference)

    assert np.allclose(response, reference)
Пример #17
0
def freedist(r):
    def _nonparametric():
        return np.eye(len(r))

    # Create model
    dd_nonparametric = Model(_nonparametric, constants='r')
    dd_nonparametric.description = 'Non-parametric distribution model'
    # Parameters
    dd_nonparametric.addlinear(
        'P',
        vec=len(r),
        lb=0,
        par0=0,
        description='Non-parametric distance distribution')
    return dd_nonparametric
Пример #18
0
def test_freeze_outofbounds():
    #================================================================
    "Check that a parameter cannot be frozen outside of the bounds"
    model = Model(gauss)
    model.mean.set(lb=0, ub=10)
    with pytest.raises(ValueError):
        model.mean.freeze(-10)
Пример #19
0
def test_parameters_set():
    #================================================================
    "Check that attributes of the parameters are editable"
    model = Model(gauss)
    model.mean.set(lb=0, ub=10)

    assert getattr(model.mean, 'lb') == 0 and getattr(model.mean, 'ub') == 10
Пример #20
0
def test_freeze():
    #================================================================
    "Check that a model parameter can be frozen to a fixed value"
    model = Model(gauss)
    model.mean.freeze(3)

    assert model.mean.value == 3 and model.mean.frozen == True
Пример #21
0
def test_call_positional():
    #================================================================
    "Check that calling the model with parameter returns the correct response"
    model = Model(gauss)
    response = model(3, 0.5)
    reference = gauss(3, 0.5)

    assert np.allclose(response, reference)
Пример #22
0
def test_call_keywords():
    #================================================================
    "Check that calling the model with parameter returns the correct response"
    model = Model(gauss)
    response = model(mean=3, width=0.5)
    reference = gauss(mean=3, width=0.5)

    assert np.allclose(response, reference)
Пример #23
0
def test_unfreeze():
    #================================================================
    "Check that a model parameter can be frozen and then reversed"
    model = Model(gauss)
    model.mean.freeze(3)
    model.mean.unfreeze()

    assert model.mean.frozen == False and model.mean.value == None
Пример #24
0
def test_model_constant_same_values_positional():
    #================================================================
    "Check that a model with axis can be defined and called with same values as parameters"
    model = Model(gauss_axis, constants='axis')

    reference = gauss_axis(3, 3, 0.5)
    response = model(3, 3, 0.5)

    assert np.allclose(reference, response)
Пример #25
0
def test_model_constant_same_values_keywords():
    #================================================================
    "Check that a model with axis can be defined and called with same values as parameters"
    model = Model(gauss_axis, constants='axis')

    reference = gauss_axis(axis=3, mean=3, width=0.5)
    response = model(axis=3, mean=3, width=0.5)

    assert np.allclose(reference, response)
Пример #26
0
def test_real_model_complex_data():
    # ======================================================================
    "Check the fit of a real-valued model to complex-valued data"
    mymodel = Model(gauss)
    mymodel.mean.set(par0=4, lb=1, ub=6)
    mymodel.width.set(par0=0.2, lb=0.05, ub=5)
    fitResult = fit(mymodel, y.real + 1j * np.zeros_like(y.imag))

    assert np.allclose(fitResult.model.real, y.real) and np.allclose(
        fitResult.model.imag, np.zeros_like(y.real))
Пример #27
0
def test_model_with_constant_mixed():
    #================================================================
    "Check that a model with axis can be defined and called via keywords"
    model = Model(gauss_axis, constants='axis')

    x = np.linspace(0, 10, 300)
    reference = gauss_axis(x, 3, 0.5)
    response = model(x, mean=3, width=0.5)

    assert np.allclose(reference, response)
Пример #28
0
def test_model_with_constant_positional():
    #================================================================
    "Check that a model with axis can be defined and called"
    model = Model(gauss_axis, constants='axis')

    x = np.linspace(0, 10, 300)
    reference = gauss_axis(x, 3, 0.5)
    response = model(x, 3, 0.5)

    assert np.allclose(reference, response)
Пример #29
0
def test_model_with_multiple_constants():
    #================================================================
    "Check that a model with axis can be defined and called"
    model = Model(gauss_multiaxis, constants=['axis1', 'axis2'])

    x1 = np.linspace(0, 5, 300)
    x2 = np.linspace(5, 10, 300)
    reference = gauss_multiaxis(x1, x2, 3, 0.5)
    response = model(x1, x2, 3, 0.5)

    assert np.allclose(reference, response)
Пример #30
0
def _getmodel(type):
    if type == 'parametric':
        model = Model(gauss2)
        model.mean1.set(lb=0, ub=10, par0=2)
        model.mean2.set(lb=0, ub=10, par0=4)
        model.width1.set(lb=0.1, ub=5, par0=0.2)
        model.width2.set(lb=0.1, ub=5, par0=0.2)
        model.amp1.set(lb=0, ub=5, par0=1)
        model.amp2.set(lb=0, ub=5, par0=1)
    elif type == 'semiparametric':
        model = Model(gauss2_design)
        model.mean1.set(lb=0, ub=10, par0=2)
        model.mean2.set(lb=0, ub=10, par0=4)
        model.width1.set(lb=0.1, ub=5, par0=0.2)
        model.width2.set(lb=0.1, ub=5, par0=0.2)
        model.addlinear('amp1', lb=0, ub=5)
        model.addlinear('amp2', lb=0, ub=5)
    elif type == 'nonparametric':
        model = Model(gauss2_design(3, 4, 0.5, 0.2))
        model.addlinear('amp1', lb=0)
        model.addlinear('amp2', lb=0)
    return model