def test_cpd_prior():
    from coherent_point_drift.align import driftRigid
    rng = np.random.RandomState(4)

    ndim = 3

    R = rotationMatrix(*next(randomRotations(ndim, rng)))
    t = rng.normal(size=ndim)
    s = rng.normal(size=1)[0]

    X = rng.normal(size=(10, ndim))
    Y = RigidXform(R, t, s) @ X

    _, cpd = last(islice(driftRigid(X, Y, w=np.eye(len(X))), 200))
    ls = align(X, Y)
    np.testing.assert_almost_equal(cpd.R, ls.R)
    np.testing.assert_almost_equal(cpd.t, ls.t)
    np.testing.assert_almost_equal(cpd.s, ls.s)
def test_cpd_prior():
    from coherent_point_drift.align import driftRigid
    rng = np.random.RandomState(4)

    ndim = 3

    R = rotationMatrix(*next(randomRotations(ndim, rng)))
    t = rng.normal(size=ndim)
    s = rng.normal(size=1)[0]

    X = rng.normal(size=(10, ndim))
    Y = RigidXform(R, t, s) @ X

    _, cpd = last(islice(driftRigid(X, Y, w=np.eye(len(X))), 200))
    ls = align(X, Y)
    np.testing.assert_almost_equal(cpd.R, ls.R)
    np.testing.assert_almost_equal(cpd.t, ls.t)
    np.testing.assert_almost_equal(cpd.s, ls.s)
def test_least_squares():
    rng = np.random.RandomState(4)

    ndim = 3

    R = rotationMatrix(*next(randomRotations(ndim, rng)))
    t = rng.normal(size=ndim)
    s = rng.lognormal(size=1)[0]
    xform = RigidXform(R, t, s)

    X = rng.normal(size=(10, ndim))
    Y = xform @ X

    alignment = align(X, Y)
    expected = xform.inverse

    np.testing.assert_almost_equal(alignment.R, expected.R)
    np.testing.assert_almost_equal(alignment.t, expected.t)
    np.testing.assert_almost_equal(alignment.s, expected.s)
    np.testing.assert_almost_equal(alignment @ Y, X)
def test_least_squares():
    rng = np.random.RandomState(4)

    ndim = 3

    R = rotationMatrix(*next(randomRotations(ndim, rng)))
    t = rng.normal(size=ndim)
    s = rng.lognormal(size=1)[0]
    xform = RigidXform(R, t, s)

    X = rng.normal(size=(10, ndim))
    Y = xform @ X

    alignment = align(X, Y)
    expected = xform.inverse

    np.testing.assert_almost_equal(alignment.R, expected.R)
    np.testing.assert_almost_equal(alignment.t, expected.t)
    np.testing.assert_almost_equal(alignment.s, expected.s)
    np.testing.assert_almost_equal(alignment @ Y, X)
Exemplo n.º 5
0
from itertools import islice, product as cartesian, starmap
from coherent_point_drift.geometry import rigidXform, rotationMatrix, RMSD, randomRotations
from coherent_point_drift.align import globalAlignment
import matplotlib.pyplot as plt
from collections import defaultdict
import numpy as np

fig, axs = plt.subplots(2, 1)

for ndim, ax in zip((2, 3), axs):
    ax.set_title("{}D".format(ndim))
    nstepss = range(1, 8)
    pointss = np.random.random((10, 12, ndim))
    rotations = starmap(rotationMatrix, islice(randomRotations(ndim), 10))

    rmsds = defaultdict(list)
    for nsteps, points, rotation in cartesian(nstepss, pointss, rotations):
        degraded = rigidXform(points, R=rotation)
        xform = globalAlignment(points, degraded, w=0.1, nsteps=nsteps)
        rmsds[nsteps].append(RMSD(points, rigidXform(degraded, *xform)))
    labels = sorted(rmsds.keys())
    ax.violinplot([rmsds[i] for i in labels], labels)
fig.tight_layout()
plt.show()