def test_representation_transform(self):

        rep = SphericalExpansion(**self.hypers)

        features = rep.transform(self.frames)

        test = features.get_features(rep)
    def test_representation_transform(self):

        rep = SphericalExpansion(**self.hypers)

        features = rep.transform([self.frame])

        test = features.get_dense_feature_matrix(rep)
예제 #3
0
def benchmark_spherical_representations(frames, optimization_args,
                                        radial_basis):
    hypers = {
        'interaction_cutoff': INTERACTION_CUTOFF,
        'max_radial': 8,
        'max_angular': 6,
        'gaussian_sigma_constant': 0.5,
        'gaussian_sigma_type': "Constant",
        'cutoff_smooth_width': 0.5,
        'radial_basis': radial_basis,
        'optimization_args': optimization_args
    }

    print("Timing SphericalExpansion")
    transform_representation(SphericalExpansion(**hypers),
                             frames,
                             nb_iterations=NB_ITERATIONS_PER_REPRESENTATION)

    hypers = {
        'soap_type': "PowerSpectrum",
        'interaction_cutoff': INTERACTION_CUTOFF,
        'max_radial': 8,
        'max_angular': 6,
        'gaussian_sigma_constant': 0.5,
        'gaussian_sigma_type': "Constant",
        'cutoff_smooth_width': 0.5,
        'normalize': False,
        'radial_basis': radial_basis,
        'optimization_args': optimization_args
    }
    print("Timing SphericalInvariants")
    transform_representation(SphericalInvariants(**hypers),
                             frames,
                             nb_iterations=NB_ITERATIONS_PER_REPRESENTATION)
예제 #4
0
def compute_se_cpp(job):
    # setup input for the script
    data = job.statepoint()
    rep = SphericalExpansion(**data['representation'])
    data['calculator'] = rep.hypers

    tojson(job.fn(group['fn_in']), data)
    # look at memory footprint
    p = Popen([
        group['executable'][data['nl_type']],
        job.fn(group['fn_in']),
        job.fn(group['fn_out'])
    ],
              stdout=PIPE,
              stderr=PIPE)
    max_mem = memory_usage(p, interval=0.1, max_usage=True)
    # look at timings
    p = Popen([
        group['executable'][data['nl_type']],
        job.fn(group['fn_in']),
        job.fn(group['fn_out'])
    ],
              stdout=PIPE,
              stderr=PIPE)
    if p.stderr.read(): print(p.stderr.read())
    data = fromjson(job.fn(group['fn_out']))
    data = data['results']
    data['mem_max'] = max_mem
    data['mem_unit'] = 'MiB'
    tojson(job.fn(group['fn_res']), data)
예제 #5
0
    def test_serialization(self):
        rep = SphericalExpansion(**self.hypers)

        rep_dict = to_dict(rep)

        rep_copy = from_dict(rep_dict)

        rep_copy_dict = to_dict(rep_copy)

        self.assertTrue(rep_dict == rep_copy_dict)
예제 #6
0
    def test_hypers_construction(self):
        """Checks that manually-constructed and automatic
        framework are consistent."""

        hypers = deepcopy(self.hypers)

        hypers["max_radial"] = self.expanded_max_radial
        spex = SphericalExpansion(**hypers)
        feats = spex.transform(self.frames).get_features_by_species(spex)

        cov = get_radial_basis_covariance(spex, feats)

        p_val, p_vec = get_radial_basis_pca(cov)

        p_mat = get_radial_basis_projections(p_vec, self.hypers["max_radial"])

        # now makes this SOAP
        hypers["max_radial"] = self.hypers["max_radial"]
        hypers["soap_type"] = "PowerSpectrum"
        hypers["optimization"] = {
            "RadialDimReduction": {
                "projection_matrices": p_mat
            },
            "Spline": {
                "accuracy": 1e-8
            },
        }

        # compute SOAP
        soap_opt = SphericalInvariants(**hypers)
        soap_feats = soap_opt.transform(self.frames).get_features(soap_opt)

        # now we do the same with the compact utils
        hypers = deepcopy(self.hypers)
        hypers["soap_type"] = "PowerSpectrum"
        hypers = get_optimal_radial_basis_hypers(
            hypers, self.frames, expanded_max_radial=self.expanded_max_radial)
        soap_opt_2 = SphericalInvariants(**hypers)
        soap_feats_2 = soap_opt_2.transform(
            self.frames).get_features(soap_opt_2)

        self.assertTrue(np.allclose(soap_feats, soap_feats_2))
