Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

Commit a96624d

Browse files
larsbratholmandersx
authored andcommitted
added kernels, tests, representations (#11)
* Formatting etc. * Added sargan and matern kernel * Improved tests, bob and eigenvalue fortran implementations, minor formatting and bugfixes * python3 fix * python3 fix * safer dictionary sorting in bob * fixed failed bob test * debugging python3 * fixed python3 * fixed math error in documentation * updated documentation
1 parent 9f92769 commit a96624d

22 files changed

+1327
-608
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ The easiest way to install the stable version from the Python Package Index (PyP
3535
pip install qml --user -U
3636
```
3737

38+
To use the Intel compiler, together with the MKL math library:
39+
40+
```bash
41+
pip install qml --user -U --global-option="build" --global-option="--compiler=intelem" --global-option="--fcompiler=intelem"
42+
```
43+
3844
Alternatively, you can install the most recent stable development snapshot directly from GitHub:
3945

4046
```bash

docs/build/html/.nojekyll

Whitespace-only changes.

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# If your documentation needs a minimal Sphinx version, state it here.
2828
#
29-
# needs_sphinx = '1.0'
29+
needs_sphinx = '1.5.4'
3030

3131
# Add any Sphinx extension module names here, as strings. They can be
3232
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom

qml/arad.py

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,86 +21,82 @@
2121
# SOFTWARE.
2222

2323
import numpy as np
24-
import copy
25-
2624

2725
class ARAD(object):
2826

29-
def getAngle(self,sp,norms):
27+
def getAngle(self, sp, norms):
3028
angles = np.zeros(sp.shape)
3129
mask1 = np.logical_and(np.abs(sp - norms) > self.epsilon ,np.abs(norms) > self.epsilon)
3230
angles[mask1] = np.arccos(sp[mask1]/norms[mask1])
3331
return angles
3432

35-
36-
def __init__(self,maxMolSize = 30,maxAts = 30,cut = 5., debug=False):
33+
def __init__(self, maxMolSize = 30, maxAts = 30, cut = 5., debug = False):
3734
self.tag = 'coords'
3835
self.debug = debug
3936
self.maxMolSize = maxMolSize
4037
self.maxAts = maxAts
4138
self.cut = cut
4239
self.epsilon = 100.0 * np.finfo(float).eps
43-
self.PTP = {\
44-
1 :[1,1] ,2: [1,8]#Row1
40+
self.PTP = {
41+
1 :[1,1] ,2: [1,8] #Row1
4542

46-
,3 :[2,1] ,4: [2,2]#Row2\
43+
,3 :[2,1] ,4: [2,2] #Row2
4744
,5 :[2,3] ,6: [2,4] ,7 :[2,5] ,8 :[2,6] ,9 :[2,7] ,10 :[2,8]\
4845

49-
,11 :[3,1] ,12: [3,2]#Row3\
46+
,11 :[3,1] ,12: [3,2] #Row3
5047
,13 :[3,3] ,14: [3,4] ,15 :[3,5] ,16 :[3,6] ,17 :[3,7] ,18 :[3,8]\
5148

52-
,19 :[4,1] ,20: [4,2]#Row4\
49+
,19 :[4,1] ,20: [4,2] #Row4
5350
,31 :[4,3] ,32: [4,4] ,33 :[4,5] ,34 :[4,6] ,35 :[4,7] ,36 :[4,8]\
5451
,21 :[4,9] ,22: [4,10],23 :[4,11],24 :[4,12],25 :[4,13],26 :[4,14],27 :[4,15],28 :[4,16],29 :[4,17],30 :[4,18]\
5552

56-
,37 :[5,1] ,38: [5,2]#Row5\
53+
,37 :[5,1] ,38: [5,2] #Row5
5754
,49 :[5,3] ,50: [5,4] ,51 :[5,5] ,52 :[5,6] ,53 :[5,7] ,54 :[5,8]\
5855
,39 :[5,9] ,40: [5,10],41 :[5,11],42 :[5,12],43 :[5,13],44 :[5,14],45 :[5,15],46 :[5,16],47 :[5,17],48 :[5,18]\
5956

60-
,55 :[6,1] ,56: [6,2]#Row6\
57+
,55 :[6,1] ,56: [6,2] #Row6
6158
,81 :[6,3] ,82: [6,4] ,83 :[6,5] ,84 :[6,6] ,85 :[6,7] ,86 :[6,8]
6259
,72: [6,10],73 :[6,11],74 :[6,12],75 :[6,13],76 :[6,14],77 :[6,15],78 :[6,16],79 :[6,17],80 :[6,18]\
6360
,57 :[6,19],58: [6,20],59 :[6,21],60 :[6,22],61 :[6,23],62 :[6,24],63 :[6,25],64 :[6,26],65 :[6,27],66 :[6,28],67 :[6,29],68 :[6,30],69 :[6,31],70 :[6,32],71 :[6,33]\
6461

65-
,87 :[7,1] ,88: [7,2]#Row7\
62+
,87 :[7,1] ,88: [7,2] #Row7
6663
,113:[7,3] ,114:[7,4] ,115:[7,5] ,116:[7,6] ,117:[7,7] ,118:[7,8]\
6764
,104:[7,10],105:[7,11],106:[7,12],107:[7,13],108:[7,14],109:[7,15],110:[7,16],111:[7,17],112:[7,18]\
6865
,89 :[7,19],90: [7,20],91 :[7,21],92 :[7,22],93 :[7,23],94 :[7,24],95 :[7,25],96 :[7,26],97 :[7,27],98 :[7,28],99 :[7,29],100:[7,30],101:[7,31],101:[7,32],102:[7,14],103:[7,33]}
6966

70-
def describe(self,coords,ocupationList,cell = None):
71-
L = len(coords)
72-
coords = np.asarray(coords)
73-
ocupationList = np.asarray(ocupationList)
74-
M = np.zeros((self.maxMolSize,5,self.maxAts))
67+
def describe(self, coords, occupationList, cell = None):
68+
L = coords.shape[0]
69+
occupationList = np.asarray(occupationList)
70+
M = np.zeros((self.maxMolSize, 5, self.maxAts))
7571

7672
if cell is not None:
77-
coords = np.dot(coords,cell)
78-
nExtend = (np.floor(self.cut/np.linalg.norm(cell,2,axis = 0)) + 1).astype(int)
79-
for i in range(-nExtend[0],nExtend[0] + 1):
80-
for j in range(-nExtend[1],nExtend[1] + 1):
81-
for k in range(-nExtend[2],nExtend[2] + 1):
73+
coords = np.dot(coords, cell)
74+
nExtend = (np.floor(self.cut/np.linalg.norm(cell, 2, axis = 0)) + 1).astype(int)
75+
for i in range(-nExtend[0], nExtend[0] + 1):
76+
for j in range(-nExtend[1], nExtend[1] + 1):
77+
for k in range(-nExtend[2], nExtend[2] + 1):
8278
if i == -nExtend[0] and j == -nExtend[1] and k == -nExtend[2]:
8379
coordsExt = coords + i*cell[0,:] + j*cell[1,:] + k*cell[2,:]
84-
ocupationListExt = copy.copy(ocupationList)
80+
occupationListExt = occupationList.copy()
8581
else:
86-
ocupationListExt = np.append(ocupationListExt,ocupationList)
87-
coordsExt = np.append(coordsExt,coords + i*cell[0,:] + j*cell[1,:] + k*cell[2,:],axis = 0)
82+
occupationListExt = np.append(occupationListExt,occupationList)
83+
coordsExt = np.append(coordsExt, coords + i*cell[0,:] + j*cell[1,:] + k*cell[2,:], axis = 0)
8884

8985
else:
90-
coordsExt = np.copy(coords)
91-
ocupationListExt = np.copy(ocupationList)
86+
coordsExt = coords.copy()
87+
occupationListExt = occupationList.copy()
9288

9389
M[:,0,:] = 1E+100
9490

9591
for i in range(L):
9692
#Calculate Distance
97-
cD = - coords[i] + coordsExt[:]
98-
ocExt = np.asarray([self.PTP[o] for o in ocupationListExt])
93+
cD = coordsExt[:] - coords[i]
94+
ocExt = np.asarray([self.PTP[o] for o in occupationListExt])
9995

10096
#Obtaining angles
10197
sp = np.sum(cD[:,np.newaxis] * cD[np.newaxis,:], axis = 2)
10298
D1 = np.sqrt(np.sum(cD**2, axis = 1))
103-
D2 = D1[:,np.newaxis]*D1[np.newaxis,:]
99+
D2 = D1[:, np.newaxis] * D1[np.newaxis, :]
104100
angs = self.getAngle(sp, D2)
105101

106102
#Obtaining cos and sine terms
@@ -113,33 +109,25 @@ def describe(self,coords,ocupationList,cell = None):
113109

114110
ocExt = np.asarray([ocExt[l] for l in args])
115111

116-
cosAngs = cosAngs[args,:]
117-
cosAngs = cosAngs[:,args]
118-
sinAngs = sinAngs[args,:]
119-
sinAngs = sinAngs[:,args]
112+
sub_indices = np.ix_(args, args)
113+
cosAngs = cosAngs[sub_indices]
114+
sinAngs = sinAngs[sub_indices]
120115

121116
args = np.where(D1 < self.cut)[0]
122117

123118
D1 = D1[args]
124119

125120
ocExt = np.asarray([ocExt[l] for l in args])
126121

127-
cosAngs = cosAngs[args,:]
128-
cosAngs = cosAngs[:,args]
129-
sinAngs = sinAngs[args,:]
130-
sinAngs = sinAngs[:,args]
131-
132-
D1 = D1
133-
ocExt = ocExt
134-
cosAngs = cosAngs
135-
sinAngs = sinAngs
136-
137-
norm = np.sum(1.0 - np.sin(np.pi * D1[np.newaxis,:]/(2.0 * self.cut)))
138-
M[i,0,: len(D1)] = D1
139-
M[i,1,: len(D1)] = ocExt[:,0]
140-
M[i,2,: len(D1)] = ocExt[:,1]
141-
M[i,3,: len(D1)] = np.sum(cosAngs,axis = 1)/(norm)
142-
M[i,4,: len(D1)] = np.sum(sinAngs,axis = 1)/(norm)
122+
sub_indices = np.ix_(args, args)
123+
cosAngs = cosAngs[sub_indices]
124+
sinAngs = sinAngs[sub_indices]
143125

126+
norm = np.sum(1.0 - np.sin(np.pi * D1[np.newaxis, :] / (2.0 * self.cut)))
127+
M[i, 0, :len(D1)] = D1
128+
M[i, 1, :len(D1)] = ocExt[:, 0]
129+
M[i, 2, :len(D1)] = ocExt[:, 1]
130+
M[i, 3, :len(D1)] = np.sum(cosAngs,axis = 1) / norm
131+
M[i, 4, :len(D1)] = np.sum(sinAngs,axis = 1) / norm
144132

145133
return M

qml/arad_kernels.py

Lines changed: 65 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,61 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
# SOFTWARE.
2222

23+
from __future__ import print_function
24+
2325
import numpy as np
2426

2527
from .farad_kernels import fget_kernels_arad
2628
from .farad_kernels import fget_symmetric_kernels_arad
2729

28-
PTP = {\
29-
1 :[1,1] ,2: [1,8]#Row1
30-
31-
,3 :[2,1] ,4: [2,2]#Row2\
32-
,5 :[2,3] ,6: [2,4] ,7 :[2,5] ,8 :[2,6] ,9 :[2,7] ,10 :[2,8]\
33-
34-
,11 :[3,1] ,12: [3,2]#Row3\
35-
,13 :[3,3] ,14: [3,4] ,15 :[3,5] ,16 :[3,6] ,17 :[3,7] ,18 :[3,8]\
36-
37-
,19 :[4,1] ,20: [4,2]#Row4\
38-
,31 :[4,3] ,32: [4,4] ,33 :[4,5] ,34 :[4,6] ,35 :[4,7] ,36 :[4,8]\
39-
,21 :[4,9] ,22: [4,10],23 :[4,11],24 :[4,12],25 :[4,13],26 :[4,14],27 :[4,15],28 :[4,16],29 :[4,17],30 :[4,18]\
40-
41-
,37 :[5,1] ,38: [5,2]#Row5\
42-
,49 :[5,3] ,50: [5,4] ,51 :[5,5] ,52 :[5,6] ,53 :[5,7] ,54 :[5,8]\
43-
,39 :[5,9] ,40: [5,10],41 :[5,11],42 :[5,12],43 :[5,13],44 :[5,14],45 :[5,15],46 :[5,16],47 :[5,17],48 :[5,18]\
44-
45-
,55 :[6,1] ,56: [6,2]#Row6\
30+
PTP = {
31+
1 :[1,1] ,2: [1,8] #Row1
32+
33+
,3 :[2,1] ,4: [2,2] #Row2
34+
,5 :[2,3] ,6: [2,4] ,7 :[2,5] ,8 :[2,6] ,9 :[2,7] ,10 :[2,8]
35+
36+
,11 :[3,1] ,12: [3,2] #Row3
37+
,13 :[3,3] ,14: [3,4] ,15 :[3,5] ,16 :[3,6] ,17 :[3,7] ,18 :[3,8]
38+
39+
,19 :[4,1] ,20: [4,2] #Row4
40+
,31 :[4,3] ,32: [4,4] ,33 :[4,5] ,34 :[4,6] ,35 :[4,7] ,36 :[4,8]
41+
,21 :[4,9] ,22: [4,10],23 :[4,11],24 :[4,12],25 :[4,13],26 :[4,14],27 :[4,15],28 :[4,16],29 :[4,17],30 :[4,18]
42+
43+
,37 :[5,1] ,38: [5,2] #Row5
44+
,49 :[5,3] ,50: [5,4] ,51 :[5,5] ,52 :[5,6] ,53 :[5,7] ,54 :[5,8]
45+
,39 :[5,9] ,40: [5,10],41 :[5,11],42 :[5,12],43 :[5,13],44 :[5,14],45 :[5,15],46 :[5,16],47 :[5,17],48 :[5,18]
46+
47+
,55 :[6,1] ,56: [6,2] #Row6
4648
,81 :[6,3] ,82: [6,4] ,83 :[6,5] ,84 :[6,6] ,85 :[6,7] ,86 :[6,8]
47-
,72: [6,10],73 :[6,11],74 :[6,12],75 :[6,13],76 :[6,14],77 :[6,15],78 :[6,16],79 :[6,17],80 :[6,18]\
48-
,57 :[6,19],58: [6,20],59 :[6,21],60 :[6,22],61 :[6,23],62 :[6,24],63 :[6,25],64 :[6,26],65 :[6,27],66 :[6,28],67 :[6,29],68 :[6,30],69 :[6,31],70 :[6,32],71 :[6,33]\
49+
,72: [6,10],73 :[6,11],74 :[6,12],75 :[6,13],76 :[6,14],77 :[6,15],78 :[6,16],79 :[6,17],80 :[6,18]
50+
,57 :[6,19],58: [6,20],59 :[6,21],60 :[6,22],61 :[6,23],62 :[6,24],63 :[6,25],64 :[6,26],65 :[6,27],66 :[6,28],67 :[6,29],68 :[6,30],69 :[6,31],70 :[6,32],71 :[6,33]
4951

50-
,87 :[7,1] ,88: [7,2]#Row7\
51-
,113:[7,3] ,114:[7,4] ,115:[7,5] ,116:[7,6] ,117:[7,7] ,118:[7,8]\
52-
,104:[7,10],105:[7,11],106:[7,12],107:[7,13],108:[7,14],109:[7,15],110:[7,16],111:[7,17],112:[7,18]\
52+
,87 :[7,1] ,88: [7,2] #Row7
53+
,113:[7,3] ,114:[7,4] ,115:[7,5] ,116:[7,6] ,117:[7,7] ,118:[7,8]
54+
,104:[7,10],105:[7,11],106:[7,12],107:[7,13],108:[7,14],109:[7,15],110:[7,16],111:[7,17],112:[7,18]
5355
,89 :[7,19],90: [7,20],91 :[7,21],92 :[7,22],93 :[7,23],94 :[7,24],95 :[7,25],96 :[7,26],97 :[7,27],98 :[7,28],99 :[7,29],100:[7,30],101:[7,31],101:[7,32],102:[7,14],103:[7,33]}
5456

5557

56-
def get_atomic_kernels_arad(X1, X2, Z1, Z2, sigmas, \
58+
def get_atomic_kernels_arad(X1, X2, Z1, Z2, sigmas,
5759
width=0.2, cut_distance=5.0, r_width=1.0, c_width=0.5):
5860
""" Calculates the Gaussian kernel matrix K for atomic ARAD
5961
descriptors for a list of different sigmas.
6062
6163
K is calculated using an OpenMP parallel Fortran routine.
6264
63-
Arguments:
64-
==============
65-
X1 -- np.array of ARAD descriptors for molecules in set 1.
66-
X2 -- np.array of ARAD descriptors for molecules in set 2.
67-
Z1 -- List of lists of nuclear charges for molecules in set 1.
68-
Z2 -- List of lists of nuclear charges for molecules in set 2.
69-
sigmas -- List of sigma for which to calculate the Kernel matrices.
70-
71-
Returns:
72-
==============
73-
K -- The kernel matrices for each sigma (3D-array, Ns x N1 x N2)
65+
:param X1: ARAD descriptors for molecules in set 1.
66+
:type X1: numpy array
67+
:param X2: Array of ARAD descriptors for molecules in set 2.
68+
:type X2: numpy array
69+
:param Z1: List of lists of nuclear charges for molecules in set 1.
70+
:type Z1: list
71+
:param Z2: List of lists of nuclear charges for molecules in set 2.
72+
:type Z2: list
73+
:param sigmas: List of sigmas for which to calculate the Kernel matrices.
74+
:type sigmas: list
75+
76+
:return: The kernel matrices for each sigma - shape (Ns, N1, N2)
77+
:rtype: numpy array
7478
"""
7579

7680
amax = X1.shape[1]
@@ -85,61 +89,42 @@ def get_atomic_kernels_arad(X1, X2, Z1, Z2, sigmas, \
8589
assert X1.shape[0] == nm1, "ERROR: Check ARAD decriptor sizes! code = 4"
8690
assert X2.shape[0] == nm2, "ERROR: Check ARAD decriptor sizes! code = 5"
8791

88-
N1 = []
89-
for Z in Z1:
90-
N1.append(len(Z))
91-
92-
N2 = []
93-
for Z in Z2:
94-
N2.append(len(Z))
95-
96-
N1 = np.array(N1,dtype=np.int32)
97-
N2 = np.array(N2,dtype=np.int32)
98-
99-
nsigmas = len(sigmas)
100-
101-
c1 = []
102-
for charges in Z1:
103-
c1.append(np.array([PTP[int(q)] for q in charges], dtype=np.int32))
104-
105-
Z1_arad = np.zeros((nm1,amax,2))
106-
92+
N1 = np.empty(nm1, dtype = np.int32)
93+
Z1_arad = np.zeros((nm1, amax, 2))
10794
for i in range(nm1):
108-
for j, z in enumerate(c1[i]):
109-
Z1_arad[i,j] = z
110-
111-
c2 = []
112-
for charges in Z2:
113-
c2.append(np.array([PTP[int(q)] for q in charges], dtype=np.int32))
114-
115-
Z2_arad = np.zeros((nm2,amax,2))
95+
N1[i] = len(Z1[i])
96+
Z1_arad[i,:N1[i]] = np.asarray([PTP[q] for q in Z1[i]], dtype = np.int32)
11697

98+
N2 = np.empty(nm2, dtype = np.int32)
99+
Z2_arad = np.zeros((nm2, amax, 2))
117100
for i in range(nm2):
118-
for j, z in enumerate(c2[i]):
119-
Z2_arad[i,j] = z
101+
N2[i] = len(Z2[i])
102+
Z2_arad[i,:N2[i]] = np.asarray([PTP[q] for q in Z2[i]], dtype = np.int32)
103+
120104

121105
sigmas = np.array(sigmas)
106+
nsigmas = sigmas.size
122107

123-
return fget_kernels_arad(X1, X2, Z1_arad, Z2_arad, N1, N2, sigmas, \
108+
return fget_kernels_arad(X1, X2, Z1_arad, Z2_arad, N1, N2, sigmas,
124109
nm1, nm2, nsigmas, width, cut_distance, r_width, c_width)
125110

126111

127-
def get_atomic_symmetric_kernels_arad(X1, Z1, sigmas, \
112+
def get_atomic_symmetric_kernels_arad(X1, Z1, sigmas,
128113
width=0.2, cut_distance=5.0, r_width=1.0, c_width=0.5):
129114
""" Calculates the Gaussian kernel matrix K for atomic ARAD
130115
descriptors for a list of different sigmas.
131116
132117
K is calculated using an OpenMP parallel Fortran routine.
133118
134-
Arguments:
135-
==============
136-
X1 -- np.array of ARAD descriptors for molecules in set 1.
137-
Z1 -- List of lists of nuclear charges for molecules in set 1.
138-
sigmas -- List of sigma for which to calculate the Kernel matrices.
119+
:param X1: ARAD descriptors for molecules in set 1.
120+
:type X1: numpy array
121+
:param Z1: List of lists of nuclear charges for molecules in set 1.
122+
:type Z1: list
123+
:param sigmas: List of sigmas for which to calculate the Kernel matrices.
124+
:type sigmas: list
139125
140-
Returns:
141-
==============
142-
K -- The kernel matrices for each sigma (3D-array, Ns x N1 x N2)
126+
:return: The kernel matrices for each sigma - shape (Ns, N1, N1)
127+
:rtype: numpy array
143128
"""
144129

145130
amax = X1.shape[1]
@@ -150,25 +135,14 @@ def get_atomic_symmetric_kernels_arad(X1, Z1, sigmas, \
150135

151136
assert X1.shape[0] == nm1, "ERROR: Check ARAD decriptor sizes! code = 4"
152137

153-
N1 = []
154-
for Z in Z1:
155-
N1.append(len(Z))
156-
157-
N1 = np.array(N1,dtype=np.int32)
158-
159-
nsigmas = len(sigmas)
160-
161-
c1 = []
162-
for charges in Z1:
163-
c1.append(np.array([PTP[int(q)] for q in charges], dtype=np.int32))
164-
165-
Z1_arad = np.zeros((nm1,amax,2))
166-
138+
N1 = np.empty(nm1, dtype = np.int32)
139+
Z1_arad = np.zeros((nm1, amax, 2))
167140
for i in range(nm1):
168-
for j, z in enumerate(c1[i]):
169-
Z1_arad[i,j] = z
141+
N1[i] = len(Z1[i])
142+
Z1_arad[i,:N1[i]] = np.asarray([PTP[q] for q in Z1[i]], dtype = np.int32)
170143

171144
sigmas = np.array(sigmas)
145+
nsigmas = sigmas.size
172146

173-
return fget_symmetric_kernels_arad(X1, Z1_arad, N1, sigmas, \
147+
return fget_symmetric_kernels_arad(X1, Z1_arad, N1, sigmas,
174148
nm1, nsigmas, width, cut_distance, r_width, c_width)

0 commit comments

Comments
 (0)