def plot_coverage_feature_space(self, axes=(0, 1), nb_stddev=1.0, specific_neurons=None, alpha_ellipses=0.5, facecolor='rand', ax=None, lim_factor=1.0): ''' Show the features. Choose the 2 dimensions you want first. ''' if specific_neurons is None: specific_neurons = self._ALL_NEURONS ells = [Ellipse(xy=self.neurons_preferred_stimulus[m, axes], width=nb_stddev*utils.kappa_to_stddev(self.neurons_sigma[m, axes[0]]), height=nb_stddev*utils.kappa_to_stddev(self.neurons_sigma[m, axes[1]]), angle=-np.degrees(self.neurons_angle[m])) for m in specific_neurons if not self.mask_neurons_unset[m]] if ax is None: fig = plt.figure() ax = fig.add_subplot(111, aspect='equal') for e in ells: ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_alpha(alpha_ellipses) if facecolor is 'rand': e.set_facecolor(np.random.rand(3)) elif facecolor is False or facecolor is None or facecolor == 'none' or facecolor == 'None': e.set_facecolor('none') else: e.set_facecolor(facecolor) e.set_transform(ax.transData) # ax.autoscale_view() # ax.set_xlim(-1.4*np.pi, 1.3*np.pi) # ax.set_ylim(-1.4*np.pi, 1.3*np.pi) ax.set_xlim(-lim_factor*np.pi, lim_factor*np.pi) ax.set_ylim(-lim_factor*np.pi, lim_factor*np.pi) ax.set_xticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi)) ax.set_xticklabels((r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'), fontsize=17) ax.set_yticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi)) ax.set_yticklabels((r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'), fontsize=17) ax.set_xlabel('Orientation', fontsize=14) ax.set_ylabel('Color', fontsize=14) ax.set_title('%d vs %d' % (axes[0]+1, axes[1]+1)) fig.set_tight_layout(True) plt.draw() plt.show() return ax
def plot_neuron_activity_1d(self, neuron_index=0, axis_to_vary=0, fix_preferred_stim=True, fixed_stim=None, precision=100., normalise=True): ''' Plot the activity of a neuron along one dimension. Either provide the stimulus to fix, or let it be at its preferred stimulus. ''' feature_space = self.init_feature_space(precision=precision) activity = np.zeros(feature_space.shape) # Fix the rest of the stimulus if fix_preferred_stim: stimulus = self.neurons_preferred_stimulus[neuron_index].copy() elif fixed_stim is not None: stimulus = fixed_stim else: stimulus = np.zeros(self.R) # Compute the response. for i in xrange(feature_space.size): stimulus[axis_to_vary] = feature_space[i] activity[i] = self.get_neuron_response(neuron_index, stimulus) # Check the gaussian fit gaussian_approx = spst.norm.pdf( feature_space, self.neurons_preferred_stimulus[neuron_index, axis_to_vary], utils.kappa_to_stddev(self.neurons_sigma[neuron_index, axis_to_vary])) if normalise: activity /= np.max(activity) gaussian_approx /= np.max(gaussian_approx) # Plot everything plt.figure() plt.plot(feature_space, activity) plt.plot(feature_space, gaussian_approx) plt.legend(['Neuron activity', 'Gaussian approximation']) plt.show()
def plot_neuron_activity(self, neuron_index=0, nb_stddev=1., precision=100): ''' Plot the activity of one specific neuron over the whole input space. ''' coverage_1D = self.init_feature_space(precision) activity = self.get_neuron_activity(neuron_index, precision=precision) # Plot it f = plt.figure() ax = f.add_subplot(111) im = ax.imshow(activity.T, origin='lower') # im.set_extent((-np.pi, np.pi, -np.pi, np.pi)) im.set_extent((coverage_1D.min(), coverage_1D.max(), coverage_1D.min(), coverage_1D.max())) ax.set_ylabel('Color') ax.set_xlabel('Orientation') # im.set_interpolation('nearest') f.colorbar(im) # Plot the ellipse showing one standard deviation e = Ellipse( xy=self.neurons_preferred_stimulus[neuron_index], width=nb_stddev * utils.kappa_to_stddev( self.neurons_sigma[neuron_index, 0]), height=nb_stddev * utils.kappa_to_stddev( self.neurons_sigma[neuron_index, 1]), angle=-np.degrees(self.neurons_angle[neuron_index])) ax.add_artist(e) e.set_clip_box(ax.bbox) e.set_alpha(0.5) e.set_facecolor('white') e.set_transform(ax.transData) ax.set_xticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi)) ax.set_xticklabels( (r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'), fontsize=17) ax.set_yticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi)) ax.set_yticklabels( (r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'), fontsize=17) # Change mouse over behaviour def report_pixel(x_mouse, y_mouse): # Extract loglik at that position try: x_display = x_mouse y_display = y_mouse x_i = np.argmin((coverage_1D - x_display)**2.) y_i = np.argmin((coverage_1D - y_display)**2.) return "x=%.2f y=%.2f value=%.2f" % (x_display, y_display, activity[x_i, y_i]) except: return "" ax.format_coord = report_pixel plt.show()
samples_w = utils.wrap_angles(samples) x = np.linspace(-np.pi, np.pi, 10000) # KDE samples_kde = stmokde.KDEUnivariate(samples) samples_kde.fit() samples_w_kde = stmokde.KDEUnivariate(samples_w) samples_w_kde.fit() # Von Mises samples_vonmises = utils.fit_vonmises_samples(samples, num_points=300, return_fitted_data=True, should_plot=False) samples_w_vonmises = utils.fit_vonmises_samples(samples_w, num_points=300, return_fitted_data=True, should_plot=False) plt.figure() plt.hist(samples, bins=100, normed=True) plt.plot(samples_vonmises['support'], samples_vonmises['fitted_data'], 'r', linewidth=3) plt.plot(samples_kde.support, samples_kde.density, 'g', linewidth=3) plt.figure() plt.hist(samples_w, bins=100, normed=True) plt.plot(samples_vonmises['support'], samples_vonmises['fitted_data'], 'r', linewidth=3) plt.plot(samples_w_kde.support, samples_w_kde.density, 'g', linewidth=3) print 'Target std: %.2f, fitted kappa: %.3f, corresponding std: %.3f' % (std_target, samples_w_vonmises['parameters'][1], utils.kappa_to_stddev(samples_w_vonmises['parameters'][1])) plt.show()
def plots_specific_stimuli_hierarchical(data_pbs, generator_module=None): ''' Reload and plot behaviour of mixed population code on specific Stimuli of 3 items. ''' #### SETUP # savefigs = True savedata = True plot_per_min_dist_all = False specific_plots_paper = False plots_emfit_allitems = False plot_min_distance_effect = True should_fit_allitems_model = True # caching_emfit_filename = None caching_emfit_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_emfitallitems_uniquekappa.pickle') colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() result_all_precisions_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_all_precisions_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_em_fits_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_fits_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_kappastddev_mean = utils.nanmean(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_em_kappastddev_std = utils.nanstd(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_responses_all = np.squeeze(data_pbs.dict_arrays['result_responses']['results']) result_target_all = np.squeeze(data_pbs.dict_arrays['result_target']['results']) result_nontargets_all = np.squeeze(data_pbs.dict_arrays['result_nontargets']['results']) all_args = data_pbs.loaded_data['args_list'] nb_repetitions = np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']).shape[-1] print nb_repetitions nb_repetitions = result_responses_all.shape[-1] print nb_repetitions K = result_nontargets_all.shape[-2] N = result_responses_all.shape[-2] enforce_min_distance_space = data_pbs.loaded_data['parameters_uniques']['enforce_min_distance'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] MMlower_valid_space = data_pbs.loaded_data['datasets_list'][0]['MMlower_valid_space'] ratio_space = MMlower_valid_space[:, 0]/float(np.sum(MMlower_valid_space[0])) print enforce_min_distance_space print sigmax_space print MMlower_valid_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Relaod cached emfitallitems if caching_emfit_filename is not None: if os.path.exists(caching_emfit_filename): # Got file, open it and try to use its contents try: with open(caching_emfit_filename, 'r') as file_in: # Load and assign values cached_data = pickle.load(file_in) result_emfitallitems = cached_data['result_emfitallitems'] should_fit_allitems_model = False except IOError: print "Error while loading ", caching_emfit_filename, "falling back to computing the EM fits" if plot_per_min_dist_all: # Do one plot per min distance. for min_dist_i, min_dist in enumerate(enforce_min_distance_space): # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist) if savefigs: dataio.save_current_figure('precision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist, log_scale=True) if savefigs: dataio.save_current_figure('logprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated model precision (kappa) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 0].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='EM precision, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('logemprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated Target, nontarget and random mixture components, in multiple subplots _, axes = plt.subplots(1, 3, figsize=(18, 6)) plt.subplots_adjust(left=0.05, right=0.97, wspace = 0.3, bottom=0.15) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Target, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[0], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 2].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Nontarget, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[1], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 3].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Random, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[2], ticks_interpolate=5) if savefigs: dataio.save_current_figure('em_mixtureprobs_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot Log-likelihood of Mixture model, sanity check utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., -1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM loglik, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('em_loglik_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) if specific_plots_paper: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.09 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) ## Do for each distance # for min_dist_i in [min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]: for min_dist_i in xrange(enforce_min_distance_space.size): # Plot precision if False: utils.plot_mean_std_area(ratio_space, result_all_precisions_mean[min_dist_i, sigmax_level_i], result_all_precisions_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Precision of recall') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, np.max(result_all_precisions_mean[min_dist_i, sigmax_level_i] + result_all_precisions_std[min_dist_i, sigmax_level_i])]) if savefigs: dataio.save_current_figure('mindist%.2f_precisionrecall_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa fitted ax_handle = utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0], result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa') # Add distance between items in kappa units dist_items_kappa = utils.stddev_to_kappa(enforce_min_distance_space[min_dist_i]) ax_handle.plot(ratio_space, dist_items_kappa*np.ones(ratio_space.size), 'k--', linewidth=3) plt.ylim([-0.1, np.max((np.max(result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0] + result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]), 1.1*dist_items_kappa))]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappa_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa-stddev fitted. Easier to visualize ax_handle = utils.plot_mean_std_area(ratio_space, result_em_kappastddev_mean[min_dist_i, sigmax_level_i], result_em_kappastddev_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa_stddev') # Add distance between items in std dev units dist_items_std = (enforce_min_distance_space[min_dist_i]) ax_handle.plot(ratio_space, dist_items_std*np.ones(ratio_space.size), 'k--', linewidth=3) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, 1.1*np.max((np.max(result_em_kappastddev_mean[min_dist_i, sigmax_level_i] + result_em_kappastddev_std[min_dist_i, sigmax_level_i]), dist_items_std))]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappastddev_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if False: # Plot LLH utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, -1], result_em_fits_std[min_dist_i, sigmax_level_i, :, -1]) #, xlabel='Ratio conjunctivity', ylabel='Loglikelihood of Mixture model fit') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emllh_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot mixture parameters, std utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Mixture parameters, SEM utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T/np.sqrt(nb_repetitions)) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_sem_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if plots_emfit_allitems: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) min_dist_i_plotting_space = np.array([min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]) if should_fit_allitems_model: # kappa, mixt_target, mixt_nontargets (K), mixt_random, LL, bic # result_emfitallitems = np.empty((min_dist_i_plotting_space.size, ratio_space.size, 2*K+5))*np.nan result_emfitallitems = np.empty((enforce_min_distance_space.size, ratio_space.size, K+5))*np.nan ## Do for each distance # for min_dist_plotting_i, min_dist_i in enumerate(min_dist_i_plotting_space): for min_dist_i in xrange(enforce_min_distance_space.size): # Fit the mixture model for ratio_i, ratio in enumerate(ratio_space): print "Refitting EM all items. Ratio:", ratio, "Dist:", enforce_min_distance_space[min_dist_i] em_fit = em_circularmixture_allitems_uniquekappa.fit( result_responses_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_target_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_nontargets_all[min_dist_i, sigmax_level_i, ratio_i].transpose((0, 2, 1)).reshape((N*nb_repetitions, K))) result_emfitallitems[min_dist_i, ratio_i] = [em_fit['kappa'], em_fit['mixt_target']] + em_fit['mixt_nontargets'].tolist() + [em_fit[key] for key in ('mixt_random', 'train_LL', 'bic')] # Save everything to a file, for faster later plotting if caching_emfit_filename is not None: try: with open(caching_emfit_filename, 'w') as filecache_out: data_em = dict(result_emfitallitems=result_emfitallitems) pickle.dump(data_em, filecache_out, protocol=2) except IOError: print "Error writing out to caching file ", caching_emfit_filename ## Plots now, for each distance! # for min_dist_plotting_i, min_dist_i in enumerate(min_dist_i_plotting_space): for min_dist_i in xrange(enforce_min_distance_space.size): # Plot now _, ax = plt.subplots() ax.plot(ratio_space, result_emfitallitems[min_dist_i, :, 1:5], linewidth=3) plt.ylim([0.0, 1.1]) plt.legend(['Target', 'Nontarget 1', 'Nontarget 2', 'Random'], loc='upper left') if savefigs: dataio.save_current_figure('mindist%.2f_emprobsfullitems_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if plot_min_distance_effect: conj_receptive_field_size = 2.*np.pi/((all_args[0]['M']*ratio_space)**0.5) target_vs_nontargets_mindist_ratio = result_emfitallitems[..., 1]/np.sum(result_emfitallitems[..., 1:4], axis=-1) nontargetsmean_vs_targnontarg_mindist_ratio = np.mean(result_emfitallitems[..., 2:4]/np.sum(result_emfitallitems[..., 1:4], axis=-1)[..., np.newaxis], axis=-1) for ratio_conj_i, ratio_conj in enumerate(ratio_space): # Do one plot per ratio, putting the receptive field size on each f, ax = plt.subplots() ax.plot(enforce_min_distance_space[1:], target_vs_nontargets_mindist_ratio[1:, ratio_conj_i], linewidth=3, label='target mixture') ax.plot(enforce_min_distance_space[1:], nontargetsmean_vs_targnontarg_mindist_ratio[1:, ratio_conj_i], linewidth=3, label='non-target mixture') # ax.plot(enforce_min_distance_space[1:], result_emfitallitems[1:, ratio_conj_i, 1:5], linewidth=3) ax.axvline(x=conj_receptive_field_size[ratio_conj_i]/2., color='k', linestyle='--', linewidth=2) ax.axvline(x=conj_receptive_field_size[ratio_conj_i]*2., color='r', linestyle='--', linewidth=2) plt.legend(loc='upper left') plt.grid() # ax.set_xlabel('Stimuli separation') # ax.set_ylabel('Ratio Target to Non-targets') plt.axis('tight') ax.set_ylim([0.0, 1.0]) ax.set_xlim([enforce_min_distance_space[1:].min(), enforce_min_distance_space[1:].max()]) if savefigs: dataio.save_current_figure('ratio%.2f_mindistpred_ratiotargetnontarget_{label}_{unique_id}.pdf' % ratio_conj) variables_to_save = ['nb_repetitions'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='specific_stimuli') plt.show() return locals()
def plots_specific_stimuli_mixed(data_pbs, generator_module=None): ''' Reload and plot behaviour of mixed population code on specific Stimuli of 3 items. ''' #### SETUP # savefigs = True savedata = True plot_per_min_dist_all = False specific_plots_paper = False specific_plots_emfits = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() result_all_precisions_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_all_precisions_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_em_fits_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_fits_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_kappastddev_mean = utils.nanmean(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_em_kappastddev_std = utils.nanstd(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_responses_all = np.squeeze(data_pbs.dict_arrays['result_responses']['results']) result_target_all = np.squeeze(data_pbs.dict_arrays['result_target']['results']) result_nontargets_all = np.squeeze(data_pbs.dict_arrays['result_nontargets']['results']) nb_repetitions = result_responses_all.shape[-1] K = result_nontargets_all.shape[-2] N = result_responses_all.shape[-2] enforce_min_distance_space = data_pbs.loaded_data['parameters_uniques']['enforce_min_distance'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] ratio_space = data_pbs.loaded_data['datasets_list'][0]['ratio_space'] print enforce_min_distance_space print sigmax_space print ratio_space print result_all_precisions_mean.shape, result_em_fits_mean.shape print result_responses_all.shape dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) if plot_per_min_dist_all: # Do one plot per min distance. for min_dist_i, min_dist in enumerate(enforce_min_distance_space): # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist) if savefigs: dataio.save_current_figure('precision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist, log_scale=True) if savefigs: dataio.save_current_figure('logprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated model precision utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 0].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM precision, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('logemprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated Target, nontarget and random mixture components, in multiple subplots _, axes = plt.subplots(1, 3, figsize=(18, 6)) plt.subplots_adjust(left=0.05, right=0.97, wspace = 0.3, bottom=0.15) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Target, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[0], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 2].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Nontarget, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[1], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 3].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Random, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[2], ticks_interpolate=5) if savefigs: dataio.save_current_figure('em_mixtureprobs_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot Log-likelihood of Mixture model, sanity check utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., -1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM loglik, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('em_loglik_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) if specific_plots_paper: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) ## Do for each distance for min_dist_i in [min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]: # for min_dist_i in xrange(enforce_min_distance_space.size): # Plot precision utils.plot_mean_std_area(ratio_space, result_all_precisions_mean[min_dist_i, sigmax_level_i], result_all_precisions_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Precision of recall') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, np.max(result_all_precisions_mean[min_dist_i, sigmax_level_i] + result_all_precisions_std[min_dist_i, sigmax_level_i])]) if savefigs: dataio.save_current_figure('mindist%.2f_precisionrecall_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa fitted utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0], result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa') plt.ylim([-0.1, np.max(result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0] + result_em_fits_std[min_dist_i, sigmax_level_i, :, 0])]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappa_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa-stddev fitted. Easier to visualize utils.plot_mean_std_area(ratio_space, result_em_kappastddev_mean[min_dist_i, sigmax_level_i], result_em_kappastddev_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa_stddev') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, 1.1*np.max(result_em_kappastddev_mean[min_dist_i, sigmax_level_i] + result_em_kappastddev_std[min_dist_i, sigmax_level_i])]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappastddev_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot LLH utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, -1], result_em_fits_std[min_dist_i, sigmax_level_i, :, -1]) #, xlabel='Ratio conjunctivity', ylabel='Loglikelihood of Mixture model fit') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emllh_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot mixture parameters, std utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Mixture parameters, SEM utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T/np.sqrt(nb_repetitions)) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_sem_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if specific_plots_emfits: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) min_dist_i_plotting_space = np.array([min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]) # kappa (K+1), mixt_target, mixt_nontargets (K), mixt_random, LL, bic result_emfitallitems = np.empty((min_dist_i_plotting_space.size, ratio_space.size, 2*K+5))*np.nan ## Do for each distance for min_dist_plotting_i, min_dist_i in enumerate(min_dist_i_plotting_space): # Fit the mixture model for ratio_i, ratio in enumerate(ratio_space): print "Refitting EM all items. Ratio:", ratio, "Dist:", enforce_min_distance_space[min_dist_i] em_fit = em_circularmixture_allitems.fit( result_responses_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_target_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_nontargets_all[min_dist_i, sigmax_level_i, ratio_i].transpose((0, 2, 1)).reshape((N*nb_repetitions, K))) result_emfitallitems[min_dist_plotting_i, ratio_i] = em_fit['kappa'].tolist() + [em_fit['mixt_target']] + em_fit['mixt_nontargets'].tolist() + [em_fit[key] for key in ('mixt_random', 'train_LL', 'bic')] # Plot now _, ax = plt.subplots() ax.plot(ratio_space, result_emfitallitems[min_dist_plotting_i, :, 3:7]) if savefigs: dataio.save_current_figure('mindist%.2f_emprobsfullitems_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['nb_repetitions'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='specific_stimuli') plt.show() return locals()
def plots_specific_stimuli_hierarchical(data_pbs, generator_module=None): ''' Reload and plot behaviour of mixed population code on specific Stimuli of 3 items. ''' #### SETUP # savefigs = True savedata = True plot_per_min_dist_all = True specific_plots_paper = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() result_all_precisions_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_all_precisions_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_em_fits_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_fits_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_kappastddev_mean = utils.nanmean(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_em_kappastddev_std = utils.nanstd(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) nb_repetitions = np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']).shape[-1] enforce_min_distance_space = data_pbs.loaded_data['parameters_uniques']['enforce_min_distance'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] MMlower_valid_space = data_pbs.loaded_data['datasets_list'][0]['MMlower_valid_space'] ratio_space = MMlower_valid_space[:, 0]/float(np.sum(MMlower_valid_space[0])) print enforce_min_distance_space print sigmax_space print MMlower_valid_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) if plot_per_min_dist_all: # Do one plot per min distance. for min_dist_i, min_dist in enumerate(enforce_min_distance_space): # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist) if savefigs: dataio.save_current_figure('precision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist, log_scale=True) if savefigs: dataio.save_current_figure('logprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated model precision utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 0].T, x=ratio_space, y=sigmax_space, xlabel='ratio layer two', ylabel='sigma_x', title='EM precision, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('logemprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated Target, nontarget and random mixture components, in multiple subplots _, axes = plt.subplots(1, 3, figsize=(18, 6)) plt.subplots_adjust(left=0.05, right=0.97, wspace = 0.3, bottom=0.15) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Target, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[0], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 2].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Nontarget, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[1], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 3].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Random, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[2], ticks_interpolate=5) if savefigs: dataio.save_current_figure('em_mixtureprobs_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot Log-likelihood of Mixture model, sanity check utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., -1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM loglik, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('em_loglik_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) if specific_plots_paper: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.09 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) ## Do for each distance # for min_dist_i in [min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]: for min_dist_i in xrange(enforce_min_distance_space.size): # Plot precision utils.plot_mean_std_area(ratio_space, result_all_precisions_mean[min_dist_i, sigmax_level_i], result_all_precisions_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Precision of recall') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_precisionrecall_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa fitted utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0], result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappa_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa-stddev fitted. Easier to visualize utils.plot_mean_std_area(ratio_space, result_em_kappastddev_mean[min_dist_i, sigmax_level_i], result_em_kappastddev_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa_stddev') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappastddev_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot LLH utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, -1], result_em_fits_std[min_dist_i, sigmax_level_i, :, -1]) #, xlabel='Ratio conjunctivity', ylabel='Loglikelihood of Mixture model fit') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emllh_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot mixture parameters utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T) plt.ylim([0.0, 1.1]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot mixture parameters, SEM utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T/np.sqrt(nb_repetitions)) plt.ylim([0.0, 1.1]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_sem_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['result_all_precisions_mean', 'result_em_fits_mean', 'result_all_precisions_std', 'result_em_fits_std', 'result_em_kappastddev_mean', 'result_em_kappastddev_std', 'enforce_min_distance_space', 'sigmax_space', 'ratio_space', 'all_args'] if savedata: dataio.save_variables(variables_to_save, locals()) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='specific_stimuli') plt.show() return locals()
def plots_specific_stimuli_mixed(data_pbs, generator_module=None): ''' Reload and plot behaviour of mixed population code on specific Stimuli of 3 items. ''' #### SETUP # savefigs = True savedata = True plot_per_min_dist_all = False specific_plots_paper = False plots_emfit_allitems = False plot_min_distance_effect = True compute_bootstraps = False should_fit_allitems_model = True # caching_emfit_filename = None mixturemodel_to_use = 'allitems_uniquekappa' # caching_emfit_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_emfitallitems_uniquekappa.pickle') # mixturemodel_to_use = 'allitems_fikappa' caching_emfit_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_emfit%s.pickle' % mixturemodel_to_use) compute_fisher_info_perratioconj = True caching_fisherinfo_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_fisherinfo.pickle') colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() all_args = data_pbs.loaded_data['args_list'] result_all_precisions_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_all_precisions_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_all_precisions']['results']), axis=-1) result_em_fits_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_fits_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results']), axis=-1) result_em_kappastddev_mean = utils.nanmean(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_em_kappastddev_std = utils.nanstd(utils.kappa_to_stddev(np.squeeze(data_pbs.dict_arrays['result_em_fits']['results'])[..., 0, :]), axis=-1) result_responses_all = np.squeeze(data_pbs.dict_arrays['result_responses']['results']) result_target_all = np.squeeze(data_pbs.dict_arrays['result_target']['results']) result_nontargets_all = np.squeeze(data_pbs.dict_arrays['result_nontargets']['results']) nb_repetitions = result_responses_all.shape[-1] K = result_nontargets_all.shape[-2] N = result_responses_all.shape[-2] enforce_min_distance_space = data_pbs.loaded_data['parameters_uniques']['enforce_min_distance'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] ratio_space = data_pbs.loaded_data['datasets_list'][0]['ratio_space'] print enforce_min_distance_space print sigmax_space print ratio_space print result_all_precisions_mean.shape, result_em_fits_mean.shape print result_responses_all.shape dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Reload cached emfitallitems if caching_emfit_filename is not None: if os.path.exists(caching_emfit_filename): # Got file, open it and try to use its contents try: with open(caching_emfit_filename, 'r') as file_in: # Load and assign values print "Reloader EM fits from cache", caching_emfit_filename cached_data = pickle.load(file_in) result_emfitallitems = cached_data['result_emfitallitems'] mixturemodel_used = cached_data.get('mixturemodel_used', '') if mixturemodel_used != mixturemodel_to_use: print "warning, reloaded model used a different mixture model class" should_fit_allitems_model = False except IOError: print "Error while loading ", caching_emfit_filename, "falling back to computing the EM fits" # Load the Fisher Info from cache if exists. If not, compute it. if caching_fisherinfo_filename is not None: if os.path.exists(caching_fisherinfo_filename): # Got file, open it and try to use its contents try: with open(caching_fisherinfo_filename, 'r') as file_in: # Load and assign values cached_data = pickle.load(file_in) result_fisherinfo_mindist_sigmax_ratio = cached_data['result_fisherinfo_mindist_sigmax_ratio'] compute_fisher_info_perratioconj = False except IOError: print "Error while loading ", caching_fisherinfo_filename, "falling back to computing the Fisher Info" if compute_fisher_info_perratioconj: # We did not save the Fisher info, but need it if we want to fit the mixture model with fixed kappa. So recompute them using the args_dicts result_fisherinfo_mindist_sigmax_ratio = np.empty((enforce_min_distance_space.size, sigmax_space.size, ratio_space.size)) # Invert the all_args_i -> min_dist, sigmax indexing parameters_indirections = data_pbs.loaded_data['parameters_dataset_index'] # min_dist_i, sigmax_level_i, ratio_i for min_dist_i, min_dist in enumerate(enforce_min_distance_space): for sigmax_i, sigmax in enumerate(sigmax_space): # Get index of first dataset with the current (min_dist, sigmax) (no need for the others, I think) arg_index = parameters_indirections[(min_dist, sigmax)][0] # Now using this dataset, reconstruct a RandomFactorialNetwork and compute the fisher info curr_args = all_args[arg_index] for ratio_conj_i, ratio_conj in enumerate(ratio_space): # Update param curr_args['ratio_conj'] = ratio_conj # curr_args['stimuli_generation'] = 'specific_stimuli' (_, _, _, sampler) = launchers.init_everything(curr_args) # Theo Fisher info result_fisherinfo_mindist_sigmax_ratio[min_dist_i, sigmax_i, ratio_conj_i] = sampler.estimate_fisher_info_theocov() print "Min dist: %.2f, Sigmax: %.2f, Ratio: %.2f: %.3f" % (min_dist, sigmax, ratio_conj, result_fisherinfo_mindist_sigmax_ratio[min_dist_i, sigmax_i, ratio_conj_i]) # Save everything to a file, for faster later plotting if caching_fisherinfo_filename is not None: try: with open(caching_fisherinfo_filename, 'w') as filecache_out: data_cache = dict(result_fisherinfo_mindist_sigmax_ratio=result_fisherinfo_mindist_sigmax_ratio) pickle.dump(data_cache, filecache_out, protocol=2) except IOError: print "Error writing out to caching file ", caching_fisherinfo_filename if plot_per_min_dist_all: # Do one plot per min distance. for min_dist_i, min_dist in enumerate(enforce_min_distance_space): # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist) if savefigs: dataio.save_current_figure('precision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Show log precision utils.pcolor_2d_data(result_all_precisions_mean[min_dist_i].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Precision, min_dist=%.3f' % min_dist, log_scale=True) if savefigs: dataio.save_current_figure('logprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated model precision utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 0].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM precision, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('logemprecision_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot estimated Target, nontarget and random mixture components, in multiple subplots _, axes = plt.subplots(1, 3, figsize=(18, 6)) plt.subplots_adjust(left=0.05, right=0.97, wspace = 0.3, bottom=0.15) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Target, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[0], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 2].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Nontarget, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[1], ticks_interpolate=5) utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., 3].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='Random, min_dist=%.3f' % min_dist, log_scale=False, ax_handle=axes[2], ticks_interpolate=5) if savefigs: dataio.save_current_figure('em_mixtureprobs_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) # Plot Log-likelihood of Mixture model, sanity check utils.pcolor_2d_data(result_em_fits_mean[min_dist_i, ..., -1].T, x=ratio_space, y=sigmax_space, xlabel='ratio', ylabel='sigma_x', title='EM loglik, min_dist=%.3f' % min_dist, log_scale=False) if savefigs: dataio.save_current_figure('em_loglik_permindist_mindist%.2f_ratiosigmax_{label}_{unique_id}.pdf' % min_dist) if specific_plots_paper: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) ## Do for each distance # for min_dist_i in [min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]: for min_dist_i in xrange(enforce_min_distance_space.size): # Plot precision if False: utils.plot_mean_std_area(ratio_space, result_all_precisions_mean[min_dist_i, sigmax_level_i], result_all_precisions_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Precision of recall') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, np.max(result_all_precisions_mean[min_dist_i, sigmax_level_i] + result_all_precisions_std[min_dist_i, sigmax_level_i])]) if savefigs: dataio.save_current_figure('mindist%.2f_precisionrecall_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa fitted ax_handle = utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0], result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa') # Add distance between items in kappa units dist_items_kappa = utils.stddev_to_kappa(enforce_min_distance_space[min_dist_i]) ax_handle.plot(ratio_space, dist_items_kappa*np.ones(ratio_space.size), 'k--', linewidth=3) plt.ylim([-0.1, np.max((np.max(result_em_fits_mean[min_dist_i, sigmax_level_i, :, 0] + result_em_fits_std[min_dist_i, sigmax_level_i, :, 0]), 1.1*dist_items_kappa))]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappa_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot kappa-stddev fitted. Easier to visualize ax_handle = utils.plot_mean_std_area(ratio_space, result_em_kappastddev_mean[min_dist_i, sigmax_level_i], result_em_kappastddev_std[min_dist_i, sigmax_level_i]) #, xlabel='Ratio conjunctivity', ylabel='Fitted kappa_stddev') # Add distance between items in std dev units dist_items_std = (enforce_min_distance_space[min_dist_i]) ax_handle.plot(ratio_space, dist_items_std*np.ones(ratio_space.size), 'k--', linewidth=3) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) plt.ylim([0, 1.1*np.max((np.max(result_em_kappastddev_mean[min_dist_i, sigmax_level_i] + result_em_kappastddev_std[min_dist_i, sigmax_level_i]), dist_items_std))]) if savefigs: dataio.save_current_figure('mindist%.2f_emkappastddev_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if False: # Plot LLH utils.plot_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, -1], result_em_fits_std[min_dist_i, sigmax_level_i, :, -1]) #, xlabel='Ratio conjunctivity', ylabel='Loglikelihood of Mixture model fit') # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) if savefigs: dataio.save_current_figure('mindist%.2f_emllh_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Plot mixture parameters, std utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) # Mixture parameters, SEM utils.plot_multiple_mean_std_area(ratio_space, result_em_fits_mean[min_dist_i, sigmax_level_i, :, 1:4].T, result_em_fits_std[min_dist_i, sigmax_level_i, :, 1:4].T/np.sqrt(nb_repetitions)) plt.ylim([0.0, 1.1]) # plt.title('Min distance %.3f' % enforce_min_distance_space[min_dist_i]) # plt.legend("Target", "Non-target", "Random") if savefigs: dataio.save_current_figure('mindist%.2f_emprobs_forpaper_sem_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if plots_emfit_allitems: # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.36 target_mindist_high = 1.5 sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) min_dist_i_plotting_space = np.array([min_dist_level_low_i, min_dist_level_medium_i, min_dist_level_high_i]) if should_fit_allitems_model: # kappa, mixt_target, mixt_nontargets (K), mixt_random, LL, bic # result_emfitallitems = np.empty((min_dist_i_plotting_space.size, ratio_space.size, 2*K+5))*np.nan result_emfitallitems = np.empty((enforce_min_distance_space.size, ratio_space.size, K+5))*np.nan ## Do for each distance # for min_dist_plotting_i, min_dist_i in enumerate(min_dist_i_plotting_space): for min_dist_i in xrange(enforce_min_distance_space.size): # Fit the mixture model for ratio_i, ratio in enumerate(ratio_space): print "Refitting EM all items. Ratio:", ratio, "Dist:", enforce_min_distance_space[min_dist_i] if mixturemodel_to_use == 'allitems_uniquekappa': em_fit = em_circularmixture_allitems_uniquekappa.fit( result_responses_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_target_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_nontargets_all[min_dist_i, sigmax_level_i, ratio_i].transpose((0, 2, 1)).reshape((N*nb_repetitions, K))) elif mixturemodel_to_use == 'allitems_fikappa': em_fit = em_circularmixture_allitems_kappafi.fit(result_responses_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_target_all[min_dist_i, sigmax_level_i, ratio_i].flatten(), result_nontargets_all[min_dist_i, sigmax_level_i, ratio_i].transpose((0, 2, 1)).reshape((N*nb_repetitions, K)), kappa=result_fisherinfo_mindist_sigmax_ratio[min_dist_i, sigmax_level_i, ratio_i]) else: raise ValueError("Wrong mixturemodel_to_use, %s" % mixturemodel_to_use) result_emfitallitems[min_dist_i, ratio_i] = [em_fit['kappa'], em_fit['mixt_target']] + em_fit['mixt_nontargets'].tolist() + [em_fit[key] for key in ('mixt_random', 'train_LL', 'bic')] # Save everything to a file, for faster later plotting if caching_emfit_filename is not None: try: with open(caching_emfit_filename, 'w') as filecache_out: data_em = dict(result_emfitallitems=result_emfitallitems, target_sigmax=target_sigmax) pickle.dump(data_em, filecache_out, protocol=2) except IOError: print "Error writing out to caching file ", caching_emfit_filename ## Plots now, for each distance! # for min_dist_plotting_i, min_dist_i in enumerate(min_dist_i_plotting_space): for min_dist_i in xrange(enforce_min_distance_space.size): # Plot now _, ax = plt.subplots() ax.plot(ratio_space, result_emfitallitems[min_dist_i, :, 1:5], linewidth=3) plt.ylim([0.0, 1.1]) plt.legend(['Target', 'Nontarget 1', 'Nontarget 2', 'Random'], loc='upper left') if savefigs: dataio.save_current_figure('mindist%.2f_emprobsfullitems_{label}_{unique_id}.pdf' % enforce_min_distance_space[min_dist_i]) if plot_min_distance_effect: conj_receptive_field_size = 2.*np.pi/((all_args[0]['M']*ratio_space)**0.5) target_vs_nontargets_mindist_ratio = result_emfitallitems[..., 1]/np.sum(result_emfitallitems[..., 1:4], axis=-1) nontargetsmean_vs_targnontarg_mindist_ratio = np.mean(result_emfitallitems[..., 2:4]/np.sum(result_emfitallitems[..., 1:4], axis=-1)[..., np.newaxis], axis=-1) for ratio_conj_i, ratio_conj in enumerate(ratio_space): # Do one plot per ratio, putting the receptive field size on each f, ax = plt.subplots() ax.plot(enforce_min_distance_space[1:], target_vs_nontargets_mindist_ratio[1:, ratio_conj_i], linewidth=3, label='target mixture') ax.plot(enforce_min_distance_space[1:], nontargetsmean_vs_targnontarg_mindist_ratio[1:, ratio_conj_i], linewidth=3, label='non-target mixture') # ax.plot(enforce_min_distance_space[1:], result_emfitallitems[1:, ratio_conj_i, 1:5], linewidth=3) ax.axvline(x=conj_receptive_field_size[ratio_conj_i]/2., color='k', linestyle='--', linewidth=2) ax.axvline(x=conj_receptive_field_size[ratio_conj_i]*2., color='r', linestyle='--', linewidth=2) plt.legend(loc='upper left') plt.grid() # ax.set_xlabel('Stimuli separation') # ax.set_ylabel('Ratio Target to Non-targets') plt.axis('tight') ax.set_ylim([0.0, 1.0]) ax.set_xlim([enforce_min_distance_space[1:].min(), enforce_min_distance_space[1:].max()]) if savefigs: dataio.save_current_figure('ratio%.2f_mindistpred_ratiotargetnontarget_{label}_{unique_id}.pdf' % ratio_conj) if compute_bootstraps: ## Bootstrap evaluation # We need to choose 3 levels of min_distances target_sigmax = 0.25 target_mindist_low = 0.15 target_mindist_medium = 0.5 target_mindist_high = 1. sigmax_level_i = np.argmin(np.abs(sigmax_space - target_sigmax)) min_dist_level_low_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_low)) min_dist_level_medium_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_medium)) min_dist_level_high_i = np.argmin(np.abs(enforce_min_distance_space - target_mindist_high)) # cache_bootstrap_fn = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_bootstrap.pickle') cache_bootstrap_fn = '/Users/loicmatthey/Dropbox/UCL/1-phd/Work/Visual_working_memory/code/git-bayesian-visual-working-memory/Experiments/specific_stimuli/specific_stimuli_corrected_mixed_sigmaxmindistance_autoset_repetitions5mult_collectall_281113_outputs/cache_bootstrap.pickle' try: with open(cache_bootstrap_fn, 'r') as file_in: # Load and assign values cached_data = pickle.load(file_in) bootstrap_ecdf_bays_sigmax_T = cached_data['bootstrap_ecdf_bays_sigmax_T'] bootstrap_ecdf_allitems_sum_sigmax_T = cached_data['bootstrap_ecdf_allitems_sum_sigmax_T'] bootstrap_ecdf_allitems_all_sigmax_T = cached_data['bootstrap_ecdf_allitems_all_sigmax_T'] should_fit_bootstrap = False except IOError: print "Error while loading ", cache_bootstrap_fn ratio_i = 0 # bootstrap_allitems_nontargets_allitems_uniquekappa = em_circularmixture_allitems_uniquekappa.bootstrap_nontarget_stat( # result_responses_all[min_dist_level_low_i, sigmax_level_i, ratio_i].flatten(), # result_target_all[min_dist_level_low_i, sigmax_level_i, ratio_i].flatten(), # result_nontargets_all[min_dist_level_low_i, sigmax_level_i, ratio_i].transpose((0, 2, 1)).reshape((N*nb_repetitions, K)), # sumnontargets_bootstrap_ecdf=bootstrap_ecdf_allitems_sum_sigmax_T[sigmax_level_i][K]['ecdf'], # allnontargets_bootstrap_ecdf=bootstrap_ecdf_allitems_all_sigmax_T[sigmax_level_i][K]['ecdf'] # TODO FINISH HERE variables_to_save = ['nb_repetitions'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='specific_stimuli') plt.show() return locals()
def plots_misbinding_logposterior(data_pbs, generator_module=None): ''' Reload 3D volume runs from PBS and plot them ''' #### SETUP # savedata = False savefigs = True plot_logpost = False plot_error = False plot_mixtmodel = True plot_hist_responses_fisherinfo = True compute_plot_bootstrap = False compute_fisher_info_perratioconj = True # mixturemodel_to_use = 'original' mixturemodel_to_use = 'allitems' # mixturemodel_to_use = 'allitems_kappafi' caching_fisherinfo_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_fisherinfo.pickle') # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() result_all_log_posterior = np.squeeze(data_pbs.dict_arrays['result_all_log_posterior']['results']) result_all_thetas = np.squeeze(data_pbs.dict_arrays['result_all_thetas']['results']) ratio_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] print ratio_space print result_all_log_posterior.shape N = result_all_thetas.shape[-1] result_prob_wrong = np.zeros((ratio_space.size, N)) result_em_fits = np.empty((ratio_space.size, 6))*np.nan all_args = data_pbs.loaded_data['args_list'] fixed_means = [-np.pi*0.6, np.pi*0.6] all_angles = np.linspace(-np.pi, np.pi, result_all_log_posterior.shape[-1]) dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) plt.rcParams['font.size'] = 18 if plot_hist_responses_fisherinfo: # From cache if caching_fisherinfo_filename is not None: if os.path.exists(caching_fisherinfo_filename): # Got file, open it and try to use its contents try: with open(caching_fisherinfo_filename, 'r') as file_in: # Load and assign values cached_data = pickle.load(file_in) result_fisherinfo_ratio = cached_data['result_fisherinfo_ratio'] compute_fisher_info_perratioconj = False except IOError: print "Error while loading ", caching_fisherinfo_filename, "falling back to computing the Fisher Info" if compute_fisher_info_perratioconj: # We did not save the Fisher info, but need it if we want to fit the mixture model with fixed kappa. So recompute them using the args_dicts result_fisherinfo_ratio = np.empty(ratio_space.shape) # Invert the all_args_i -> ratio_conj direction parameters_indirections = data_pbs.loaded_data['parameters_dataset_index'] for ratio_conj_i, ratio_conj in enumerate(ratio_space): # Get index of first dataset with the current ratio_conj (no need for the others, I think) arg_index = parameters_indirections[(ratio_conj,)][0] # Now using this dataset, reconstruct a RandomFactorialNetwork and compute the fisher info curr_args = all_args[arg_index] curr_args['stimuli_generation'] = lambda T: np.linspace(-np.pi*0.6, np.pi*0.6, T) (random_network, data_gen, stat_meas, sampler) = launchers.init_everything(curr_args) # Theo Fisher info result_fisherinfo_ratio[ratio_conj_i] = sampler.estimate_fisher_info_theocov() del curr_args['stimuli_generation'] # Save everything to a file, for faster later plotting if caching_fisherinfo_filename is not None: try: with open(caching_fisherinfo_filename, 'w') as filecache_out: data_cache = dict(result_fisherinfo_ratio=result_fisherinfo_ratio) pickle.dump(data_cache, filecache_out, protocol=2) except IOError: print "Error writing out to caching file ", caching_fisherinfo_filename # Now plots. Do histograms of responses (around -pi/6 and pi/6), add Von Mises derived from Theo FI on top, and vertical lines for the correct target/nontarget angles. for ratio_conj_i, ratio_conj in enumerate(ratio_space): # Histogram ax = utils.hist_angular_data(result_all_thetas[ratio_conj_i], bins=100, title='ratio %.2f, fi %.0f' % (ratio_conj, result_fisherinfo_ratio[ratio_conj_i])) bar_heights, _, _ = utils.histogram_binspace(result_all_thetas[ratio_conj_i], bins=100, norm='density') # Add Fisher info prediction on top x = np.linspace(-np.pi, np.pi, 1000) if result_fisherinfo_ratio[ratio_conj_i] < 700: # Von Mises PDF utils.plot_vonmises_pdf(x, utils.stddev_to_kappa(1./result_fisherinfo_ratio[ratio_conj_i]**0.5), mu=fixed_means[-1], ax_handle=ax, linewidth=3, color='r', scale=np.max(bar_heights), fmt='-') else: # Switch to Gaussian instead utils.plot_normal_pdf(x, mu=fixed_means[-1], std=1./result_fisherinfo_ratio[ratio_conj_i]**0.5, ax_handle=ax, linewidth=3, color='r', scale=np.max(bar_heights), fmt='-') # ax.set_xticks([]) # ax.set_yticks([]) # Add vertical line to correct target/nontarget ax.axvline(x=fixed_means[0], color='g', linewidth=2) ax.axvline(x=fixed_means[1], color='r', linewidth=2) ax.get_figure().canvas.draw() if savefigs: # plt.tight_layout() dataio.save_current_figure('results_misbinding_histresponses_vonmisespdf_ratioconj%.2f{label}_{unique_id}.pdf' % (ratio_conj)) if plot_logpost: for ratio_conj_i, ratio_conj in enumerate(ratio_space): # ax = utils.plot_mean_std_area(all_angles, nanmean(result_all_log_posterior[ratio_conj_i], axis=0), nanstd(result_all_log_posterior[ratio_conj_i], axis=0)) # ax.set_xlim((-np.pi, np.pi)) # ax.set_xticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi)) # ax.set_xticklabels((r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$')) # ax.set_yticks(()) # ax.get_figure().canvas.draw() # if savefigs: # dataio.save_current_figure('results_misbinding_logpost_ratioconj%.2f_{label}_global_{unique_id}.pdf' % ratio_conj) # Compute the probability of answering wrongly (from fitting mixture distrib onto posterior) for n in xrange(result_all_log_posterior.shape[1]): result_prob_wrong[ratio_conj_i, n], _, _ = utils.fit_gaussian_mixture_fixedmeans(all_angles, np.exp(result_all_log_posterior[ratio_conj_i, n]), fixed_means=fixed_means, normalise=True, return_fitted_data=False, should_plot=False) # ax = utils.plot_mean_std_area(ratio_space, nanmean(result_prob_wrong, axis=-1), nanstd(result_prob_wrong, axis=-1)) plt.figure() plt.plot(ratio_space, utils.nanmean(result_prob_wrong, axis=-1)) # ax.get_figure().canvas.draw() if savefigs: dataio.save_current_figure('results_misbinding_probwrongpost_allratioconj_{label}_global_{unique_id}.pdf') if plot_error: ## Compute Standard deviation/precision from samples and plot it as a function of ratio_conj stats = utils.compute_mean_std_circular_data(utils.wrap_angles(result_all_thetas - fixed_means[1]).T) f = plt.figure() plt.plot(ratio_space, stats['std']) plt.ylabel('Standard deviation [rad]') if savefigs: dataio.save_current_figure('results_misbinding_stddev_allratioconj_{label}_global_{unique_id}.pdf') f = plt.figure() plt.plot(ratio_space, utils.compute_angle_precision_from_std(stats['std'], square_precision=False), linewidth=2) plt.ylabel('Precision [$1/rad$]') plt.xlabel('Proportion of conjunctive units') plt.grid() if savefigs: dataio.save_current_figure('results_misbinding_precision_allratioconj_{label}_global_{unique_id}.pdf') ## Compute the probability of misbinding # 1) Just count samples < 0 / samples tot # 2) Fit a mixture model, average over mixture probabilities prob_smaller0 = np.sum(result_all_thetas <= 1, axis=1)/float(result_all_thetas.shape[1]) em_centers = np.zeros((ratio_space.size, 2)) em_covs = np.zeros((ratio_space.size, 2)) em_pk = np.zeros((ratio_space.size, 2)) em_ll = np.zeros(ratio_space.size) for ratio_conj_i, ratio_conj in enumerate(ratio_space): cen_lst, cov_lst, em_pk[ratio_conj_i], em_ll[ratio_conj_i] = pygmm.em(result_all_thetas[ratio_conj_i, np.newaxis].T, K = 2, max_iter = 400, init_kw={'cluster_init':'fixed', 'fixed_means': fixed_means}) em_centers[ratio_conj_i] = np.array(cen_lst).flatten() em_covs[ratio_conj_i] = np.array(cov_lst).flatten() # print em_centers # print em_covs # print em_pk f = plt.figure() plt.plot(ratio_space, prob_smaller0) plt.ylabel('Misbound proportion') if savefigs: dataio.save_current_figure('results_misbinding_countsmaller0_allratioconj_{label}_global_{unique_id}.pdf') f = plt.figure() plt.plot(ratio_space, np.max(em_pk, axis=-1), 'g', linewidth=2) plt.ylabel('Mixture proportion, correct') plt.xlabel('Proportion of conjunctive units') plt.grid() if savefigs: dataio.save_current_figure('results_misbinding_emmixture_allratioconj_{label}_global_{unique_id}.pdf') # Put everything on one figure f = plt.figure(figsize=(10, 6)) norm_for_plot = lambda x: (x - np.min(x))/np.max((x - np.min(x))) plt.plot(ratio_space, norm_for_plot(stats['std']), ratio_space, norm_for_plot(utils.compute_angle_precision_from_std(stats['std'], square_precision=False)), ratio_space, norm_for_plot(prob_smaller0), ratio_space, norm_for_plot(em_pk[:, 1]), ratio_space, norm_for_plot(em_pk[:, 0])) plt.legend(('Std dev', 'Precision', 'Prob smaller 1', 'Mixture proportion correct', 'Mixture proportion misbinding')) # plt.plot(ratio_space, norm_for_plot(compute_angle_precision_from_std(stats['std'], square_precision=False)), ratio_space, norm_for_plot(em_pk[:, 1]), linewidth=2) # plt.legend(('Precision', 'Mixture proportion correct'), loc='best') plt.grid() if savefigs: dataio.save_current_figure('results_misbinding_allmetrics_allratioconj_{label}_global_{unique_id}.pdf') if plot_mixtmodel: # Fit Paul's model target_angle = np.ones(N)*fixed_means[1] nontarget_angles = np.ones((N, 1))*fixed_means[0] for ratio_conj_i, ratio_conj in enumerate(ratio_space): print "Ratio: ", ratio_conj responses = result_all_thetas[ratio_conj_i] if mixturemodel_to_use == 'allitems_kappafi': curr_params_fit = em_circularmixture_allitems_kappafi.fit(responses, target_angle, nontarget_angles, kappa=result_fisherinfo_ratio[ratio_conj_i]) elif mixturemodel_to_use == 'allitems': curr_params_fit = em_circularmixture_allitems_uniquekappa.fit(responses, target_angle, nontarget_angles) else: curr_params_fit = em_circularmixture.fit(responses, target_angle, nontarget_angles) result_em_fits[ratio_conj_i] = [curr_params_fit['kappa'], curr_params_fit['mixt_target']] + utils.arrnum_to_list(curr_params_fit['mixt_nontargets']) + [curr_params_fit[key] for key in ('mixt_random', 'train_LL', 'bic')] print curr_params_fit if False: f, ax = plt.subplots() ax2 = ax.twinx() # left axis, kappa ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 0], 0*result_em_fits[:, 0], xlabel='Proportion of conjunctive units', ylabel="Inverse variance $[rad^{-2}]$", ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Fitted kappa', color='k') # Right axis, mixture probabilities utils.plot_mean_std_area(ratio_space, result_em_fits[:, 1], 0*result_em_fits[:, 1], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Target') utils.plot_mean_std_area(ratio_space, result_em_fits[:, 2], 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Nontarget') utils.plot_mean_std_area(ratio_space, result_em_fits[:, 3], 0*result_em_fits[:, 3], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Random') lines, labels = ax.get_legend_handles_labels() lines2, labels2 = ax2.get_legend_handles_labels() ax.legend(lines + lines2, labels + labels2, fontsize=12, loc='right') # ax.set_xlim([0.9, 5.1]) # ax.set_xticks(range(1, 6)) # ax.set_xticklabels(range(1, 6)) plt.grid() f.canvas.draw() if True: # Mixture probabilities ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 1], 0*result_em_fits[:, 1], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", linewidth=3, fmt='-', markersize=8, label='Target') utils.plot_mean_std_area(ratio_space, result_em_fits[:, 2], 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='-', markersize=8, label='Nontarget') utils.plot_mean_std_area(ratio_space, result_em_fits[:, 3], 0*result_em_fits[:, 3], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='-', markersize=8, label='Random') ax.legend(loc='right') # ax.set_xlim([0.9, 5.1]) # ax.set_xticks(range(1, 6)) # ax.set_xticklabels(range(1, 6)) plt.grid() if savefigs: dataio.save_current_figure('results_misbinding_emmixture_allratioconj_{label}_global_{unique_id}.pdf') if True: # Kappa # ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 0], 0*result_em_fits[:, 0], xlabel='Proportion of conjunctive units', ylabel="$\kappa [rad^{-2}]$", linewidth=3, fmt='-', markersize=8, label='Kappa') ax = utils.plot_mean_std_area(ratio_space, utils.kappa_to_stddev(result_em_fits[:, 0]), 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Standard deviation [rad]", linewidth=3, fmt='-', markersize=8, label='Mixture model $\kappa$') # Add Fisher Info theo ax = utils.plot_mean_std_area(ratio_space, utils.kappa_to_stddev(result_fisherinfo_ratio), 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Standard deviation [rad]", linewidth=3, fmt='-', markersize=8, label='Fisher Information', ax_handle=ax) ax.legend(loc='best') # ax.set_xlim([0.9, 5.1]) # ax.set_xticks(range(1, 6)) # ax.set_xticklabels(range(1, 6)) plt.grid() if savefigs: dataio.save_current_figure('results_misbinding_kappa_allratioconj_{label}_global_{unique_id}.pdf') if compute_plot_bootstrap: ## Compute the bootstrap pvalue for each ratio # use the bootstrap CDF from mixed runs, not the exact current ones, not sure if good idea. bootstrap_to_load = 1 if bootstrap_to_load == 1: cache_bootstrap_fn = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_bootstrap_mixed_from_bootstrapnontargets.pickle') bootstrap_ecdf_sum_label = 'bootstrap_ecdf_allitems_sum_sigmax_T' bootstrap_ecdf_all_label = 'bootstrap_ecdf_allitems_all_sigmax_T' elif bootstrap_to_load == 2: cache_bootstrap_fn = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_bootstrap_misbinding_mixed.pickle') bootstrap_ecdf_sum_label = 'bootstrap_ecdf_allitems_sum_ratioconj' bootstrap_ecdf_all_label = 'bootstrap_ecdf_allitems_all_ratioconj' try: with open(cache_bootstrap_fn, 'r') as file_in: # Load and assign values cached_data = pickle.load(file_in) assert bootstrap_ecdf_sum_label in cached_data assert bootstrap_ecdf_all_label in cached_data should_fit_bootstrap = False except IOError: print "Error while loading ", cache_bootstrap_fn # Select the ECDF to use if bootstrap_to_load == 1: sigmax_i = 3 # corresponds to sigmax = 2, input here. T_i = 1 # two possible targets here. bootstrap_ecdf_sum_used = cached_data[bootstrap_ecdf_sum_label][sigmax_i][T_i]['ecdf'] bootstrap_ecdf_all_used = cached_data[bootstrap_ecdf_all_label][sigmax_i][T_i]['ecdf'] elif bootstrap_to_load == 2: ratio_conj_i = 4 bootstrap_ecdf_sum_used = cached_data[bootstrap_ecdf_sum_label][ratio_conj_i]['ecdf'] bootstrap_ecdf_all_used = cached_data[bootstrap_ecdf_all_label][ratio_conj_i]['ecdf'] result_pvalue_bootstrap_sum = np.empty(ratio_space.size)*np.nan result_pvalue_bootstrap_all = np.empty((ratio_space.size, nontarget_angles.shape[-1]))*np.nan for ratio_conj_i, ratio_conj in enumerate(ratio_space): print "Ratio: ", ratio_conj responses = result_all_thetas[ratio_conj_i] bootstrap_allitems_nontargets_allitems_uniquekappa = em_circularmixture_allitems_uniquekappa.bootstrap_nontarget_stat(responses, target_angle, nontarget_angles, sumnontargets_bootstrap_ecdf=bootstrap_ecdf_sum_used, allnontargets_bootstrap_ecdf=bootstrap_ecdf_all_used) result_pvalue_bootstrap_sum[ratio_conj_i] = bootstrap_allitems_nontargets_allitems_uniquekappa['p_value'] result_pvalue_bootstrap_all[ratio_conj_i] = bootstrap_allitems_nontargets_allitems_uniquekappa['allnontarget_p_value'] ## Plots # f, ax = plt.subplots() # ax.plot(ratio_space, result_pvalue_bootstrap_all, linewidth=2) # if savefigs: # dataio.save_current_figure("pvalue_bootstrap_all_ratioconj_{label}_{unique_id}.pdf") f, ax = plt.subplots() ax.plot(ratio_space, result_pvalue_bootstrap_sum, linewidth=2) plt.grid() if savefigs: dataio.save_current_figure("pvalue_bootstrap_sum_ratioconj_{label}_{unique_id}.pdf") # plt.figure() # plt.plot(ratio_MMlower, results_filtered_smoothed/np.max(results_filtered_smoothed, axis=0), linewidth=2) # plt.plot(ratio_MMlower[np.argmax(results_filtered_smoothed, axis=0)], np.ones(results_filtered_smoothed.shape[-1]), 'ro', markersize=10) # plt.grid() # plt.ylim((0., 1.1)) # plt.subplots_adjust(right=0.8) # plt.legend(['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='center right', bbox_to_anchor=(1.3, 0.5)) # plt.xticks(np.linspace(0, 1.0, 5)) variables_to_save = ['target_angle', 'nontarget_angles'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='misbindings') plt.show() return locals()