def plot_random_fields(): width = 32 height = 32 x = np.arange(width) y = np.arange(height) x_grid, y_grid = np.meshgrid(x, y) test_distances = np.arange(1, 30) distances = distance_matrix(x_grid, y_grid) length_scales = np.array([2, 16]) rand_gen = random_field_generator(x_grid, y_grid, length_scales, spatial_pattern="blended") rand_fields = [next(rand_gen) for x in range(25)] for rand_field in rand_fields: print(rand_field.std(), rand_field.mean()) covariances = np.array([spatial_covariance(distances, rand_field, test_distances, 0.5) for rand_field in rand_fields]) plt.figure(figsize=(6, 4)) # plt.fill_between(test_distances, covariances.max(axis=0), covariances.min(axis=0), color='red', alpha=0.2) for cov_inst in covariances: plt.plot(test_distances, cov_inst, color='pink', marker='o', ls='-') plt.plot(test_distances, covariances.mean(axis=0), 'ro-') plt.plot(test_distances, exp_kernel(test_distances, length_scales[0]), 'bo-') fig, axes = plt.subplots(5, 5, figsize=(9, 9)) plt.subplots_adjust(wspace=0,hspace=0) axef = axes.ravel() for a, ax in enumerate(axef): ax.contourf(rand_fields[a], np.linspace(-4, 4, 20), cmap="RdBu_r", extend="both") ax.tick_params(axis='both', which='both', bottom='off', top='off', right='off', left='off', labelleft='off', labelbottom='off') plt.show() return
def generate_random_fields(set_size, data_width, length_scale_str): length_scale_list = length_scale_str.split(";") spatial_pattern = length_scale_list[0] length_scales = [float(v) for v in length_scale_list[1:]] x = np.arange(data_width) y = np.arange(data_width) x_grid, y_grid = np.meshgrid(x, y) rand_gen = random_field_generator(x_grid, y_grid, length_scales, spatial_pattern=spatial_pattern) data = np.stack([next(rand_gen) for i in range(set_size)], axis=0) return data
def eval_full_gan_config(gan_index, length_scale_str, epochs, gan_path, data_width, eval_distances, tolerance, batch_size=256): """ Evaluate GAN configurations with the full configuration Args: gan_index: epochs: gan_path: data_width: eval_distances: tolerance: Returns: """ gan_mean_cols = ["cov_mean_{0:02d}".format(x) for x in eval_distances] gan_sd_cols = ["cov_sd_{0:02d}".format(x) for x in eval_distances] gan_t_cols = ["cov_t_{0:02d}".format(x) for x in eval_distances] gan_stat_cols = ["Index", "Epoch" ] + gan_mean_cols + gan_sd_cols + gan_t_cols + [ "mean_tscore", "max_tscore" ] gan_stats = pd.DataFrame(np.ones( (len(epochs), len(gan_stat_cols))) * np.nan, columns=gan_stat_cols, index=np.arange(len(epochs))) x = np.arange(data_width) y = np.arange(data_width) x_g, y_g = np.meshgrid(x, y) distances = distance_matrix(x_g, y_g) length_scale_list = length_scale_str.split(";") spatial_pattern = length_scale_list[0] length_scales = [float(v) for v in length_scale_list[1:]] corr = exp_kernel(distances, length_scales[0]) cho = cholesky(corr, lower=True) cho_inv = np.linalg.inv(cho) rand_covs = np.zeros((batch_size, eval_distances.size)) random_gen = random_field_generator(x_g, y_g, length_scales[0]) random_fields = np.stack([next(random_gen) for x in range(batch_size)], axis=0)[:, :, :, 0] random_noise = np.stack([ np.matmul(cho_inv, random_field.reshape(random_field.size, 1)).reshape( random_fields.shape[1:]) for random_field in random_fields ], axis=0) noise_cov = np.zeros((batch_size, eval_distances.size)) for p, patch in enumerate(random_noise): noise_cov[p] = spatial_covariance(distances, patch, eval_distances, tolerance) noise_mean = noise_cov.mean(axis=0) noise_sd = noise_cov.std(axis=0) for e, epoch in enumerate(epochs): gan_patch_file = join( gan_path, "gan_gen_patches_{0:04d}_epoch_{1:04d}.nc".format( gan_index, epoch)) if exists(gan_patch_file): gan_patch_ds = Dataset(gan_patch_file) gan_patches = np.array(gan_patch_ds.variables["gen_patch"][:, :, :, 0]) gan_patch_ds.close() for p, patch in enumerate(gan_patches): rand_patch = np.matmul(cho_inv, patch.reshape(patch.size, 1)).reshape(patch.shape) rand_covs[p] = spatial_covariance(distances, rand_patch, eval_distances, tolerance) gan_stats.loc[e, "Index"] = gan_index gan_stats.loc[e, "Epoch"] = epoch gan_stats.loc[e, gan_mean_cols] = rand_covs.mean(axis=0) gan_stats.loc[e, gan_sd_cols] = rand_covs.std(axis=0) gan_stats.loc[e, gan_t_cols] = ttest_ind_from_stats( gan_stats.loc[e, gan_mean_cols].values, gan_stats.loc[e, gan_sd_cols].values, np.ones(len(eval_distances) * batch_size), noise_mean, noise_sd, np.ones(len(eval_distances) * batch_size), equal_var=False)[0] gan_stats.loc[e, "mean_tscore"] = np.abs( gan_stats.loc[e, gan_t_cols]).mean() gan_stats.loc[e, "max_tscore"] = np.abs( gan_stats.loc[e, gan_t_cols]).max() return gan_stats