test_energies.py 2.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
import torch
import torchani
import unittest
import os
import pickle


path = os.path.dirname(os.path.realpath(__file__))
N = 97


12
class TestEnergies(torchani.testing.TestCase):
Gao, Xiang's avatar
Gao, Xiang committed
13
14
    # tests the predicions for a torchani.nn.Sequential(AEVComputer(),
    # ANIModel(), EnergyShifter()) against precomputed values
15

16
    def setUp(self):
17
18
19
20
        model = torchani.models.ANI1x(model_index=0)
        self.aev_computer = model.aev_computer
        self.nnp = model.neural_networks
        self.energy_shifter = model.energy_shifter
21
        self.model = torchani.nn.Sequential(self.aev_computer, self.nnp, self.energy_shifter)
22

23
    def testIsomers(self):
24
        for i in range(N):
25
            datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
26
27
            with open(datafile, 'rb') as f:
                coordinates, species, _, _, energies, _ = pickle.load(f)
28
29
30
                coordinates = torch.from_numpy(coordinates).to(torch.float)
                species = torch.from_numpy(species)
                energies = torch.from_numpy(energies).to(torch.float)
Ignacio Pickering's avatar
Ignacio Pickering committed
31
                energies_ = self.model((species, coordinates)).energies
32
                self.assertEqual(energies, energies_, exact_dtype=False)
33
34
35
36
37

    def testPadding(self):
        species_coordinates = []
        energies = []
        for i in range(N):
38
            datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
39
40
            with open(datafile, 'rb') as f:
                coordinates, species, _, _, e, _ = pickle.load(f)
41
42
43
                coordinates = torch.from_numpy(coordinates).to(torch.float)
                species = torch.from_numpy(species)
                e = torch.from_numpy(e).to(torch.float)
44
45
                species_coordinates.append(
                    torchani.utils.broadcast_first_dim({'species': species, 'coordinates': coordinates}))
46
                energies.append(e)
47
        species_coordinates = torchani.utils.pad_atomic_properties(
48
49
            species_coordinates)
        energies = torch.cat(energies)
Ignacio Pickering's avatar
Ignacio Pickering committed
50
        energies_ = self.model((species_coordinates['species'], species_coordinates['coordinates'])).energies
51
        self.assertEqual(energies, energies_, exact_dtype=False)
52
53


54
class TestEnergiesEnergyShifterJIT(TestEnergies):
Gao, Xiang's avatar
Gao, Xiang committed
55
    # only JIT compile the energy shifter and repeat all tests
56

57
58
59
    def setUp(self):
        super().setUp()
        self.energy_shifter = torch.jit.script(self.energy_shifter)
60
        self.model = torchani.nn.Sequential(self.aev_computer, self.nnp, self.energy_shifter)
61
62


63
class TestEnergiesANIModelJIT(TestEnergies):
Gao, Xiang's avatar
Gao, Xiang committed
64
    # only JIT compile the ANI nnp ANIModel and repeat all tests
65
66
67
68
69
70
71
72

    def setUp(self):
        super().setUp()
        self.nnp = torch.jit.script(self.nnp)
        self.model = torchani.nn.Sequential(self.aev_computer, self.nnp, self.energy_shifter)


class TestEnergiesJIT(TestEnergies):
Gao, Xiang's avatar
Gao, Xiang committed
73
    # JIT compile the whole model and repeat all tests
74
75
76
77
78
79

    def setUp(self):
        super().setUp()
        self.model = torch.jit.script(self.model)


80
81
if __name__ == '__main__':
    unittest.main()