Пример #1
0
def test_molecular_kernel():
    molecules = [molecule('H2'), molecule('O2'), molecule('CH4')]

    graphs = [Graph.from_ase(m) for m in molecules]

    kernel = Tang2019MolecularKernel(starting_probability='uniform')

    R = kernel(graphs)
    D = np.diag(np.diag(R)**-0.5)
    K = D.dot(R).dot(D)

    assert (R.shape == (3, 3))
    for i in range(len(molecules)):
        assert (K[i, i] == pytest.approx(1, 1e-6))

    R_nodal = kernel(graphs, nodal=True)
    D_nodal = np.diag(np.diag(R_nodal)**-0.5)
    K_nodal = D_nodal.dot(R_nodal).dot(D_nodal)

    natoms = np.sum([len(m) for m in molecules])
    assert (R_nodal.shape == (natoms, natoms))
    for i in range(natoms):
        assert (K_nodal[i, i] == pytest.approx(1, 1e-6))

    kernel_nocarbon = Tang2019MolecularKernel(
        starting_probability=lambda n: 0.0 if n[1]['element'] == 6 else 1.0)

    R_nocarbon_nodal = kernel_nocarbon(graphs, nodal=True)
    k = 0
    for i, m in enumerate(molecules):
        for j, a in enumerate(m):
            if a.symbol == 'C':
                assert (R_nocarbon_nodal[k, :].sum() == 0)
                assert (R_nocarbon_nodal[:, k].sum() == 0)
            k += 1
Пример #2
0
def test_mlgk_on_permuted_graph():
    g = Graph.from_ase(molecule('C6H6'))
    for _ in range(10):
        h = g.permute(np.random.permutation(len(g.nodes)))
        kernel = MarginalizedGraphKernel(
            TensorProduct(element=KroneckerDelta(0.5)),
            TensorProduct(length=SquareExponential(0.1)))
        assert (kernel([g], [h]).item() == pytest.approx(kernel([g]).item()))
Пример #3
0
def test_molecular_kernel_custom_pstart():
    molecules = [molecule('H2'), molecule('O2'), molecule('CH4')]

    graphs = [Graph.from_ase(m) for m in molecules]

    kernel_nocarbon = Tang2019MolecularKernel(
        starting_probability=(lambda ns: np.where(ns.element == 6, 0.0, 1.0),
                              'n.element == 6 ? 0.f : 1.f'))

    R_nocarbon_nodal = kernel_nocarbon(graphs, nodal=True)
    k = 0
    for i, m in enumerate(molecules):
        for j, a in enumerate(m):
            if a.symbol == 'C':
                assert (R_nocarbon_nodal[k, :].sum() == 0)
                assert (R_nocarbon_nodal[:, k].sum() == 0)
            k += 1
Пример #4
0
def test_molecular_kernel():
    molecules = [molecule('H2'), molecule('O2'), molecule('CH4')]

    graphs = [Graph.from_ase(m) for m in molecules]

    kernel = Tang2019MolecularKernel()

    R = kernel(graphs)
    D = np.diag(np.diag(R)**-0.5)
    K = D.dot(R).dot(D)

    assert (R.shape == (3, 3))
    for i in range(len(molecules)):
        assert (K[i, i] == pytest.approx(1, 1e-6))

    R_nodal = kernel(graphs, nodal=True)
    D_nodal = np.diag(np.diag(R_nodal)**-0.5)
    K_nodal = D_nodal.dot(R_nodal).dot(D_nodal)

    natoms = np.sum([len(m) for m in molecules])
    assert (R_nodal.shape == (natoms, natoms))
    for i in range(natoms):
        assert (K_nodal[i, i] == pytest.approx(1, 1e-6))
Пример #5
0
import numpy as np
import pandas as pd
from ase.build import molecule, bulk
from graphdot import Graph
from graphdot.kernel.molecular import Tang2019MolecularKernel

# build sample molecules
small_title = ['H2O', 'HCl', 'NaCl']
bulk_title = ['NaCl-bulk', 'NaCl-bulk2']
bulk = [
    bulk('NaCl', 'rocksalt', a=5.64),
    bulk('NaCl', 'rocksalt', a=5.66),
]
molecules = [molecule(name) for name in small_title] + bulk

# convert to molecular graphs
graphs = [Graph.from_ase(m) for m in molecules]

# use pre-defined molecular kernel
kernel = Tang2019MolecularKernel(edge_length_scale=0.1)

R = kernel(graphs)

# normalize the similarity matrix
d = np.diag(R)**-0.5
K = np.diag(d).dot(R).dot(np.diag(d))

# note the difference between the NaCl variants
title = small_title + bulk_title
print(pd.DataFrame(K, columns=title, index=title))
Пример #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import pytest
from ase.build import molecule
from graphdot import Graph
from graphdot.metric.maximin import MaxiMin
from graphdot.microkernel import (
    KroneckerDelta,
    SquareExponential,
    TensorProduct,
)
from graphdot.kernel.marginalized.starting_probability import Uniform

G = [Graph.from_ase(molecule(f)) for f in ['CH3SCH3', 'CH3OCH3']]
H = [Graph.from_ase(molecule(f)) for f in ['CH4', 'NH3', 'H2O']]


def test_maximin_basic():
    metric = MaxiMin(node_kernel=TensorProduct(element=KroneckerDelta(0.5)),
                     edge_kernel=TensorProduct(length=SquareExponential(0.1)),
                     q=0.01)
    distance = metric(G)
    assert distance.shape == (len(G), len(G))
    assert np.allclose(distance.diagonal(), 0, atol=1e-3)
    assert np.all(distance >= 0)
    assert np.allclose(distance, distance.T, rtol=1e-14, atol=1e-14)

    distance = metric(G, G)
    assert distance.shape == (len(G), len(G))
    assert np.allclose(distance.diagonal(), 0, atol=1e-3)