Exemplo n.º 1
0
def test_solution_reconstruction():
    nodes = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
    point = np.array([0.1, 0.1])  # Point to interpolate.

    D1 = 6
    r0 = 2  # rank sample 0
    r1 = 3  # rank sample 1
    r2 = 4  # rank sample 2
    r3 = 3  # rank sample 2

    np.random.seed(1111)  # For reproducibility.

    # Creating a list of solutions.
    Solutions = [sol0, sol1, sol2, sol3]

    manifold_projection = SVDProjection(Solutions, p="max")

    interpolation = GrassmannInterpolation(interpolation_method=None,
                                           manifold_data=manifold_projection.v,
                                           coordinates=nodes,
                                           distance=GeodesicDistance())

    interpolated_solution = interpolation.interpolate_manifold(point=point)
    # assert round(interpolated_solution.data[0, 0], 9) == -0.353239531
    assert round(interpolated_solution.data[0, 0], 9) == -0.316807309
Exemplo n.º 2
0
def test_log_exp_maps():
    sol0 = np.array([[0.61415, 1.03029, 1.02001, 0.57327, 0.79874, 0.73274],
                     [0.56924, 0.91700, 0.88841, 0.53737, 0.68676, 0.67751],
                     [0.51514, 0.87898, 0.87779, 0.47850, 0.69085, 0.61525],
                     [0.63038, 1.10822, 1.12313, 0.58038, 0.89142, 0.75429],
                     [0.69666, 1.03114, 0.95037, 0.67211, 0.71184, 0.82522],
                     [0.66595, 1.03789, 0.98690, 0.63420, 0.75416, 0.79110]])

    sol1 = np.array([[1.05134, 1.37652, 0.95634, 0.85630, 0.47570, 1.22488],
                     [0.16370, 0.63105, 0.14533, 0.81030, 0.44559, 0.43358],
                     [1.23478, 2.10342, 1.04698, 1.68755, 0.92792, 1.73277],
                     [0.90538, 1.64067, 0.62027, 1.17577, 0.63644, 1.34925],
                     [0.58210, 0.75795, 0.65519, 0.65712, 0.37251, 0.65740],
                     [0.99174, 1.59375, 0.63724, 0.89107, 0.47631, 1.36581]])

    sol2 = np.array([[1.04142, 0.91670, 1.47962, 1.23350, 0.94111, 0.61858],
                     [1.00464, 0.65684, 1.35136, 1.11288, 0.96093, 0.42340],
                     [1.05567, 1.33192, 1.56286, 1.43412, 0.77044, 0.97182],
                     [0.89812, 0.86136, 1.20204, 1.17892, 0.83788, 0.61160],
                     [0.46935, 0.39371, 0.63534, 0.57856, 0.47615, 0.26407],
                     [1.14102, 0.80869, 1.39123, 1.33076, 0.47719, 0.68170]])

    sol3 = np.array([[0.60547, 0.11492, 0.78956, 0.13796, 0.76685, 0.41661],
                     [0.32771, 0.11606, 0.67630, 0.15208, 0.44845, 0.34840],
                     [0.58959, 0.10156, 0.72623, 0.11859, 0.73671, 0.38714],
                     [0.36283, 0.07979, 0.52824, 0.09760, 0.46313, 0.27906],
                     [0.87487, 0.22452, 1.30208, 0.30189, 1.22015, 0.62918],
                     [0.56006, 0.16879, 1.09635, 0.20431, 0.69439, 0.60317]])

    # Creating a list of matrices.
    matrices = [sol0, sol1, sol2, sol3]
    manifold_projection = SVDProjection(matrices, p="max")

    points_tangent = GrassmannOperations.log_map(
        grassmann_points=manifold_projection.u,
        reference_point=manifold_projection.u[0])

    assert np.round(points_tangent[0][0][0], 2) == 0.0
    assert np.round(points_tangent[1][0][0], 8) == 0.0
    assert np.round(points_tangent[2][0][0], 8) == 0.0
    assert np.round(points_tangent[3][0][0], 8) == 0.0

    manifold_points = GrassmannOperations.exp_map(
        tangent_points=points_tangent,
        reference_point=manifold_projection.u[0])

    assert np.round(manifold_points[0].data[0][0], 5) == -0.41808
    assert np.round(manifold_points[1].data[0][0], 8) == -0.4180759
    assert np.round(manifold_points[2].data[0][0], 8) == -0.4180759
    assert np.round(manifold_points[3].data[0][0], 8) == -0.4180759