def benchmark_spherical_representations(frames, optimization_args,
                                        radial_basis):
    hypers = {
        "interaction_cutoff": INTERACTION_CUTOFF,
        "max_radial": 8,
        "max_angular": 6,
        "gaussian_sigma_constant": 0.5,
        "gaussian_sigma_type": "Constant",
        "cutoff_smooth_width": 0.5,
        "radial_basis": radial_basis,
        "optimization": optimization_args,
    }

    print("Timing SphericalExpansion")
    transform_representation(
        SphericalExpansion(**hypers),
        frames,
        nb_iterations=NB_ITERATIONS_PER_REPRESENTATION,
    )

    hypers = {
        "soap_type": "PowerSpectrum",
        "interaction_cutoff": INTERACTION_CUTOFF,
        "max_radial": 8,
        "max_angular": 6,
        "gaussian_sigma_constant": 0.5,
        "gaussian_sigma_type": "Constant",
        "cutoff_smooth_width": 0.5,
        "normalize": False,
        "radial_basis": radial_basis,
        "optimization": optimization_args,
    }
    print("Timing SphericalInvariants")
    transform_representation(
        SphericalInvariants(**hypers),
        frames,
        nb_iterations=NB_ITERATIONS_PER_REPRESENTATION,
    )
예제 #8
0
def compute_ri_cpp(job):
    # setup input for the script
    data = job.statepoint()
    rep = SphericalExpansion(**data['representation'])
    data['calculator'] = rep.hypers

    tojson(job.fn(group['fn_in']), data)
    # look at memory footprint
    p = Popen(
        [group['executable'],
         job.fn(group['fn_in']),
         job.fn(group['fn_out'])],
        stdout=PIPE,
        stderr=PIPE)
    max_mem = memory_usage(p, interval=0.001, max_usage=True)
    # look at timings
    carry_on = True
    N_ITERATIONS = [
        int(data['N_ITERATIONS'] * (1 + f))
        for f in [0., 0.5, 1, 2, 3, 5, 10, 20, 30, 50, 100]
    ]
    for N_ITERATION in N_ITERATIONS:
        if run(job, N_ITERATION, max_mem):
            break
예제 #9
0
    def test_radial_dimension_reduction_test(self):
        rep = SphericalExpansion(**self.hypers)
        features_ref = rep.transform(self.frames).get_features(rep)
        K_ref = features_ref.dot(features_ref.T)

        # identity test
        hypers = deepcopy(self.hypers)
        projection_matrices = [
            np.eye(self.max_radial).tolist()
            for _ in range(self.max_angular + 1)
        ]

        hypers["optimization"] = {
            "Spline": {
                "accuracy": 1e-8
            },
            "RadialDimReduction": {
                "projection_matrices":
                {sp: projection_matrices
                 for sp in self.species}
            },
        }
        rep = SphericalExpansion(**hypers)
        features_test = rep.transform(self.frames).get_features(rep)
        self.assertTrue(np.allclose(features_ref, features_test))

        # random orthogonal matrix test,
        # for angular_l=0 we can do the projection on the python side
        hypers = deepcopy(self.hypers)
        np.random.seed(self.seed)
        projection_matrices = {
            sp: [
                ortho_group.rvs(self.max_radial)[:self.max_radial -
                                                 1, :].tolist()
                for _ in range(self.max_angular + 1)
            ]
            for sp in self.species
        }

        hypers["max_radial"] = self.max_radial - 1
        hypers["optimization"] = {
            "Spline": {
                "accuracy": 1e-8
            },
            "RadialDimReduction": {
                "projection_matrices": projection_matrices
            },
        }
        rep = SphericalExpansion(**hypers)
        features_test = rep.transform(self.frames).get_features(rep)
        features_test = features_test.reshape(
            len(features_test),
            len(self.species),
            self.max_radial - 1,
            (self.max_angular + 1)**2,
        )

        hypers["max_radial"] = self.max_radial
        hypers["optimization"] = {
            "Spline": {
                "accuracy": 1e-8
            },
        }
        rep = SphericalExpansion(**hypers)
        features_ref = rep.transform(self.frames).get_features(rep)
        features_ref = features_ref.reshape(
            len(features_ref),
            len(self.species),
            self.max_radial,
            (self.max_angular + 1)**2,
        )

        for a, species in enumerate(self.species):
            self.assertTrue(
                np.allclose(
                    features_ref[:, a, :, 0] @ np.array(
                        projection_matrices[species][0]).T,
                    features_test[:, a, :, 0],
                ))

        # checks if error is raised if wrong number of species is given
        with self.assertRaises(RuntimeError):
            species = [1]
            hypers["optimization"] = {
                "Spline": {
                    "accuracy": 1e-8
                },
                "RadialDimReduction": {
                    "projection_matrices":
                    {sp: projection_matrices
                     for sp in species}
                },
            }
            rep = SphericalExpansion(**hypers)
            features_test = rep.transform(self.frames).get_features(rep)
