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

Tests cleanup #495

Merged
merged 19 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
7 changes: 0 additions & 7 deletions tests/common_aev_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
import unittest
import torch
import torchani
Expand All @@ -14,12 +13,6 @@ def setUp(self):
self.radial_length = self.aev_computer.radial_length
self.debug = False

def transform(self, x):
return x

def random_skip(self, prob=0):
return random.random() < prob

def assertAEVEqual(self, expected_radial, expected_angular, aev, tolerance=tolerance):
radial = aev[..., :self.radial_length]
angular = aev[..., self.radial_length:]
Expand Down
13 changes: 1 addition & 12 deletions tests/test_aev.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class TestIsolated(unittest.TestCase):
# a distance greater than the cutoff radius from all other atoms
# this can throw an IndexError for large distances or lone atoms
def setUp(self):
if torch.cuda.is_available():
self.device = 'cuda'
else:
self.device = 'cpu'
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
ani1x = torchani.models.ANI1x().to(self.device)
self.aev_computer = ani1x.aev_computer
self.species_to_tensor = ani1x.species_to_tensor
Expand Down Expand Up @@ -95,10 +92,6 @@ def testIsomers(self):
species = torch.from_numpy(species)
expected_radial = torch.from_numpy(expected_radial)
expected_angular = torch.from_numpy(expected_angular)
coordinates = self.transform(coordinates)
species = self.transform(species)
expected_radial = self.transform(expected_radial)
expected_angular = self.transform(expected_angular)
_, aev = self.aev_computer((species, coordinates))
self.assertAEVEqual(expected_radial, expected_angular, aev)

Expand All @@ -113,10 +106,6 @@ def testPadding(self):
species = torch.from_numpy(species)
radial = torch.from_numpy(radial)
angular = torch.from_numpy(angular)
coordinates = self.transform(coordinates)
species = self.transform(species)
radial = self.transform(radial)
angular = self.transform(angular)
species_coordinates.append(torchani.utils.broadcast_first_dim(
{'species': species, 'coordinates': coordinates}))
radial_angular.append((radial, angular))
Expand Down
4 changes: 0 additions & 4 deletions tests/test_aev_benzene_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ def testBenzeneMD(self):
cell = torch.from_numpy(cell).float()
pbc = torch.from_numpy(pbc)
coordinates = torchani.utils.map2central(cell, coordinates, pbc)
coordinates = self.transform(coordinates)
species = self.transform(species)
expected_radial = self.transform(expected_radial)
expected_angular = self.transform(expected_angular)
_, aev = self.aev_computer((species, coordinates), cell=cell, pbc=pbc)
self.assertAEVEqual(expected_radial, expected_angular, aev, 5e-5)

Expand Down
2 changes: 0 additions & 2 deletions tests/test_aev_nist.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ def testNIST(self):
with open(datafile, 'rb') as f:
data = pickle.load(f)
for coordinates, species, radial, angular, _, _ in data:
if self.random_skip():
continue
coordinates = torch.from_numpy(coordinates).to(torch.float)
species = torch.from_numpy(species)
radial = torch.from_numpy(radial).to(torch.float)
Expand Down
4 changes: 0 additions & 4 deletions tests/test_aev_tripeptide_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ def testTripeptideMD(self):
species = torch.from_numpy(species).unsqueeze(0)
expected_radial = torch.from_numpy(expected_radial).float().unsqueeze(0)
expected_angular = torch.from_numpy(expected_angular).float().unsqueeze(0)
coordinates = self.transform(coordinates)
species = self.transform(species)
expected_radial = self.transform(expected_radial)
expected_angular = self.transform(expected_angular)
_, aev = self.aev_computer((species, coordinates))
self.assertAEVEqual(expected_radial, expected_angular, aev, tol)

Expand Down
1 change: 0 additions & 1 deletion tests/test_ase.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import os

path = os.path.dirname(os.path.realpath(__file__))
N = 97
tol = 5e-5


