def test_convert_atom_to_voxel(self): # 20 points with coords between -5 and 5, centered at 0 coords_range = 10 xyz = (np.random.rand(20, 3) - 0.5) * coords_range for idx in np.random.choice(20, 6): for box_width in (10, 20, 40): for voxel_width in (0.5, 1, 2): voxel = rgf.convert_atom_to_voxel(xyz, idx, box_width, voxel_width) self.assertIsInstance(voxel, list) self.assertEqual(len(voxel), 1) self.assertIsInstance(voxel[0], np.ndarray) self.assertEqual(voxel[0].shape, (3,)) self.assertIs(voxel[0].dtype, np.dtype('int')) # indices are positive self.assertTrue((voxel[0] >= 0).all()) # coordinates were properly translated and scaled self.assertTrue( (voxel[0] < (box_width + coords_range) / 2.0 / voxel_width).all()) self.assertTrue( np.allclose(voxel[0], np.floor((xyz[idx] + box_width / 2.0) / voxel_width))) # for coordinates outside of the box function should properly transform them # to indices and warn the user for args in ((np.array([[0, 1, 6]]), 0, 10, 1.0), (np.array([[0, 4, -6]]), 0, 10, 1.0)): # TODO check if function warns. There is assertWarns method in unittest, # but it is not implemented in 2.7 and buggy in 3.5 (issue 29620) voxel = rgf.convert_atom_to_voxel(*args) self.assertTrue( np.allclose(voxel[0], np.floor((args[0] + args[2] / 2.0) / args[3])))
def test_convert_atom_pair_to_voxel(self): # 20 points with coords between -5 and 5, centered at 0 coords_range = 10 xyz1 = (np.random.rand(20, 3) - 0.5) * coords_range xyz2 = (np.random.rand(20, 3) - 0.5) * coords_range # 3 pairs of indices for idx1, idx2 in np.random.choice(20, (3, 2)): for box_width in (10, 20, 40): for voxel_width in (0.5, 1, 2): v1 = rgf.convert_atom_to_voxel(xyz1, idx1, box_width, voxel_width) v2 = rgf.convert_atom_to_voxel(xyz2, idx2, box_width, voxel_width) v_pair = rgf.convert_atom_pair_to_voxel((xyz1, xyz2), (idx1, idx2), box_width, voxel_width) self.assertEqual(len(v_pair), 2) self.assertTrue((v1 == v_pair[0]).all()) self.assertTrue((v2 == v_pair[1]).all())