import matplotlib.pyplot as plt
import numpy as np
from clocks import (
ClockArray, InferenceConfig, MassConfig, NoiseConfig,
PriorConfig, SimulationConfig, infer, simulate,
)
from clocks.viz import THEMES, show, style
clock_positions = np.array([
[-4.0, 0.0], [-2.0, 3.0], [1.0, 4.0], [4.0, 2.0],
[5.0, -1.0], [2.0, -4.0], [-1.0, -3.0], [-3.0, -1.5],
])
clock_array = ClockArray(positions=clock_positions, track_offset=3.0)
truth = MassConfig(positions=np.array([[1.5, -1.0]]), masses=np.array([0.15]))
sim = simulate(SimulationConfig(
clock_array=clock_array, ground_truth=truth,
noise=NoiseConfig(observation_std=0.005), n_observations=25, seed=42,
))
result = infer(sim.observations, InferenceConfig(
clock_array=clock_array, noise=NoiseConfig(observation_std=0.005),
prior=PriorConfig(position_range=(-8.0, 8.0), mass_range=(0.005, 0.15)),
n_particles=500, n_masses=1,
ess_target=0.8, rejuvenation_steps=2, proposal_scale=2.38, seed=42,
))
particles = result.particle_state.particles
true_x, true_y = truth.positions[0]
for theme in THEMES:
with style(theme) as colors:
fig, ax = plt.subplots(figsize=(7.5, 5.4))
cloud = ax.scatter(
particles[:, 0], particles[:, 1],
s=10, alpha=0.35, linewidths=0, color=colors.belief,
)
ring = ax.scatter(
true_x, true_y, marker="o", s=300,
facecolors="none", edgecolors=colors.truth, linewidths=1.3,
)
crosshair = ax.scatter(
true_x, true_y, marker="+", s=300,
color=colors.truth, linewidths=1.3,
)
clock_marks = ax.scatter(
clock_positions[:, 0], clock_positions[:, 1],
marker="s", s=50, color=colors.ink,
)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_aspect("equal")
legend = ax.legend(
[cloud, (ring, crosshair), clock_marks],
["particles", "true position", "clocks"],
)
# The cloud's own dots are too small and faint to read as a key.
legend.legend_handles[0].set_sizes([40])
legend.legend_handles[0].set_alpha(0.8)
show(fig)