Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken python tests and refactor assertions #3419

Merged
merged 4 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions testsuite/python/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def test_deactivation(self):
self.assertFalse(a.is_active())

params = a.get_params()
self.assertTrue(params["a"])
self.assertFalse(params["b"])
self.assertTrue(params["c"])
self.assertEqual(params["a"], True)
self.assertEqual(params["b"], False)
self.assertEqual(params["c"], True)


if __name__ == "__main__":
Expand Down
12 changes: 6 additions & 6 deletions testsuite/python/analyze_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ def test_nbhood(self):
for i in range(1, 10, 2):
self.system.part[:].pos = np.random.random(
(len(self.system.part), 3)) * BOX_L
self.assertTrue(
np.allclose(self.system.analysis.nbhood([i, i, i], i * 2),
self.nbhood([i, i, i], i * 2)))
np.testing.assert_allclose(
self.system.analysis.nbhood([i, i, i], i * 2),
self.nbhood([i, i, i], i * 2))

def test_dist_to_pos(self):
# try five times
for i in range(5):
self.system.part[:].pos = np.random.random(
(len(self.system.part), 3)) * BOX_L
self.assertTrue(
np.allclose(self.system.analysis.dist_to(pos=[i, i, i]),
self.dist_to_pos([i, i, i])))
np.testing.assert_allclose(
self.system.analysis.dist_to(pos=[i, i, i]),
self.dist_to_pos([i, i, i]))

def test_dist_to_id(self):
# try five times
Expand Down
31 changes: 15 additions & 16 deletions testsuite/python/analyze_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_rdf(self):
bin_width = (r_max - r_min) / r_bins
bins = np.arange(r_min, r_max + bin_width, bin_width)
bin_volume = 4. / 3. * np.pi * (bins[1:]**3 - bins[:-1]**3)
box_volume = np.prod(self.system.box_l)
box_volume = np.prod(np.copy(self.system.box_l))
# all the same type
core_rdf = self.system.analysis.rdf(rdf_type='rdf',
type_list_a=[0],
Expand All @@ -75,11 +75,11 @@ def test_rdf(self):
num_pair = 0.5 * (self.num_part) * (self.num_part - 1)
r = self.system.part[:].pos
# bins
self.assertTrue(np.allclose(core_rdf[0], (bins[1:] + bins[:-1]) * 0.5))
np.testing.assert_allclose(core_rdf[0], (bins[1:] + bins[:-1]) * 0.5)
# rdf
self.assertTrue(
np.allclose(core_rdf[1] * bin_volume * num_pair / box_volume,
self.calc_rdf(r, bins)))
np.testing.assert_allclose(
core_rdf[1] * bin_volume * num_pair / box_volume,
self.calc_rdf(r, bins))
# change one type
self.system.part[0].type = 1
r = self.system.part[1:].pos
Expand All @@ -90,9 +90,9 @@ def test_rdf(self):
r_max=r_max,
r_bins=r_bins)
num_pair = 0.5 * (self.num_part - 1) * (self.num_part - 2)
self.assertTrue(
np.allclose(core_rdf[1] * bin_volume * num_pair / box_volume,
self.calc_rdf(r, bins)))
np.testing.assert_allclose(
core_rdf[1] * bin_volume * num_pair / box_volume,
self.calc_rdf(r, bins))

# compare with type
core_rdf = self.system.analysis.rdf(rdf_type='rdf',
Expand All @@ -105,9 +105,8 @@ def test_rdf(self):
dist = np.sqrt(
np.sum((self.system.part[1:].pos - self.system.part[0].pos)**2, axis=1))
hist = np.histogram(dist, bins=bins, density=False)[0]
self.assertTrue(
np.allclose(core_rdf[1] * bin_volume * num_pair / box_volume,
hist))
np.testing.assert_allclose(
core_rdf[1] * bin_volume * num_pair / box_volume, hist)
# restore PBC
self.system.box_l = self.system.box_l / 2.
self.system.part[:].pos = old_pos
Expand All @@ -131,11 +130,11 @@ def test_distribution_lin(self):
log_flag=0,
int_flag=0)
# bins
self.assertTrue(np.allclose(core_rdf[0], (bins[1:] + bins[:-1]) * 0.5))
np.testing.assert_allclose(core_rdf[0], (bins[1:] + bins[:-1]) * 0.5)

