Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new "ndirect" mode for preprocessing #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Then, you can use the `preprocess.py` to preprocess the `traj.npz`. It will crea
python preprocess.py traj.npz graph.npz
```

Note that the graph construction is slow especially for large MD trajectories. There two different graph construction algorithms implemented. The default `--backend kdtree` has a linear scaling but only works for orthogonal simulation box. For non-orthogonal simulation, use flag `--backend direct` which has a quadratic scaling. You can also take advantage of the multiprocessing with flag `--n-workers`. For other flags, checkout the help information with `python preprocess.py -h`.
Note that the graph construction is slow especially for large MD trajectories. There two different graph construction algorithms implemented. The default `--backend kdtree` has a linear scaling but only works for orthogonal simulation box. For non-orthogonal simulation, use flag `--backend direct` or `--backend ndirect` which has a quadratic scaling (for the two choices, the latter is specially efficient for large cells, while the former could be quick for small ones). You can also take advantage of the multiprocessing with flag `--n-workers`. For other flags, checkout the help information with `python preprocess.py -h`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typos:
Two different graph convolution algorithm -> three


### Training the model

Expand Down
8 changes: 5 additions & 3 deletions gdynet/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@
prep_parser.add_argument('--radius', type=float, default=7.,
help='search radius for finding nearest neighbors '
'(default: 7.)')
prep_parser.add_argument('--backend', choices=['kdtree', 'direct'],
default='kdtree', help='either "kdtree" or "direct", '
prep_parser.add_argument('--backend', choices=['kdtree', 'direct', 'ndirect'],
default='kdtree', help='"kdtree", "direct" or "ndirect" available, '
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove “available” after “ndirect”

'the backend used to search for nearest neighbors. '
'"kdtree" has linear scaling but only works for '
'orthogonal lattices. "direct" works for trigonal '
'lattices but has quadratic scaling. '
'lattices but has quadratic scaling. "ndirect" is '
'an enhanced method for "direct" which could '
'accelarate the process ofdealing with large lattices.'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ofdealing -> of dealing

'(default: "kdtree")')
33 changes: 30 additions & 3 deletions gdynet/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
from .utils import PeriodicCKDTree, distance_pbc
from tqdm import tqdm
from pymatgen.core.structure import IStructure
from pymatgen.core.structure import IStructure, Structure


class Preprocess(object):
Expand Down Expand Up @@ -72,8 +72,8 @@ def __init__(self, input_file, output_file, n_nbrs=20, radius=7.,
self.output_file = output_file
self.n_nbrs = n_nbrs
self.radius = radius
if backend not in ['kdtree', 'direct']:
raise ValueError('backend should be either "kdtree" or "direct", '
if backend not in ['kdtree', 'direct', 'ndirect']:
raise ValueError('backend should be "kdtree", "ndirect" or "direct", '
'but got {}'.format(backend))
self.backend = backend
self.verbose = verbose
Expand Down Expand Up @@ -169,6 +169,33 @@ def construct_graph(self, traj_coords, lattices, atom_types, target_index):
'target_index': target_index,
'nbr_lists': nbr_lists,
'nbr_dists': nbr_dists}
elif self.backend == 'ndirect':
stcs = [Structure(lattice=lattices[i],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t use such complex list comprehensions. Use a for loop for code readability.

species=atom_types,
coords=traj_coords[i],
coords_are_cartesian=True)
for i in tqdm(range(len(traj_coords)),
desc='Generating structure...', disable=not self.verbose)]
a, b, c = [np.ceil(2*self.radius/d).astype('int')
for d in stcs[0].lattice.abc]
if [a, b, c] != [1, 1, 1]:
_ = [stc.make_supercell(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a for loop here. As well as several places below.

[np.ceil(2*self.radius/d).astype('int') for d in stc.lattice.abc])
for stc in tqdm(stcs, desc='Building supercell...', disable=not self.verbose)]
nbr_lists = np.array([stc.distance_matrix.argsort()[
:, 1:1+self.n_nbrs] for stc in tqdm(
stcs, desc='Generating neighbor index...', disable=not self.verbose)], dtype='int32')
nbr_dists = np.array([np.sort(stc.distance_matrix)[
:, 1:1+self.n_nbrs] for stc in tqdm(
Copy link
Owner

@txie-93 txie-93 Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reformat your code according to PEP8? There should be whitespaces between 1+ for example. You can do it with automated tools.

stcs, desc='Generating neighbor distance...', disable=not self.verbose)], dtype='float32')
nbr_lists, nbr_dists = np.stack(nbr_lists), np.stack(nbr_dists)
if not np.all((nbr_dists < self.radius) & (nbr_dists > 0)):
raise('not find enough neighbors')
return {'traj_coords': traj_coords,
'atom_types': atom_types,
'target_index': target_index,
'nbr_lists': nbr_lists,
'nbr_dists': nbr_dists}

def preprocess(self):
if self.verbose:
Expand Down