def test_rotate(self): # ensure that selection isn't centered at 0, 0, 0 self.u.atoms.translate((1, 1, 1)) self.coords += 1 # check identify does nothing R = np.eye(3) self.u.atoms.rotate(R) assert_array_almost_equal(self.u.atoms.positions, self.coords) # check default rotation center is at 0, 0, 0. Changing this center # will break an unpredictable amount of old code. ag = self.u.atoms[:2] ag.positions = np.array([[1, 0, 0], [-1, 0, 0]]) ag.rotate(transformations.rotation_matrix(1, [0, 0, 1])[:3, :3]) assert_array_almost_equal(ag.positions[0], [np.cos(1), np.sin(1), 0]) # check general rotation cases vec = np.array([[1, 0, 0], [-1, 0, 0]]) axis = np.array([0, 0, 1]) for angle in np.linspace(0, np.pi): R = transformations.rotation_matrix(angle, axis) ag.positions = vec.copy() res_ag = ag.rotate(R[:3, :3]) assert_equal(ag, res_ag) assert_array_almost_equal( ag.positions[0], [np.cos(angle), np.sin(angle), 0]) ag.positions = vec.copy() ag.rotate(R[:3, :3], vec[0]) assert_array_almost_equal(ag.positions[0], vec[0]) assert_array_almost_equal( ag.positions[1], [-2 * np.cos(angle) + 1, -2 * np.sin(angle), 0])
def test_rotation_from_matrix(): angle = (random.random() - 0.5) * (2*np.pi) direc = np.random.random(3) - 0.5 point = np.random.random(3) - 0.5 R0 = t.rotation_matrix(angle, direc, point) angle, direc, point = t.rotation_from_matrix(R0) R1 = t.rotation_matrix(angle, direc, point) assert_equal(t.is_same_transform(R0, R1), True)
def test_rotation_from_matrix(): angle = (random.random() - 0.5) * (2 * np.pi) direc = np.random.random(3) - 0.5 point = np.random.random(3) - 0.5 R0 = t.rotation_matrix(angle, direc, point) angle, direc, point = t.rotation_from_matrix(R0) R1 = t.rotation_matrix(angle, direc, point) assert_equal(t.is_same_transform(R0, R1), True)
def test_rotation_from_matrix(): angle = 0.2 * 2 * np.pi # arbitrary values direc = np.array([0.2, 0.2, 0.2]) point = np.array([0.4, 0.4, 0.4]) R0 = t.rotation_matrix(angle, direc, point) angle, direc, point = t.rotation_from_matrix(R0) R1 = t.rotation_matrix(angle, direc, point) assert_equal(t.is_same_transform(R0, R1), True)
def test_rotation_from_matrix(): angle = 0.2 * 2 * np.pi # arbitrary values direc = np.array([0.2, 0.2, 0.2]) point = np.array([0.4, 0.4, 0.4]) R0 = t.rotation_matrix(angle, direc, point) angle, direc, point = t.rotation_from_matrix(R0) R1 = t.rotation_matrix(angle, direc, point) assert_equal(t.is_same_transform(R0, R1), True)
def rotation(xyz, idx, max_angle): angle = np.random.uniform(0, max_angle) vec = xyz[idx + 1] - xyz[idx - 1] vec /= norm(vec) x = xyz[idx] - xyz[idx - 1] mat = transformations.rotation_matrix(angle, vec)[:3, :3] xyz[idx] = np.dot(mat, x) + xyz[idx - 1] return xyz
def test_rotation_matrix(): # test if the rotation_matrix function is working properly # simple angle and unit vector on origin angle = 180 vector = [0, 0, 1] pos = [0, 0, 0] ref_matrix = np.asarray([[-1, 0, 0], [0, -1, 0], [0, 0, 1]], np.float64) matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3] assert_array_almost_equal(matrix, ref_matrix, decimal=6) # another angle in a custom axis angle = 60 vector = [1, 2, 3] pos = [1, 2, 3] ref_matrix = np.asarray([[0.53571429, -0.6229365, 0.57005291], [0.76579365, 0.64285714, -0.01716931], [-0.35576719, 0.44574074, 0.82142857]], np.float64) matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3] assert_array_almost_equal(matrix, ref_matrix, decimal=6)
def test_rotation_matrix(): # test if the rotation_matrix function is working properly # simple angle and unit vector on origin angle = 180 vector = [0, 0, 1] pos = [0, 0, 0] ref_matrix = np.asarray([[-1, 0, 0], [0, -1, 0], [0, 0, 1]], np.float64) matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3] assert_array_almost_equal(matrix, ref_matrix, decimal=6) # another angle in a custom axis angle = 60 vector = [1, 2, 3] pos = [1, 2, 3] ref_matrix = np.asarray([[ 0.53571429, -0.6229365 , 0.57005291], [ 0.76579365, 0.64285714, -0.01716931], [-0.35576719, 0.44574074, 0.82142857]], np.float64) matrix = rotation_matrix(np.deg2rad(angle), vector, pos)[:3, :3] assert_array_almost_equal(matrix, ref_matrix, decimal=6)
def test_fit_rot_trans_no_options(fit_universe): test_u = fit_universe[0] ref_u = fit_universe[1] ref_com = ref_u.atoms.center(None) ref_u.trajectory.ts.positions -= ref_com R = rotation_matrix(np.pi / 3, [1, 0, 0])[:3, :3] ref_u.trajectory.ts.positions = np.dot(ref_u.trajectory.ts.positions, R) ref_u.trajectory.ts.positions += ref_com fit_rot_trans(test_u, ref_u)(test_u.trajectory.ts) assert_array_almost_equal(test_u.trajectory.ts.positions, ref_u.trajectory.ts.positions, decimal=3)
def test_rotateby_vector(rotate_universes, vector): # what happens when we use a custom point for the axis of rotation? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts point = [0, 0, 0] angle = 90 vec = vector.reshape(3, ) matrix = rotation_matrix(np.deg2rad(angle), vec, point) ref_u.atoms.transform(matrix) transformed = rotateby(angle, vector, point=point)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def test_rotateby_vector(rotate_universes, vector): # what happens when we use a custom point for the axis of rotation? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts point = [0, 0, 0] angle = 90 vec = vector.reshape(3, ) matrix = rotation_matrix(np.deg2rad(angle), vec, point) ref_u.atoms.transform(matrix) transformed = rotateby(angle, vector, point=point)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def test_transform_translation_and_rotation(self): angle = np.pi / 4 axis = [0, 0, 1] disp = np.ones(3) T = transformations.rotation_matrix(angle, axis) T[:3, 3] = disp ag = self.u.atoms[:2] ag.positions = [[1, 0, 0], [-1, 0, 0]] ag.transform(T) assert_array_almost_equal( ag.positions[0], [np.cos(angle) + 1, np.sin(angle) + 1, 1])
def test_rotateby_atomgroup_cog_nopbc(rotate_universes): # what happens when we rotate arround the center of geometry of a residue # without pbc? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts center_pos = [6,7,8] vector = [1,0,0] angle = 90 matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos) ref_u.atoms.transform(matrix) selection = trans_u.residues[0].atoms transformed = rotateby(angle, vector, ag=selection, weights=None)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def test_rotateby_atomgroup_com_pbc(rotate_universes): # what happens when we rotate arround the center of mass of a residue # with pbc? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts vector = [1,0,0] angle = 90 selection = trans_u.residues[0].atoms center_pos = selection.center_of_mass(pbc=True) matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos) ref_u.atoms.transform(matrix) transformed = rotateby(angle, vector, ag=selection, weights='mass', wrap=True)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def test_rotateby_atomgroup_cog_nopbc(rotate_universes): # what happens when we rotate arround the center of geometry of a residue # without pbc? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts center_pos = [6, 7, 8] vector = [1, 0, 0] angle = 90 matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos) ref_u.atoms.transform(matrix) selection = trans_u.residues[0].atoms transformed = rotateby(angle, vector, ag=selection, weights=None)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def rotation(angle, random_state=None): """random rotation with given angle about a random axis Parameters ---------- angle : float angle in radian Returns ------- M : ndarray (3, 3) rotation matrix """ random_state = check_random_state(random_state) axis = random_state.normal(size=3) r = transformations.rotation_matrix(angle, axis) return r[:3, :3]
def test_transform_rotation_only(self): R = np.eye(3) self.u.atoms.rotate(R) assert_array_almost_equal(self.u.atoms.positions, self.coords) vec = np.array([[1, 0, 0], [-1, 0, 0]]) axis = np.array([0, 0, 1]) ag = self.u.atoms[:2] ag.positions = vec for angle in np.linspace(0, np.pi): R = transformations.rotation_matrix(angle, axis) ag.positions = vec.copy() ag.transform(R) assert_array_almost_equal( ag.positions[0], [np.cos(angle), np.sin(angle), 0])
def test_rotateby_atomgroup_com_pbc(rotate_universes): # what happens when we rotate arround the center of mass of a residue # with pbc? ref_u = rotate_universes[0] trans_u = rotate_universes[1] trans = trans_u.trajectory.ts ref = ref_u.trajectory.ts vector = [1, 0, 0] angle = 90 selection = trans_u.residues[0].atoms center_pos = selection.center_of_mass(pbc=True) matrix = rotation_matrix(np.deg2rad(angle), vector, center_pos) ref_u.atoms.transform(matrix) transformed = rotateby(angle, vector, ag=selection, weights='mass', wrap=True)(trans) assert_array_almost_equal(transformed.positions, ref.positions, decimal=6)
def test_fit_rot_trans_plane(fit_universe, plane): # the reference is rotated in the x axis so removing the translations and rotations # in the yz plane should return the same as the fitting without specifying a plane test_u = fit_universe[0] ref_u = fit_universe[1] ref_com = ref_u.atoms.center(None) mobile_com = test_u.atoms.center(None) axes = {'yz': 0, 'xz': 1, 'xy': 2} idx = axes[plane] rotaxis = np.asarray([0, 0, 0]) rotaxis[idx] = 1 ref_u.trajectory.ts.positions -= ref_com R = rotation_matrix(np.pi / 3, rotaxis)[:3, :3] ref_u.trajectory.ts.positions = np.dot(ref_u.trajectory.ts.positions, R) ref_com[idx] = mobile_com[idx] ref_u.trajectory.ts.positions += ref_com fit_rot_trans(test_u, ref_u, plane=plane)(test_u.trajectory.ts) assert_array_almost_equal(test_u.trajectory.ts.positions[:, idx], ref_u.trajectory.ts.positions[:, idx], decimal=3)
def right_angle_mat(): return transformations.rotation_matrix(np.radians(90), [1, -1, 1])[:3, :3]
def test_quaternion_matrix_1(self): M = self.f([0.99810947, 0.06146124, 0, 0]) assert_allclose(M, t.rotation_matrix(0.123, (1, 0, 0)), atol=_ATOL)
def test_quaternion_from_matrix_3(self): R = t.rotation_matrix(0.123, (1, 2, 3)) q = self.f(R, True) assert_allclose(q, [0.9981095, 0.0164262, 0.0328524, 0.0492786], atol=_ATOL)
def test_quaternion_matrix_1(self): M = self.f([0.99810947, 0.06146124, 0, 0]) assert_allclose(M, t.rotation_matrix(0.123, (1, 0, 0)), atol=_ATOL)
def test_quaternion_from_matrix_3(self): R = t.rotation_matrix(0.123, (1, 2, 3)) q = self.f(R, True) assert_allclose(q, [0.9981095, 0.0164262, 0.0328524, 0.0492786], atol=_ATOL)