generate-unit-test-expect.py 3.18 KB
Newer Older
1
2
3
4
5
6
7
8
import torch
import os
import ase
import pyNeuroChem
import ase_interface
import numpy
import torchani
import pickle
9
from torchani import buildin_const_file, buildin_sae_file, \
Gao, Xiang's avatar
Gao, Xiang committed
10
    buildin_network_dir
11
import torchani.training.pyanitools
12
13
14
15
16

path = os.path.dirname(os.path.realpath(__file__))
conv_au_ev = 27.21138505


Gao, Xiang's avatar
Gao, Xiang committed
17
class NeuroChem (torchani.aev.AEVComputer):
18

Gao, Xiang's avatar
Gao, Xiang committed
19
20
    def __init__(self, const_file=buildin_const_file,
                 sae_file=buildin_sae_file,
21
                 network_dir=buildin_network_dir):
Gao, Xiang's avatar
Gao, Xiang committed
22
        super(NeuroChem, self).__init__(False, const_file)
23
24
25
26
27
28
        self.sae_file = sae_file
        self.network_dir = network_dir
        self.nc = pyNeuroChem.molecule(
            self.const_file, self.sae_file, self.network_dir, 0)

    def _get_radial_part(self, fullaev):
Gao, Xiang's avatar
Gao, Xiang committed
29
        return fullaev[:, :, :self.radial_length]
30
31

    def _get_angular_part(self, fullaev):
Gao, Xiang's avatar
Gao, Xiang committed
32
        return fullaev[:, :, self.radial_length:]
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

    def _per_conformation(self, coordinates, species):
        atoms = coordinates.shape[0]
        mol = ase.Atoms(''.join(species), positions=coordinates)
        mol.set_calculator(ase_interface.ANI(False))
        mol.calc.setnc(self.nc)
        energy = mol.get_potential_energy() / conv_au_ev
        aevs = [mol.calc.nc.atomicenvironments(j) for j in range(atoms)]
        force = mol.get_forces() / conv_au_ev
        aevs = numpy.stack(aevs)
        return aevs, energy, force

    def forward(self, coordinates, species):
        conformations = coordinates.shape[0]
        results = [self._per_conformation(
            coordinates[i], species) for i in range(conformations)]
        aevs, energies, forces = zip(*results)
        aevs = torch.from_numpy(numpy.stack(aevs)).type(
Gao, Xiang's avatar
Gao, Xiang committed
51
            self.EtaR.dtype).to(self.EtaR.device)
52
        energies = torch.from_numpy(numpy.stack(energies)).type(
Gao, Xiang's avatar
Gao, Xiang committed
53
            self.EtaR.dtype).to(self.EtaR.device)
54
        forces = torch.from_numpy(numpy.stack(forces)).type(
Gao, Xiang's avatar
Gao, Xiang committed
55
            self.EtaR.dtype).to(self.EtaR.device)
56
57
58
        return self._get_radial_part(aevs), \
            self._get_angular_part(aevs), \
            energies, forces
59
60


61
62
consts = torchani.neurochem.Constants()
aev = torchani.AEVComputer(**consts)
Gao, Xiang's avatar
Gao, Xiang committed
63
ncaev = NeuroChem().to(torch.device('cpu'))
64
65
mol_count = 0

66

67
species_indices = {consts.species[i]: i for i in range(len(aev.species))}
68
69
for i in [1, 2, 3, 4]:
    data_file = os.path.join(
Gao, Xiang's avatar
Gao, Xiang committed
70
        path, '../dataset/ani_gdb_s0{}.h5'.format(i))
71
    adl = torchani.training.pyanitools.anidataloader(data_file)
72
73
    for data in adl:
        coordinates = data['coordinates'][:10, :]
Gao, Xiang's avatar
Gao, Xiang committed
74
        coordinates = torch.from_numpy(coordinates).type(ncaev.EtaR.dtype)
75
76
77
        species = torch.tensor([species_indices[i] for i in data['species']],
                               dtype=torch.long, device=torch.device('cpu')) \
                       .expand(10, -1)
78
        smiles = ''.join(data['smiles'])
79
        radial, angular, energies, forces = ncaev(coordinates, data['species'])
80
81
82
83
        pickleobj = (coordinates, species, radial, angular, energies, forces)
        dumpfile = os.path.join(
            path, '../tests/test_data/{}'.format(mol_count))
        with open(dumpfile, 'wb') as f:
84
            pickle.dump(pickleobj, f, protocol=2)
85
        mol_count += 1