File size: 4,576 Bytes
e11c92f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
"""Test diffusion."""
import matplotlib.pyplot as plt
import numpy as np
from numpy.testing import assert_array_almost_equal
from MARBLE import construct_dataset
from MARBLE import dynamics
from MARBLE import geometry
from MARBLE import plotting
from MARBLE.layers import Diffusion
def f1(x):
"""f1"""
eps = 1e-1
norm = np.sqrt((x[:, [0]] - 1) ** 2 + x[:, [1]] ** 2 + eps)
u = x[:, [1]] / norm
v = -(x[:, [0]] - 1) / norm
return np.hstack([u, v])
def f2(x):
"""f2"""
y = []
for _ in range(x.shape[0]):
y_ = np.random.uniform(size=(3))
y_ /= np.linalg.norm(y_)
y.append(y_)
return np.vstack(y)
def sphere():
"""sphere"""
u, v = np.mgrid[0 : 2 * np.pi : 20j, 0 : np.pi : 11j]
x = np.cos(u) * np.sin(v)
y = np.sin(u) * np.sin(v)
z = np.cos(v)
return np.vstack([x.flatten(), y.flatten(), z.flatten()]).T
def test_diffusion(plot=False):
"""Test diffusion and laplacian creation."""
# parameters
n = 512
k = 30
tau0 = 50
# f1: constant, f2: linear, f3: parabola, f4: saddle
x = dynamics.sample_2d(n, [[-1, -1], [1, 1]], "random")
y = f1(x) # evaluated functions
# #construct PyG data object
data = construct_dataset(x, y, graph_type="cknn", k=k)
gauges, _ = geometry.compute_gauges(data)
assert_array_almost_equal(
gauges.numpy()[:5],
np.array(
[
[[-0.19064367, 0.9816593], [0.9816593, 0.19064367]],
[[-0.97356814, 0.22839674], [0.22839674, 0.97356814]],
[[-0.91470975, -0.40411147], [-0.40411147, 0.91470975]],
[[-0.1206701, 0.99269265], [0.99269265, 0.1206701]],
[[-0.37872583, 0.9255089], [0.9255089, 0.37872583]],
]
),
decimal=5,
)
R = geometry.compute_connections(data, gauges)
assert_array_almost_equal(
R.to_dense()[:5, :5],
np.array(
[
[1.0, 0.0, 0.0, 0.0, -0.22231616],
[0.0, 1.0, 0.0, 0.0, -0.97497463],
[0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[-0.22231616, -0.97497463, 0.0, 0.0, 1.0],
],
),
decimal=5,
)
L = geometry.compute_laplacian(data)
assert_array_almost_equal(
L.to_dense().numpy()[:5, :5],
np.array(
[
[1.0, 0.0, -0.01967779, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0, 0.0],
[-0.02420571, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
],
),
decimal=5,
)
Lc = geometry.compute_connection_laplacian(data, R)
assert_array_almost_equal(
Lc.to_dense().numpy()[:5, :5],
np.array(
[
[1.0, 0.0, 0.0, 0.0, 0.00437469],
[0.0, 1.0, 0.0, 0.0, 0.01918535],
[0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[0.00538132, 0.02359995, 0.0, 0.0, 1.0],
],
),
decimal=5,
)
diffusion = Diffusion(tau0=tau0)
data.x = diffusion(data.x, L, Lc, method="matrix_exp")
assert_array_almost_equal(
data.x.detach().numpy()[:5],
np.array(
[
[0.8945822, 0.2413084],
[-0.06356261, 0.9169159],
[-0.5462601, 0.7629167],
[0.9424986, -0.01397797],
[0.57801795, 0.68634313],
]
),
decimal=5,
)
if plot:
plotting.fields(data)
plt.show()
def test_diffusion_sphere(plot=False):
"""Test diffusion on sphere."""
# parameters
k = 0.4
tau0 = 10.0
x = sphere()
y = f2(x)
# construct PyG data object
data = construct_dataset(
x, y, graph_type="radius", k=k, frac_geodesic_nb=1.5, var_explained=0.9
)
L = geometry.compute_laplacian(data)
diffusion = Diffusion(tau0=tau0)
data.x = diffusion(data.x, L, method="matrix_exp")
assert_array_almost_equal(
data.x.detach().numpy()[:5],
np.array(
[
[0.513162, 0.44882008, 0.5685046],
[0.35709542, 0.67346, 0.44372997],
[0.32471117, 0.3551194, 0.81424344],
[0.6844833, 0.53020036, 0.4575338],
[0.5897326, 0.68115395, 0.41908088],
]
),
decimal=5,
)
if plot:
plotting.fields(data, alpha=1)
plt.show()
|