# rdf
self.assertTrue(np.allclose(core_rdf[1],
self.calc_min_distribution(bins)))
np.testing.assert_allclose(core_rdf[1],
self.calc_min_distribution(bins))
# with int flag
core_rdf = self.system.analysis.distribution(type_list_a=[0],
type_list_b=[0],
Expand All @@ -144,8 +143,8 @@ def test_distribution_lin(self):
r_bins=r_bins,
log_flag=0,
int_flag=1)
self.assertTrue(np.allclose(core_rdf[1],
np.cumsum(self.calc_min_distribution(bins))))
np.testing.assert_allclose(core_rdf[1],
np.cumsum(self.calc_min_distribution(bins)))


if __name__ == "__main__":
Expand Down
35 changes: 17 additions & 18 deletions testsuite/python/analyze_gyration_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,36 +50,35 @@ def test_gyration_tensor_cube(self):
res = self.system.analysis.gyration_tensor(p_type=self.type_cube)
rg = self.system.analysis.calc_rg(
chain_start=0, number_of_chains=1, chain_length=self.cube_len**3)[0]
# make sure all eigenvalues (for the cube) are identical
self.assertTrue(
np.allclose(np.abs(res['eva0'][0]), np.abs(res['eva1'][0]), np.abs(res['eva2'][0]), atol=1e-6))
self.assertTrue(np.allclose(rg**2, res['Rg^2'], atol=1e-6))
# check eigenvalues are identical
np.testing.assert_allclose(
[np.abs(res['eva' + x][0]) for x in '012'], 3 * [1.25], atol=1e-6)
np.testing.assert_allclose(rg**2, res['Rg^2'], atol=1e-6)

def test_gyration_tensor(self):
# get results
res = self.system.analysis.gyration_tensor(
p_type=[self.type_stick, self.type_cube])
rg = self.system.analysis.calc_rg(
chain_start=0, number_of_chains=1, chain_length=len(self.system.part[:]))[0]
# test if principal and secondary axis is [0,0,1] and [0,1,0]
self.assertTrue(
np.allclose(np.abs(res['eva0'][1]), [0., 0., 1.], atol=1e-6))
self.assertTrue(
np.allclose(np.abs(res['eva1'][1]), [0., 1., 0.], atol=1e-6))
self.assertTrue(
np.allclose(np.abs(res['eva2'][1]), [1., 0., 0.], atol=1e-6))
self.assertTrue(np.allclose(rg**2, res['Rg^2'], atol=1e-6))
# check eigenvectors
np.testing.assert_allclose(
np.abs(res['eva0'][1]), [0., 0., 1.], atol=1e-6)
np.testing.assert_allclose(
np.abs(res['eva1'][1]), [0., 1., 0.], atol=1e-6)
np.testing.assert_allclose(
np.abs(res['eva2'][1]), [1., 0., 0.], atol=1e-6)
np.testing.assert_allclose(rg**2, res['Rg^2'], atol=1e-6)

def test_mom_intertia(self):

sqr_dist = np.sum(
(self.system.analysis.center_of_mass(p_type=0) - self.system.part.select(type=0).pos)**2, axis=0)
mom_I = self.system.analysis.moment_of_inertia_matrix(p_type=0)
# the cube case should have zero as off- diagonal components
self.assertTrue(
np.allclose([mom_I[0, 1], mom_I[0, 2], mom_I[1, 2], mom_I[1, 0], mom_I[2, 0], mom_I[2, 1]], np.zeros(6), atol=1e-6))
self.assertTrue(np.allclose([mom_I[0, 0], mom_I[1, 1], mom_I[2, 2]],
[sqr_dist[1] + sqr_dist[2], sqr_dist[0] + sqr_dist[2], sqr_dist[1] + sqr_dist[2]], atol=1e-6))
# the cube case should have zero as off-diagonal components
np.testing.assert_allclose(
mom_I, np.diag(np.diag(mom_I)), rtol=0, atol=1e-6)
np.testing.assert_allclose(
np.diag(mom_I), sqr_dist[(1, 0, 1), ] + sqr_dist[2], atol=1e-6)


