Skip to content

Instantly share code, notes, and snippets.

@andersx
Created January 23, 2018 13:07
Show Gist options
  • Select an option

  • Save andersx/1ed800e089f52ecbb5cb63eac994729c to your computer and use it in GitHub Desktop.

Select an option

Save andersx/1ed800e089f52ecbb5cb63eac994729c to your computer and use it in GitHub Desktop.
Example how to generate a local kernel matrix with FCHL
#!/usr/bin/env python2
import sys
sys.path.insert(0,"/home/andersx/dev/qml/fchl_arad/build/lib.linux-x86_64-2.7")
# sys.path.insert(0,"/home/andersx/dev/qml/fchl/build/lib.linux-x86_64-2.7")
import numpy as np
from time import time
import qml
import qml.fchl
from qml.fchl import generate_fchl_representation
from qml.fchl import get_local_symmetric_kernels_fchl
def parse_dir(xyz_dir, datafile):
f = open(datafile, "r")
lines = f.readlines()
f.close()
compounds = []
for line in lines[:1000]:
if "#" in line[:2]: continue
tokens = line.split()
name = tokens[0]
energy = float(tokens[1])
xyz_name = "%s%s" % (xyz_dir, name)
c = qml.Compound(xyz=xyz_name)
c.properties = energy
compounds.append(c)
return compounds
cut_distance = 1e6
if __name__ == "__main__":
xyz_dir = sys.argv[1]
datafile = sys.argv[2]
compounds = parse_dir(xyz_dir, datafile)
Y = np.array([compound.properties for compound in compounds])
np.save("Y.npy", Y)
nmax = max([compound.natoms for compound in compounds])
X = []
for c in compounds:
rep = generate_fchl_representation(c.coordinates, c.nuclear_charges,
size=nmax, neighbors=nmax, cut_distance=cut_distance)
X.append(rep)
X = np.array(X)
np.save("X.npy", X)
print X.shape
sigmas = [0.01 * 2**i for i in range(20)]
print "K ..."
t_start = time()
K = get_local_symmetric_kernels_fchl(X, sigmas,
cut_distance=cut_distance,
alchemy='off',
)
print "done", time() - t_start
np.save("K_arad.npy" , K)
print K[10]
print K.shape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment