def plot_latents(df, X_trans, Z_mu, labels, distances, latent_titles=None, suptitle=None, path=None): ''' plot latents ''' util_funcs.plt_setup() fig, axes = plt.subplots(3, 8, figsize=(12, 8)) # plot correlations for latent_idx, ax in enumerate(axes[0]): heatmap_corrs = plot_funcs.correlate_heatmap(len(labels), len(distances), X_trans, Z_mu[:, latent_idx]) plot_funcs.plot_heatmap(ax, heatmap=heatmap_corrs, row_labels=labels, col_labels=distances, set_row_labels=latent_idx == 0, cbar=True) if latent_titles is not None: t = latent_titles[latent_idx] ax.set_title(t) # plot maps for city_ax_row, city_name, x, y in zip([axes[1], axes[2]], ['Cambridge', 'Milton Keynes'], (545700, 485970), (258980, 236920)): x_extents = (x - 2500, x + 2500) y_extents = (y - 4500, y + 4500) for latent_idx, ax in enumerate(city_ax_row): plot_funcs.plot_scatter(ax, df.x, df.y, Z_mu[:, latent_idx], x_extents=x_extents, # 10000 y_extents=y_extents, # 10000 relative_extents=False, s_min=0, s_max=0.8, c_exp=5, s_exp=3.5) ax.set_xlabel(f'{city_name} #{latent_idx}') if suptitle is not None: plt.suptitle(suptitle) if path is not None: plt.savefig(path, dpi=300) else: plt.show()
np.exp(Z_log_var.numpy().astype('float64'))) # average kl divergence per latent kl_vec = np.mean(kl_loss, axis=0) # plot util_funcs.plt_setup() fig, axes = plt.subplots(2, 6, figsize=(12, 8)) for ax_row, inverse in zip(axes, [False, True]): L = np.copy(Z_mu) if inverse: L *= -1 for latent_idx in range(latent_dim): ax = ax_row[latent_idx] plot_funcs.plot_scatter(ax, df_20.x, df_20.y, L[:, latent_idx], s_min=0, s_max=0.8, c_exp=3, s_exp=5) ax.set_title(f'Dim. {latent_idx + 1} - ' + r'$D_{KL}=' + f'{kl_vec[latent_idx]:.1f}$') if inverse: ax.set_title(f'Dim. {latent_idx + 1} - Inverse') plt.suptitle( r"Geographic mappings (and inverses) for the latents of the VAE model" ) path = f'../phd-doc/doc/images/signatures/latents_map_{beta}_{cap}_{seed}_{theme_base}.pdf' plt.savefig(path, dpi=300)
for x, y, town_name in [(485970, 236920, 'Milton Keynes'), (545700, 258980, 'Cambridge'), (635633, 170829, 'Margate')]: x_extents = (x - 2500, x + 2500) y_extents = (y - 5000, y + 5000) util_funcs.plt_setup() fig, ax = plt.subplots(1, 1, figsize=(8, 8)) plot_funcs.plot_scatter( ax, df_20.x, df_20.y, # color range is from 0.0 to 1.0 # don't manipulate in this context c=y_col, cmap=util_funcs.cityseer_cmap(), s=1, vmin=0.0, # in some cases plt doesn't correct infer extents vmax=1.0, x_extents=x_extents, y_extents=y_extents, relative_extents=False) plt.suptitle(f"{town_name} hyperlocal predictions") path = f'../phd-doc/doc/images/predictive/stage_2_DNN_{town_name}_{clf.theme}_yc{y_conf}.pdf' plt.savefig(path, dpi=300) # %% # create columns for percentages for pop_id, town_row in selected_data.iterrows(): pred = y_plot[X_raw.city_pop_id == pop_id] selected_data.loc[pop_id,
for theme_set, theme_labels, t_meta, p_meta, weighted in zip( theme_sets, theme_label_sets, theme_metas, path_metas, theme_wt): print(f'processing metas: {t_meta}') util_funcs.plt_setup() fig, axes = plt.subplots(3, 2, figsize=(7, 10)) for t_idx, (t, label) in enumerate(zip(theme_set, theme_labels)): for d_idx, (dist, beta) in enumerate(zip([200, 800], [r'-0.02', r'-0.005'])): ax = axes[t_idx][d_idx] theme = t.format(dist=dist) data = df_20[theme] plot_funcs.plot_scatter(fig, ax, df_20.x, df_20.y, km_per_inch=3.5, vals=data, s_exp=4) ax.set_xlabel(f'{label} ' + r'$d_{max}=' + f'{dist}m$') plt.suptitle(f'Plots of {t_meta} mixed-use measures for inner London') path = f'../phd-doc/doc/images/diversity/diversity_comparisons_{p_meta}.pdf' plt.savefig(path, dpi=300) # %% prepare PCA # use of bands gives slightly more defined delineations for latent dimensions pca_columns_dist = [ 'ac_accommodation_{dist}', 'ac_eating_{dist}', 'ac_drinking_{dist}', 'ac_commercial_{dist}', 'ac_tourism_{dist}', 'ac_entertainment_{dist}', 'ac_government_{dist}', 'ac_manufacturing_{dist}', 'ac_retail_food_{dist}', 'ac_retail_other_{dist}', 'ac_transport_{dist}', 'ac_health_{dist}',
util_funcs.plt_setup() fig, axes = plt.subplots(4, 4, figsize=(7, 10)) # iterate each town for a given set for ax_col_idx, (centre, town_name, town_pop_id) in enumerate(set): # map 1) Boundary designation # targets will be 1 vs. 0 for new town like or not c = cmap(float(selected_data.loc[town_pop_id, 'targets'])) ax = axes[0, ax_col_idx] ax.set_xlabel(f'{town_name}') if ax_col_idx == 0: ax.set_ylabel(f'Predictions by boundary') plot_funcs.plot_scatter(fig, ax, df_20.x, df_20.y, c=c, cmap=cmap, s=s, centre=centre, km_per_inch=3) # map 2) classifier ax = axes[1, ax_col_idx] ax.set_xlabel(f'{town_name}') if ax_col_idx == 0: ax.set_ylabel(f'DNN local predictions') plot_funcs.plot_scatter( fig, ax, df_20.x, df_20.y, # color range is from 0.0 to 1.0
theme_label_sets, theme_metas, theme_wts)): print(f'processing set: {n}') util_funcs.plt_setup() fig, axes = plt.subplots(3, 2, figsize=(7, 10)) for t_idx, (t, label) in enumerate(zip(theme_set, theme_labels)): for d_idx, (dist, beta) in enumerate(zip([400, 1600], [r'0.01', r'0.0025'])): ax = axes[t_idx][d_idx] theme = t.format(dist=dist) data = df_20[theme] plot_funcs.plot_scatter(fig, ax, df_20.x, df_20.y, vals=data, km_per_inch=3.5, s_exp=theme_wt) ax.set_xlabel(f'{label} ' + '$d_{max}=' + f'{dist}m$') plt.suptitle(f'Plots of centrality measures for inner London (set {n + 1})') path = f'../phd-doc/doc/images/centrality/primal_centrality_comparisons_{theme_meta}.pdf' plt.savefig(path, dpi=300) # %% ''' demonstrate the typical distributions the contention is that low values preclude mixed-uses but high values don't guarantee them the spike in the distributions is an artefact of search distances vis-a-vis straight roads with no intersections, in which case a certain number of adjacent nodes is reached, resulting in a stable preponderance of a certain value this would not be present in gravity-weighted forms of centrality
(409352, 425145, 'Halifax'), (497561, 371383, 'Lincoln'), (635633, 170829, 'Margate'), (439743, 557041, 'Sunderland'), (623184, 308525, 'Norwich'), (353876, 429315, 'Preston'), (330891, 436268, 'Blackpool') ]: x_extents = (x - 2500, x + 2500) y_extents = (y - 5000, y + 5000) util_funcs.plt_setup() fig, ax = plt.subplots(1, 1, figsize=(8, 8)) mappable = plot_funcs.plot_scatter( ax, df_20.x, df_20.y, # color range is from 0.0 to 1.0 # don't manipulate in this context c=m2_pred_nt, cmap=util_funcs.cityseer_cmap(), s=1, x_extents=x_extents, y_extents=y_extents, relative_extents=False) ax.set_axis_off() divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='2%', pad=0.05) plt.colorbar(mappable, cax=cax, aspect=80) plt.suptitle( f'{town_name} - 20m resolution "New Town" predicted probabilities') path = f'../phd-doc/doc/images/predictive/nt_hyperlocal_pred_{town_name}_{m2.theme}_{clf.theme}.pdf' plt.savefig(path, dpi=300) # %%
np.nanmax(y_pred), abs(np.nanmin(y_all_idx)), np.nanmax(y_all_idx) ]) vmin, vmax = 0, mm cmap = LinearSegmentedColormap.from_list( 'reds', ['#ffffff', '#ff6659', '#d32f2f']) pow = 1.25 c = vals**pow s = 0.01 + (vals**pow / vmax * 1.5) im = plot_funcs.plot_scatter(fig, ax, df_20.x[select_idx], df_20.y[select_idx], c=c, s=s, vmin=vmin, vmax=vmax, cmap=cmap, centre=centre, km_per_inch=km_per_inch) ax.set_xlabel(title) cbar = fig.colorbar(im, ax=ax, aspect=100, pad=0.05) plt.suptitle( f'Observed, predicted, differenced eateries at 400m threshold. Test set accuracy: {y_score_r2:.1%} r2' ) path = f'../phd-doc/doc/images/predictive/eatery_predictions_400_{reg.theme}.pdf' plt.savefig(path, dpi=300) # %% util_funcs.plt_setup()
cluster_assign_VaDE = np.argmax(cluster_probs_VaDE, axis=1) # %% ''' Overall cluster max plot ''' util_funcs.plt_setup() fig, ax = plt.subplots(1, 1, figsize=(7, 7)) sample_colours, sample_sizes, mu_means, mu_means_cols, sorted_cluster_idx = \ plot_funcs.map_diversity_colour(X_raw, cluster_assign_VaDE, n_components=n_components) plot_funcs.plot_scatter(fig, ax, table.x, table.y, c=sample_colours, s=sample_sizes, km_per_inch=3.25, rasterized=True) # custom legend mu_means_sorted = mu_means[sorted_cluster_idx] mu_means_cols_sorted = mu_means_cols[sorted_cluster_idx] legend_elements = [] for cluster_idx, mu_mean, mu_mean_col in zip(sorted_cluster_idx, mu_means_sorted, mu_means_cols_sorted): legend_elements.append( Patch(facecolor=mu_mean_col, edgecolor='w', label=f'#{cluster_idx + 1}: {mu_mean:.2f}')) leg = ax.legend(handles=legend_elements,
test_indices=test_idx) trainer.train() else: vade.load_weights(str(dir_path / 'weights')) # %% classify cluster_probs_VaDE = vade.classify(X_trans).numpy() cluster_assign_VaDE = np.argmax(cluster_probs_VaDE, axis=1) # %% plot cluster matrix plot_funcs.plot_prob_clusters( X_raw, cluster_probs_VaDE, n_components, f'{vade.theme}_d{latent_dim}_e{epochs}_comp{n_components}_clust_ind', table.x, table.y) # plot combined map util_funcs.plt_setup() fig, ax = plt.subplots(1, 1, figsize=(12, 8)) colours, sizes = plot_funcs.map_diversity_colour(X_raw, cluster_assign_VaDE, n_components=n_components) plot_funcs.plot_scatter(ax, table.x, table.y, c=colours, s=sizes, x_extents=(-7000, 11500), y_extents=(-4500, 7500), rasterized=True) path = f'../phd-doc/doc/images/signatures/{vade.theme}_d{latent_dim}_e{epochs}_comp{n_components}_clust_comb.pdf' plt.savefig(path, dpi=300)
plot latent maps ''' # plot util_funcs.plt_setup() fig, axes = plt.subplots(2, 6, figsize=(7, 9)) for ax_row, inverse in zip(axes, [False, True]): L = np.copy(Z_mu) if inverse: L *= -1 for latent_idx in range(latent_dim): ax = ax_row[latent_idx] plot_funcs.plot_scatter(fig, ax, table.x, table.y, vals=L[:, latent_idx], s_min=0, s_max=1, c_exp=2, s_exp=3, rasterized=True) if not inverse: ax.set_xlabel(f'#{latent_idx + 1} - ' + r'$D_{KL}=' + f'{kl_vec[latent_idx]:.2f}$') else: ax.set_xlabel(f'#{latent_idx + 1} - Inverse') plt.suptitle( r"Geographic mappings (and inverses) for the latents of the VAE model") path = f'../phd-doc/doc/images/signatures/latents_map_inv_{beta}_{str(cap).replace(".", "_")}.pdf' plt.savefig(path, dpi=300) # %%