def setUp_Options(self):
     # CONSTRAINTS, EXCLUSIONS
     exclude_center_pids = []
     # EXPANSIONS
     options = soap.Options()
     options.excludeCenters([])
     options.excludeTargets([])
     options.excludeCenterIds(exclude_center_pids)
     options.excludeTargetIds([])
     options.set('radialbasis.type', 'gaussian')
     options.set('radialbasis.mode', 'adaptive')
     options.set('radialbasis.N', 9)  # 9
     options.set('radialbasis.sigma', 0.5)  # 0.9
     options.set('radialbasis.integration_steps', 15)
     options.set('radialcutoff.Rc', 6.8)
     options.set('radialcutoff.Rc_width', 0.5)
     options.set('radialcutoff.type', 'heaviside')
     options.set('radialcutoff.center_weight', 0.5)
     options.set('angularbasis.type', 'spherical-harmonic')
     options.set('angularbasis.L', 6)  # 6
     options.set('densitygrid.N', 20)
     options.set('densitygrid.dx', 0.15)
     options.set('spectrum.gradients', True)
     options.set('spectrum.2l1_norm', False)  # <- pull here
     # KERNEL OPTIONS
     options.set('kernel.adaptor', 'global-generic')  # <- pull here
     options.set('kernel.type', 'dot')
     options.set('kernel.delta', 1.)
     options.set('kernel.xi', 4.)  # <- pull here
     # STORE
     self.options = options
     return
Ejemplo n.º 2
0
def compute_ftd(struct, options, fragment_based):
    for atom in struct:
        atom.sigma = options["fieldtensor.sigma"]
        log << atom.name << atom.type << atom.weight << atom.sigma << atom.pos << log.endl
    # OPTIONS
    options_soap = soap.Options()
    for key, val in options.items():
        if type(val) == list: continue
        options_soap.set(key, val)
    # Exclusions
    excl_targ_list = options['exclude_targets']
    excl_cent_list = options['exclude_centers']
    excl_targ_list.append('COM')
    if not fragment_based:
        excl_cent_list.append('COM')
    options_soap.excludeCenters(excl_cent_list)
    options_soap.excludeTargets(excl_targ_list)
    # SPECTRUM
    ftspectrum = soap.FTSpectrum(struct, options_soap)
    ftspectrum.compute()
    # Adapt spectra
    adaptor = soap.soapy.kernel.KernelAdaptorFactory["ftd-specific"](
        {}, options["type_list"])
    IX, IR, types = adaptor.adapt(ftspectrum, return_pos_matrix=True)
    return IX, IR, types
Ejemplo n.º 3
0
def compute_soap(struct, options):
    # OPTIONS
    options_soap = soap.Options()
    for key, val in options.items():
        if isinstance(val, list):
            continue
        options_soap.set(key, val)
    # Exclusions
    excl_targ_list = options['exclude_targets']
    excl_cent_list = options['exclude_centers']
    options_soap.excludeCenters(excl_cent_list)
    options_soap.excludeTargets(excl_targ_list)
    # Compute spectrum
    spectrum = soap.Spectrum(struct, options_soap)
    spectrum.compute()
    spectrum.computePower()
    if options['spectrum.gradients']:
        spectrum.computePowerGradients()
    if options['spectrum.global']:
        spectrum.computeGlobal()
    # Adapt spectrum
    adaptor = soap.soapy.kernel.KernelAdaptorFactory[options['kernel.adaptor']](
        options_soap,
        types_global=options['type_list'])
    IX, IR, types = adaptor.adapt(spectrum, return_pos_matrix=True)
    # Calculate average
    X_avg = IX.sum(axis=0)
    X_avg = X_avg / X_avg.dot(X_avg)**0.5
    return IX, X_avg, IR, types
