def plot_all_traces(samp, data=None, prior_func=None): """ Call prior_func(name, data, x) to get plot data. """ N = len(samp) fig, axes = plt.subplots(nrows=N, ncols=N, figsize=(N*1.5, N*1.5)) percent_ticks = [0.1, 0.5, 0.9] for i, (yname, y) in enumerate(samp.items()): ymin, ymax = range_from_pts(y) yticklabels = [ymin+ p*(ymax - ymin) for p in percent_ticks] for j, (xname, x) in enumerate(samp.items()): xmin, xmax = range_from_pts(x) xticklabels = [xmin + p*(xmax - xmin) for p in percent_ticks] ax = axes[i][j] if i != j: if i < j: ax.plot(x, y, '.', markersize=4, alpha=0.1) else: sns.kdeplot(x, y, ax=ax) ax.set_xlim(xmin, xmax) ax.set_ylim(ymin, ymax) ax.set_yticklabels(['']) ax.set_xticklabels(['']) namey = AnchoredText(yname, 2, frameon=False) namex = AnchoredText(xname, 4, frameon=False) ax.add_artist(namey) ax.add_artist(namex) else: sns.kdeplot(x, ax=ax) if prior_func is not None and data is not None: line = ax.get_lines()[0] xdata = line.get_xdata() ydata = prior_func(xname, data, xdata) if ydata is not None: ax.plot(xdata, ydata) ax.set_yticklabels(['']) ax.set_xticklabels(['']) ax.set_xlim(xmin, xmax) name = AnchoredText(xname,2, frameon=False) ax.add_artist(name) # Add axis labels, limits to left, bottom plots. if j == 0: ax.set_ylabel(yname) ylim = ax.get_ylim() yticks = [ylim[0]+ p*(ylim[1] - ylim[0]) for p in percent_ticks] ax.set_yticks(yticks) ax.set_yticklabels(["{:.3g}".format(num) for num in yticklabels], fontsize=8) if i == len(samp.items())-1: ax.set_xlabel(xname) xlim = ax.get_xlim() xticks = [xlim[0]+ p*(xlim[1] - xlim[0]) for p in percent_ticks] ax.set_xticks(xticks) ax.set_xticklabels(["{:.3g}".format(num) for num in xticklabels], fontsize=8) fig.tight_layout() return fig, axes
def _update_plot(self, axis, view): if self.joint: self.style.pop('cmap', None) self.handles['fig'] = sns.jointplot(view.data[:,0], view.data[:,1], **self.style).fig else: label = view.label if self.overlaid == 1 else '' sns.kdeplot(view.data, ax=axis, label=label, zorder=self.zorder, **self.style)
def kdeplot(x, color='w', **kwargs): global row_count, col_count if color != 'w': color = sns.light_palette( color, n_colors=len(data) + 1)[row_count + 1] sns.kdeplot(x, color=color, **kwargs) col_count = (col_count + 1) % n_feat if col_count == 0: row_count = (row_count + 1) % len(data)
def _update_plot(self, axis, view): if self.joint: self.style.pop('cmap', None) self.handles['fig'] = sns.jointplot(view.data[:,0], view.data[:,1], **self.style).fig else: kwargs = self.style[self.cyclic_index] label = view.label if self.overlaid >= 1 else '' if label: kwargs['label'] = label sns.kdeplot(view.data, ax=axis, zorder=self.zorder, **kwargs)
def init_artists(self, ax, plot_data, plot_kwargs): if self.joint: if self.joint and self.subplot: raise Exception("Joint plots can't be animated or laid out in a grid.") return {'fig': sns.jointplot(*plot_data, **plot_kwargs).fig} else: return {'axis': sns.kdeplot(*plot_data, ax=ax, **plot_kwargs)}
def plot_score_comparison(experiment, reference_vals=None, show_score=True): target_vals = experiment.observations.get_kinematic_dict(trim_endzones=True) if show_score: if experiment.is_scored: score, score_components = experiment.score, experiment.score_components else: from roboskeeter.math.scoring import scoring print "condition {}".format(experiment.experiment_conditions['condition']) scorer = scoring.Scoring(reference_data=None, condition=experiment.experiment_conditions['condition']) # None loads default ref data score, score_components = scorer.calc_score(experiment) if reference_vals is None: # get reference data if None is given to func. if anything string is passed instead, we won't plot reference data def load_reference_vals(condition): from roboskeeter.experiments import load_experiment reference_experiment = load_experiment([condition]) return reference_experiment.observations.get_kinematic_dict(trim_endzones=True) reference_vals = load_reference_vals(experiment.experiment_conditions['condition']) else: reference_vals=reference_vals titleappend, _, _ = get_agent_info(experiment) for kinematic, targ_val in target_vals.iteritems(): sns.kdeplot(data=targ_val, color='blue', label='simulation', lw=3, shade=True, cut=0) try: ref_val = reference_vals[kinematic] sns.kdeplot(data=ref_val, color='red', label='experiment', lw=3, shade=True, cut=0) plt.legend() # only show legend if plotting both distributions except TypeError: # no reference data to be plotted pass try: sns.plt.title("{}. distribution score = {}".format(kinematic, score_components[kinematic])) except TypeError: print score_components sns.plt.show()
zh_gmm = gmm(2, Y) ### estimated classes x1_true = X[np.where(z == 0)] x2_true = X[np.where(z == 1)] x1_energy = X[np.where(zh_energy == 0)] x2_energy = X[np.where(zh_energy == 1)] x1_kmeans = X[np.where(zh_kmeans == 0)] x2_kmeans = X[np.where(zh_kmeans == 1)] x1_gmm = X[np.where(zh_gmm == 0)] x2_gmm = X[np.where(zh_gmm == 1)] ### doing density estimation and ploting ax = sns.kdeplot(x1_true, shade=True, linewidth=1, linestyle='-', label=r"truth", color='k') sns.kdeplot(x2_true, shade=True, linewidth=1, linestyle='-', ax=ax, color='k') sns.kdeplot(x2_energy, shade=False, linewidth=1.5, ax=ax, linestyle='-', color='b', label=r"$\mathcal{E}^{1D}$") sns.kdeplot(x1_energy, shade=False, linewidth=1.5, ax=ax,
def plot_separately(data, best_configs, kde_num=500, labels=['a', 'b', 'c', 'd', 'e', 'f', 'g'], rounds_num=2, xranges=(4, 6)): if data == 'unimodal': dataloader = GausUniffMixture(n_mixture=1, mean_dist=5, sigma=0.1, unif_intsect=5, unif_ratio=3, device=device, seed=2020, extend_dim=False) else: dataloader = GausUniffMixture(n_mixture=8, mean_dist=10, sigma=2, unif_intsect=1.5, unif_ratio=1., device=device, seed=2020, extend_dim=False) real = dataloader.get_sample(eval_size) real_sample = real.cpu().data.numpy().squeeze() min_value, max_value = xranges step = (max_value - min_value) / eval_size real_range = np.arange(min_value, max_value, step) real_density = dataloader.density(real_range) model_path = os.path.join(curPath, 'wgan_results', data) # real = dataloader.get_sample(eval_size) # real_sample = real.cpu().data.numpy().squeeze() plt.cla() fig, axes = plt.subplots(figsize=(FIG_W, FIG_H * len(labels)), nrows=len(labels), ncols=1) ax = axes[0] ax.set_title(f'({labels[0]})', fontsize=FONTSIZE * 1.5) ax.set_facecolor('whitesmoke') ax.grid(True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) # real data ax.fill_between(real_range, 0, real_density, color='green', alpha=shade_alpha) ax.plot(real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position( [box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks(np.round(np.arange(start + jump, end, jump), rounds_num)) for i, test in enumerate( ['base', 'uniform', 'resnet', 'dropout', 'clr', 'all']): # Load wgan config = best_configs[test] path = model_path + '/' + test print('Start: ' + path) generator = torch.load(path + '/generator.pth', map_location=device) critic = torch.load(path + '/critic.pth', map_location=device) try: print( f'Parameters number: generator: {count_parameters(generator)}, critic: {count_parameters(critic)}' ) except AttributeError: print( 'Old version results: network state dict, try to overwrite files with model.' ) config['norm'] = 'batch' if config['batch_norm'] else None generator = Generator(input_size=config['prior_size'], n_hidden=config['n_hidden'], hidden_size=config['hidden_size'], activation_slope=config['activation_slope'], init_method=config['init_method'], activation_fn=config['activation_fn'], norm=config['norm'], res_block=config['residual_block'], dropout=config['dropout']).to( config['device']) critic = Critic(n_hidden=config['n_hidden'], hidden_size=config['hidden_size'], activation_slope=config['activation_slope'], init_method=config['init_method'], activation_fn=config['activation_fn'], norm=config['norm'], res_block=config['residual_block'], dropout=config['dropout'], spect_norm=config['spect_norm']).to( config['device']) generator.load_state_dict(torch.load(path + '/generator.pth')) critic.load_state_dict(torch.load(path + '/critic.pth')) torch.save(generator, path + '/generator.pth') torch.save(critic, path + '/critic.pth') print( f'Parameters number: generator: {count_parameters(generator)}, critic: {count_parameters(critic)}' ) generator.eval() critic.eval() prior = torch.randn if config['prior'] == 'uniform' else partial( torch.normal, mean=0., std=1.) z = prior(size=(eval_size, config['prior_size']), device=device) fake = generator(z) w_distance_est = critic(real).mean() - critic(fake).mean() w_distance_est = abs(round(w_distance_est.item(), 4)) w_distance_wgan = w_distance(real, fake) fake_wgan = fake.cpu().data.numpy().squeeze() ax = axes[i + 1] ax.set_title(f'({labels[i + 1]})', fontsize=FONTSIZE * 1.5) ax.set_facecolor('whitesmoke') ax.grid(True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) # wgan min_value, max_value = min(fake_wgan), max(fake_wgan) kde_width = kde_num * (max_value - min_value) / eval_size sns.kdeplot( fake_wgan, bw=kde_width, label=f'{test.upper()}:({w_distance_wgan},({w_distance_est}))', color='coral', shade=True, linewidth=12, alpha=shade_alpha, ax=ax) sns.kdeplot(fake_wgan, bw=kde_width, color='coral', shade=False, linewidth=12, ax=ax) # real data if data_line: ax.plot(real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position( [box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks( np.round(np.arange(start + jump, end, jump), rounds_num)) cur_img_path = os.path.join(model_path, data + '.jpg') print('Saving to: ' + cur_img_path) plt.savefig(cur_img_path) plt.close()
def plot_trace(data, label=None, window=1, ax=None, side_ax=None, color='beryl', alpha=0.8, legend=None, xlabel=None, ylabel=None, labelsize=14, rolling_kwargs=None): """ Plot trace of time-series data. Parameters ---------- data : array-like (nsamples, ) The samples. This should be a 1-D time-series array. label : str, optional Label for time-series. window : int, optional (default: 1) Size of the moving window. This is the number of observations used for calculating the average. ax : matplotlib axis, optional main matplotlib figure axis for trace. side_ax : matplotlib axis, optional side matplotlib figure axis for histogram. If you provide ax but not side_ax, we won't plot the histogram. If provide neither ax nor side_ax, we will set up the axes for you. If you supply side_ax but not ax, an error will be raised. color : str, optional (default: 'beryl') Style color of the trace. alpha : float, optional (default: 0.5) Opacity of shaded area. legend : bool, optional Whether to display legend in plot. Defaults to True if legend is provided, otherwise defaults to False. xlabel : str, optional x-axis label ylabel : str, optional y-axis label labelsize : int, optional (default: 14) Font side for axes labels. rolling_kwargs : dict, optional Keyword arguments for ``pandas.DataFrame.rolling``. Returns ------- ax : matplotlib axis main matplotlib figure axis for trace. side_ax : matplotlib axis side matplotlib figure axis for histogram. """ if ax is None: if side_ax is not None: raise ValueError("If you're supplying ax, you must also " "supply side_ax.") f, (ax, side_ax) = pp.subplots(1, 2, sharey=True, figsize=(20, 5), gridspec_kw={'width_ratios': [6, 1], 'wspace': 0.01}) if rolling_kwargs is None: rolling_kwargs = {} if legend is None: legend = (label is not None) df = (pd.DataFrame(data, columns=(label,)) .rolling(window, **rolling_kwargs) .mean()) df.plot(ax=ax, color=color, alpha=alpha, legend=legend) ax.tick_params(top='off', right='off') if xlabel: ax.set_xlabel(xlabel, size=labelsize) if ylabel: ax.set_ylabel(ylabel, size=labelsize) if side_ax is not None: df.hist(bins=30, normed=True, color=color, alpha=0.3, orientation='horizontal', ax=side_ax) sns.kdeplot(df[label], color=color, ax=side_ax, vertical=True) side_ax.tick_params(top='off', right='off') side_ax.xaxis.set_ticklabels([]) side_ax.legend([]) side_ax.set_title('') return ax, side_ax
def kdeplot(self, x=None, y=None, data=None, *args, **kwargs): """ Fit and plot a univariate or bivariate kernel density estimate Parameters ---------- x : a list of names of variable in data that need to visualize \ their distribution y : a list of names of variable in data that need to visualize \ its joint distribution against every x above data : pandas dataframe **kwargs : other arguments in seaborn.kdeplot shade : bool, optional vertical : bool, optional kernel : {'gau' | 'cos' | 'biw' | 'epa' | 'tri' | 'triw' } bw : {'scott' | 'silverman' | scalar | pair of scalars }, optional gridsize : int, optional cut : scalar, optional clip : pair of scalars, or pair of pair of scalars, optional legend : bool, optional cumulative : bool, optional shade_lowest : bool, optional cbar : bool, optional cbar_ax : matplotlib axes, optional cbar_kws : dict, optional Returns ------- figure : matplotlib figure with multiple axes References ---------- Seaborn kdeplot further documentation https://seaborn.pydata.org/generated/seaborn.kdeplot.html """ # check data if not isinstance(data, (pd.DataFrame)): raise ValueError('data must be pandas dataframe') # check x and y if x is None: raise ValueError('x can NOT be None') else: # x is NOT None if not isinstance(x, (list, tuple, np.ndarray, pd.Index)): x = [x] if not isinstance(y, (list, tuple, np.ndarray, pd.Index)): y = [y] # create fig and axes nrows = len(y) * len(x) plt.close() fig, axes = plt.subplots(nrows=nrows, ncols=1, sharex=self.sharex, figsize=(self.size[0], nrows * self.size[1])) # HACK: handle Axes indexing when only one ax in fig if nrows == 1: axes = [axes] # iterate thru x for i, col_y in enumerate(y): if col_y is not None: if col_y not in data.columns.values: raise ValueError('{} is NOT in data'.format(col_y)) b = data[col_y] b_not_nan = np.ones(b.shape[0], dtype=np.bool) if np.logical_not(np.isfinite(b)).any(): logger.warning('RUNTIME WARNING: {} column has inf or nan ' ''.format(col_y)) b = b.replace([-np.inf, np.inf], np.nan) # filter b_not_nan = np.logical_not(b.isnull()) elif col_y is None: b = None b_not_nan = np.ones(data.shape[0], dtype=np.bool) # get ax count axes_start_count = i * len(x) for j, col_x in enumerate(x): # get ax location ax_loc = axes_start_count + j # check if col in data if col_x not in data.columns.values: raise ValueError('{} is NOT in data'.format(col_x)) a = data[col_x] a_not_nan = np.ones(a.shape[0], dtype=np.bool) if np.logical_not(np.isfinite(a)).any(): logger.warning('RUNTIME WARNING: {} column has inf or ' 'nan'.format(col_x)) a = a.replace([-np.inf, np.inf], np.nan) # filter a_not_nan = np.logical_not(a.isnull()) # joint filter not_nan = b_not_nan & a_not_nan a = a[not_nan] if b is not None: b = b[not_nan] sns.kdeplot(data=a, data2=b, ax=axes[ax_loc], *args, **kwargs) if b is not None: axes[ax_loc].set_title( label='Joint Distribution of {} and {} ' ''.format(col_y, col_x), fontsize=self.title_fontsize) axes[ax_loc].set_xlabel(xlabel=col_x, fontsize=self.label_fontsize) axes[ax_loc].set_ylabel(ylabel=col_y, fontsize=self.label_fontsize) else: # b is None axes[ax_loc].set_title( label='Distribution of {}'.format(col_x), fontsize=self.title_fontsize) axes[ax_loc].set_xlabel(xlabel=col_x, fontsize=self.label_fontsize) axes[ax_loc].set_ylabel(ylabel='frequency', fontsize=self.label_fontsize) axes[ax_loc].tick_params(axis='both', which='maj', labelsize=self.tick_fontsize) axes[ax_loc].legend(loc='lower right') fig.subplots_adjust(wspace=0.5, hspace=0.3, left=0.125, right=0.9, top=0.9, bottom=0.1) fig.tight_layout() plt.show() return axes
flierprops = dict(marker='.', markerfacecolor='black', markersize=1, linestyle='none') whiskerprops = dict(linestyle='-', color=(0.3, 0.3, 0.3)) medianprops = dict(linestyle='-', linewidth=1.0, color=(0.3, 0.3, 0.3)) meanpointprops = dict(marker='D', markeredgecolor='black', markerfacecolor=(0, 3, 0.3, 0.3)) meanlineprops = dict(linestyle='-', linewidth=1.0, color='black') f = plt.figure(figsize=(4, 4)) ax = plt.subplot(111) kde = sns.kdeplot(errs[:, 1], errs[:, 0], ax=ax, shade=True, n_levels=8, cmap=colors, rug=True) ax.plot(np.clip(errs[:, 1], 0, 14), [-0.5] * len(errs[:, 1]), '|', color=(0, 0.0, 0.0, 0.005), clip_on=False) ax.plot([-0.5] * len(errs[:, 0]), errs[:, 0], '_', color=(0, 0.0, 0.0, 0.005), clip_on=False) #sns.rugplot(errs[::10,1], color=(0.1, 0.1, 0.1, 0.05), height=0.05, ax=ax) #sns.rugplot(errs[::10,0], color=(0.1, 0.1, 0.1, 0.05), height=0.05, vertical=True, ax=ax); #ax.figure.colorbar();
def train(model, dataloader, optimizer, args): start = time.time() running_loss = 0. DATA = dataloader.get_sample(args.total_datapoints) DATA = DATA.view(DATA.shape[0], -1) for i in range(1, args.niters + 1): model.train() if (i + 1) % args.adjust_step == 0: logger.info("Adjusting learning rate, divide lr by 2") for g in optimizer.param_groups: g['lr'] = g['lr'] / 2. x = dataloader.get_sample(args.batch_size) x = x.view(x.shape[0], -1).to(args.device) data, log_det, _ = model.forward(x, DATA, args.process_size) loss = flow_loss(data, log_det) running_loss += loss.item() optimizer.zero_grad() loss.backward() optimizer.step() if i % args.log_interval == 0: model.eval() with torch.no_grad(): # save model cur_state_path = os.path.join(model_path, str(i)) torch.save(model, cur_state_path + '.pth') real = dataloader.get_sample(args.eval_size) prior = model.base_dist.sample((args.eval_size, )) fake = model.sampling(DATA, prior, process_size=args.eval_size, sample_num=args.eval_size) w_distance_real = w_distance(real, fake) logger.info( f'Iter {i} / {args.niters}, Time {round(time.time() - start, 4)}, ' f'w_distance_real: {w_distance_real}, loss {round(running_loss / args.log_interval, 5)}' ) real_sample = real.cpu().data.numpy().squeeze() fake_sample = fake.cpu().data.numpy().squeeze() # plot. plt.cla() fig = plt.figure(figsize=(FIG_W, FIG_H)) ax = fig.add_subplot(111) ax.set_facecolor('whitesmoke') ax.grid(True, color='white', linewidth=2) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) ax.get_xaxis().tick_bottom() # _sample = np.concatenate([real_sample, fake_sample]) kde_num = 200 min_real, max_real = min(real_sample), max(real_sample) kde_width_real = kde_num * \ (max_real - min_real) / args.eval_size min_fake, max_fake = min(fake_sample), max(fake_sample) kde_width_fake = kde_num * \ (max_fake - min_fake) / args.eval_size sns.kdeplot(real_sample, bw=kde_width_real, label='Data', color='green', shade=True, linewidth=6) sns.kdeplot(fake_sample, bw=kde_width_fake, label='Model', color='orange', shade=True, linewidth=6) ax.set_title(f'True EM Distance: {w_distance_real}.', fontsize=FONTSIZE) ax.legend(loc=2, fontsize=FONTSIZE) ax.set_ylabel('Estimated Density by KDE', fontsize=FONTSIZE) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.5, direction='in') cur_img_path = os.path.join(image_path, str(i) + '.jpg') plt.tight_layout() plt.savefig(cur_img_path) plt.close() start = time.time() running_loss = 0
def plot_separately( data, wgan_config=None, kde_num=500, labels=[ 'a', 'b', 'c', 'd'], xranges=( 0, 100), yranges=( 0, 1), rounds_num=2): config = wgan_config if data == 'unimodal': dataloader = GausUniffMixture( n_mixture=1, mean_dist=5, sigma=0.1, unif_intsect=5, unif_ratio=3, device=device, seed=2020, extend_dim=False) else: dataloader = GausUniffMixture( n_mixture=8, mean_dist=10, sigma=2, unif_intsect=1.5, unif_ratio=1., device=device, seed=2020, extend_dim=False) real = dataloader.get_sample(eval_size) real_sample = real.cpu().data.numpy().squeeze() min_value, max_value = xranges step = (max_value - min_value) / eval_size real_range = np.arange(min_value, max_value, step) real_density = dataloader.density(real_range) model_path = os.path.join(curPath, 'model_results', data) # Load gaussianization_flow gaussianization_flow = torch.load( model_path + '/gaussianzation_flow.pth', map_location=device) gaussianization_flow.eval() # Load ffjord ffjord = torch.load(model_path + '/ffjord.pth', map_location=device) ffjord.eval() # Load wgan generator = torch.load(model_path + '/generator.pth', map_location=device) critic = torch.load(model_path + '/critic.pth', map_location=device) generator.eval() critic.eval() # gaussianization_flow args = gaus_flow_parser.parse_args() DATA = dataloader.get_sample(args.total_datapoints) DATA = DATA.view(DATA.shape[0], -1) prior = gaussianization_flow.base_dist.sample((eval_size,)).to(device) fake = gaussianization_flow.sampling( DATA, prior, process_size=eval_size, sample_num=eval_size) w_distance_gaussianization = w_distance(real, fake) fake_gaussianization = fake.cpu().data.numpy().squeeze() # ffjord sample_fn, density_fn = gu_ffjord.get_transforms(ffjord) z = torch.randn(eval_size, 1).type(torch.float32).to(device) zk, inds = [], torch.arange(0, z.shape[0]).to(torch.int64) for ii in torch.split(inds, int(10000)): zk.append(sample_fn(z[ii])) fake = torch.cat(zk, 0) w_distance_ffjord = w_distance(real, fake) fake_ffjord = fake.cpu().data.numpy().squeeze() # wgan prior = torch.randn if config['prior'] == 'uniform' else partial( torch.normal, mean=0., std=1.) z = prior(size=(eval_size, config['prior_size']), device=device) fake = generator(z) w_distance_est = critic(real).mean() - critic(fake).mean() w_distance_est = abs(round(w_distance_est.item(), 4)) w_distance_wgan = w_distance(real, fake) fake_wgan = fake.cpu().data.numpy().squeeze() plt.cla() fig = plt.figure( figsize=( FIG_W, FIG_H * 4) if vertical else ( FIG_W * 4, FIG_H)) ax = fig.add_subplot(411) if vertical else fig.add_subplot(141) ax.set_title(f'({labels[0]})', fontsize=FONTSIZE * 1.2) ax.set_facecolor('whitesmoke') ax.grid( True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) # real data ax.fill_between( real_range, 0, real_density, color='green', alpha=shade_alpha) ax.plot( real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks( np.round( np.arange( start + jump, end, jump), rounds_num)) # wgan ax = fig.add_subplot(412) if vertical else fig.add_subplot(142) ax.set_title(f'({labels[1]})', fontsize=FONTSIZE * 1.2) ax.set_facecolor('whitesmoke') ax.grid( True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) # wgan min_value, max_value = min(fake_wgan), max(fake_wgan) kde_width = kde_num * (max_value - min_value) / eval_size sns.kdeplot( fake_wgan, bw=kde_width, label=f'WGAN', color='coral', shade=True, linewidth=12, alpha=shade_alpha, ax=ax) sns.kdeplot( fake_wgan, bw=kde_width, color='coral', shade=False, linewidth=12, ax=ax) # real data if data_line: ax.plot( real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks( np.round( np.arange( start + jump, end, jump), rounds_num)) # ffjord ax = fig.add_subplot(413) if vertical else fig.add_subplot(143) ax.set_title(f'({labels[2]})', fontsize=FONTSIZE * 1.2) ax.set_facecolor('whitesmoke') ax.grid( True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) min_value, max_value = min(fake_ffjord), max(fake_ffjord) kde_width = kde_num * (max_value - min_value) / eval_size sns.kdeplot( fake_ffjord, bw=kde_width, label=f'FFJORD', color='coral', shade=True, linewidth=12, alpha=shade_alpha, ax=ax) sns.kdeplot( fake_ffjord, bw=kde_width, color='coral', shade=False, linewidth=12, ax=ax) # real data if data_line: ax.plot( real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks( np.round( np.arange( start + jump, end, jump), rounds_num)) # gaussianization_flow ax = fig.add_subplot(414) if vertical else fig.add_subplot(144) ax.set_title(f'({labels[3]})', fontsize=FONTSIZE * 1.2) ax.set_facecolor('whitesmoke') ax.grid( True, color='white', linewidth=3, linestyle='--', alpha=shade_alpha) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) min_value, max_value = min(fake_gaussianization), max(fake_gaussianization) kde_width = kde_num * (max_value - min_value) / eval_size sns.kdeplot( fake_gaussianization, bw=kde_width, label=f'Gaussianization Flows', color='coral', shade=True, linewidth=12, alpha=shade_alpha, ax=ax) sns.kdeplot( fake_gaussianization, bw=kde_width, color='coral', shade=False, linewidth=12, ax=ax) # real data if data_line: ax.plot( real_range, real_density, label='Data', color='darkgreen', linewidth=20) ax.tick_params(axis='x', labelsize=FONTSIZE * 0.7) ax.tick_params(axis='y', labelsize=FONTSIZE * 0.7, direction='in') box = ax.get_position() ax.set_position([box.x0, box.y0 + box.height * 0.1, box.width, box.height * 0.9]) ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05), fontsize=FONTSIZE * 0.7, fancybox=True, shadow=True, ncol=5) ax.set_xlim(xranges) ax.set_ylim(yranges) ax.set_ylabel('Density', fontsize=FONTSIZE * 0.7, labelpad=labelpad) start, end = yranges jump = (end - start) / (y_num + 1) ax.yaxis.set_ticks( np.round( np.arange( start + jump, end, jump), rounds_num)) cur_img_path = os.path.join(model_path, data + '.jpg') print('Saving to: ' + cur_img_path) plt.savefig(cur_img_path) plt.close()
#n_noslam, bins_noslam, patches_noslam = ax.hist(anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 12])), 50, normed=1, facecolor=(0.3, 0.3, 0.3)); #n_slam, bins_slam, patches_slam = ax.hist(anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 6])), 50, normed=1, facecolor=(0.7, 0.7, 1.0)); #ax.plot(anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 12])), '.', color=(0.3, 0.3, 0.3)); #ax.plot(anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 6])), '.', color=(0.7, 0.7, 1.0)); #ax.plot(data[:, i + 6], '-', color=(0.8, 0.8, 1.0)); #boxprops = dict(linestyle='-', linewidth=1, color=(0.3, 0.3, 0.3)) #flierprops = dict(marker='.', markerfacecolor='black', markersize=1, # linestyle='none') #whiskerprops = dict(linestyle='-', color=(0.3, 0.3,0.3)) #medianprops = dict(linestyle='-', linewidth=1.0, color=(0.3, 0.3, 0.3)) #meanpointprops = dict(marker='D', markeredgecolor='black', # markerfacecolor=(0,3, 0.3, 0.3)) #meanlineprops = dict(linestyle='-', linewidth=1.0, color='black') #ax.boxplot( [anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 12])), anglediff(rad2deg(data[:, i]), rad2deg(data[:, i + 6]))], labels=['No SLAM', 'With SLAM'], boxprops=boxprops, flierprops=flierprops, medianprops=medianprops, meanprops = meanlineprops, whiskerprops = whiskerprops) #ax.boxplot() sns.kdeplot(B, A, ax=ax, shade=True, n_levels=8, cmap=colors) ax.plot(numpy.clip(B[::10], 0, 12), [-0.5] * (len(B[::10])), '|', color=(0, 0.0, 0.0, 0.01), clip_on=False) ax.plot([-0.5] * (len(A[::10])), A[::10], '_', color=(0, 0.0, 0.0, 0.01), clip_on=False) #ax.plot(B, A, '.', color='blue', alpha=0.01) ax.plot(t, t, '--', color='black', linewidth=0.5) ax.set_xlabel('Joint ' + str(i + 1) + ' initial error') if (i == 0): ax.set_ylabel('Final error (degrees)')