return model # We need to know the maximum number of latent factors that we'll need to plot. n_latent_factors = [] for comparison_model_path in comparison_model_paths: if not os.path.exists(comparison_model_path): continue model = _get_model(comparison_model_path) n_latent_factors.append(model.n_latent_factors) # TODO: Set this from config, or something crazy to keep colours consistent? J_max = np.max(n_latent_factors) #J_max = 8 cmap = mpl_utils.discrete_cmap(J_max, base_cmap="Spectral_r") colors = [cmap(j) for j in range(J_max)][::-1] # Create comparison figure. #A_original = original_model.theta_[original_model.parameter_names.index("A")] D = 17 J_ast = len(config["grouped_elements"]) A_astrophysical = np.zeros((D, J_ast))#np.random.normal(0, 0.1, size=A_est.shape) for i, tes in enumerate(config["grouped_elements"][:J_ast]): for j, te in enumerate(tes): try: idx = label_names.index("{0}_h".format(te.lower()))
ax.set_xticks(xi) ax.set_xlabel(r"$\textrm{dimension}$") ax.set_ylabel(r"$\mathbf{L}$") ylim = np.ceil(10 * np.abs(ax.get_ylim()).max()) / 10 ax.plot([0, D + 1], [0, 0], ":", c="#000000", zorder=-1, lw=0.5) ax.set_xlim(0.5, D + 0.5) ax.set_ylim(-ylim, +ylim) ax.set_yticks([-ylim, 0, ylim]) fig_factor_loads.tight_layout() savefig(fig_factor_loads, "factor_loads") # Plot the latent space. cmap = mpl_utils.discrete_cmap(data_kwds["n_components"], "Spectral") label_names = [ f"$\\mathbf{{S}}_{{{i}}}$" for i in range(data_kwds["n_latent_factors"]) ] fig_latent = mpl_utils.plot_latent_space(model, Y, cmap=cmap, label_names=label_names) for ax in fig_latent.axes: if ax.get_visible(): if ax.is_last_row(): ax.xaxis.set_major_locator(MaxNLocator(3)) if ax.is_first_col(): ax.yaxis.set_major_locator(MaxNLocator(3))
AL = linalg.cholesky(R_opt.T @ R_opt) R_opt2 = R_opt @ linalg.solve(AL, np.eye(model.n_latent_factors)) chi1 = np.sum(np.abs(A_est @ R - A_astrophysical)) chi2 = np.sum(np.abs(A_est @ R_opt2 - A_astrophysical)) R = R_opt2 if chi2 < chi1 else R # Now make it a valid rotation matrix. model.rotate(R, X=X, ensure_valid_rotation=True) # Identifiability. J_max = config["max_n_latent_factors_for_colormap"] cmap = mpl_utils.discrete_cmap(J_max, base_cmap="Spectral_r") colors = [cmap(j) for j in range(J_max)][::-1] fig_fac = mpl_utils.plot_factor_loads_and_contributions(model, X, label_names=label_names, colors=colors) savefig(fig_fac, "latent-factors-and-contributions") fig_fac = mpl_utils.plot_factor_loads_and_contributions(model, X, label_names=label_names, colors=colors, target_loads=A_astrophysical) savefig(fig_fac, "latent-factors-and-contributions-with-targets") # Plot the specific variances. xticklabels = [r"$\textrm{{{0}}}$".format(ln.split("_")[0].title()) for ln in label_names] fig_scatter = mpl_utils.plot_specific_scatter(model,
savefig(fig_mml, f"size-{size}-gridsearch-mml") except: None # F*****g save everything. with open(output_path, "wb") as fp: pickle.dump((Js, Ks, gJs, gKs, converged, meta, X_H, label_names), fp) # Compare model with previously saved model. model = meta["best_models"][config["adopted_metric"]] saved_models[size] = model # Plot clustering in data space. component_cmap = mpl_utils.discrete_cmap(7, base_cmap="Spectral_r") fig, axes = plt.subplots(5, 3, figsize=(7.1, 9.0)) axes = np.atleast_1d(axes).flatten() x = X_H.T[label_names.index("fe_h")] c = np.argmax(model.tau_, axis=1) K = model.n_components y_idx = 0 for i, ax in enumerate(axes): if label_names[i] == "fe_h": y_idx += 1 y = X_H.T[y_idx] - x
ax.set_xticks(xi) ax.set_xlabel(r"$\textrm{dimension}$") ax.set_ylabel(r"$\mathbf{L}$") ylim = np.ceil(10 * np.abs(ax.get_ylim()).max()) / 10 ax.plot([0, D + 1], [0, 0], ":", c="#000000", zorder=-1, lw=0.5) ax.set_xlim(0.5, D + 0.5) ax.set_ylim(-ylim, +ylim) ax.set_yticks([-ylim, 0, ylim]) fig_factor_loads.tight_layout() savefig(fig_factor_loads, "factor_loads") # Plot the latent space. cmap = mpl_utils.discrete_cmap(n_components, "Spectral") label_names = [f"$\\mathbf{{S}}_{{{i}}}$" for i in range(n_latent_factors)] fig_latent = mpl_utils.plot_latent_space(model, Y, cmap=cmap, label_names=label_names) for ax in fig_latent.axes: if ax.get_visible(): if ax.is_last_row(): ax.xaxis.set_major_locator(MaxNLocator(3)) if ax.is_first_col(): ax.yaxis.set_major_locator(MaxNLocator(3)) xlim = np.max(np.abs(ax.get_xlim())) ylim = np.max(np.abs(ax.get_ylim()))