"""Regenerate the article's vector figures from the archived trials.""" from pathlib import Path import json import numpy as np from matplotlib import pyplot as plt from matplotlib.collections import LineCollection from matplotlib.lines import Line2D from scipy.spatial.distance import cdist import patch_seam_trials import plot_sphere_trials as sphere def plot_defects(trial, archive, output): mesh, charge = sphere.load_mesh(archive, trial) theta = trial["theta"] pos, neg, distance = sphere.pair_distances(mesh, charge, theta) pairs, matched = sphere.shortest_maximum_matching(pos, neg, distance, sphere.PAIR_RADIUS) unmatched = len(pos) + len(neg) - 2 * matched assert unmatched == trial["unmatched_units"][str(sphere.PAIR_RADIUS)] radius = 4 defects = mesh[charge != 0] chords = cdist(defects, defects) center = defects[np.argmax(np.count_nonzero(chords <= 2 * np.sin(radius * theta / 2), axis=1))] meridian = np.arctan2(center[1], center[0]) projected = sphere.lon_lat(mesh, meridian) local = sphere.normal_coordinates(mesh, center, theta) fig = plt.figure(figsize=(6.2, 2.77)) grid = fig.add_gridspec(1, 2, width_ratios=(1.9, 1), left=.045, right=.968, top=.875, bottom=.24, wspace=.25) globe = fig.add_subplot(grid[0, 0], projection="mollweide") zoom = fig.add_subplot(grid[0, 1]) globe.set_title("(a) Spherical mesh", loc="left", fontsize=9) zoom.set_title("(b) Local window", loc="left", fontsize=9) for axis in (globe, zoom): axis.set_facecolor("#F6F8F7") axis.grid(color="#DCE5E4", linewidth=.45) axis.set_axisbelow(True) globe.set_xticklabels([]) globe.set_yticklabels([]) globe.add_collection(LineCollection(sphere.geodesic_arcs(mesh, pairs, meridian), colors=sphere.LINK, linewidths=.35, zorder=2)) zoom.set(xlim=(-4.25, 4.25), ylim=(-4.25, 4.25), aspect="equal") zoom.set_xlabel(r"East$/\theta$", fontsize=8, labelpad=1.5) zoom.set_ylabel(r"North$/\theta$", fontsize=8, labelpad=1.5) zoom.set_xticks([-4, -2, 0, 2, 4]) zoom.set_yticks([-4, -2, 0, 2, 4]) zoom.tick_params(labelsize=7.5, length=3, pad=2) zoom.add_collection(LineCollection(local[pairs], colors=sphere.LINK, linewidths=.8, zorder=2)) zoom.add_patch(plt.Circle((0, 0), radius, fill=False, linestyle="--", linewidth=.65, edgecolor=sphere.INK, zorder=4)) east = np.cross([0, 0, 1], center) if np.linalg.norm(east) < 1e-8: east = np.array([0., 1., 0.]) east /= np.linalg.norm(east) north = np.cross(center, east) angle = np.linspace(0, 2 * np.pi, 181) outline = np.cos(radius * theta) * center + np.sin(radius * theta) * ( np.cos(angle[:, None]) * east + np.sin(angle[:, None]) * north ) globe.plot(*sphere.lon_lat(outline, meridian).T, linestyle="--", color=sphere.INK, linewidth=.65, zorder=4) for axis, coords, size, size_neutral in ((globe, projected, 4.2, .42), (zoom, local, 12., 3.2)): axis.scatter(*coords[charge == 0].T, s=size_neutral, color=sphere.MESH, alpha=.64, edgecolors="none", zorder=1) for sign, color, marker in ((1, sphere.POSITIVE, "o"), (-1, sphere.NEGATIVE, "^")): mask = charge * sign > 0 axis.scatter(*coords[mask].T, s=size * np.abs(charge[mask]), color=color, marker=marker, linewidths=.14, edgecolors="white", zorder=3) legend = [ Line2D([], [], marker="o", linestyle="none", color=sphere.MESH, markersize=3.5, label="Degree six"), Line2D([], [], marker="o", linestyle="none", color=sphere.POSITIVE, markersize=4.5, label="Positive charge"), Line2D([], [], marker="^", linestyle="none", color=sphere.NEGATIVE, markersize=4.5, label="Negative charge"), Line2D([], [], color=sphere.LINK, linewidth=.8, label="Matched pair"), ] fig.legend(handles=legend, loc="lower center", bbox_to_anchor=(.5, .013), frameon=False, ncol=4, fontsize=8, handletextpad=.4, columnspacing=1.2) fig.savefig(output, bbox_inches="tight", pad_inches=.05) plt.close(fig) def main(): import argparse parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--data", type=Path, default=Path(__file__).resolve().parent) parser.add_argument("--figures", type=Path) args = parser.parse_args() data = args.data figures = args.figures or data.parent / "figures" figures.mkdir(exist_ok=True) trials = json.loads((data / "trial_results.json").read_text(encoding="utf-8"))["trials"] largest = max(trial["nominal_count"] for trial in trials) chosen = min( (trial for trial in trials if trial["nominal_count"] == largest), key=lambda trial: trial["trial_energy"], ) with np.load(data / "trial_configurations.npz") as archive: plot_defects(chosen, archive, figures / "Fig1.pdf") seam_trials = json.loads((data / "patch_seam_results.json").read_text(encoding="utf-8"))["trials"] with plt.rc_context({"font.size": 11, "axes.labelsize": 11}): patch_seam_trials.plot_comparison(seam_trials, figures / "Fig2.pdf") if __name__ == "__main__": main()