예제 #10
0
 def test_pickle(self):
     rep = SphericalExpansion(**self.hypers)
     serialized = pickle.dumps(rep)
     rep_ = pickle.loads(serialized)
     self.assertTrue(to_dict(rep) == to_dict(rep_))
def dump_reference_json():
    import ubjson
    from copy import copy
    from itertools import product

    sys.path.insert(0, os.path.join(root, 'build/'))
    sys.path.insert(0, os.path.join(root, 'tests/'))

    cutoffs = [2, 3]
    gaussian_sigmas = [0.2, 0.5]
    max_radials = [4, 10]
    max_angulars = [3, 6]
    cutoff_smooth_widths = [0., 1.]
    radial_basis = ["GTO", "DVR"]
    cutoff_function_types = ['ShiftedCosine', 'RadialScaling']
    fns = [
        os.path.join(inputs_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
        os.path.join(inputs_path, "small_molecule.json")
    ]
    fns_to_write = [
        os.path.join(dump_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
        os.path.join(dump_path, "small_molecule.json"),
    ]

    data = dict(filenames=fns_to_write,
                cutoffs=cutoffs,
                gaussian_sigmas=gaussian_sigmas,
                max_radials=max_radials,
                rep_info=[])

    for fn in fns:
        for cutoff in cutoffs:
            data['rep_info'].append([])
            for (gaussian_sigma, max_radial, max_angular, cutoff_smooth_width,
                 rad_basis, cutoff_function_type) in product(
                     gaussian_sigmas, max_radials, max_angulars,
                     cutoff_smooth_widths, radial_basis,
                     cutoff_function_types):
                frames = read(fn)
                if cutoff_function_type == 'RadialScaling':
                    cutoff_function_parameters = dict(rate=1,
                                                      scale=cutoff * 0.5,
                                                      exponent=3)
                else:
                    cutoff_function_parameters = dict()

                hypers = {
                    "interaction_cutoff": cutoff,
                    "cutoff_smooth_width": cutoff_smooth_width,
                    "max_radial": max_radial,
                    "max_angular": max_angular,
                    "gaussian_sigma_type": "Constant",
                    "cutoff_function_type": cutoff_function_type,
                    'cutoff_function_parameters': cutoff_function_parameters,
                    "gaussian_sigma_constant": gaussian_sigma,
                    "radial_basis": rad_basis
                }

                sph_expn = SphericalExpansion(**hypers)
                expansions = sph_expn.transform(frames)
                x = expansions.get_features(sph_expn)
                x[np.abs(x) < 1e-300] = 0.
                data['rep_info'][-1].append(
                    dict(feature_matrix=x.tolist(),
                         hypers=copy(sph_expn.hypers)))

    with open(
            os.path.join(root, dump_path,
                         "spherical_expansion_reference.ubjson"), 'wb') as f:
        ubjson.dump(data, f)
def get_soap_vectors(hypers, frames):
    with ostream_redirect():
        sph_expn = SphericalExpansion(**hypers)
        expansions = sph_expn.transform(frames)
        soap_vectors = expansions.get_features(sph_expn)
    return soap_vectors
예제 #13
0
def dump_reference_json():
    import ubjson
    import os
    from copy import copy
    path = '../'
    sys.path.insert(0, os.path.join(path, 'build/'))
    sys.path.insert(0, os.path.join(path, 'tests/'))

    cutoffs = [2, 3]
    gaussian_sigmas = [0.2, 0.5]
    max_radials = [4, 10]
    max_angulars = [3, 6]
    cutoff_smooth_widths = [0., 1.]
    radial_basis = ["GTO"]

    fns = [
        os.path.join(
            path, "tests/reference_data/CaCrP2O7_mvc-11955_symmetrized.json"),
        os.path.join(path, "tests/reference_data/small_molecule.json")
    ]
    fns_to_write = [
        "reference_data/CaCrP2O7_mvc-11955_symmetrized.json",
        "reference_data/small_molecule.json",
    ]

    data = dict(filenames=fns_to_write,
                cutoffs=cutoffs,
                gaussian_sigmas=gaussian_sigmas,
                max_radials=max_radials,
                rep_info=[])

    # An example of the gruesomeness of using 4 spaces for one tab
    for fn in fns:
        for cutoff in cutoffs:
            data['rep_info'].append([])
            for gaussian_sigma in gaussian_sigmas:
                for max_radial in max_radials:
                    for max_angular in max_angulars:
                        for cutoff_smooth_width in cutoff_smooth_widths:
                            for rad_basis in radial_basis:
                                frames = [json2ase(load_json(fn))]
                                hypers = {
                                    "interaction_cutoff": cutoff,
                                    "cutoff_smooth_width": cutoff_smooth_width,
                                    "max_radial": max_radial,
                                    "max_angular": max_angular,
                                    "gaussian_sigma_type": "Constant",
                                    "cutoff_function_type": "Cosine",
                                    "gaussian_sigma_constant": gaussian_sigma,
                                    "radial_basis": rad_basis
                                }
                                # x = get_soap_vectors(hypers, frames)
                                sph_expn = SphericalExpansion(**hypers)
                                expansions = sph_expn.transform(frames)
                                x = expansions.get_feature_matrix()
                                data['rep_info'][-1].append(
                                    dict(feature_matrix=x.tolist(),
                                         hypers=copy(sph_expn.hypers)))

    with open(
            path + "tests/reference_data/spherical_expansion_reference.ubjson",
            'wb') as f:
        ubjson.dump(data, f)
def dump_reference_json():
    import ubjson
    from copy import copy
    from itertools import product

    sys.path.insert(0, os.path.join(root, "build/"))
    sys.path.insert(0, os.path.join(root, "tests/"))

    cutoffs = [2, 3]
    gaussian_sigmas = [0.2, 0.5]
    max_radials = [4]
    max_angulars = [3]
    cutoff_smooth_widths = [1.0]
    radial_basis = ["GTO", "DVR"]
    cutoff_function_types = ["ShiftedCosine", "RadialScaling"]
    fns = [
        os.path.join(inputs_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
        os.path.join(inputs_path, "small_molecule.json"),
    ]
    fns_to_write = [
        os.path.join(read_inputs_path, "CaCrP2O7_mvc-11955_symmetrized.json"),
        os.path.join(read_inputs_path, "small_molecule.json"),
    ]
    optimization_args = [
        {
            "type": "None",
            "accuracy": 1e-8
        },
        {
            "type": "Spline",
            "accuracy": 1e-8
        },
    ]
    data = dict(
        filenames=fns_to_write,
        cutoffs=cutoffs,
        gaussian_sigmas=gaussian_sigmas,
        max_radials=max_radials,
        rep_info=[],
    )

    for fn in fns:
        for cutoff in cutoffs:
            data["rep_info"].append([])
            for (
                    gaussian_sigma,
                    max_radial,
                    max_angular,
                    cutoff_smooth_width,
                    rad_basis,
                    cutoff_function_type,
                    opt_args,
            ) in product(
                    gaussian_sigmas,
                    max_radials,
                    max_angulars,
                    cutoff_smooth_widths,
                    radial_basis,
                    cutoff_function_types,
                    optimization_args,
            ):
                frames = read(fn)
                if cutoff_function_type == "RadialScaling":
                    cutoff_function_parameters = dict(rate=1,
                                                      scale=cutoff * 0.5,
                                                      exponent=3)
                else:
                    cutoff_function_parameters = dict()

                hypers = {
                    "interaction_cutoff": cutoff,
                    "cutoff_smooth_width": cutoff_smooth_width,
                    "max_radial": max_radial,
                    "max_angular": max_angular,
                    "gaussian_sigma_type": "Constant",
                    "cutoff_function_type": cutoff_function_type,
                    "cutoff_function_parameters": cutoff_function_parameters,
                    "gaussian_sigma_constant": gaussian_sigma,
                    "radial_basis": rad_basis,
                    "optimization_args": opt_args,
                }

                sph_expn = SphericalExpansion(**hypers)
                expansions = sph_expn.transform(frames)
                x = expansions.get_features(sph_expn)
                x[np.abs(x) < 1e-300] = 0.0
                data["rep_info"][-1].append(
                    dict(feature_matrix=x.tolist(),
                         hypers=copy(sph_expn.hypers)))

    with open(
            os.path.join(root, dump_path,
                         "spherical_expansion_reference.ubjson"),
            "wb",
    ) as f:
        ubjson.dump(data, f)