def test_Condition_save_load(test_A, test_alpha):
    condition = hs.Condition()
    condition.add(test_alpha, test_A)
    condition.save(temp_file.name)
    loaded_condition = hs.Condition()
    loaded_condition.load(temp_file.name)
    np.testing.assert_allclose(loaded_condition.alpha.value, test_alpha.value)
    np.testing.assert_allclose(loaded_condition.A, test_A)
def test_model_conditions():
    test_alpha_1 = hs.Alpha()
    test_alpha_1.add(np.ones((2, 2)))
    test_alpha_2 = hs.Alpha()
    test_alpha_2.add(np.ones((2, 2)) * 2)
    test_A_1 = np.ones((2, 1))
    test_A_2 = np.ones((2, 1)) * 2
    condition_1 = hs.Condition()
    condition_1.add(test_alpha_1, test_A_1)
    condition_2 = hs.Condition()
    condition_2.add(test_alpha_2, test_A_2)
    model = hs.LeastSquares(conditions=[condition_1, condition_2])
    np.testing.assert_allclose(model.ALPHA,
                               np.array([[1, 1], [1, 1], [2, 2], [2, 2]]))
    np.testing.assert_allclose(model.A, np.array([[1], [1], [2], [2]]))
def test_Condition(test_A, test_alpha):
    condition = hs.Condition()
    condition.add(test_alpha, test_A)
    assert condition.alpha == test_alpha
    np.testing.assert_allclose(condition.A, test_A)

    with pytest.raises(IndexError) as e_info:
        row_A = np.random.rand(5)
        condition.add(test_alpha, row_A)
    assert 'A should be column vector of Mx1 dimension' in str(e_info)

    with pytest.raises(IndexError) as e_info:
        square_A = np.random.rand(5, 5)
        condition.add(test_alpha, square_A)
    assert 'A should be column vector of Mx1 dimension' in str(e_info)

    with pytest.raises(IndexError) as e_info:
        mismatch_A = np.random.rand(3, 1)
        condition.add(test_alpha, mismatch_A)
    assert 'A and alpha should have the same 0 dimension(M).' in str(e_info)

    with pytest.raises(TypeError) as e_info:
        A = [[3], [3], [3]]
        condition.add(test_alpha, A)
    assert 'numpy array' in str(e_info)

    with pytest.raises(IndexError) as e_info:
        alpha = hs.Alpha()
        alpha.add(np.random.rand(5, 5))
        wrong_first_dim_A = np.random.rand(3, 1)
        condition.add(alpha, wrong_first_dim_A)
    print(condition.alpha.value, condition.A)
    assert 'same 0 dimension(M)' in str(e_info)
def test_info_model():
    # Set random data
    np.random.seed(42)
    m = 3
    n = 3
    real = np.random.rand(m, n)
    imag = np.random.rand(m, n)
    comp = real + imag * 1j
    alpha1 = hs.Alpha()
    alpha1.add(comp)
    real = np.random.rand(m, n)
    imag = np.random.rand(m, n)
    comp = real + imag * 1j
    alpha2 = hs.Alpha()
    alpha2.add(comp)
    # Condition logging and printing
    condition1 = hs.Condition(name='Speed 1300')
    condition1.add(alpha2, A=np.random.rand(m, 1))
    condition2 = hs.Condition(name='Speed 2500')
    condition2.add(alpha2, A=np.random.rand(m, 1))
    model = hs.LeastSquares(conditions=[condition1, condition2])
    model.solve()
    angles = np.arange(100, 300, 10)  # angles
    split1 = model.create_split()
    split1.split_setup(0,
                       max_number_weights_per_hole=1,
                       holes_available=[angles],
                       weights_available=[0.1])
    split2 = model.create_split()
    split2.split_setup(1,
                       max_number_weights_per_hole=1,
                       holes_available=[angles],
                       weights_available=[0.2])
    split1.split_solve()
    split2.split_solve()
    split1.update(confirm=True)
    split2.update(confirm=True)
    print(model.info())
Esempio n. 5
0
def test_info():
    # Set random data
    np.random.seed(42)
    alpha1 = hs.Alpha()
    alpha1.name = 'Turbine'
    m = 3
    n = 3
    real = np.random.rand(m, n)
    imag = np.random.rand(m, n)
    comp = real + imag * 1j
    # alpha1 from A, B, U
    alpha1.add(A=np.random.rand(3, 1) + np.random.rand(3, 1) * 1j,
               B=np.random.rand(3, 5) + np.random.rand(3, 5) * 1j,
               U=np.random.rand(5) + np.random.rand(5) * 1j)
    # alpha2 from direct_matrix
    alpha2 = hs.Alpha()
    alpha2.add(comp)
    # Condition logging and printing
    condition1 = hs.Condition(name='Speed 2500')
    condition1.add(alpha2, A=np.random.rand(m, 1))
    print(alpha1, alpha2, condition1)