From c21bd0dd85d0857f5cf1c4a81a716f6ad9d256d3 Mon Sep 17 00:00:00 2001 From: Anika Date: Mon, 7 Oct 2024 10:25:30 -0600 Subject: [PATCH] Make compute angle an internal function --- ensemble_md/tests/test_coordinate_swap.py | 14 -------- ensemble_md/utils/coordinate_swap.py | 41 ++++++++++++----------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/ensemble_md/tests/test_coordinate_swap.py b/ensemble_md/tests/test_coordinate_swap.py index 56f8476..6b1bf6b 100644 --- a/ensemble_md/tests/test_coordinate_swap.py +++ b/ensemble_md/tests/test_coordinate_swap.py @@ -281,20 +281,6 @@ def test_find_rotation_angle(): assert np.isclose(angle, test_angle, 10**(-5)) -def test_compute_angle(): - coords_1 = [ - np.array([0.0, 0.0, 0.0]), - np.array([1.0, 0.0, 0.0]), - np.array([0.0, 1.0, 0.0]) - ] - coords_2 = coords_1[-1::-1] - coords_3 = [coords_1[1], coords_1[0], coords_1[2]] - - assert np.isclose(coordinate_swap.compute_angle(coords_1), np.pi / 4) - assert np.isclose(coordinate_swap.compute_angle(coords_2), np.pi / 4) - assert np.isclose(coordinate_swap.compute_angle(coords_3), np.pi / 2) - - def test_add_or_swap(): test_file = open('test_add_or_swap.gro', 'w') df = pd.read_csv(f'{input_path}/coord_swap/df_atom_swap.csv') diff --git a/ensemble_md/utils/coordinate_swap.py b/ensemble_md/utils/coordinate_swap.py index 32c878e..adc4c92 100644 --- a/ensemble_md/utils/coordinate_swap.py +++ b/ensemble_md/utils/coordinate_swap.py @@ -443,6 +443,28 @@ def get_miss_coord(mol_align, mol_ref, name_align, name_ref, df_atom_swap, dir, df_atom_swap : pandas.DataFrame Same dataframe as the input, but with coordinates for the missing atoms. """ + def compute_angle(coords): + """ + Computes the angle between two vectors. + + Parameters + ---------- + coords : list + A list of numpy arrays containing the XYZ coordinates of 3 points, for which the angle 1-2-3 is to be computed. + + Returns + ------- + angle : int + Angle in radians between the two points. + """ + vec1 = coords[0] - coords[1] + vec2 = coords[2] - coords[1] + + angle = np.arccos(np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))) + + return angle + + # Create a new column for coordinates if one does not exist if 'X Coordinates' not in df_atom_swap.columns: df_atom_swap['X Coordinates'] = np.NaN @@ -974,26 +996,7 @@ def find_rotation_angle(initial_point, vertex, rotated_point, axis): return angle -def compute_angle(coords): - """ - Computes the angle between two vectors. - Parameters - ---------- - coords : list - A list of numpy arrays containing the XYZ coordinates of 3 points, for which the angle 1-2-3 is to be computed. - - Returns - ------- - angle : int - Angle in radians between the two points. - """ - vec1 = coords[0] - coords[1] - vec2 = coords[2] - coords[1] - - angle = np.arccos(np.dot(vec1, vec2) / (np.linalg.norm(vec1) * np.linalg.norm(vec2))) - - return angle def add_or_swap(df_select, file_new, resnum, resname, vel, atom_num, orig_coor, skip_line, R_o_D_num, pick):