Skip to content

Commit

Permalink
Update example notebook, remove tqdm dependency from bindings
Browse files Browse the repository at this point in the history
Note also: i-PI examples dir has been renamed

Co-authored-by: Guillaume Fraux <[email protected]>
  • Loading branch information
max-veit and Luthaf committed Apr 12, 2021
1 parent 3281e07 commit 8a074b3
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 110 deletions.
51 changes: 31 additions & 20 deletions bindings/rascal/models/krr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@
Kernel Kernel Ridge Regression model (sparse GPR only).
Public functions:
compute_kernel_single Compute GAP kernel of a single structure
compute_KNM Compute GAP kernel of a set of structures
train_gap_model Train a GAP model given a kernel matrix and sparse points
"""
from ..utils import BaseIO, is_notebook
from ..utils import BaseIO
from ..lib import compute_sparse_kernel_gradients, compute_sparse_kernel_neg_stress

import numpy as np
import ase

try:
if is_notebook():
from tqdm.notebook import tqdm
else:
from tqdm import tqdm
except ImportError:
from ..utils.misc import tqdm_nop as tqdm


class KRR(BaseIO):
"""Kernel Ridge Regression model. Only supports sparse GPR
Expand Down Expand Up @@ -241,7 +233,6 @@ def get_representation_calculator(self):
return self.kernel._rep


# TODO(max, felix) I think this belongs in utils; it's not KRR-specific
def _get_kernel_strides(frames):
"""Get strides for total-energy/gradient kernels of the given structures
Expand All @@ -256,11 +247,11 @@ def _get_kernel_strides(frames):
Returns
-------
(1)
int
the number of structures
(2)
int
the number of gradient entries (== 3 * the total number of atoms)
(3)
np.array(int)
strides for assigning the gradient entries for each structure
"""
Nstructures = len(frames)
Expand All @@ -274,7 +265,7 @@ def _get_kernel_strides(frames):
return Nstructures, Ngrads, Ngrad_stride


def compute_kernel_single(i_frame, frame, representation, X_sparse, kernel):
def _compute_kernel_single(i_frame, frame, representation, X_sparse, kernel):
"""Compute GAP kernel of the (new) structure against the sparse points
Parameters
Expand Down Expand Up @@ -321,18 +312,38 @@ def compute_KNM(frames, X_sparse, kernel, soap):
-------
K_NM: np.array
Summed total-energy kernel stacked with the atom-position gradient of the kernel
Notes
-----
This function can take quite a long time to run. To get a progress bar,
you can wrap the `frames` parameter in a [tqdm]_ object like this:
.. code-block:: python
from tqdm.notebook import tqdm # for Jupyter
#from tqdm import tqdm # on the command line
K_NM = compute_KNM(
tqdm(frames, desc="compute KNM", leave=False),
X_sparse,
kernel,
soap
)
.. [tqdm] https://github.com/tqdm/tqdm
"""
Nstructures, Ngrads, Ngrad_stride = _get_kernel_strides(frames)
# If frames has been wrapped in a tqdm, use the underlying iterable
# so as not to "use up" the progress bar prematurely
if hasattr(frames, "iterable"):
Nstructures, Ngrads, Ngrad_stride = _get_kernel_strides(frames.iterable)
else:
Nstructures, Ngrads, Ngrad_stride = _get_kernel_strides(frames)
KNM = np.zeros((Nstructures + Ngrads, X_sparse.size()))
pbar = tqdm(frames, desc="compute KNM", leave=False)
for i_frame, frame in enumerate(frames):
en_row, grad_rows = compute_kernel_single(
en_row, grad_rows = _compute_kernel_single(
i_frame, frame, soap, X_sparse, kernel
)
KNM[Ngrad_stride[i_frame] : Ngrad_stride[i_frame + 1]] = grad_rows
KNM[i_frame] = en_row
pbar.update()
pbar.close()
return KNM


Expand Down
1 change: 0 additions & 1 deletion bindings/rascal/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
dump_obj,
load_obj,
)
from .misc import is_notebook

# Warning potential dependency loop: FPS imports models, which imports KRR,
# which imports this file again
Expand Down
39 changes: 0 additions & 39 deletions bindings/rascal/utils/misc.py

This file was deleted.

2 changes: 1 addition & 1 deletion docs/source/examples/zundel_IP.ipynb
2 changes: 0 additions & 2 deletions docs/source/reference/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ Models

.. autofunction:: rascal.models.compute_KNM

.. autofunction:: rascal.models.krr.compute_kernel_single

IO
===

Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions examples/iPi/zundel/run.sh → examples/i-PI/zundel/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# remove the driver file if it already exists
rm /tmp/ipi_zundel
# make sure rascal can be imported if not installed
export PYTHONPATH="../../../build_b/:$PYTHONPATH"
#export PYTHONPATH="../../../build/:$PYTHONPATH"
# path to the i-Pi driver
RASCAL_DRIVER="../../../../i-pi/drivers/py/driver.py"
#RASCAL_DRIVER="../../../../i-pi/drivers/py/driver.py"
# (or add it to your PATH and use the below:)
RASCAL_DRIVER="driver.py"
# i-Pi executable
IPI="i-pi"

Expand Down
Loading

0 comments on commit 8a074b3

Please sign in to comment.