Ejemplo n.º 4
0
def compute_spectrum(config, options_dict, exclude_centers, exclude_targets):
    # ANNOUNCE
    global MP_LOCK
    MP_LOCK.acquire()
    osio << osio.item \
         << "Soap for:  " << config.config_file \
         << "PID=%d" % mp.current_process().pid << endl
    MP_LOCK.release()
    # SET OPTIONS
    options = soap.Options()
    for item in options_dict.iteritems():
        options.set(*item)
    options.excludeCenters(exclude_centers)
    options.excludeTargets(exclude_targets)
    # PROCESS STRUCTURE
    structure = soap.tools.setup_structure_ase(config.config_file,
                                               config.atoms)
    for particle in structure:
        particle.sigma = 0.5 * element_vdw_radius[particle.type]
        particle.weight = element_valence_order[particle.type]
    # COMPUTE SPECTRUM
    spectrum = soap.Spectrum(structure, options)
    spectrum.compute()
    spectrum.computePower()
    spectrum.save('%s.spectrum' % config.config_file)
    return config.config_file
Ejemplo n.º 5
0
def get_cxx_kernel(options):
    options_cxx = soap.Options()
    log << "Kernel:" << log.endl
    for k, v in options.iteritems():
        log << "- Setting %s=%s" % (k, str(v)) << log.endl
        options_cxx.set(k, v)
    kernel = soap.Kernel(options_cxx)
    return kernel
Ejemplo n.º 6
0
 def getCxxOptions(self, options):
     options_cxx = soap.Options()
     for key, val in options.items():
         if type(val) == list: continue
         options_cxx.set(key, val)
     # Exclusions
     excl_targ_list = options['exclude_targets']
     excl_cent_list = options['exclude_centers']
     options_cxx.excludeCenters(excl_cent_list)
     options_cxx.excludeTargets(excl_targ_list)
     return options_cxx
Ejemplo n.º 7
0
def convert_json_to_cxx(options):
    options_cxx = soap.Options()
    for key, val in options.items():
        if type(val) == list: continue
        options_cxx.set(key, val)
    # Exclusions
    excl_targ_list = options['exclude_targets']
    excl_cent_list = options['exclude_centers']
    options_cxx.excludeCenters(excl_cent_list)
    options_cxx.excludeTargets(excl_targ_list)
    excl_targ_id_list = options['exclude_target_ids']
    excl_cent_id_list = options['exclude_center_ids']
    options_cxx.excludeCenterIds(excl_cent_id_list)
    options_cxx.excludeTargetIds(excl_targ_id_list)
    return options_cxx
Ejemplo n.º 8
0
def test_reduction_matrix_average(options):
    log << log.mg << "<test_reduction_matrix_average>" << log.flush
    kernel_options = soap.Options()
    kernel_options.set("basekernel_type", "dot")
    kernel_options.set("base_exponent", 3.)
    kernel_options.set("base_filter", False)
    kernel_options.set("topkernel_type", "average")
    kernel = soap.Kernel(kernel_options)
    for i in range(9):
        K_base = np.random.uniform(size=(i + 1, ((i + 2) % 4) + 1))
        k = kernel.evaluateTop(K_base, "float64")
        P = kernel.attributeTopGetReductionMatrix(K_base, "float64")
        k_red = np.sum(K_base * P)
        assert_equal(k - k_red, 0.0, 1e-6)
    log << log.endl
Ejemplo n.º 9
0
def test_attribution_average(dset, options):
    log << log.mg << "<test_attribution_average>" << log.flush
    kernel_options = soap.Options()
    kernel_options.set("basekernel_type", "dot")
    kernel_options.set("base_exponent", 3.)
    kernel_options.set("base_filter", False)
    kernel_options.set("topkernel_type", "average")
    kernel = soap.Kernel(kernel_options)
    K = kernel.evaluate(dset, dset, False, "float64")
    for i in range(len(dset)):
        KK = kernel.attributeLeft(dset[i], dset, "float64")
        KK_sum = np.sum(KK, axis=0)
        for j in range(len(dset)):
            assert_equal(KK_sum[j] - K[i, j], 0.0, 1e-6)
    log << log.endl