Expand Down
16 changes: 2 additions & 14 deletions tests/test_energies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ def setUp(self):
self.nn = torchani.nn.Sequential(self.nnp, self.energy_shifter)
self.model = torchani.nn.Sequential(self.aev_computer, self.nnp, self.energy_shifter)

def random_skip(self):
return False

def transform(self, x):
return x

def testIsomers(self):
for i in range(N):
datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
Expand All @@ -34,10 +28,7 @@ def testIsomers(self):
coordinates = torch.from_numpy(coordinates).to(torch.float)
species = torch.from_numpy(species)
energies = torch.from_numpy(energies).to(torch.float)
coordinates = self.transform(coordinates)
species = self.transform(species)
energies = self.transform(energies)
_, energies_ = self.model((species, coordinates))
energies_ = self.model((species, coordinates)).energies
max_diff = (energies - energies_).abs().max().item()
self.assertLess(max_diff, self.tolerance)

Expand All @@ -51,16 +42,13 @@ def testPadding(self):
coordinates = torch.from_numpy(coordinates).to(torch.float)
species = torch.from_numpy(species)
e = torch.from_numpy(e).to(torch.float)
coordinates = self.transform(coordinates)
species = self.transform(species)
e = self.transform(e)
species_coordinates.append(
torchani.utils.broadcast_first_dim({'species': species, 'coordinates': coordinates}))
energies.append(e)
species_coordinates = torchani.utils.pad_atomic_properties(
species_coordinates)
energies = torch.cat(energies)
_, energies_ = self.model((species_coordinates['species'], species_coordinates['coordinates']))
energies_ = self.model((species_coordinates['species'], species_coordinates['coordinates'])).energies
max_diff = (energies - energies_).abs().max().item()
self.assertLess(max_diff, self.tolerance)

Expand Down
12 changes: 0 additions & 12 deletions tests/test_forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ def setUp(self):
self.nnp = model.neural_networks
self.model = torchani.nn.Sequential(self.aev_computer, self.nnp)

def random_skip(self):
return False

def transform(self, x):
return x

def testIsomers(self):
for i in range(N):
datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
Expand All @@ -31,9 +25,6 @@ def testIsomers(self):
coordinates = torch.from_numpy(coordinates)
species = torch.from_numpy(species)
forces = torch.from_numpy(forces)
coordinates = self.transform(coordinates)
species = self.transform(species)
forces = self.transform(forces)
coordinates.requires_grad_(True)
_, energies = self.model((species, coordinates))
derivative = torch.autograd.grad(energies.sum(),
Expand All @@ -51,9 +42,6 @@ def testPadding(self):
coordinates = torch.from_numpy(coordinates)
species = torch.from_numpy(species)
forces = torch.from_numpy(forces)
coordinates = self.transform(coordinates)
species = self.transform(species)
forces = self.transform(forces)
coordinates.requires_grad_(True)
species_coordinates.append(torchani.utils.broadcast_first_dim(
{'species': species, 'coordinates': coordinates}))
Expand Down
4 changes: 4 additions & 0 deletions tests/test_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ def testGradGradCheck(self):
torch.autograd.gradgradcheck(lambda x: self.model((species, x)).energies,
coordinates,
nondet_tol=1e-13)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_neurochem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class TestNeuroChem(unittest.TestCase):

def testNeuroChemTrainer(self):
d = torch.device('cpu')
d = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
trainer = torchani.neurochem.Trainer(iptpath, d, True, os.path.join(path, 'runs'))

# test if loader construct correct model
Expand Down
2 changes: 1 addition & 1 deletion tests/test_periodic_table_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def setUp(self):
[0.45554739, 0.54289633, 0.81170881],
[0.66091919, -0.16799635, -0.91037834]]],
requires_grad=True)
self.species1 = self.model1.species_to_tensor('CHHHH').unsqueeze(0)
self.species1 = self.model1.species_to_tensor(['C', 'H', 'H', 'H', 'H']).unsqueeze(0)
self.species2 = torch.tensor([[6, 1, 1, 1, 1]])

def testCH4Ensemble(self):
Expand Down