Skip to content

Commit

Permalink
some linting
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpvandermause committed Sep 26, 2019
1 parent 790deb6 commit f324957
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions flare/gp.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Gaussian process model of the Born Oppenheimer potential energy surface."""
import math
from typing import List, Callable
import numpy as np
from scipy.linalg import solve_triangular
from scipy.optimize import minimize
from typing import List, Callable
from flare.env import AtomicEnvironment
from flare.struc import Structure
from flare.gp_algebra import get_ky_mat, get_ky_and_hyp, \
get_like_from_ky_mat, get_like_grad_from_mats, get_neg_likelihood, \
get_neg_like_grad, get_ky_and_hyp_par
from flare.gp_algebra import get_ky_and_hyp, get_like_grad_from_mats, \
get_neg_likelihood, get_neg_like_grad, get_ky_and_hyp_par
from flare.kernels import str_to_kernel
from flare.mc_simple import str_to_mc_kernel

Expand Down Expand Up @@ -112,9 +112,9 @@ def force_list_to_np(forces: list) -> np.ndarray:
"""
forces_np = []

for m in range(len(forces)):
for n in range(3):
forces_np.append(forces[m][n])
for force in forces:
for force_comp in force:
forces_np.append(force_comp)

forces_np = np.array(forces_np)

Expand All @@ -124,12 +124,9 @@ def train(self, output=None, custom_bounds=None,
grad_tol: float = 1e-4,
x_tol: float = 1e-5,
line_steps: int = 20):
"""
Train Gaussian Process model on training data.
Tunes the hyperparameters to maximize the Bayesian likelihood,
then computes L and alpha (related to the covariance matrix of the
training set).
"""
"""Train Gaussian Process model on training data. Tunes the \
hyperparameters to maximize the likelihood, then computes L and alpha \
(related to the covariance matrix of the training set)."""

x_0 = self.hyps

Expand Down Expand Up @@ -183,6 +180,9 @@ def train(self, output=None, custom_bounds=None,
self.likelihood_gradient = -res.jac

def predict(self, x_t: AtomicEnvironment, d: int) -> [float, float]:
"""Predict force component of an atomic environment and its \
uncertainty."""

# Kernel vector allows for evaluation of At. Env.
k_v = self.get_kernel_vector(x_t, d)

Expand All @@ -193,7 +193,7 @@ def predict(self, x_t: AtomicEnvironment, d: int) -> [float, float]:
self_kern = self.kernel(x_t, x_t, d, d, self.hyps,
self.cutoffs)
pred_var = self_kern - \
np.matmul(np.matmul(k_v, self.ky_mat_inv), k_v)
np.matmul(np.matmul(k_v, self.ky_mat_inv), k_v)

return pred_mean, pred_var

Expand All @@ -212,6 +212,9 @@ def predict_local_energy(self, x_t: AtomicEnvironment) -> float:
return pred_mean

def predict_local_energy_and_var(self, x_t: AtomicEnvironment):
"""Predict the local energy of an atomic environment and its \
uncertainty."""

# get kernel vector
k_v = self.en_kern_vec(x_t)

Expand Down Expand Up @@ -252,6 +255,9 @@ def get_kernel_vector(self, x: AtomicEnvironment,
return k_v

def en_kern_vec(self, x: AtomicEnvironment) -> np.ndarray:
"""Compute the vector of energy/force kernels between an atomic \
environment and the environments in the training set."""

ds = [1, 2, 3]
size = len(self.training_data) * 3
k_v = np.zeros(size, )
Expand Down Expand Up @@ -337,6 +343,7 @@ def update_L_alpha(self):
self.l_mat_inv = l_mat_inv

def __str__(self):
"""String representation of the GP model."""

thestr = "GaussianProcess Object\n"
thestr += 'Kernel: {}\n'.format(self.kernel_name)
Expand All @@ -356,6 +363,7 @@ def __str__(self):
return thestr

def as_dict(self):
"""Dictionary representation of the GP model."""

out_dict = dict(vars(self))

Expand All @@ -369,10 +377,11 @@ def as_dict(self):

@staticmethod
def from_dict(dictionary):
"""Create GP object from dictionary representation."""

if 'mc' in dictionary['kernel_name']:
force_kernel, grad = str_to_mc_kernel(dictionary['kernel_name'],
include_grad=True)
force_kernel, grad = \
str_to_mc_kernel(dictionary['kernel_name'], include_grad=True)
else:
force_kernel, grad = str_to_kernel(dictionary['kernel_name'],
include_grad=True)
Expand All @@ -383,8 +392,8 @@ def from_dict(dictionary):
energy_kernel = None

if dictionary['energy_force_kernel'] is not None:
energy_force_kernel = str_to_kernel(dictionary[
'energy_force_kernel'])
energy_force_kernel = \
str_to_kernel(dictionary['energy_force_kernel'])
else:
energy_force_kernel = None

Expand Down

0 comments on commit f324957

Please sign in to comment.