Ejemplo n.º 10
0
def compute_soap(struct, options, fragment_based=False):
    # OPTIONS
    options_soap = soap.Options()
    for key, val in options.items():
        if type(val) == list: continue
        options_soap.set(key, val)
    # Exclusions
    excl_targ_list = options['exclude_targets']
    excl_cent_list = options['exclude_centers']
    excl_targ_list.append('COM')
    if not fragment_based:
        excl_cent_list.append('COM')
    options_soap.excludeCenters(excl_cent_list)
    options_soap.excludeTargets(excl_targ_list)
    # SPECTRUM
    spectrum = soap.Spectrum(struct, options_soap)
    # Compute density expansion
    if fragment_based:
        # Divide onto segments and their COM representatives
        seg_by_name = {}
        comseg_by_name = {}
        for seg in struct.segments:
            if seg.name.split('.')[-1] == 'COM':
                comseg_by_name[seg.name] = seg
            else:
                seg_by_name[seg.name] = seg
        # Compute segment descriptors
        for name, seg in seg_by_name.items():
            comseg = comseg_by_name[name + '.COM']
            spectrum.compute(comseg, seg)
    else:
        spectrum.compute()
    # Compute power spectrum,
    spectrum.computePower()
    if options['spectrum.gradients']:
        spectrum.computePowerGradients()
    if options['spectrum.global']:
        spectrum.computeGlobal()
    # Adapt spectrum
    adaptor = soap.soapy.kernel.KernelAdaptorFactory[
        options['kernel.adaptor']](options_soap,
                                   types_global=options['type_list'])
    IX, IR, types = adaptor.adapt(spectrum, return_pos_matrix=True)
    return IX, IR, types
Ejemplo n.º 11
0
def optimize(cgraph,
             X,
             Y,
             opt=None,
             dropout=None,
             method="steep",
             do_resample=False,
             n_batches=1,
             n_steps_batch=100,
             rate=0.001,
             node_map=None):
    # OPTIMIZER
    if opt is None:
        options = soap.Options()
        options.set("opt.type", method)
        opt = soap.CGraphOptimizer(options)
    # FIT
    t0 = time.time()
    for i in range(n_batches):
        log << "Batch %3d  " % i << log.flush
        if do_resample:
            subsel = np.random.randint(0, X.shape[0], size=(X.shape[0], ))
        else:
            subsel = np.arange(X.shape[0])
        if dropout is not None: dropout.sample()
        # >>> assert len(cgraph.outputs()) == 1
        # >>> nodes = cgraph.outputs()[0].inputs()
        # >>> print len(nodes), len(filter(lambda n: n.active, nodes))
        opt.step(cgraph, X[subsel], Y[subsel], str(X.dtype), str(Y.dtype),
                 n_steps_batch, rate)
        # >>> if node_map is not None:
        # >>>     for k,n in node_map.iteritems():
        # >>>         if n.active:
        # >>>             nans = len(np.where(np.isnan(n.vals("float64")))[0])
        # >>>             if nans > 0:
        # >>>                 print n.vals("float64")
        # >>>                 print "%50s %15s" % (k, n.op), nans
    t1 = time.time()
    log << "Time taken for optimization: %+1.4fs" % (t1 - t0) << log.endl
Ejemplo n.º 12
0
#! /usr/bin/env python
import soap
import soap.tools

import os
import numpy as np
import logging
from momo import osio, endl, flush

options = soap.Options()
options.excludeCenters([])
options.excludeTargets([])
options.set('radialbasis.type', 'gaussian')
options.set('radialbasis.mode', 'adaptive')
options.set('radialbasis.N', 9)  # 9
options.set('radialbasis.sigma', 0.5)  # 0.9
options.set('radialbasis.integration_steps', 15)
options.set('radialcutoff.Rc', 6.8)
options.set('radialcutoff.Rc_width', 0.5)
options.set('radialcutoff.type', 'heaviside')
options.set('radialcutoff.center_weight', 1.)
options.set('angularbasis.type', 'spherical-harmonic')
options.set('angularbasis.L', 6)  # 6
options.set('densitygrid.N', 20)
options.set('densitygrid.dx', 0.15)
options.set('spectrum.gradients', True)
options.set('kernel.adaptor', 'generic')
options.set('kernel.type', 'dot')
options.set('kernel.delta', 0.1)
options.set('kernel.xi', 1.)
Ejemplo n.º 13
0
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
select_struct_label = 'config.xyz'
exclude_centers = []
adaptor_type = 'global-generic'
kernel_type = 'dot'  # 'dot-harmonic'
alpha = +1.
mu = 0.9
sc = 0.1  # 1.5 # 0.1
average_energy = True
lj_sigma = 0.02  # 0.00001 # 0.02
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# NOTE adaptor_type = 'generic' => exclude center if not constrained in optimization