if __name__ == "__main__":
Expand Down
7 changes: 3 additions & 4 deletions testsuite/python/analyze_mass_related.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,12 @@ def test_itensor(self):
# Particles of type 0
I0 = self.i_tensor(self.system.part.select(
lambda p: (not p.virtual) and p.type == 0).id)

np.testing.assert_allclose(
I0, self.system.analysis.moment_of_inertia_matrix(p_type=0), atol=1E-9)
# type=1
# Particles of type 1
I1 = self.i_tensor(self.system.part.select(type=1).id)
self.assertTrue(
np.allclose(I1, self.system.analysis.moment_of_inertia_matrix(p_type=1), atol=1E-9))
np.testing.assert_allclose(
I1, self.system.analysis.moment_of_inertia_matrix(p_type=1), atol=1E-9)

def test_center_of_mass(self):
no_virtual_type_0 = self.system.part.select(
Expand Down
58 changes: 34 additions & 24 deletions testsuite/python/array_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ def test_unlocked_operators(self):
add = v + w
sub = v - w

self.assertTrue(isinstance(add, np.ndarray))
self.assertTrue(isinstance(sub, np.ndarray))
self.assertIsInstance(add, np.ndarray)
self.assertIsInstance(sub, np.ndarray)

self.assertTrue(add.flags.writeable)
self.assertTrue(sub.flags.writeable)
Expand Down Expand Up @@ -136,17 +136,20 @@ def test_common(self):
# Check (allowed) setter
# Particle
self.system.part[0].pos = [2, 2, 2]
self.assertTrue((self.system.part[0].pos == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].pos), [2, 2, 2])

self.system.part[0].v = [2, 2, 2]
self.assertTrue((self.system.part[0].v == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].v), [2, 2, 2])

self.system.part[0].f = [2, 2, 2]
self.assertTrue((self.system.part[0].f == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].f), [2, 2, 2])

# System
self.system.box_l = [2, 2, 2]
self.assertTrue((self.system.box_l == [2, 2, 2]).all())
np.testing.assert_array_equal(np.copy(self.system.box_l), [2, 2, 2])

# Check if copy is settable
# Particle
Expand Down Expand Up @@ -175,25 +178,28 @@ def test_rotation(self):
# Check (allowed) setter
# Particle
self.system.part[0].quat = [0.5, 0.5, 0.5, 0.5]
self.assertTrue(
(self.system.part[0].quat == [0.5, 0.5, 0.5, 0.5]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].quat), [0.5, 0.5, 0.5, 0.5])

self.system.part[0].omega_lab = [2, 2, 2]
self.assertTrue((self.system.part[0].omega_lab == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].omega_lab), [2, 2, 2])

self.system.part[0].rotation = [1, 1, 1]
self.assertTrue((self.system.part[0].rotation == [1, 1, 1]).all())
self.system.part[0].rotation = [True, True, True]
self.assertEqual(list(self.system.part[0].rotation), 3 * [True])

self.system.part[0].omega_body = [2, 2, 2]
self.assertTrue((self.system.part[0].omega_body == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].omega_body), [2, 2, 2])

self.system.part[0].torque_lab = [2, 2, 2]
self.assertTrue((self.system.part[0].torque_lab == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].torque_lab), [2, 2, 2])

if espressomd.has_features("EXTERNAL_FORCES"):
self.system.part[0].ext_torque = [2, 2, 2]
self.assertTrue(
(self.system.part[0].ext_torque == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].ext_torque), [2, 2, 2])

# Check if copy is settable
# Particle
Expand All @@ -215,7 +221,8 @@ def test_rotational_inertia(self):
# Check (allowed) setter
# Particle
self.system.part[0].rinertia = [2, 2, 2]
self.assertTrue((self.system.part[0].rinertia == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].rinertia), [2, 2, 2])

# Check if copy is settable
# Particle
Expand All @@ -232,10 +239,11 @@ def test_external_forces(self):
# Check (allowed) setter
# Particle
self.system.part[0].ext_force = [2, 2, 2]
self.assertTrue((self.system.part[0].ext_force == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].ext_force), [2, 2, 2])

