test_aev.py 4.37 KB
Newer Older
1
2
3
4
5
import torch
import torchani
import unittest
import os
import pickle
6
import random
7
8
9
10
11
12
13

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


class TestAEV(unittest.TestCase):

14
    def setUp(self):
15
16
        builtins = torchani.neurochem.Builtins()
        self.aev_computer = builtins.aev_computer
17
        self.radial_length = self.aev_computer.radial_length()
18
19
        self.tolerance = 1e-5

20
21
22
23
24
25
    def random_skip(self):
        return False

    def transform(self, x):
        return x

26
    def _assertAEVEqual(self, expected_radial, expected_angular, aev):
27
28
        radial = aev[..., :self.radial_length]
        angular = aev[..., self.radial_length:]
29
30
31
32
33
34
35
        radial_diff = expected_radial - radial
        radial_max_error = torch.max(torch.abs(radial_diff)).item()
        angular_diff = expected_angular - angular
        angular_max_error = torch.max(torch.abs(angular_diff)).item()
        self.assertLess(radial_max_error, self.tolerance)
        self.assertLess(angular_max_error, self.tolerance)

36
37
    def testIsomers(self):
        for i in range(N):
38
            datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
39
40
41
            with open(datafile, 'rb') as f:
                coordinates, species, expected_radial, expected_angular, _, _ \
                    = pickle.load(f)
42
43
44
45
46
47
48
49
                coordinates = torch.from_numpy(coordinates)
                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)
50
                _, aev = self.aev_computer((species, coordinates))
51
52
53
54
55
                self._assertAEVEqual(expected_radial, expected_angular, aev)

    def testPadding(self):
        species_coordinates = []
        radial_angular = []
56
        for i in range(N):
57
            datafile = os.path.join(path, 'test_data/ANI1_subset/{}'.format(i))
58
59
            with open(datafile, 'rb') as f:
                coordinates, species, radial, angular, _, _ = pickle.load(f)
60
61
62
63
64
65
66
67
                coordinates = torch.from_numpy(coordinates)
                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)
68
                species_coordinates.append((species, coordinates))
69
                radial_angular.append((radial, angular))
70
        species, coordinates = torchani.utils.pad_coordinates(
71
72
73
74
75
76
77
78
79
            species_coordinates)
        _, aev = self.aev_computer((species, coordinates))
        start = 0
        for expected_radial, expected_angular in radial_angular:
            conformations = expected_radial.shape[0]
            atoms = expected_radial.shape[1]
            aev_ = aev[start:start+conformations, 0:atoms]
            start += conformations
            self._assertAEVEqual(expected_radial, expected_angular, aev_)
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
    def testNIST(self):
        datafile = os.path.join(path, 'test_data/NIST/all')
        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)
                angular = torch.from_numpy(angular).to(torch.float)
                _, aev = self.aev_computer((species, coordinates))
                self._assertAEVEqual(radial, angular, aev)

95

Gao, Xiang's avatar
Gao, Xiang committed
96
97
98
99
100
101
class TestAEVASENeighborList(TestAEV):

    def setUp(self):
        super(TestAEVASENeighborList, self).setUp()
        self.aev_computer.neighborlist = torchani.ase.NeighborList()

102
103
104
105
106
107
108
109
    def transform(self, x):
        """To reduce the size of test cases for faster test speed"""
        return x[:2, ...]

    def random_skip(self):
        """To reduce the size of test cases for faster test speed"""
        return random.random() < 0.95

Gao, Xiang's avatar
Gao, Xiang committed
110

111
112
if __name__ == '__main__':
    unittest.main()