# OPTIONS
options = soap.Options()
options.excludeCenters(['H'])
options.excludeTargets(['H'])
options.excludeCenterIds(exclude_centers)
options.excludeTargetIds([])
# Spectrum
options.set('spectrum.gradients', True)
options.set('spectrum.2l1_norm', False)  # <- pull here (True/False)
# Basis
options.set('radialbasis.type', 'gaussian')
options.set('radialbasis.mode', 'adaptive')
options.set('radialbasis.N', 9)
options.set('radialbasis.sigma', 0.5)
options.set('radialbasis.integration_steps', 15)
options.set('radialcutoff.Rc', 6.8)
options.set('radialcutoff.Rc_width', 0.5)
Ejemplo n.º 14
0
 def setupVertexFeatures(self, atoms, options):
     n_atoms = len(atoms)
     positions = [atom.position for atom in atoms]
     descriptor_type = options['graph']['descriptor']
     options_descriptor = options['descriptor'][descriptor_type]
     if descriptor_type == 'atom_type':
         feature_map = {}
         feature_list = options_descriptor['type_map']
         dim = len(feature_list)
         P = np.zeros((n_atoms, dim))
         for idx, atom in enumerate(atoms):
             p = np.zeros((dim))
             atom_type = atom.number
             for i in range(dim):
                 if feature_list[i] == atom_type: p[i] = 1
                 else: p[i] = 0
             P[idx, :] = p
     elif descriptor_type == 'soap':
         # Structure
         structure = soap.tools.setup_structure_ase(self.label, atoms)
         # Customize density
         density_type = options["atomic_density"]["density_type"]
         use_covrad = options["atomic_density"]["use_covrad"]
         atom_rad_scale = float(options["atomic_density"]["atomic_radius"])
         # ... Particle radii
         if use_covrad:
             radius_map = soap.soapy.elements.periodic_table.getPropertyDict(
                 "covrad", convert=lambda x: x * atom_rad_scale)
         else:
             radius_map = {}
             for elem in soap.soapy.elements.PeriodicTable.element_names:
                 radius_map[elem] = atom_rad_scale
         # ... Particle 'charge'
         if density_type == "elneg_density":
             charge_map = soap.soapy.elements.periodic_table.getPropertyDict(
                 "elneg")
         elif density_type == "z_density":
             charge_map = soap.soapy.elements.periodic_table.getPropertyDict(
                 "z")
         elif density_type == "number_density":
             charge_map = {}
             for elem in soap.soapy.elements.PeriodicTable.element_names:
                 charge_map[elem] = 1.
         elif density_type == "number_density_generic":
             charge_map = {}
             for elem in soap.soapy.elements.PeriodicTable.element_names:
                 charge_map[elem] = 1.
         elif density_type == "valence_charge_density":
             charge_map = soap.soapy.elements.periodic_table.getPropertyDict(
                 "valence")
         else:
             raise NotImplementedError("Density type '%s'" % density_type)
         # ... Apply
         soap.tools.setup_structure_density(structure,
                                            radius_map=radius_map,
                                            charge_map=charge_map)
         # Options
         options_soap = soap.Options()
         for item in options_descriptor.items():
             key = item[0]
             val = item[1]
             if type(val) == list:
                 continue  # TODO Exclusions loaded separately, fix this
             options_soap.set(key, val)
         options_soap.excludeCenters(options_descriptor['exclude_centers'])
         options_soap.excludeTargets(options_descriptor['exclude_targets'])
         # Spectrum
         spectrum = soap.Spectrum(structure, options_soap)
         spectrum.compute()
         spectrum.computePower()
         if options_descriptor['spectrum.gradients']:
             spectrum.computePowerGradients()
         spectrum.computeGlobal()
         # Adapt spectrum
         adaptor = kern.KernelAdaptorFactory[options_soap.get(
             'kernel.adaptor')](
                 options_soap, types_global=options_descriptor['type_list'])
         ix = adaptor.adapt(spectrum)
         dim = ix.shape[1]
         P = ix
         if options_soap.get('kernel.adaptor') in [
                 'global-generic', 'global-specific'
         ]:
             pass
         else:
             assert P.shape[0] == n_atoms
     elif descriptor_type == 'soap-quippy':
         atoms_quippy = datasets.gdb.convert_ase2quippy_atomslist([atoms
                                                                   ])[0]
         # Read options
         options_xml_file = options_descriptor["options_xml"]
         opt_interface = momo.OptionsInterface()
         xml_options = opt_interface.ParseOptionsFile(
             options_xml_file, 'options')
         # Finalize options
         xml_options.kernel.alchemy = xml_options.kernel.alchemy.As(str)
         xml_options.kernel.alchemy_rules = xml_options.kernel.alchemy
         xml_options.soap.nocenter = xml_options.soap.nocenter.As(str)
         xml_options.soap.noatom = []  # TODO
         if xml_options.soap.nocenter and xml_options.soap.nocenter != 'None':
             xml_options.soap.nocenter = map(
                 int, xml_options.soap.nocenter.split())
         else:
             xml_options.soap.nocenter = []
         datasets.soap.finalize_options([], xml_options)
         # Process
         z_types = options_descriptor["z_type_list"]
         struct = libmatch.structures.structure(xml_options.kernel.alchemy)
         soap_raw = struct.parse(atoms_quippy,
                                 xml_options.soap.R.As(float),
                                 xml_options.soap.N.As(int),
                                 xml_options.soap.L.As(int),
                                 xml_options.soap.sigma.As(float),
                                 xml_options.soap.w0.As(float),
                                 xml_options.soap.nocenter,
                                 xml_options.soap.noatom,
                                 types=z_types,
                                 kit=xml_options.kernel.kit)
         # Assign raw soaps to atoms (currently stored by z-key)
         z_idx_counter = {}
         for z in soap_raw:
             #print z, soap_raw[z].shape
             z_idx_counter[z] = 0
         ix = []
         for i, z in enumerate(atoms.get_atomic_numbers()):
             z_idx = z_idx_counter[z]
             ix.append(soap_raw[z][z_idx])
             z_idx_counter[z] += 1
             #print z, z_idx
         P = np.array(ix)
         dim = P.shape[1]
         assert P.shape[0] == n_atoms
         #print P.dot(P.T)
     elif descriptor_type == 'npy_load':
         folder = options_descriptor["folder"]
         npy_file = '%s/%s.x.npy' % (folder, self.label)
         #print npy_file
         P = np.load(npy_file)
         dim = P.shape[1]
         assert P.shape[0] == n_atoms
     elif descriptor_type == 'none':
         dim = 1
         P = np.zeros((n_atoms, dim))
         for idx, atom in enumerate(atoms):
             P[idx, 0] = 1.
     else:
         raise NotImplementedError(descriptor_type)
     return P, positions