self.system.part[0].fix = [1, 1, 1]
self.assertTrue((self.system.part[0].fix == [1, 1, 1]).all())
self.system.part[0].fix = [True, True, True]
self.assertEqual(list(self.system.part[0].fix), [True, True, True])

# Check if copy is settable
# Particle
Expand All @@ -252,7 +260,8 @@ def test_rot_aniso(self):
# Check (allowed) setter
# Particle
self.system.part[0].gamma_rot = [2, 2, 2]
self.assertTrue((self.system.part[0].gamma_rot == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].gamma_rot), [2, 2, 2])

# Check if copy is settable
# Particle
Expand All @@ -277,7 +286,8 @@ def test_langevinpp_aniso(self):
# Check (allowed) setter
# Particle
self.system.part[0].gamma = [2, 2, 2]
self.assertTrue((self.system.part[0].gamma == [2, 2, 2]).all())
np.testing.assert_array_equal(
np.copy(self.system.part[0].gamma), [2, 2, 2])

# Check if copy is settable
# Particle
Expand All @@ -294,7 +304,7 @@ def test_dipoles(self):
# Particle
self.system.part[0].dip = [2, 2, 2]
np.testing.assert_allclose(
[2, 2, 2], np.copy(self.system.part[0].dip), atol=1E-15)
np.copy(self.system.part[0].dip), [2, 2, 2], atol=1E-15)

# Check if copy is settable
# Particle
Expand All @@ -315,8 +325,8 @@ def test_partial_periodic(self):

# Check (allowed) setter
# System
self.system.periodicity = [1, 0, 0]
self.assertTrue((self.system.periodicity == [1, 0, 0]).all())
self.system.periodicity = [True, False, False]
self.assertEqual(list(self.system.periodicity), [True, False, False])

# Check if copy is settable
# System
Expand Down
12 changes: 6 additions & 6 deletions testsuite/python/auto_exclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def test_linear(self):
for p in range(1, 9):
excl = s.part[p].exclusions
self.assertEqual(len(excl), 2)
self.assertTrue((p - 1) in excl)
self.assertTrue((p + 1) in excl)
self.assertIn(p - 1, excl)
self.assertIn(p + 1, excl)

excl = s.part[0].exclusions
self.assertEqual(len(excl), 1)
self.assertTrue(1 in excl)
self.assertIn(1, excl)

excl = s.part[9].exclusions
self.assertEqual(len(excl), 1)
self.assertTrue(8 in excl)
self.assertIn(8, excl)

def test_ring(self):
bond = espressomd.interactions.Virtual()
Expand All @@ -73,8 +73,8 @@ def test_ring(self):
excl = s.part[p].exclusions
self.assertEqual(len(excl), 4)
for i in range(1, 3):
self.assertTrue((p - i) % 10 in excl)
self.assertTrue((p + i) % 10 in excl)
self.assertIn((p - i) % 10, excl)
self.assertIn((p + i) % 10, excl)


if __name__ == "__main__":
Expand Down
14 changes: 7 additions & 7 deletions testsuite/python/cluster_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ def test_zz_single_cluster_analysis(self):
self.assertLess(np.linalg.norm(c.center_of_mass()), 1E-8)

# Longest distance
self.assertLess(
abs(c.longest_distance()
- self.es.distance(self.es.part[0],
self.es.part[len(self.es.part) - 1])),
1E-8)
self.assertAlmostEqual(
c.longest_distance(),
self.es.distance(self.es.part[0],
self.es.part[len(self.es.part) - 1]),
delta=1E-8)

# Radius of gyration
rg = 0.
Expand All @@ -143,11 +143,11 @@ def test_zz_single_cluster_analysis(self):
rg += self.es.distance(p, com_particle)**2
rg /= len(self.es.part)
rg = np.sqrt(rg)
self.assertTrue(abs(c.radius_of_gyration() - rg) <= 1E-6)
self.assertAlmostEqual(c.radius_of_gyration(), rg, delta=1E-6)

# Fractal dimension calc require gsl
if not espressomd.has_features("GSL"):
print("Skipping fractal dimension tests for lack of GSL")
print("Skipping fractal dimension tests due to missing GSL dependency")
return
# The fractal dimension of a line should be 1

Expand Down
Loading