Skip to content

Commit

Permalink
Merge pull request #191 from mir-group/bugfix/lixin/parameter_helper
Browse files Browse the repository at this point in the history
fix optimization flag in ParameterHelper class
  • Loading branch information
nw13slx authored Jun 24, 2020
2 parents fb87644 + 65253da commit cd79a0f
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 123 deletions.
2 changes: 0 additions & 2 deletions docs/source/tutorials/gpfa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ Roadmap Figure
In this tutorial, we will walk through the first two steps contained in the below figure. the GP from AIMD module is designed to give you the tools necessary to extract FLARE structures from a previously existing molecular dynamics run.

.. figure:: ../../images/GPFA_tutorial.png
:figwidth: 800 %
:align: center


Step 1: Setting up a Gaussian Process Object
--------------------------------------------

Expand Down
16 changes: 12 additions & 4 deletions flare/mgp/mgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,21 @@ def __init__(self,
self.hyps_mask = None
self.cutoffs = None

species_labels = []
coded_species = []
for i, ele in enumerate(unique_species):
if isinstance(ele, str):
self.species_labels.append(ele)
self.coded_species.append(element_to_Z(ele))
species_labels.append(ele)
coded_species.append(element_to_Z(ele))
elif isinstance(ele, int):
self.coded_species.append(ele)
self.species_labels.append(Z_to_element(ele))
coded_species.append(ele)
species_labels.append(Z_to_element(ele))
else:
print("element type not accepted", ele, type(ele))
sort_id = np.argsort(coded_species)
for i in sort_id:
self.coded_species.append(coded_species[i])
self.species_labels.append(species_labels[i])

self.load_grid = grid_params.get('load_grid', None)
self.update = grid_params.get('update', False)
Expand Down
52 changes: 34 additions & 18 deletions flare/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
from flare.output import set_logger
from flare.utils.element_coder import element_to_Z, Z_to_element


class Parameters():
'''
'''

all_kernel_types = ['twobody', 'threebody', 'manybody']
cutoff_types = {'cut3b': 'threebody'}
ndim = {'twobody': 2, 'threebody': 3, 'manybody': 2, 'cut3b': 2}
n_kernel_parameters = {'twobody': 2, 'threebody': 2, 'manybody': 2, 'cut3b': 0}
n_kernel_parameters = {'twobody': 2,
'threebody': 2, 'manybody': 2, 'cut3b': 0}

cutoff_types_keys = list(cutoff_types.keys())
cutoff_types_values = list(cutoff_types.values())

logger = set_logger("Parameters", stream=True,
fileout_name=None, verbose="info")
Expand Down Expand Up @@ -53,7 +59,7 @@ def __init__(self):
'map': None,
'original_hyps': [],
'original_labels': []
}
}
self.hyps = None
self.hyp_labels = None
self.cutoffs = {}
Expand Down Expand Up @@ -103,7 +109,8 @@ def backward(kernels, param_dict):
# add a couple new keys that was not there previously
if 'train_noise' not in param_dict:
param_dict['train_noise'] = True
DeprecationWarning("train_noise has to be in hyps_mask, set to True")
DeprecationWarning(
"train_noise has to be in hyps_mask, set to True")
if 'nspecie' not in param_dict:
param_dict['nspecie'] = 1