Exemplo n.º 3
0
def test_kernel():

    np.random.seed(1111)  # For reproducibility.
    from numpy.random import RandomState
    rnd = RandomState(0)

    # Creating a list of solutions.
    Solutions = [sol0, sol1, sol2, sol3]
    manifold_projection = SVDProjection(Solutions, p="max")
    kernel = ProjectionKernel()

    kernel.calculate_kernel_matrix(manifold_projection.u)

    assert np.round(kernel.kernel_matrix[0, 1], 8) == 6.0
Exemplo n.º 4
0
def test_karcher_stochastic():
    matrices = [sol0, sol1, sol2, sol3]
    manifold_projection = SVDProjection(matrices, p="max")

    # optimization_method = GradientDescent(acceleration=True, error_tolerance=1e-4, max_iterations=1000)
    psi_mean = GrassmannOperations.karcher_mean(
        grassmann_points=manifold_projection.u,
        optimization_method="StochasticGradientDescent",
        distance=GeodesicDistance())

    phi_mean = GrassmannOperations.karcher_mean(
        grassmann_points=manifold_projection.v,
        optimization_method="StochasticGradientDescent",
        distance=GeodesicDistance())
Exemplo n.º 5
0
def test_karcher():
    # Creating a list of matrices.
    matrices = [sol0, sol1, sol2, sol3]
    manifold_projection = SVDProjection(matrices, p="max")

    # optimization_method = GradientDescent(acceleration=True, error_tolerance=1e-4, max_iterations=1000)
    psi_mean = GrassmannOperations.karcher_mean(
        grassmann_points=manifold_projection.u,
        optimization_method="GradientDescent",
        distance=GeodesicDistance())

    phi_mean = GrassmannOperations.karcher_mean(
        grassmann_points=manifold_projection.v,
        optimization_method="GradientDescent",
        distance=GeodesicDistance())

    assert round(psi_mean.data[0, 0], 9) == -0.387197957
Exemplo n.º 6
0
ax1.imshow(Sol0)
ax2.title.set_text('Matrix 1')
ax2.imshow(Sol1)
ax3.title.set_text('Matrix 2')
ax3.imshow(Sol2)
ax4.title.set_text('Matrix 3')
ax4.imshow(Sol3)
plt.show()

# %% md
#
# Instantiate the SvdProjection class that projects the raw data to the manifold.

# %%

manifold_projection = SVDProjection(matrices, p="max")

# %% md
#
# Compute the kernels for :math:`\Psi` and :math:`\Phi`, the left and right -singular eigenvectors, respectively, of
# singular value decomposition of each solution.

# %%
projection_kernel = ProjectionKernel()

projection_kernel.calculate_kernel_matrix(points=manifold_projection.u)
kernel_psi = projection_kernel.kernel_matrix

projection_kernel.calculate_kernel_matrix(points=manifold_projection.v)
kernel_phi = projection_kernel.kernel_matrix
Exemplo n.º 7
0
# Recast the data into a matrix
data_matrix = np.concatenate(
    [X.reshape(-1, 1), Y.reshape(-1, 1),
     Z.reshape(-1, 1)], axis=1)

# Convert each data point into a 2d array with shape (1, 3)
data = [data2dArray.reshape(1, -1).T for data2dArray in data_matrix]

#%%
#
# Project each data point onto the Grassmann manifold using SVD. Use only 300 points from the origonal data.
# Select the work with maximum rank (number of planes) from each data point (p=sys.maxsize).
# Use the matrix of left eigenvectors to calculate the kernel (KernelComposition.LEFT).

Grassmann_projection = SVDProjection(data=data[::10], p="max")
psi = Grassmann_projection.u

#%%
#
# Plot the projected data on the Grassmann (3, 1) which is a unit sphere.

Grassmann_data = np.zeros((len(psi), 3))
for i in range(len(psi)):
    Grassmann_data[i, :] = psi[i].data.reshape(1, -1)

fig = plt.figure()
plt.rcParams["figure.figsize"] = (8, 8)
plt.rcParams.update({'font.size': 18})
ax = fig.add_subplot(111, projection="3d")