def test_model_with_no_argument(): with pytest.raises(TypeError) as e_info: model = hs.LeastSquares() assert 'Either (A and Alpha) or `conditions` should be assigned.' in str( e_info) A = np.random.rand(2, 1) condition = hs.Conditions() model = hs.LeastSquares(A=A, conditions=[condition]) assert 'Either (A and Alpha) or `conditions` should be assigned.' in str( e_info)
def test_A_dim(): ''' Test the dimension of A to be 2x1 ''' real = np.random.uniform(0, 10, [1, 2]) imag = np.random.uniform(0, 10, [1, 2]) A = real + imag * 1j with pytest.raises(hs.CustomError) as e_info: hs.LeastSquares(test_A_dim, test_alpha) real = np.random.uniform(0, 10, [2, 2]) imag = np.random.uniform(0, 10, [2, 2]) A = real + imag * 1j with pytest.raises(hs.CustomError) as e_info: hs.LeastSquares(test_A_dim, test_alpha)
def test_WLS(param, expected): ''' Testing instantiate LMI model and test it against test cases ''' my_ALPHA = hs.Alpha() A = hs.convert_matrix_to_cart(param[0]['A']) A0 = [0] # It is acceptable to enter either direct_matrix or A,B,U matrices try: direct_matrix = hs.convert_matrix_to_cart(param[0]['ALPHA']) my_ALPHA.add(direct_matrix=direct_matrix) except KeyError: B = hs.convert_matrix_to_cart(param[0]['B']) U = hs.convert_matrix_to_cart(param[0]['U']) my_ALPHA.add(A=A, B=B, U=U) try: A0 = hs.convert_matrix_to_cart(param[0]['A0']) except KeyError: pass expected_W = hs.convert_matrix_to_cart(expected) my_model = hs.LeastSquares(A - A0, my_ALPHA, name='simple_least_square') W = my_model.solve(solver='WLS') print('Residual Vibration rmse calculated = ', my_model.rmse()) print('Residual Vibration rmse from test_case = ', hs.rmse(hs.residual_vibration(my_ALPHA.value, expected_W, A))) print('expected_residual_vibration', hs.convert_matrix_to_math(my_model.expected_residual_vibration())) np.testing.assert_allclose(W, expected_W, rtol=0.05) # allowance 5% error
def test_performance(n): alpha = hs.Alpha() real = np.random.uniform(0, 10, [n, n]) imag = np.random.uniform(0, 10, [n, n]) alpha.add(real + imag * 1j) real = np.random.uniform(0, 10, [n, 1]) imag = np.random.uniform(0, 10, [n, 1]) A = real + imag * 1j start = time.time() w = hs.LeastSquares(A, alpha).solve() error = hs.residual_vibration(alpha.value, w, A) t = time.time() - start return round(t, 2)
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_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())
def test_model_LSQ(test_big_A, test_big_alpha): ''' Creating a test model ''' w = hs.LeastSquares(test_big_A, test_big_alpha).solve() return w
def test_faults(): with pytest.raises(hs.CustomError) as error: hs.LeastSquares([1, 2], [1, 2])
import hsbalance as hs A = [['170@112'], ['53@78']] # --> Initial vibration conditions First Row in Table above. B = [ [ '235@94', '185@115' ], # --> Vibration at sensor 1 when trial masses were added at plane 1&2 (First column for both trial runs) ['58@68', '77@104'] ] # Vibration at sensor 2 when trial masses were added at plane 1&2 (Second column for both trial runs) U = ['1.15@0', '1.15@0'] # Trial masses 2.5 g at plane 1 and 2 consequently A = hs.convert_math_cart(A) B = hs.convert_math_cart(B) U = hs.convert_math_cart(U) alpha = hs.Alpha() # Instantiate Alpha class alpha.add(A=A, B=B, U=U) model_LeastSquares = hs.LeastSquares(A=A, alpha=alpha) w = model_LeastSquares.solve( ) # Solve the model and get the correction weights vector # Calculate Residual vibration vector residual_vibration = hs.residual_vibration(alpha.value, w, A) # Caculate Root mean square error for model RMSE = hs.rmse(residual_vibration) # Convert w back into mathmatical expression w = hs.convert_cart_math(w) # print results print(model_LeastSquares.info())