Expand All @@ -117,14 +124,15 @@ def backward(kernels, param_dict):
if k+'_start' not in param_dict:
param_dict[k+'_start'] = start
if 'n'+k not in param_dict:
Parameters.logger.debug("add in hyper parameter separators"\
Parameters.logger.debug("add in hyper parameter separators"
"for", k)
param_dict['n'+k] = 1
start += Parameters.n_kernel_parameters[k]
else:
start += param_dict['n'+k] * Parameters.n_kernel_parameters[k]
start += param_dict['n'+k] * \
Parameters.n_kernel_parameters[k]
else:
Warning("inconsistency between input kernel and kernel list"\
Warning("inconsistency between input kernel and kernel list"
"stored in hyps_mask")

Parameters.logger.debug("Replace kernel array in param_dict")
Expand Down Expand Up @@ -161,12 +169,12 @@ def check_instantiation(hyps, cutoffs, kernels, param_dict):
# and the length of corresponding hyper-parameters
hyps_length = 0
used_parameters = np.zeros_like(hyps, dtype=bool)
for kernel in kernels+['cut3b']:
for kernel in kernels+list(Parameters.cutoff_types.keys()):

n = param_dict.get(f'n{kernel}', 0)
assert isinstance(n, int)

if kernel != 'cut3b':
if kernel not in list(Parameters.cutoff_types.keys()):
hyps_length += Parameters.n_kernel_parameters[kernel]*n
assert n > 0, f"{kernel} has n 0"

Expand Down Expand Up @@ -215,7 +223,7 @@ def check_instantiation(hyps, cutoffs, kernels, param_dict):
assert mask[mask_id] == mask_value, \
f'{kernel}_mask has to be symmetrical'

if kernel != 'cut3b':
if kernel not in list(Parameters.cutoff_types.keys()):
if kernel+'_cutoff_list' in param_dict:
cutoff_list = param_dict[kernel+'_cutoff_list']
assert len(cutoff_list) == n, \
Expand Down Expand Up @@ -322,8 +330,11 @@ def get_component_mask(param_dict, kernel_name, hyps=None):
'n'+kernel_name, kernel_name+'_mask',
kernel_name+'_cutoff_list']

if kernel_name == 'threebody':
name_list += ['ncut3b', 'cut3b_mask']
if kernel_name in Parameters.cutoff_types_values:

key_ind = Parameters.cutoff_types_values.index(kernel_name)
cutoff_key = Parameters.cutoff_types_keys[key_ind]
name_list += ['n'+cutoff_key, cutoff_key+'_mask']

for name in name_list:
if name in param_dict:
Expand Down Expand Up @@ -380,7 +391,7 @@ def get_cutoff(kernel_name, coded_species, param_dict):
specie_mask = param_dict['species_mask']
cutoff_list = param_dict[f'{kernel_name}_cutoff_list']

if kernel_name != 'threebody':
if kernel_name not in Parameters.cutoff_types_values:
mask_id = 0
for ele in coded_specie:
mask_id += specie_mask[ele]
Expand All @@ -389,13 +400,17 @@ def get_cutoff(kernel_name, coded_species, param_dict):
mask_id = param_dict[kernel_name+'_mask'][mask_id]
return cutoff_list[mask_id]
else:
cut3b_mask = param_dict['cut3b_mask']

key_ind = Parameters.cutoff_types_values.index(kernel_name)
cutoff_key = Parameters.cutoff_types_keys[cutoff_key]

cut_mask = param_dict[cutoff_key+'_mask']
ele1 = species_mask[coded_species[0]]
ele2 = species_mask[coded_species[1]]
ele3 = species_mask[coded_species[2]]
twobody1 = cut3b_mask[param_dict['nspecie']*ele1 + ele2]
twobody2 = cut3b_mask[param_dict['nspecie']*ele1 + ele3]
twobody12 = cut3b_mask[param_dict['nspecie']*ele2 + ele3]
twobody1 = cut_mask[param_dict['nspecie']*ele1 + ele2]
twobody2 = cut_mask[param_dict['nspecie']*ele1 + ele3]
twobody12 = cut_mask[param_dict['nspecie']*ele2 + ele3]
return np.array([cutoff_list[twobody1],
cutoff_list[twobody2],
cutoff_list[twobody12]])
Expand Down Expand Up @@ -454,8 +469,9 @@ def compare_dict(dict1, dict2):
list_of_names += [k+'_mask']
list_of_names += ['cutoff_'+k]
list_of_names += [k+'_cutoff_list']
list_of_names += ['ncut3b']
list_of_names += ['cut3b_mask']
for k in Parameters.cutoff_types:
list_of_names += ['n'+k]
list_of_names += [k+'_mask']

for k in list_of_names:
if (k in dict1) != (k in dict2):
Expand Down
Loading

0 comments on commit cd79a0f

Please sign in to comment.