Ejemplo n.º 15
0
        z = 1. / np.sqrt(K.diagonal())
        K = K * np.outer(z, z)
        log << "Kernel: Output slot" << slot << log.endl
        log << K << log.endl


if __name__ == "__main__":
    configs = soap.tools.io.read('data/structures.xyz')

    # Compute SOAP descriptors
    options = soap.soapy.configure_default()
    dset = evaluate_soap(configs, options)

    # Multikernel: Variant A
    log << log.mg << "Variant A" << log.endl
    kernel_options = soap.Options()
    kernel_options.set("basekernel_type", "dot")
    kernel_options.set("base_exponent", 3.)
    kernel_options.set("base_filter", False)
    kernel_options.set("topkernel_type", "average;canonical;rematch")
    kernel_options.set("canonical_beta", 0.5)
    kernel_options.set("rematch_gamma", 0.05)
    kernel_options.set("rematch_eps", 1e-6)
    kernel_options.set("rematch_omega", 1.0)
    evaluate_kernel(dset, options=kernel_options)

    # Multikernel: Variant B
    log << log.mg << "Variant B" << log.endl
    kernel_options.set("topkernel_type", "")
    kernel = soap.Kernel(kernel_options)
    for beta in [0.01, 0.05, 0.1, 0.5, 1.0, 10.0, 100.0]: