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_Min_max(param, expected): ''' Testing instantiate Min_Max model and test it against test cases ''' my_ALPHA = hs.Alpha() A = hs.convert_matrix_to_cart(param[0]['A']) weight_const = param[0]['weight_const'] 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.Min_max(A, my_ALPHA, weight_const=weight_const,name='Min_max') # Setting the model almost with no constraints W = my_model.solve() print((expected)) 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())) print('Correction weights', hs.convert_cart_math(W)) # Constraint Minmax algorithm was slightly inefficient in CVXPY # The rmse was marginally more than the author solution np.testing.assert_allclose(W, expected_W, rtol=0.09) # allowance 9% error
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_alpha_save_load(test_alpha): np.random.seed(42) test_alpha.add(np.random.rand(6, 4)) test_alpha.save(temp_file.name) loaded_alpha = hs.Alpha() loaded_alpha.load(temp_file.name) np.testing.assert_allclose(loaded_alpha.value, test_alpha.value)
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_alpha(): ''' Creating alpha instance to test throwing faults ''' alpha = hs.Alpha() value = np.random.uniform(0, 10, [2, 2]) alpha.add(value + value * 1j) return alpha
def test_big_alpha(n): ''' Creating alpha instance to test throwing faults ''' alpha = hs.Alpha() real = np.random.uniform(0, 10, [n, n]) imag = np.random.uniform(0, 10, [n, n]) alpha.add(real + imag * 1j) return alpha
def test_alpha(m, n): ''' Creating alpha instance ''' alpha = hs.Alpha() real = np.random.uniform(0, 1, [m, n]) imag = np.random.uniform(0, 1, [m, n]) alpha.add(real + imag * 1j) return alpha
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)
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_ALPHA_direct(param, expected): ''' Pytest function to test instantiate Alpha from direct_matrix Inputs: param, expected : from config.yaml file ''' # Instantiate Alpha class my_ALPHA = hs.Alpha() # Add direct matrix to the instance my_ALPHA.add(direct_matrix=hs.convert_matrix_to_cart(param)) expected = list(list(complex(x) for x in item) for item in expected) np.testing.assert_allclose(my_ALPHA.value, expected, rtol=0.05) # allowance 5% error
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_ALPHA_from_matrices(param, expected): ''' Function to test instantiating Alpha class from matrices param, expected : from config.yaml file ''' for key, value in param[0].items(): globals()[key] = value my_ALPHA = hs.Alpha() my_A = hs.convert_matrix_to_cart(A) my_B = hs.convert_matrix_to_cart(B) my_U = hs.convert_matrix_to_cart(U) print(A, B, U, keep_trial) my_ALPHA.add(A=my_A, B=my_B, U=my_U, keep_trial=keep_trial) expected = hs.convert_matrix_to_cart(expected) np.testing.assert_allclose(my_ALPHA.value, expected, rtol=0.05) # allowance 5% error
def test_LMI(param, expected): ''' Testing insantiate Least square model and test it against test cases ''' my_ALPHA = hs.Alpha() A = hs.convert_matrix_to_cart(param[0]['A']) weight_const = param[0]['weight_const'] 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.LMI(A, my_ALPHA, weight_const=weight_const, V_max=76, critical_planes={1, 9}, name='LMI') W = my_model.solve() 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())) print('Correction weights', hs.convert_cart_math(W)) np.testing.assert_allclose(W, expected_W, rtol=0.05) # allowance 5% error
def test_alpha(): ''' creating alpha instance to test throwing faults ''' return hs.Alpha()
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())