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

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


class TestAEV(unittest.TestCase):

13
    def setUp(self):
14
        self.aev_computer = torchani.AEVComputer()
Gao, Xiang's avatar
Gao, Xiang committed
15
        self.radial_length = self.aev_computer.radial_length
16
17
        self.tolerance = 1e-5

18
    def _assertAEVEqual(self, expected_radial, expected_angular, aev):
19
20
        radial = aev[..., :self.radial_length]
        angular = aev[..., self.radial_length:]
21
22
23
24
25
26
27
        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)

28
29
30
31
32
33
    def testIsomers(self):
        for i in range(N):
            datafile = os.path.join(path, 'test_data/{}'.format(i))
            with open(datafile, 'rb') as f:
                coordinates, species, expected_radial, expected_angular, _, _ \
                    = pickle.load(f)
34
                _, aev = self.aev_computer((species, coordinates))
35
36
37
38
39
                self._assertAEVEqual(expected_radial, expected_angular, aev)

    def testPadding(self):
        species_coordinates = []
        radial_angular = []
40
41
42
43
        for i in range(N):
            datafile = os.path.join(path, 'test_data/{}'.format(i))
            with open(datafile, 'rb') as f:
                coordinates, species, radial, angular, _, _ = pickle.load(f)
44
                species_coordinates.append((species, coordinates))
45
46
47
48
49
50
51
52
53
54
55
                radial_angular.append((radial, angular))
        species, coordinates = torchani.padding.pad_and_batch(
            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_)
56
57
58
59


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