|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from mpl_toolkits.mplot3d import Axes3D # noqa: F401 |
| 4 | + |
| 5 | +from .. import style |
| 6 | + |
| 7 | + |
| 8 | +def make_axes(fig_width, wpad_edge=0, wpad_mid=0.05, hpad_top=0.05, hpad_bottom=0.05, |
| 9 | + small_sq_width=0.07): |
| 10 | + sq_width = (1. - 2 * wpad_edge - small_sq_width - 4 * wpad_mid) / 4. |
| 11 | + sq_height = 1. - hpad_top - hpad_bottom |
| 12 | + fig_height = sq_width * fig_width / sq_height |
| 13 | + small_sq_height = small_sq_width * fig_width / fig_height |
| 14 | + fig = plt.figure(figsize=(fig_width, fig_height)) |
| 15 | + # 2 small squares |
| 16 | + ax2 = fig.add_axes((wpad_edge, hpad_bottom, small_sq_width, small_sq_height)) |
| 17 | + ax1 = fig.add_axes((wpad_edge, 1. - hpad_top - small_sq_height, |
| 18 | + small_sq_width, small_sq_height)) |
| 19 | + # 3 big squares |
| 20 | + ax3 = fig.add_axes((wpad_edge + small_sq_width + wpad_mid, hpad_bottom, sq_width, sq_height)) |
| 21 | + ax4 = fig.add_axes((wpad_edge + small_sq_width + 2 * wpad_mid + sq_width, hpad_bottom, |
| 22 | + sq_width, sq_height)) |
| 23 | + ax5 = fig.add_axes((wpad_edge + small_sq_width + 3 * wpad_mid + 2 * sq_width, hpad_bottom, |
| 24 | + sq_width, sq_height)) |
| 25 | + ax6 = fig.add_axes((wpad_edge + small_sq_width + 4 * wpad_mid + 3 * sq_width, hpad_bottom, |
| 26 | + sq_width, sq_height)) |
| 27 | + axes = [ax1, ax2, ax3, ax4, ax5, ax6] |
| 28 | + for ax in axes: |
| 29 | + ax.set_xticks([]) |
| 30 | + ax.set_yticks([]) |
| 31 | + |
| 32 | + label_dx = -0.02 |
| 33 | + label_dy = 0.05 |
| 34 | + label_y = hpad_bottom + sq_height + label_dy |
| 35 | + fig.text(wpad_edge + label_dx, label_y, |
| 36 | + "A", va="bottom", ha="right", color="black", |
| 37 | + **style.panel_letter_fontstyle) |
| 38 | + fig.text(wpad_edge + small_sq_width + wpad_mid, label_y, |
| 39 | + "B", va="bottom", ha="center", color="black", |
| 40 | + **style.panel_letter_fontstyle) |
| 41 | + fig.text(wpad_edge + small_sq_width + 2 * wpad_mid + sq_width, label_y, |
| 42 | + "C", va="bottom", ha="center", color="black", |
| 43 | + **style.panel_letter_fontstyle) |
| 44 | + fig.text(wpad_edge + small_sq_width + 3 * wpad_mid + 2 * sq_width, label_y, |
| 45 | + "D", va="bottom", ha="center", color="black", |
| 46 | + **style.panel_letter_fontstyle) |
| 47 | + fig.text(wpad_edge + small_sq_width + 4 * wpad_mid + 3 * sq_width, label_y, |
| 48 | + "E", va="bottom", ha="center", color="black", |
| 49 | + **style.panel_letter_fontstyle) |
| 50 | + return axes |
| 51 | + |
| 52 | + |
| 53 | +def disp_heatmap(ax, heatmap, show_xlabels=True, show_ylabels=True, title=None): |
| 54 | + N_theta, N_phi = heatmap.shape |
| 55 | + ax.imshow(heatmap, origin="lower left", cmap="gray", aspect="equal") |
| 56 | + if show_xlabels: |
| 57 | + ax.set_xlabel("$\phi$", fontsize=style.axis_label_fontsize, labelpad=-8.5) |
| 58 | + ax.set_xticks([0, N_phi - 1]) |
| 59 | + ax.set_xticklabels(["0", "$\pi$"], fontsize=style.ticklabel_fontsize) |
| 60 | + else: |
| 61 | + ax.set_xticks([]) |
| 62 | + if show_ylabels: |
| 63 | + ax.set_ylabel("$\\theta$", fontsize=style.axis_label_fontsize, labelpad=-8.5) |
| 64 | + ax.set_yticks([0, N_theta - 1]) |
| 65 | + ax.set_yticklabels(["0", "$\pi$"], fontsize=style.ticklabel_fontsize) |
| 66 | + else: |
| 67 | + ax.set_yticks([]) |
| 68 | + ax.set_xlim([0, heatmap.shape[1] - 1]) |
| 69 | + ax.set_ylim([0, heatmap.shape[0] - 1]) |
| 70 | + if title is not None: |
| 71 | + ax.set_title(title, fontsize=style.axis_label_fontsize * 0.8, pad=1) |
| 72 | + |
| 73 | + |
| 74 | +def disp_scatter(ax, pi_gp, pi_knn, trajectories=None, diag_text=False, |
| 75 | + arrow=True, xlabel="full PI", ylabel="Gaussian PI"): |
| 76 | + # Note that gp=y and knn=x, but 0 index is gp and 1 is knn in data arrays! |
| 77 | + traj_color = "#C63F3A" |
| 78 | + all_gp_vals = [pi_gp] |
| 79 | + all_knn_vals = [pi_knn] |
| 80 | + if trajectories is not None: |
| 81 | + all_gp_vals += [traj[:, 0] for traj in trajectories] |
| 82 | + all_knn_vals += [traj[:, 1] for traj in trajectories] |
| 83 | + all_gp_vals = np.concatenate(all_gp_vals) |
| 84 | + all_knn_vals = np.concatenate(all_knn_vals) |
| 85 | + min_gp, max_gp = all_gp_vals.min(), all_gp_vals.max() |
| 86 | + range_gp = max_gp - min_gp |
| 87 | + min_knn, max_knn = all_knn_vals.min(), all_knn_vals.max() |
| 88 | + range_knn = max_knn - min_knn |
| 89 | + pi_gp_norm = (pi_gp - min_gp) / range_gp |
| 90 | + pi_knn_norm = (pi_knn - min_knn) / range_knn |
| 91 | + ax.hexbin(pi_knn_norm, pi_gp_norm, gridsize=50, extent=(0, 1, 0, 1), |
| 92 | + cmap="gray_r", bins="log", linewidth=0.05) |
| 93 | + if trajectories is not None: |
| 94 | + for traj_idx in range(len(trajectories)): |
| 95 | + traj = np.copy(trajectories[traj_idx]) |
| 96 | + traj[:, 0] = (traj[:, 0] - min_gp) / range_gp |
| 97 | + traj[:, 1] = (traj[:, 1] - min_knn) / range_knn |
| 98 | + ax.plot(traj[:, 1], traj[:, 0], linewidth=0.5, color=traj_color) |
| 99 | + ax.set_xlim([0, 1.025]) |
| 100 | + ax.set_ylim([0, 1.025]) |
| 101 | + ax.set_xticks([0, 1]) |
| 102 | + ax.set_xticklabels([0, 1], fontsize=style.ticklabel_fontsize) |
| 103 | + ax.set_yticks([0, 1]) |
| 104 | + ax.set_yticklabels([0, 1], fontsize=style.ticklabel_fontsize) |
| 105 | + ax.spines['left'].set_bounds(0, 1) |
| 106 | + ax.spines['bottom'].set_bounds(0, 1) |
| 107 | + ax.spines['right'].set_visible(False) |
| 108 | + ax.spines['top'].set_visible(False) |
| 109 | + ax.set_xlabel(xlabel, fontsize=style.axis_label_fontsize, labelpad=-9.5) |
| 110 | + ax.set_ylabel(ylabel, fontsize=style.axis_label_fontsize, labelpad=-8) |
| 111 | + |
| 112 | + theta_deg = 47 |
| 113 | + if diag_text: |
| 114 | + ax.text(0.5, 0.65, "DCA trajectories", fontsize=style.ticklabel_fontsize * 0.8, |
| 115 | + rotation=theta_deg, rotation_mode="anchor", ha="center", va="center", |
| 116 | + color=traj_color) |
| 117 | + if arrow: |
| 118 | + len_x = np.cos(np.deg2rad(theta_deg)) |
| 119 | + len_y = np.sin(np.deg2rad(theta_deg)) |
| 120 | + mag = 0.425 |
| 121 | + ax.quiver(0.475, 0.40, mag * len_x, mag * len_y, |
| 122 | + angles='xy', scale_units='xy', scale=1, width=0.015, |
| 123 | + color=traj_color) |
| 124 | + |
| 125 | + |
| 126 | +def plot_deflation_results(ax, pi_regular, pi_def, pi_fft): |
| 127 | + dim_vals = np.arange(len(pi_def) + 1) |
| 128 | + pi_vals = (pi_regular, pi_def, pi_fft) |
| 129 | + labels = ["DCA", "deflation", "FFT deflation"] |
| 130 | + markersize = 1.5 |
| 131 | + colors = ["#C63F3A", "gray", "black"] |
| 132 | + for i in range(len(pi_vals)): |
| 133 | + if i < 2: |
| 134 | + ax.plot(dim_vals, [0] + list(pi_vals[i]), label=labels[i], |
| 135 | + linewidth=0.85, color=colors[i], linestyle="-") |
| 136 | + else: |
| 137 | + ax.plot(dim_vals, [0] + list(pi_vals[i]), label=labels[i], |
| 138 | + linewidth=0, marker=".", markersize=markersize, |
| 139 | + color=colors[i]) |
| 140 | + |
| 141 | + ax.legend(fontsize=style.ticklabel_fontsize * 0.8, frameon=False, |
| 142 | + labelspacing=0.1, bbox_to_anchor=(0.2, 0, 1, 1)) |
| 143 | + ax.set_xlabel("dimension", fontsize=style.axis_label_fontsize, labelpad=-9.5) |
| 144 | + ax.set_ylabel("PI (nats)", fontsize=style.axis_label_fontsize, labelpad=-13) |
| 145 | + ax.spines["right"].set_visible(False) |
| 146 | + ax.spines["top"].set_visible(False) |
| 147 | + |
| 148 | + max_dim = len(pi_regular) |
| 149 | + max_dim_padded = max_dim * 1.025 |
| 150 | + ax.set_xticks([0, max_dim]) |
| 151 | + ax.set_xticklabels([0, max_dim], fontsize=style.ticklabel_fontsize) |
| 152 | + ax.set_xlim([0, max_dim_padded]) |
| 153 | + ax.spines["bottom"].set_bounds(0, max_dim) |
| 154 | + |
| 155 | + max_pi = np.max(np.concatenate(pi_vals)) |
| 156 | + max_pi_padded = max_pi * 1.025 |
| 157 | + ax.set_yticks([0, max_pi]) |
| 158 | + ax.set_yticklabels([0, np.round(max_pi, 1)], fontsize=style.ticklabel_fontsize) |
| 159 | + ax.set_ylim([0, max_pi_padded]) |
| 160 | + ax.spines["left"].set_bounds(0, max_pi) |
0 commit comments