def compute_result_distfitexpbic(self, all_variables): ''' Result is the summed BIC score of the FitExperiment result on all datasets ''' if 'result_fitexperiments' in all_variables: # We have result_fitexperiments, that's good. # Make it work for either fixed T or multiple T. if len(all_variables['result_fitexperiments'].shape) == 2: # Single T. Average over axis -1 bic_summed = utils.nanmean(all_variables['result_fitexperiments'][0]) elif len(all_variables['result_fitexperiments'].shape) == 3: # Multiple T. Average over axis -1, then sum bic_summed = np.nansum( utils.nanmean( all_variables['result_fitexperiments'][:, 0], axis=-1)) else: raise ValueError("wrong shape for result_fitexperiments: {}".format( all_variables['result_fitexperiments'].shape)) else: # We do not have it, could instantiate a FitExperiment with "normal" parameters and work from there instead raise NotImplementedError( 'version without result_fitexperiments not implemented yet') return bic_summed
def compute_result_dist_prodll_allt(self, all_variables): ''' Given outputs from FitExperimentAllT, will compute the geometric mean of the LL. UGLY HACK: in order to keep track of the minLL, we return it here. You should have a cma_iter_function that cleans it before cma_es.tell() is called... ''' if 'result_ll_sum' in all_variables: repetitions_axis = all_variables.get('repetitions_axis', -1) # Shift to get LL > 0 always currMinLL = np.min(all_variables['result_ll_sum']) if currMinLL < all_variables['all_parameters']['shiftMinLL']: all_variables['all_parameters']['shiftMinLL'] = currMinLL # Remove the current minLL, to make sure fitness > 0 print 'Before: ', all_variables['result_ll_sum'] all_variables['result_ll_sum'] -= all_variables['all_parameters'][ 'shiftMinLL'] all_variables['result_ll_sum'] += 0.001 print 'Shifted: ', all_variables['result_ll_sum'] result_dist_nll_geom = -mstats.gmean( utils.nanmean(all_variables['result_ll_sum'], axis=repetitions_axis), axis=-1) print result_dist_nll_geom return np.array([ result_dist_nll_geom, all_variables['all_parameters']['shiftMinLL'] ]) else: raise ValueError('result_ll_sum was not found in the outputs')
def _build_graph(self): self._normalize() with tf.device(self.device): _prediction_norm = self._inference(self.x_norm) self.loss = self._loss(_prediction_norm) self._optimize() self.prediction = _prediction_norm * tf.sqrt(self.y_variance) + self.y_mean self.rmse = tf.sqrt(utils.nanmean(tf.square(self.prediction - self.y)), name='rmse') self._summaries()
def _build_graph(self): self._normalize() with tf.device(self.device): _prediction_norm = self._inference(self.x_norm) self.loss = self._loss(_prediction_norm) self._optimize() self.prediction = _prediction_norm * tf.sqrt( self.y_variance) + self.y_mean self.rmse = tf.sqrt(utils.nanmean(tf.square(self.prediction - self.y)), name='rmse') self._summaries()
def plots_memmixtcurves_fig6fig13(self, num_repetitions=1, cache=True): """ Plots the memory fidelity for all T and the mixture proportions for all T """ if self.result_em_fits_stats is None: search_progress = progress.Progress(self.fit_exp.T_space.size * num_repetitions) # kappa, mixt_target, mixt_nontarget, mixt_random, ll result_em_fits = np.nan * np.ones((self.fit_exp.T_space.size, 5, num_repetitions)) for T_i, T in enumerate(self.fit_exp.T_space): self.fit_exp.setup_experimental_stimuli_T(T) for repet_i in xrange(num_repetitions): print "%.2f%%, %s left - %s" % ( search_progress.percentage(), search_progress.time_remaining_str(), search_progress.eta_str(), ) print "Fit for T=%d, %d/%d" % (T, repet_i + 1, num_repetitions) self.fit_exp.sampler.force_sampling_round() # Fit mixture model curr_params_fit = self.fit_exp.sampler.fit_mixture_model(use_all_targets=False) result_em_fits[T_i, :, repet_i] = [ curr_params_fit[key] for key in ("kappa", "mixt_target", "mixt_nontargets_sum", "mixt_random", "train_LL") ] search_progress.increment() # Get stats of EM Fits self.result_em_fits_stats = dict( mean=utils.nanmean(result_em_fits, axis=-1), std=utils.nanstd(result_em_fits, axis=-1) ) # Do the plots if self.do_memcurves_fig6 and self.do_mixtcurves_fig13: f, axes = plt.subplots(nrows=2, figsize=(14, 18)) else: f, ax = plt.subplots(figsize=(14, 9)) axes = [ax] ax_i = 0 if self.do_memcurves_fig6: self.__plot_memcurves(self.result_em_fits_stats, suptitle_text="Memory fidelity", ax=axes[ax_i]) ax_i += 1 if self.do_mixtcurves_fig13: self.__plot_mixtcurves(self.result_em_fits_stats, suptitle_text="Mixture proportions", ax=axes[ax_i]) return axes
def compute_result_distfit_givenexp( self, all_variables, experiment_id='bays09', metric_index=0, target_array_name='result_fitexperiments_all'): ''' Result is the summed BIC score of the FitExperiment result on a given dataset ''' if target_array_name in all_variables: # We have target_array_name (result_fitexperiments_all or result_fitexperiments_noiseconv_all say), that's good. # Extract only the Bays09 result experiment_index = all_variables['all_parameters'][ 'experiment_ids'].index(experiment_id) # Make it work for either fixed T or multiple T. if len(all_variables[target_array_name].shape) == 3: # Single T. Average over axis -1 bic_summed = utils.nanmean( all_variables[target_array_name][metric_index, experiment_index]) elif len(all_variables[target_array_name].shape) == 4: # Multiple T. Average over axis -1, then sum bic_summed = np.nansum( utils.nanmean( all_variables[target_array_name][:, metric_index, experiment_index], axis=-1)) else: raise ValueError( "wrong shape for result_fitexperiments_all: {}".format( all_variables[target_array_name].shape)) else: # We do not have it, could instantiate a FitExperiment with "normal" parameters and work from there instead raise NotImplementedError( 'version without result_fitexperiments_all not implemented yet') return bic_summed
def compute_result_dist_ll_median_allt(self, all_variables): ''' Use the median of LL to get a score. ''' if 'result_ll_n' in all_variables: repetitions_axis = all_variables.get('repetitions_axis', -1) data_ll = -all_variables['result_ll_n'] data_ll = data_ll.reshape(-1, data_ll.shape[repetitions_axis]) result_dist = utils.nanmean(np.nanmedian(data_ll, axis=0)) print result_dist return result_dist else: raise ValueError("%s was not found in the outputs" % res_variant)
def compute_result_dist_collapsedemfit_gorgo11seq(self, all_variables): ''' Use the collapsed mixture fits to Gorgo11 Sequential. Should be precomputed by FitExperimentSequential ''' res_variant = 'result_emfit_mse' if res_variant in all_variables: repetitions_axis = all_variables.get('repetitions_axis', -1) data_fits = all_variables[res_variant] result_dist = utils.nanmean(data_fits, axis=repetitions_axis) print result_dist return result_dist else: raise ValueError("%s was not found in the outputs" % res_variant)
def dice_coefficient(hist): """Computes the Sørensen–Dice coefficient, a.k.a the F1 score. Args: hist: confusion matrix. Returns: avg_dice: the average per-class dice coefficient. """ A_inter_B = torch.diag(hist) A = hist.sum(dim=1) B = hist.sum(dim=0) dice = (2 * A_inter_B) / (A + B + EPS) avg_dice = nanmean(dice) return avg_dice
def jaccard_index(hist): """Computes the Jaccard index, a.k.a the Intersection over Union (IoU). Args: hist: confusion matrix. Returns: avg_jacc: the average per-class jaccard index. """ A_inter_B = torch.diag(hist) A = hist.sum(dim=1) B = hist.sum(dim=0) jaccard = A_inter_B / (A + B - A_inter_B + EPS) avg_jacc = nanmean(jaccard) return avg_jacc
def compute_result_dist_emfit_scaled(self, all_variables): ''' Use the scaled MSE to the mixture model fits. Should have been precomputed by launchers_fitexperiment_allt. ''' res_variant = 'result_emfit_mse_scaled' if res_variant in all_variables: repetitions_axis = all_variables.get('repetitions_axis', -1) data_fits = all_variables[res_variant] result_dist = np.nansum(utils.nanmean(data_fits, axis=repetitions_axis)) print result_dist return result_dist else: raise ValueError("%s was not found in the outputs" % res_variant)
def _compute_dist_llvariant(self, all_variables, variant='ll'): ''' Given outputs from FitExperimentAllT, will compute the summed LL, as this seems to be an acceptable metric for data fitting. ''' res_variant = 'result_%s_sum' % variant if res_variant in all_variables: # Average over repetitions and sum over the rest. repetitions_axis = all_variables.get('repetitions_axis', -1) result_dist = np.nansum( utils.nanmean(-all_variables[res_variant], axis=repetitions_axis)) print result_dist return result_dist else: raise ValueError("%s was not found in the outputs" % res_variant)
def per_class_pixel_accuracy(hist): """Computes the average per-class pixel accuracy. The per-class pixel accuracy is a more fine-grained version of the overall pixel accuracy. A model could score a relatively high overall pixel accuracy by correctly predicting the dominant labels or areas in the image whilst incorrectly predicting the possibly more important/rare labels. Such a model will score a low per-class pixel accuracy. Args: hist: confusion matrix. Returns: avg_per_class_acc: the average per-class pixel accuracy. """ correct_per_class = torch.diag(hist) total_per_class = hist.sum(dim=1) per_class_acc = correct_per_class / (total_per_class + EPS) avg_per_class_acc = nanmean(per_class_acc) return avg_per_class_acc
def plots_fitmixtmodel_rcscale_effect(data_pbs, generator_module=None): ''' Reload runs from PBS ''' #### SETUP # savefigs = True savedata = True plots_all_T = True plots_per_T = True # do_relaunch_bestparams_pbs = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", data_pbs.dataset_infos['parameters'] # parameters: M, ratio_conj, sigmax # Extract data T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] result_em_fits_flat = np.array(data_pbs.dict_arrays['result_em_fits']['results_flat']) result_precisions_flat = np.array(data_pbs.dict_arrays['result_all_precisions']['results_flat']) result_dist_bays09_flat = np.array(data_pbs.dict_arrays['result_dist_bays09']['results_flat']) result_dist_gorgo11_flat = np.array(data_pbs.dict_arrays['result_dist_gorgo11']['results_flat']) result_dist_bays09_emmixt_KL = np.array(data_pbs.dict_arrays['result_dist_bays09_emmixt_KL']['results_flat']) result_dist_gorgo11_emmixt_KL = np.array(data_pbs.dict_arrays['result_dist_gorgo11_emmixt_KL']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_em_fits']['parameters_flat']) rc_scale_space = data_pbs.loaded_data['parameters_uniques']['rc_scale'] num_repetitions = generator_module.num_repetitions parameter_names_sorted = data_pbs.dataset_infos['parameters'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Load bays09 data_bays09 = load_experimental_data.load_data_bays09(fit_mixture_model=True) bays09_nitems = data_bays09['data_to_fit']['n_items'] bays09_em_target = np.nan*np.empty((bays09_nitems.max(), 4)) #kappa, prob_target, prob_nontarget, prob_random bays09_em_target[bays09_nitems - 1] = data_bays09['em_fits_nitems_arrays']['mean'].T bays09_emmixt_target = bays09_em_target[:, 1:] ## Compute some stuff result_parameters_flat = result_parameters_flat.flatten() result_em_fits_all_avg = utils.nanmean(result_em_fits_flat, axis=-1) result_em_kappa_allT = result_em_fits_all_avg[..., 0] result_em_emmixt_allT = result_em_fits_all_avg[..., 1:4] result_precisions_all_avg = utils.nanmean(result_precisions_flat, axis=-1) # Square distance to kappa result_dist_bays09_allT_avg = utils.nanmean(result_dist_bays09_flat, axis=-1) result_dist_bays09_emmixt_KL_allT_avg = utils.nanmean(result_dist_bays09_emmixt_KL, axis=-1) result_dist_bays09_kappa_allT = result_dist_bays09_allT_avg[..., 0] # result_dist_bays09_allT_avg = utils.nanmean((result_em_fits_flat[:, :, :4] - bays09_em_target[np.newaxis, :, :, np.newaxis])**2, axis=-1) # result_dist_bays09_kappa_sum = np.nansum(result_dist_bays09_allT_avg[:, :, 0], axis=-1) # result_dist_bays09_kappa_T1_sum = result_dist_bays09_allT_avg[:, 0, 0] # result_dist_bays09_kappa_T25_sum = np.nansum(result_dist_bays09_allT_avg[:, 1:, 0], axis=-1) # # Square and KL distance for EM Mixtures # result_dist_bays09_emmixt_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, :, 1:], axis=-1), axis=-1) # result_dist_bays09_emmixt_T1_sum = np.nansum(result_dist_bays09_allT_avg[:, 0, 1:], axis=-1) # result_dist_bays09_emmixt_T25_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, 1:, 1:], axis=-1), axis=-1) # result_dist_bays09_emmixt_KL = utils.nanmean(utils.KL_div(result_em_fits_flat[:, :, 1:4], bays09_emmixt_target[np.newaxis, :, :, np.newaxis], axis=-2), axis=-1) # KL over dimension of mixtures, then mean over repetitions # result_dist_bays09_emmixt_KL_sum = np.nansum(result_dist_bays09_emmixt_KL, axis=-1) # sum over T # result_dist_bays09_emmixt_KL_T1_sum = result_dist_bays09_emmixt_KL[:, 0] # result_dist_bays09_emmixt_KL_T25_sum = np.nansum(result_dist_bays09_emmixt_KL[:, 1:], axis=-1) # result_dist_bays09_both_normalised = result_dist_bays09_emmixt_sum/np.max(result_dist_bays09_emmixt_sum) + result_dist_bays09_kappa_sum/np.max(result_dist_bays09_kappa_sum) # # Mask kappa for performance too bad # result_dist_bays09_kappa_sum_masked = np.ma.masked_greater(result_dist_bays09_kappa_sum, 2*np.median(result_dist_bays09_kappa_sum)) # result_dist_bays09_emmixt_KL_sum_masked = np.ma.masked_greater(result_dist_bays09_emmixt_KL_sum, 2*np.median(result_dist_bays09_emmixt_KL_sum)) # result_dist_bays09_both_normalised_mult_masked = 1-(1. - result_dist_bays09_emmixt_KL_sum/np.max(result_dist_bays09_emmixt_KL_sum))*(1. - result_dist_bays09_kappa_sum_masked/np.max(result_dist_bays09_kappa_sum_masked)) # Compute optimal rc_scale all_args = data_pbs.loaded_data['args_list'] specific_arg = all_args[0] specific_arg['autoset_parameters'] = True (_, _, _, sampler) = launchers.init_everything(specific_arg) optimal_rc_scale = sampler.random_network.rc_scale[0] if plots_all_T: # Show Kappa evolution wrt rc_scale f, ax = plt.subplots() # utils.plot_mean_std_from_samples(result_parameters_flat, np.nansum(result_em_kappa_allT, axis=-1), bins=60, bins_y=150, xlabel='rc_scale', ylabel='EM kappa', title='Kappa, summed T', ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, np.nansum(result_em_kappa_allT, axis=-1), window=35, xlabel='rc_scale', ylabel='EM kappa', title='Kappa, summed T', ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_kappa_summedT_{label}_{unique_id}.pdf') # Show Mixt proportions f, ax = plt.subplots() for i in xrange(3): # utils.plot_mean_std_from_samples(result_parameters_flat, np.nansum(result_em_emmixt_allT[..., i], axis=-1), bins=60, bins_y=100, xlabel='rc_scale', ylabel='EM mixt proportions', title='EM mixtures, summed T', ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, np.nansum(result_em_emmixt_allT[..., i], axis=-1), window=35, xlabel='rc_scale', ylabel='EM mixt proportions', title='EM mixtures, summed T', ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_mixtprop_summedT_{label}_{unique_id}.pdf') # Show Precision f, ax = plt.subplots() # utils.plot_mean_std_from_samples(result_parameters_flat, np.nansum(result_precisions_all_avg, axis=-1), bins=60, bins_y=150, xlabel='rc_scale', ylabel='Precision', title='Precision, summed T', ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, np.nansum(result_precisions_all_avg, axis=-1), window=35, xlabel='rc_scale', ylabel='Precision', title='Precision, summed T', ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_precision_summedT_{label}_{unique_id}.pdf') plt.close('all') if plots_per_T: for T_i, T in enumerate(T_space): # Show Kappa evolution wrt rc_scale f, ax = plt.subplots() # utils.plot_mean_std_from_samples(result_parameters_flat, result_em_kappa_allT[:, T_i], bins=40, bins_y=100, xlabel='rc_scale', ylabel='EM kappa', title='Kappa, T %d' % T, ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, result_em_kappa_allT[:, T_i], window=35, xlabel='rc_scale', ylabel='EM kappa', title='Kappa, T %d' % T, ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_kappa_T%d_{label}_{unique_id}.pdf' % T) # Show Mixt proportions f, ax = plt.subplots() for i in xrange(3): # utils.plot_mean_std_from_samples(result_parameters_flat, result_em_emmixt_allT[:, T_i, i], bins=40, bins_y=100, xlabel='rc_scale', ylabel='EM mixt proportions', title='EM mixtures, T %d' % T, ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, result_em_emmixt_allT[:, T_i, i], window=35, xlabel='rc_scale', ylabel='EM mixt proportions', title='EM mixtures, T %d' % T, ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_mixtprop_T%d_{label}_{unique_id}.pdf' % T) # Show Precision f, ax = plt.subplots() # utils.plot_mean_std_from_samples(result_parameters_flat, result_precisions_all_avg[:, T_i], bins=40, bins_y=100, xlabel='rc_scale', ylabel='Precision', title='Precision, T %d' % T, ax_handle=ax, show_scatter=False) utils.plot_mean_std_from_samples_rolling(result_parameters_flat, result_precisions_all_avg[:, T_i], window=35, xlabel='rc_scale', ylabel='Precision', title='Precision, T %d' % T, ax_handle=ax, show_scatter=False) ax.axvline(x=optimal_rc_scale, color='g', linewidth=2) ax.axvline(x=2*optimal_rc_scale, color='r', linewidth=2) f.canvas.draw() if savefigs: dataio.save_current_figure('rcscaleeffect_precision_T%d_{label}_{unique_id}.pdf' % T) plt.close('all') # # Interpolate # if plots_interpolate: # sigmax_target = 0.9 # M_interp_space = np.arange(6, 625, 5) # ratio_interp_space = np.linspace(0.01, 1.0, 50) # # sigmax_interp_space = np.linspace(0.01, 1.0, 50) # sigmax_interp_space = np.array([sigmax_target]) # params_crossspace = np.array(utils.cross(M_interp_space, ratio_interp_space, sigmax_interp_space)) # interpolated_data = rbf_interpolator(params_crossspace[:, 0], params_crossspace[:, 1], params_crossspace[:, 2]).reshape((M_interp_space.size, ratio_interp_space.size)) # utils.pcolor_2d_data(interpolated_data, M_interp_space, ratio_interp_space, 'M', 'ratio', 'interpolated, fixing sigmax= %.2f' % sigmax_target) # points_closeby = ((result_parameters_flat[:, 2] - sigmax_target)**2)< 0.01 # plt.figure() # # plt.imshow(interpolated_data, extent=(M_interp_space.min(), M_interp_space.max(), ratio_interp_space.min(), ratio_interp_space.max())) # plt.imshow(interpolated_data) # plt.scatter(result_parameters_flat[points_closeby, 0], result_parameters_flat[points_closeby, 1], s=100, c=result_fitexperiments_bic_avg[points_closeby], marker='o') # if plot_per_ratio: # # Plot the evolution of loglike as a function of sigmax, with std shown # for ratio_conj_i, ratio_conj in enumerate(ratio_space): # ax = utils.plot_mean_std_area(sigmax_space, result_log_posterior_mean[ratio_conj_i], result_log_posterior_std[ratio_conj_i]) # ax.get_figure().canvas.draw() # if savefigs: # dataio.save_current_figure('results_fitexp_%s_loglike_ratioconj%.2f_{label}_global_{unique_id}.pdf' % (exp_dataset, ratio_conj)) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['parameter_names_sorted'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='rcscale_characterisation') plt.show() return locals()
def plots_ratioMscaling(data_pbs, generator_module=None): ''' Reload and plot precision/fits of a Mixed code. ''' #### SETUP # savefigs = True savedata = True plots_pcolor_all = False plots_effect_M_target_kappa = False plots_kappa_fi_comparison = False plots_multiple_fisherinfo = False specific_plot_effect_R = 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(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_all_precisions_std = (utils.nanstd(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_em_fits_mean = (utils.nanmean(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) result_em_fits_std = (utils.nanstd(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) result_fisherinfo_mean = (utils.nanmean(data_pbs.dict_arrays['result_fisher_info']['results'], axis=-1)) result_fisherinfo_std = (utils.nanstd(data_pbs.dict_arrays['result_fisher_info']['results'], axis=-1)) all_args = data_pbs.loaded_data['args_list'] result_em_fits_kappa = result_em_fits_mean[..., 0] result_em_fits_target = result_em_fits_mean[..., 1] result_em_fits_kappa_valid = np.ma.masked_where(result_em_fits_target < 0.9, result_em_fits_kappa) M_space = data_pbs.loaded_data['parameters_uniques']['M'].astype(int) ratio_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] R_space = data_pbs.loaded_data['parameters_uniques']['R'].astype(int) num_repetitions = generator_module.num_repetitions print M_space print ratio_space print R_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) MAX_DISTANCE = 100. if plots_pcolor_all: # Do one pcolor for M and ratio per R for R_i, R in enumerate(R_space): # Check evolution of precision given M and ratio # utils.pcolor_2d_data(result_all_precisions_mean[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='precision, R=%d' % R) # if savefigs: # dataio.save_current_figure('pcolor_precision_R%d_log_{label}_{unique_id}.pdf' % R) # Show kappa try: utils.pcolor_2d_data(result_em_fits_kappa_valid[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='kappa, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_kappa_R%d_log_{label}_{unique_id}.pdf' % R) except ValueError: pass # Show probability on target # utils.pcolor_2d_data(result_em_fits_target[..., R_i], log_scale=False, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='target, R=%d' % R) # if savefigs: # dataio.save_current_figure('pcolor_target_R%d_{label}_{unique_id}.pdf' % R) # # Show Fisher info # utils.pcolor_2d_data(result_fisherinfo_mean[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='fisher info, R=%d' % R) # if savefigs: # dataio.save_current_figure('pcolor_fisherinfo_R%d_log_{label}_{unique_id}.pdf' % R) plt.close('all') if plots_effect_M_target_kappa: def plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa, R): f, ax = plt.subplots() ax.plot(M_space, ratio_target_kappa_given_M) ax.set_xlabel('M') ax.set_ylabel('Optimal ratio') ax.set_title('Optimal Ratio for kappa %d, R=%d' % (target_kappa, R)) if savefigs: dataio.save_current_figure('optratio_M_targetkappa%d_R%d_{label}_{unique_id}.pdf' % (target_kappa, R)) target_kappas = np.array([100, 200, 300, 500, 1000, 3000]) for R_i, R in enumerate(R_space): for target_kappa in target_kappas: dist_to_target_kappa = (result_em_fits_kappa[..., R_i] - target_kappa)**2. best_dist_to_target_kappa = np.argmin(dist_to_target_kappa, axis=1) ratio_target_kappa_given_M = np.ma.masked_where(dist_to_target_kappa[np.arange(dist_to_target_kappa.shape[0]), best_dist_to_target_kappa] > MAX_DISTANCE, ratio_space[best_dist_to_target_kappa]) # replot plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa, R) plt.close('all') if plots_kappa_fi_comparison: # result_em_fits_kappa and fisher info if True: for R_i, R in enumerate(R_space): for M_tot_selected_i, M_tot_selected in enumerate(M_space[::2]): # M_conj_space = ((1.-ratio_space)*M_tot_selected).astype(int) # M_feat_space = M_tot_selected - M_conj_space f, axes = plt.subplots(2, 1) axes[0].plot(ratio_space, result_em_fits_kappa[2*M_tot_selected_i, ..., R_i]) axes[0].set_xlabel('ratio') axes[0].set_title('Fitted kappa') axes[1].plot(ratio_space, utils.stddev_to_kappa(1./result_fisherinfo_mean[2*M_tot_selected_i, ..., R_i]**0.5)) axes[1].set_xlabel('ratio') axes[1].set_title('kappa_FI') f.suptitle('M_tot %d' % M_tot_selected, fontsize=15) f.set_tight_layout(True) if savefigs: dataio.save_current_figure('comparison_kappa_fisher_R%d_M%d_{label}_{unique_id}.pdf' % (R, M_tot_selected)) plt.close(f) if plots_multiple_fisherinfo: target_fisherinfos = np.array([100, 200, 300, 500, 1000]) for R_i, R in enumerate(R_space): for target_fisherinfo in target_fisherinfos: dist_to_target_fisherinfo = (result_fisherinfo_mean[..., R_i] - target_fisherinfo)**2. utils.pcolor_2d_data(dist_to_target_fisherinfo, log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='Fisher info, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_distfi%d_R%d_log_{label}_{unique_id}.pdf' % (target_fisherinfo, R)) plt.close('all') if specific_plot_effect_R: # Choose a M, find which ratio gives best fit to a given kappa M_target = 228 M_target_i = np.argmin(np.abs(M_space - M_target)) # target_kappa = np.ma.mean(result_em_fits_kappa_valid[M_target_i]) # target_kappa = 5*1e3 target_kappa = 1e3 dist_target_kappa = (result_em_fits_kappa_valid[M_target_i] - target_kappa)**2. utils.pcolor_2d_data(dist_target_kappa, log_scale=True, x=ratio_space, y=R_space, xlabel='ratio', ylabel='R', ylabel_format="%d", title='Kappa dist %.2f, M %d' % (target_kappa, M_target)) if savefigs: dataio.save_current_figure('pcolor_distkappa%d_M%d_log_{label}_{unique_id}.pdf' % (target_kappa, M_target)) all_args = data_pbs.loaded_data['args_list'] variables_to_save = [] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='higher_dimensions_R') plt.show() return locals()
def do_sigma_output_plot(variables_launcher_running, plotting_parameters): dataio = variables_launcher_running['dataio'] sigmaoutput_space = variables_launcher_running['sigmaoutput_space'] result_em_fits_mean = utils.nanmean(variables_launcher_running['result_em_fits'], axis=-1) result_em_fits_std = utils.nanstd(variables_launcher_running['result_em_fits'], axis=-1) plt.ion() # Memory curve kappa def sigmaoutput_plot_kappa(sigmaoutput_space, result_em_fits_mean, result_em_fits_std=None, exp_name='', ax=None): if ax is not None: plt.figure(ax.get_figure().number) ax.hold(False) ax = utils.plot_mean_std_area(sigmaoutput_space, result_em_fits_mean[..., 0], result_em_fits_std[..., 0], xlabel='sigma output', ylabel='Memory fidelity', linewidth=3, fmt='o-', markersize=8, label='Noise output effect', ax_handle=ax) ax.hold(True) ax.set_title("{{exp_name}} {T} {M} {ratio_conj:.2f} {sigmax:.3f} {sigmay:.2f}".format(**variables_launcher_running['all_parameters']).format(exp_name=exp_name)) ax.legend() # ax.set_xlim([0.9, T_space_exp.max()+0.1]) # ax.set_xticks(range(1, T_space_exp.max()+1)) # ax.set_xticklabels(range(1, T_space_exp.max()+1)) ax.get_figure().canvas.draw() dataio.save_current_figure('noiseoutput_kappa_%s_T{T}_M{M}_ratio{ratio_conj}_sigmax{sigmax}_sigmay{sigmay}_{{label}}_{{unique_id}}.pdf'.format(**variables_launcher_running['all_parameters']) % (exp_name)) return ax # Plot EM Mixtures proportions def sigmaoutput_plot_mixtures(sigmaoutput_space, result_em_fits_mean, result_em_fits_std, exp_name='', ax=None): if ax is None: _, ax = plt.subplots() if ax is not None: plt.figure(ax.get_figure().number) ax.hold(False) # mixture probabilities print result_em_fits_mean[..., 1] result_em_fits_mean[np.isnan(result_em_fits_mean)] = 0.0 result_em_fits_std[np.isnan(result_em_fits_std)] = 0.0 utils.plot_mean_std_area(sigmaoutput_space, result_em_fits_mean[..., 1], result_em_fits_std[..., 1], xlabel='sigma output', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Target') ax.hold(True) utils.plot_mean_std_area(sigmaoutput_space, result_em_fits_mean[..., 2], result_em_fits_std[..., 2], xlabel='sigma output', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Nontarget') utils.plot_mean_std_area(sigmaoutput_space, result_em_fits_mean[..., 3], result_em_fits_std[..., 3], xlabel='sigma output', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Random') ax.legend(prop={'size':15}) ax.set_title("{{exp_name}} {T} {M} {ratio_conj:.2f} {sigmax:.3f} {sigmay:.2f}".format(**variables_launcher_running['all_parameters']).format(exp_name=exp_name)) ax.set_ylim([0.0, 1.1]) ax.get_figure().canvas.draw() dataio.save_current_figure('memorycurves_emfits_%s_T{T}_M{M}_ratio{ratio_conj}_sigmax{sigmax}_sigmay{sigmay}_{{label}}_{{unique_id}}.pdf'.format(**variables_launcher_running['all_parameters']) % (exp_name)) return ax # Do plots plotting_parameters['axes']['ax_sigmaoutput_kappa'] = sigmaoutput_plot_kappa(sigmaoutput_space, result_em_fits_mean, result_em_fits_std, exp_name='kappa', ax=plotting_parameters['axes']['ax_sigmaoutput_kappa']) plotting_parameters['axes']['ax_sigmaoutput_mixtures'] = sigmaoutput_plot_mixtures(sigmaoutput_space, result_em_fits_mean, result_em_fits_std, exp_name='mixt probs', ax=plotting_parameters['axes']['ax_sigmaoutput_mixtures'])
def plots_ratioMscaling(data_pbs, generator_module=None): ''' Reload and plot precision/fits of a Mixed code. ''' #### SETUP # savefigs = True savedata = True plots_pcolor_all = False plots_effect_M_target_kappa = False plots_kappa_fi_comparison = False plots_multiple_fisherinfo = False specific_plot_effect_R = True specific_plot_effect_ratio_M = False convert_M_realsizes = False plots_pcolor_realsizes_Msubs = True plots_pcolor_realsizes_Mtot = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # interpolation_method = 'linear' interpolation_method = 'nearest' # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() result_all_precisions_mean = (utils.nanmean(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_all_precisions_std = (utils.nanstd(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_em_fits_mean = (utils.nanmean(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) result_em_fits_std = (utils.nanstd(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) result_fisherinfo_mean = (utils.nanmean(data_pbs.dict_arrays['result_fisher_info']['results'], axis=-1)) result_fisherinfo_std = (utils.nanstd(data_pbs.dict_arrays['result_fisher_info']['results'], axis=-1)) all_args = data_pbs.loaded_data['args_list'] result_em_fits_kappa = result_em_fits_mean[..., 0] result_em_fits_target = result_em_fits_mean[..., 1] result_em_fits_kappa_valid = np.ma.masked_where(result_em_fits_target < 0.8, result_em_fits_kappa) # flat versions result_parameters_flat = np.array(data_pbs.dict_arrays['result_all_precisions']['parameters_flat']) result_all_precisions_mean_flat = np.mean(np.array(data_pbs.dict_arrays['result_all_precisions']['results_flat']), axis=-1) result_em_fits_mean_flat = np.mean(np.array(data_pbs.dict_arrays['result_em_fits']['results_flat']), axis=-1) result_fisherinfor_mean_flat = np.mean(np.array(data_pbs.dict_arrays['result_fisher_info']['results_flat']), axis=-1) result_em_fits_kappa_flat = result_em_fits_mean_flat[..., 0] result_em_fits_target_flat = result_em_fits_mean_flat[..., 1] result_em_fits_kappa_valid_flat = np.ma.masked_where(result_em_fits_target_flat < 0.8, result_em_fits_kappa_flat) M_space = data_pbs.loaded_data['parameters_uniques']['M'].astype(int) ratio_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] R_space = data_pbs.loaded_data['parameters_uniques']['R'].astype(int) num_repetitions = generator_module.num_repetitions T = generator_module.T print M_space print ratio_space print R_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) MAX_DISTANCE = 100. if convert_M_realsizes: # alright, currently M*ratio_conj gives the conjunctive subpopulation, # but only floor(M_conj**1/R) neurons are really used. So we should # convert to M_conj_real and M_feat_real instead of M and ratio result_parameters_flat_subM_converted = [] result_parameters_flat_Mtot_converted = [] for params in result_parameters_flat: M = params[0]; ratio_conj = params[1]; R = int(params[2]) M_conj_prior = int(M*ratio_conj) M_conj_true = int(np.floor(M_conj_prior**(1./R))**R) M_feat_true = int(np.floor((M-M_conj_prior)/R)*R) # result_parameters_flat_subM_converted contains (M_conj, M_feat, R) result_parameters_flat_subM_converted.append(np.array([M_conj_true, M_feat_true, R])) # result_parameters_flat_Mtot_converted contains (M_tot, ratio_conj, R) result_parameters_flat_Mtot_converted.append(np.array([float(M_conj_true+M_feat_true), float(M_conj_true)/float(M_conj_true+M_feat_true), R])) result_parameters_flat_subM_converted = np.array(result_parameters_flat_subM_converted) result_parameters_flat_Mtot_converted = np.array(result_parameters_flat_Mtot_converted) if plots_pcolor_all: if convert_M_realsizes: def plot_interp(points, data, currR_indices, title='', points_label='', xlabel='', ylabel=''): utils.contourf_interpolate_data_interactive_maxvalue(points[currR_indices][..., :2], data[currR_indices], xlabel=xlabel, ylabel=ylabel, title='%s, R=%d' % (title, R), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False) if savefigs: dataio.save_current_figure('pcolortrueM%s_%s_R%d_log_%s_{label}_{unique_id}.pdf' % (points_label, title, R, interpolation_method)) all_datas = [dict(name='precision', data=result_all_precisions_mean_flat), dict(name='kappa', data=result_em_fits_kappa_flat), dict(name='kappavalid', data=result_em_fits_kappa_valid_flat), dict(name='target', data=result_em_fits_target_flat), dict(name='fisherinfo', data=result_fisherinfor_mean_flat)] all_points = [] if plots_pcolor_realsizes_Msubs: all_points.append(dict(name='sub', data=result_parameters_flat_subM_converted, xlabel='M_conj', ylabel='M_feat')) if plots_pcolor_realsizes_Mtot: all_points.append(dict(name='tot', data=result_parameters_flat_Mtot_converted, xlabel='Mtot', ylabel='ratio_conj')) for curr_points in all_points: for curr_data in all_datas: for R_i, R in enumerate(R_space): currR_indices = curr_points['data'][:, 2] == R plot_interp(curr_points['data'], curr_data['data'], currR_indices, title=curr_data['name'], points_label=curr_points['name'], xlabel=curr_points['xlabel'], ylabel=curr_points['ylabel']) else: # Do one pcolor for M and ratio per R for R_i, R in enumerate(R_space): # Check evolution of precision given M and ratio utils.pcolor_2d_data(result_all_precisions_mean[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='precision, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_precision_R%d_log_{label}_{unique_id}.pdf' % R) # Show kappa try: utils.pcolor_2d_data(result_em_fits_kappa_valid[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='kappa, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_kappa_R%d_log_{label}_{unique_id}.pdf' % R) except ValueError: pass # Show probability on target utils.pcolor_2d_data(result_em_fits_target[..., R_i], log_scale=False, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='target, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_target_R%d_{label}_{unique_id}.pdf' % R) # # Show Fisher info utils.pcolor_2d_data(result_fisherinfo_mean[..., R_i], log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='fisher info, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_fisherinfo_R%d_log_{label}_{unique_id}.pdf' % R) plt.close('all') if plots_effect_M_target_kappa: def plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa, R): f, ax = plt.subplots() ax.plot(M_space, ratio_target_kappa_given_M) ax.set_xlabel('M') ax.set_ylabel('Optimal ratio') ax.set_title('Optimal Ratio for kappa %d, R=%d' % (target_kappa, R)) if savefigs: dataio.save_current_figure('optratio_M_targetkappa%d_R%d_{label}_{unique_id}.pdf' % (target_kappa, R)) target_kappas = np.array([100, 200, 300, 500, 1000, 3000]) for R_i, R in enumerate(R_space): for target_kappa in target_kappas: dist_to_target_kappa = (result_em_fits_kappa[..., R_i] - target_kappa)**2. best_dist_to_target_kappa = np.argmin(dist_to_target_kappa, axis=1) ratio_target_kappa_given_M = np.ma.masked_where(dist_to_target_kappa[np.arange(dist_to_target_kappa.shape[0]), best_dist_to_target_kappa] > MAX_DISTANCE, ratio_space[best_dist_to_target_kappa]) # replot plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa, R) plt.close('all') if plots_kappa_fi_comparison: # result_em_fits_kappa and fisher info if True: for R_i, R in enumerate(R_space): for M_tot_selected_i, M_tot_selected in enumerate(M_space[::2]): # M_conj_space = ((1.-ratio_space)*M_tot_selected).astype(int) # M_feat_space = M_tot_selected - M_conj_space f, axes = plt.subplots(2, 1) axes[0].plot(ratio_space, result_em_fits_kappa[2*M_tot_selected_i, ..., R_i]) axes[0].set_xlabel('ratio') axes[0].set_title('Fitted kappa') axes[1].plot(ratio_space, utils.stddev_to_kappa(1./result_fisherinfo_mean[2*M_tot_selected_i, ..., R_i]**0.5)) axes[1].set_xlabel('ratio') axes[1].set_title('kappa_FI') f.suptitle('M_tot %d' % M_tot_selected, fontsize=15) f.set_tight_layout(True) if savefigs: dataio.save_current_figure('comparison_kappa_fisher_R%d_M%d_{label}_{unique_id}.pdf' % (R, M_tot_selected)) plt.close(f) if plots_multiple_fisherinfo: target_fisherinfos = np.array([100, 200, 300, 500, 1000]) for R_i, R in enumerate(R_space): for target_fisherinfo in target_fisherinfos: dist_to_target_fisherinfo = (result_fisherinfo_mean[..., R_i] - target_fisherinfo)**2. utils.pcolor_2d_data(dist_to_target_fisherinfo, log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='Fisher info, R=%d' % R) if savefigs: dataio.save_current_figure('pcolor_distfi%d_R%d_log_{label}_{unique_id}.pdf' % (target_fisherinfo, R)) plt.close('all') if specific_plot_effect_R: M_target = 356 if convert_M_realsizes: M_tot_target = M_target delta_around_target = 30 filter_points_totM_indices = np.abs(result_parameters_flat_Mtot_converted[:, 0] - M_tot_target) < delta_around_target # first check landscape utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_Mtot_converted[filter_points_totM_indices][..., 1:], result_em_fits_kappa_flat[filter_points_totM_indices], xlabel='ratio_conj', ylabel='R', title='kappa, M_target=%d +- %d' % (M_tot_target, delta_around_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, show_slider=False) if savefigs: dataio.save_current_figure('specific_pcolortrueMtot_kappa_M%d_log_%s_{label}_{unique_id}.pdf' % (M_tot_target, interpolation_method)) # Then plot distance to specific kappa # target_kappa = 1.2e3 target_kappa = 580 dist_target_kappa_flat = np.abs(result_em_fits_kappa_flat - target_kappa) mask_greater_than = 5e3 utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_Mtot_converted[filter_points_totM_indices][..., 1:], dist_target_kappa_flat[filter_points_totM_indices], xlabel='ratio_conj', ylabel='R', title='dist kappa %d, M_target=%d +- %d' % (target_kappa, M_tot_target, delta_around_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, mask_greater_than=mask_greater_than, mask_smaller_than=0) if savefigs: dataio.save_current_figure('specific_pcolortrueMtot_distkappa%d_M%d_log_%s_{label}_{unique_id}.pdf' % (target_kappa, M_tot_target, interpolation_method)) else: # Choose a M, find which ratio gives best fit to a given kappa M_target_i = np.argmin(np.abs(M_space - M_target)) utils.pcolor_2d_data(result_em_fits_kappa[M_target_i], log_scale=True, x=ratio_space, y=R_space, xlabel='ratio', ylabel='R', ylabel_format="%d", title='Kappa, M %d' % (M_target)) plt.gcf().set_tight_layout(True) plt.gcf().canvas.draw() if savefigs: dataio.save_current_figure('specific_Reffect_pcolor_kappa_M%dT%d_log_{label}_{unique_id}.pdf' % (M_target, T)) # target_kappa = np.ma.mean(result_em_fits_kappa_valid[M_target_i]) # target_kappa = 5*1e3 # target_kappa = 1.2e3 target_kappa = 580 # dist_target_kappa = np.abs(result_em_fits_kappa_valid[M_target_i] - target_kappa) dist_target_kappa = result_em_fits_kappa[M_target_i]/target_kappa dist_target_kappa[dist_target_kappa > 2.0] = 2.0 # dist_target_kappa[dist_target_kappa < 0.5] = 0.5 utils.pcolor_2d_data(dist_target_kappa, log_scale=False, x=ratio_space, y=R_space, xlabel='ratio', ylabel='R', ylabel_format="%d", title='Kappa dist %.2f, M %d' % (target_kappa, M_target), cmap='RdBu_r') plt.gcf().set_tight_layout(True) plt.gcf().canvas.draw() if savefigs: dataio.save_current_figure('specific_Reffect_pcolor_distkappa%d_M%dT%d_log_{label}_{unique_id}.pdf' % (target_kappa, M_target, T)) # Plot the probability of being on-target utils.pcolor_2d_data(result_em_fits_target[M_target_i], log_scale=False, x=ratio_space, y=R_space, xlabel='ratio', ylabel='R', ylabel_format="%d", title='target mixture proportion, M %d' % (M_target), vmin=0.0, vmax=1.0) #cmap='RdBu_r' plt.gcf().set_tight_layout(True) plt.gcf().canvas.draw() if savefigs: dataio.save_current_figure('specific_Reffect_pcolor_target_M%dT%d_{label}_{unique_id}.pdf' % (M_target, T)) if specific_plot_effect_ratio_M: # try to do the same plots as in ratio_scaling_M but with the current data R_target = 2 interpolation_method = 'cubic' mask_greater_than = 1e3 if convert_M_realsizes: # filter_points_targetR_indices = np.abs(result_parameters_flat_Mtot_converted[:, -1] - R_target) == 0 filter_points_targetR_indices = np.abs(result_parameters_flat_subM_converted[:, -1] - R_target) == 0 # utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., :-1], result_em_fits_kappa_flat[filter_points_targetR_indices], xlabel='M', ylabel='ratio_conj', title='kappa, R=%d' % (R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False) utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_subM_converted[filter_points_targetR_indices][..., :-1], result_em_fits_kappa_flat[filter_points_targetR_indices], xlabel='M_conj', ylabel='M_feat', title='kappa, R=%d' % (R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, show_slider=False) if savefigs: dataio.save_current_figure('specific_ratioM_pcolorsubM_kappa_R%d_log_%s_{label}_{unique_id}.pdf' % (R_target, interpolation_method)) # Then plot distance to specific kappa target_kappa = 580 # target_kappa = 2000 dist_target_kappa_flat = np.abs(result_em_fits_kappa_flat - target_kappa) dist_target_kappa_flat = result_em_fits_kappa_flat/target_kappa dist_target_kappa_flat[dist_target_kappa_flat > 1.45] = 1.45 dist_target_kappa_flat[dist_target_kappa_flat < 0.5] = 0.5 # utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., :-1], dist_target_kappa_flat[filter_points_targetR_indices], xlabel='M', ylabel='ratio_conj', title='kappa, R=%d' % (R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, mask_smaller_than=0, show_slider=False, mask_greater_than =mask_greater_than) # utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat[filter_points_targetR_indices][..., :-1], dist_target_kappa_flat[filter_points_targetR_indices], xlabel='M', ylabel='ratio_conj', title='kappa, R=%d' % (R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, mask_smaller_than=0, show_slider=False, mask_greater_than =mask_greater_than) utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_subM_converted[filter_points_targetR_indices][..., :-1], dist_target_kappa_flat[filter_points_targetR_indices], xlabel='M_conj', ylabel='M_feat', title='Kappa dist %.2f, R=%d' % (target_kappa, R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, mask_greater_than=mask_greater_than, mask_smaller_than=0, show_slider=False) if savefigs: dataio.save_current_figure('specific_ratioM_pcolorsubM_distkappa%d_R%d_log_%s_{label}_{unique_id}.pdf' % (target_kappa, R_target, interpolation_method)) ## Scatter plot, works better... f, ax = plt.subplots() ss = ax.scatter(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0], result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1], 500, c=(dist_target_kappa_flat[filter_points_targetR_indices]), norm=matplotlib.colors.LogNorm()) plt.colorbar(ss) ax.set_xlabel('M') ax.set_ylabel('ratio') ax.set_xlim((result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0].min()*0.8, result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0].max()*1.03)) ax.set_ylim((-0.05, result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1].max()*1.05)) ax.set_title('Kappa dist %.2f, R=%d' % (target_kappa, R_target)) if savefigs: dataio.save_current_figure('specific_ratioM_scattertotM_distkappa%d_R%d_log_%s_{label}_{unique_id}.pdf' % (target_kappa, R_target, interpolation_method)) ## Spline interpolation distkappa_spline_int_params = spint.bisplrep(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0], result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1], np.log(dist_target_kappa_flat[filter_points_targetR_indices]), kx=3, ky=3, s=1) if True: M_interp_space = np.linspace(100, 740, 100) ratio_interp_space = np.linspace(0.0, 1.0, 100) # utils.pcolor_2d_data(spint.bisplev(M_interp_space, ratio_interp_space, distkappa_spline_int_params), y=ratio_interp_space, x=M_interp_space, ylabel='ratio', xlabel='M', xlabel_format="%d", title='Kappa dist %.2f, R %d' % (target_kappa, R_target), ticks_interpolate=11) utils.pcolor_2d_data(np.exp(spint.bisplev(M_interp_space, ratio_interp_space, distkappa_spline_int_params)), y=ratio_interp_space, x=M_interp_space, ylabel='ratio conjunctivity', xlabel='M', xlabel_format="%d", title='Ratio kappa/%.2f, R %d' % (target_kappa, R_target), ticks_interpolate=11, log_scale=False, cmap='RdBu_r') # plt.scatter(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0], result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1], marker='o', c='b', s=5) # plt.scatter(np.argmin(np.abs(M_interp_space[:, np.newaxis] - result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0]), axis=0), np.argmin(np.abs(ratio_interp_space[:, np.newaxis] - result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1]), axis=0), marker='o', c='b', s=5) else: M_interp_space = M_space ratio_interp_space = ratio_space utils.pcolor_2d_data(spint.bisplev(M_interp_space, ratio_interp_space, distkappa_spline_int_params), y=ratio_interp_space, x=M_interp_space, ylabel='ratio', xlabel='M', xlabel_format="%d", title='Kappa dist %.2f, R %d' % (target_kappa, R_target)) plt.gcf().set_tight_layout(True) plt.gcf().canvas.draw() if savefigs: dataio.save_current_figure('specific_ratioM_pcolorsplinetotM_distkappa%d_R%d_log_%s_{label}_{unique_id}.pdf' % (target_kappa, R_target, interpolation_method)) ### Distance to Fisher Info target_fi = 2*target_kappa dist_target_fi_flat = np.abs(result_fisherinfor_mean_flat - target_fi) dist_target_fi_flat = result_fisherinfor_mean_flat/target_fi dist_target_fi_flat[dist_target_fi_flat > 1.45] = 1.45 dist_target_fi_flat[dist_target_fi_flat < 0.5] = 0.5 # utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., :-1], dist_target_kappa_flat[filter_points_targetR_indices], xlabel='M', ylabel='ratio_conj', title='kappa, R=%d' % (R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False) utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat_subM_converted[filter_points_targetR_indices][..., :-1], dist_target_fi_flat[filter_points_targetR_indices], xlabel='M_conj', ylabel='M_feat', title='FI dist %.2f, R=%d' % (target_fi, R_target), interpolation_numpoints=200, interpolation_method=interpolation_method, log_scale=False, mask_greater_than=mask_greater_than, show_slider=False) if savefigs: dataio.save_current_figure('specific_ratioM_pcolorsubM_distfi%d_R%d_log_%s_{label}_{unique_id}.pdf' % (target_fi, R_target, interpolation_method)) distFI_spline_int_params = spint.bisplrep(result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 0], result_parameters_flat_Mtot_converted[filter_points_targetR_indices][..., 1], np.log(dist_target_fi_flat[filter_points_targetR_indices]), kx=3, ky=3, s=1) M_interp_space = np.linspace(100, 740, 100) ratio_interp_space = np.linspace(0.0, 1.0, 100) # utils.pcolor_2d_data(spint.bisplev(M_interp_space, ratio_interp_space, spline_int_params), y=ratio_interp_space, x=M_interp_space, ylabel='ratio', xlabel='M', xlabel_format="%d", title='Kappa dist %.2f, R %d' % (target_kappa, R_target), ticks_interpolate=11) utils.pcolor_2d_data(np.exp(spint.bisplev(M_interp_space, ratio_interp_space, distFI_spline_int_params)), y=ratio_interp_space, x=M_interp_space, ylabel='ratio conjunctivity', xlabel='M', xlabel_format="%d", title='Ratio FI/%.2f, R %d' % (target_fi, R_target), ticks_interpolate=11, log_scale=False, cmap='RdBu_r') plt.gcf().set_tight_layout(True) plt.gcf().canvas.draw() if savefigs: dataio.save_current_figure('specific_ratioM_pcolorsplinetotM_distfi%d_R%d_log_%s_{label}_{unique_id}.pdf' % (target_fi, R_target, interpolation_method)) else: R_target_i = np.argmin(np.abs(R_space - R_target)) utils.pcolor_2d_data(result_em_fits_kappa_valid[..., R_target_i], log_scale=True, y=ratio_space, x=M_space, ylabel='ratio', xlabel='M', xlabel_format="%d", title='Kappa, R %d' % (R_target)) if savefigs: dataio.save_current_figure('specific_ratioM_pcolor_kappa_R%d_log_{label}_{unique_id}.pdf' % (R_target)) # target_kappa = np.ma.mean(result_em_fits_kappa_valid[R_target_i]) # target_kappa = 5*1e3 target_kappa = 580 dist_target_kappa = np.ma.masked_greater(np.abs(result_em_fits_kappa_valid[..., R_target_i] - target_kappa), mask_greater_than*5) utils.pcolor_2d_data(dist_target_kappa, log_scale=True, y=ratio_space, x=M_space, ylabel='ratio', xlabel='M', xlabel_format="%d", title='Kappa dist %.2f, R %d' % (target_kappa, R_target)) if savefigs: dataio.save_current_figure('specific_ratioM_pcolor_distkappa%d_R%d_log_{label}_{unique_id}.pdf' % (target_kappa, R_target)) all_args = data_pbs.loaded_data['args_list'] variables_to_save = [] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='higher_dimensions_R') 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_fit_mixturemodels_random(data_pbs, generator_module=None): ''' Reload runs from PBS ''' #### SETUP # savefigs = True savedata = True savemovies = True plots_dist_bays09 = True plots_per_T = True plots_interpolate = False # do_relaunch_bestparams_pbs = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", data_pbs.dataset_infos['parameters'] # parameters: M, ratio_conj, sigmax # Extract data T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] result_em_fits_flat = np.array(data_pbs.dict_arrays['result_em_fits']['results_flat']) result_dist_bays09_flat = np.array(data_pbs.dict_arrays['result_dist_bays09']['results_flat']) result_dist_gorgo11_flat = np.array(data_pbs.dict_arrays['result_dist_gorgo11']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_em_fits']['parameters_flat']) sigmaoutput_space = data_pbs.loaded_data['parameters_uniques']['sigma_output'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] ratio_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] num_repetitions = generator_module.num_repetitions parameter_names_sorted = data_pbs.dataset_infos['parameters'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Load bays09 data_bays09 = load_experimental_data.load_data_bays09(fit_mixture_model=True) bays09_nitems = data_bays09['data_to_fit']['n_items'] bays09_em_target = np.nan*np.empty((bays09_nitems.max(), 4)) #kappa, prob_target, prob_nontarget, prob_random bays09_em_target[bays09_nitems - 1] = data_bays09['em_fits_nitems_arrays']['mean'].T bays09_emmixt_target = bays09_em_target[:, 1:] ## Compute some stuff # result_dist_bays09_kappa_T1_avg = utils.nanmean(result_dist_bays09_flat[:, 0, 0], axis=-1) # result_dist_bays09_kappa_allT_avg = np.nansum(utils.nanmean(result_dist_bays09_flat[:, :, 0], axis=-1), axis=1) # Square distance to kappa result_dist_bays09_allT_avg = utils.nanmean((result_em_fits_flat[:, :, :4] - bays09_em_target[np.newaxis, :, :, np.newaxis])**2, axis=-1) result_dist_bays09_kappa_sum = np.nansum(result_dist_bays09_allT_avg[:, :, 0], axis=-1) result_dist_bays09_kappa_T1_sum = result_dist_bays09_allT_avg[:, 0, 0] result_dist_bays09_kappa_T25_sum = np.nansum(result_dist_bays09_allT_avg[:, 1:, 0], axis=-1) # Square and KL distance for EM Mixtures result_dist_bays09_emmixt_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, :, 1:], axis=-1), axis=-1) result_dist_bays09_emmixt_T1_sum = np.nansum(result_dist_bays09_allT_avg[:, 0, 1:], axis=-1) result_dist_bays09_emmixt_T25_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, 1:, 1:], axis=-1), axis=-1) result_dist_bays09_emmixt_KL = utils.nanmean(utils.KL_div(result_em_fits_flat[:, :, 1:4], bays09_emmixt_target[np.newaxis, :, :, np.newaxis], axis=-2), axis=-1) # KL over dimension of mixtures, then mean over repetitions result_dist_bays09_emmixt_KL_sum = np.nansum(result_dist_bays09_emmixt_KL, axis=-1) # sum over T result_dist_bays09_emmixt_KL_T1_sum = result_dist_bays09_emmixt_KL[:, 0] result_dist_bays09_emmixt_KL_T25_sum = np.nansum(result_dist_bays09_emmixt_KL[:, 1:], axis=-1) result_dist_bays09_both_normalised = result_dist_bays09_emmixt_sum/np.max(result_dist_bays09_emmixt_sum) + result_dist_bays09_kappa_sum/np.max(result_dist_bays09_kappa_sum) if plots_dist_bays09: nb_best_points = 30 size_normal_points = 8 size_best_points = 50 def plot_scatter(all_vars, result_dist_to_use_name, title='', log_color=True, downsampling=1, label_file=''): fig = plt.figure() ax = Axes3D(fig) result_dist_to_use = all_vars[result_dist_to_use_name] if not log_color: result_dist_to_use = np.exp(result_dist_to_use) utils.scatter3d(result_parameters_flat[:, 0], result_parameters_flat[:, 1], result_parameters_flat[:, 2], s=size_normal_points, c=np.log(result_dist_to_use), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[2], title=title, ax_handle=ax) best_points_result_dist_to_use = np.argsort(result_dist_to_use)[:nb_best_points] utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, 0], result_parameters_flat[best_points_result_dist_to_use, 1], result_parameters_flat[best_points_result_dist_to_use, 2], c='r', s=size_best_points, ax_handle=ax) print "Best points, %s:" % title print '\n'.join(['sigma output %.2f, ratio %.2f, sigmax %.2f: %f' % (result_parameters_flat[i, 0], result_parameters_flat[i, 1], result_parameters_flat[i, 2], result_dist_to_use[i]) for i in best_points_result_dist_to_use]) if savefigs: dataio.save_current_figure('scatter3d_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, label_file)) if savemovies: try: utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s%s_{label}_{unique_id}.mp4' % (result_dist_to_use_name, label_file)), bitrate=8000, min_duration=8) utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s%s_{label}_{unique_id}.gif' % (result_dist_to_use_name, label_file)), nb_frames=30, min_duration=8) except Exception: # Most likely wrong aggregator... print "failed when creating movies for ", result_dist_to_use_name ax.view_init(azim=90, elev=10) dataio.save_current_figure('scatter3d_view2_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, label_file)) return ax # Distance for kappa, all T plot_scatter(locals(), 'result_dist_bays09_kappa_sum', 'kappa all T') # Distance for em fits, all T, Squared distance plot_scatter(locals(), 'result_dist_bays09_emmixt_sum', 'em fits, all T') # Distance for em fits, all T, KL distance plot_scatter(locals(), 'result_dist_bays09_emmixt_KL_sum', 'em fits, all T, KL') # Distance for sum of normalised em fits + normalised kappa, all T plot_scatter(locals(), 'result_dist_bays09_both_normalised', 'summed normalised em mixt + kappa') # Distance kappa T = 1 plot_scatter(locals(), 'result_dist_bays09_kappa_T1_sum', 'Kappa T=1') # Distance kappa T = 2...5 plot_scatter(locals(), 'result_dist_bays09_kappa_T25_sum', 'Kappa T=2/5') # Distance em fits T = 1 plot_scatter(locals(), 'result_dist_bays09_emmixt_T1_sum', 'em fits T=1') # Distance em fits T = 2...5 plot_scatter(locals(), 'result_dist_bays09_emmixt_T25_sum', 'em fits T=2/5') # Distance em fits T = 1, KL plot_scatter(locals(), 'result_dist_bays09_emmixt_KL_T1_sum', 'em fits T=1, KL') # Distance em fits T = 2...5, KL plot_scatter(locals(), 'result_dist_bays09_emmixt_KL_T25_sum', 'em fits T=2/5, KL') if plots_per_T: for T_i, T in enumerate(T_space): # Kappa per T, fit to Bays09 result_dist_bays09_kappa_currT = result_dist_bays09_allT_avg[:, T_i, 0] result_dist_bays09_kappa_currT_masked = mask_outliers(result_dist_bays09_kappa_currT) plot_scatter(locals(), 'result_dist_bays09_kappa_currT_masked', 'kappa T %d masked' % T, label_file="T{}".format(T)) # EM Mixt per T, fit to Bays09 result_dist_bays09_emmixt_sum_currT = np.nansum(result_dist_bays09_allT_avg[:, T_i, 1:], axis=-1) result_dist_bays09_emmixt_sum_currT_masked = mask_outliers(result_dist_bays09_emmixt_sum_currT) plot_scatter(locals(), 'result_dist_bays09_emmixt_sum_currT_masked', 'EM mixt T %d masked' % T, label_file="T{}".format(T)) # EM Mixt per T, fit to Bays09 KL divergence result_dist_bays09_emmixt_KL_sum_currT = result_dist_bays09_emmixt_KL[:, T_i] plot_scatter(locals(), 'result_dist_bays09_emmixt_KL_sum_currT', 'KL EM mixt T %d masked' % T, label_file="T{}".format(T)) # # Interpolate # if plots_interpolate: # sigmax_target = 0.9 # M_interp_space = np.arange(6, 625, 5) # ratio_interp_space = np.linspace(0.01, 1.0, 50) # # sigmax_interp_space = np.linspace(0.01, 1.0, 50) # sigmax_interp_space = np.array([sigmax_target]) # params_crossspace = np.array(utils.cross(M_interp_space, ratio_interp_space, sigmax_interp_space)) # interpolated_data = rbf_interpolator(params_crossspace[:, 0], params_crossspace[:, 1], params_crossspace[:, 2]).reshape((M_interp_space.size, ratio_interp_space.size)) # utils.pcolor_2d_data(interpolated_data, M_interp_space, ratio_interp_space, 'M', 'ratio', 'interpolated, fixing sigmax= %.2f' % sigmax_target) # points_closeby = ((result_parameters_flat[:, 2] - sigmax_target)**2)< 0.01 # plt.figure() # # plt.imshow(interpolated_data, extent=(M_interp_space.min(), M_interp_space.max(), ratio_interp_space.min(), ratio_interp_space.max())) # plt.imshow(interpolated_data) # plt.scatter(result_parameters_flat[points_closeby, 0], result_parameters_flat[points_closeby, 1], s=100, c=result_fitexperiments_bic_avg[points_closeby], marker='o') # if plot_per_ratio: # # Plot the evolution of loglike as a function of sigmax, with std shown # for ratio_conj_i, ratio_conj in enumerate(ratio_space): # ax = utils.plot_mean_std_area(sigmax_space, result_log_posterior_mean[ratio_conj_i], result_log_posterior_std[ratio_conj_i]) # ax.get_figure().canvas.draw() # if savefigs: # dataio.save_current_figure('results_fitexp_%s_loglike_ratioconj%.2f_{label}_global_{unique_id}.pdf' % (exp_dataset, ratio_conj)) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['parameter_names_sorted'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='output_noise') 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_fitting_experiments_random(data_pbs, generator_module=None): ''' Reload 2D volume runs from PBS and plot them ''' #### SETUP # savefigs = True savedata = True savemovies = True plots_per_T = True scatter3d_all_T = True # do_relaunch_bestparams_pbs = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() # parameters: ratio_conj, sigmax, T # Extract data result_fitexperiments_flat = np.array(data_pbs.dict_arrays['result_fitexperiments']['results_flat']) result_fitexperiments_all_flat = np.array(data_pbs.dict_arrays['result_fitexperiments_all']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_fitexperiments']['parameters_flat']) # Extract order of datasets experiment_ids = data_pbs.loaded_data['datasets_list'][0]['fitexperiment_parameters']['experiment_ids'] parameter_names_sorted = data_pbs.dataset_infos['parameters'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Compute some stuff result_fitexperiments_bic_avg = utils.nanmean(result_fitexperiments_flat[:,0], axis=-1) T_space = np.unique(result_parameters_flat[:, 2]) if plots_per_T: for T in T_space: currT_indices = result_parameters_flat[:, 2] == T utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat[currT_indices][..., :2], result_fitexperiments_bic_avg[currT_indices], xlabel='Ratio_conj', ylabel='sigma x', title='BIC, T %d' % T, interpolation_numpoints=200, interpolation_method='nearest', log_scale=False) if savefigs: dataio.save_current_figure('contourf_fittingexp_bic_random_ratiosigma_T%d_{label}_{unique_id}.pdf' % (T)) if scatter3d_all_T: nb_best_points_per_T = 2 size_normal_points = 8 size_best_points = 50 # Get the best points for a specific T def best_points_T(result_dist_to_use, T): currT_indices = result_parameters_flat[:, 2] == T best_points_indices_currT = np.argsort(result_dist_to_use[currT_indices])[:nb_best_points_per_T] # Indirect it all return np.nonzero(currT_indices)[0][best_points_indices_currT] def best_points_allT(result_dist_to_use): return np.array(utils.flatten_list([best_points_T(result_dist_to_use, T) for T in T_space])) def plot_scatter(all_vars, result_dist_to_use_name, title=''): fig = plt.figure() ax = Axes3D(fig) result_dist_to_use = all_vars[result_dist_to_use_name] utils.scatter3d(result_parameters_flat[:, 0], result_parameters_flat[:, 1], result_parameters_flat[:, 2], s=size_normal_points, c=np.log(result_dist_to_use), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[2], title=title, ax_handle=ax) best_points_result_dist_to_use = best_points_allT(result_dist_to_use) utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, 0], result_parameters_flat[best_points_result_dist_to_use, 1], result_parameters_flat[best_points_result_dist_to_use, 2], c='r', s=size_best_points, ax_handle=ax) print "Best points, %s:" % title print '\n'.join(['ratio %.2f, sigmax %.2f, T %d: %f' % (result_parameters_flat[i, 0], result_parameters_flat[i, 1], result_parameters_flat[i, 2], result_dist_to_use[i]) for i in best_points_result_dist_to_use]) if savefigs: dataio.save_current_figure('scatter3d_%s_{label}_{unique_id}.pdf' % result_dist_to_use_name) if savemovies: try: utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_{label}_{unique_id}.mp4' % result_dist_to_use_name), bitrate=8000, min_duration=8) utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_{label}_{unique_id}.gif' % result_dist_to_use_name), nb_frames=30, min_duration=8) except Exception: # Most likely wrong aggregator... print "failed when creating movies for ", result_dist_to_use_name ax.view_init(azim=90, elev=10) dataio.save_current_figure('scatter3d_view2_%s_{label}_{unique_id}.pdf' % result_dist_to_use_name) return ax plot_scatter(locals(), 'result_fitexperiments_bic_avg', 'BIC all T') # if plot_per_ratio: # # Plot the evolution of loglike as a function of sigmax, with std shown # for ratio_conj_i, ratio_conj in enumerate(ratio_space): # ax = utils.plot_mean_std_area(sigmax_space, result_log_posterior_mean[ratio_conj_i], result_log_posterior_std[ratio_conj_i]) # ax.get_figure().canvas.draw() # if savefigs: # dataio.save_current_figure('results_fitexp_%s_loglike_ratioconj%.2f_{label}_global_{unique_id}.pdf' % (exp_dataset, ratio_conj)) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['experiment_ids', 'parameter_names_sorted'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='fitting_experiments') 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_ratioMscaling(data_pbs, generator_module=None): ''' Reload and plot precision/fits of a Mixed code. ''' #### SETUP # savefigs = True savedata = True plots_pcolor_all = False plots_effect_M_target_precision = False plots_multiple_precisions = False plots_effect_M_target_kappa = False plots_subpopulations_effects = False plots_subpopulations_effects_kappa_fi = True 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() result_all_precisions_mean = (utils.nanmean(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_all_precisions_std = (utils.nanstd(data_pbs.dict_arrays['result_all_precisions']['results'], axis=-1)) result_em_fits_mean = (utils.nanmean(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) result_em_fits_std = (utils.nanstd(data_pbs.dict_arrays['result_em_fits']['results'], axis=-1)) all_args = data_pbs.loaded_data['args_list'] result_em_fits_kappa = result_em_fits_mean[..., 0] M_space = data_pbs.loaded_data['parameters_uniques']['M'].astype(int) ratio_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] num_repetitions = generator_module.num_repetitions print M_space print ratio_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) target_precision = 100. dist_to_target_precision = (result_all_precisions_mean - target_precision)**2. best_dist_to_target_precision = np.argmin(dist_to_target_precision, axis=1) MAX_DISTANCE = 100. ratio_target_precision_given_M = np.ma.masked_where(dist_to_target_precision[np.arange(dist_to_target_precision.shape[0]), best_dist_to_target_precision] > MAX_DISTANCE, ratio_space[best_dist_to_target_precision]) if plots_pcolor_all: # Check evolution of precision given M and ratio utils.pcolor_2d_data(result_all_precisions_mean, log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='precision wrt M / ratio') if savefigs: dataio.save_current_figure('precision_log_pcolor_{label}_{unique_id}.pdf') # See distance to target precision evolution utils.pcolor_2d_data(dist_to_target_precision, log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='Dist to target precision %d' % target_precision) if savefigs: dataio.save_current_figure('dist_targetprecision_log_pcolor_{label}_{unique_id}.pdf') # Show kappa utils.pcolor_2d_data(result_em_fits_kappa, log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='kappa wrt M / ratio') if savefigs: dataio.save_current_figure('kappa_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data((result_em_fits_kappa - 200)**2., log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='dist to kappa') if savefigs: dataio.save_current_figure('dist_kappa_log_pcolor_{label}_{unique_id}.pdf') if plots_effect_M_target_precision: def plot_ratio_target_precision(ratio_target_precision_given_M, target_precision): f, ax = plt.subplots() ax.plot(M_space, ratio_target_precision_given_M) ax.set_xlabel('M') ax.set_ylabel('Optimal ratio') ax.set_title('Optimal Ratio for precison %d' % target_precision) if savefigs: dataio.save_current_figure('effect_ratio_M_targetprecision%d_{label}_{unique_id}.pdf' % target_precision) plot_ratio_target_precision(ratio_target_precision_given_M, target_precision) if plots_multiple_precisions: target_precisions = np.array([100, 200, 300, 500, 1000]) for target_precision in target_precisions: dist_to_target_precision = (result_all_precisions_mean - target_precision)**2. best_dist_to_target_precision = np.argmin(dist_to_target_precision, axis=1) ratio_target_precision_given_M = np.ma.masked_where(dist_to_target_precision[np.arange(dist_to_target_precision.shape[0]), best_dist_to_target_precision] > MAX_DISTANCE, ratio_space[best_dist_to_target_precision]) # replot plot_ratio_target_precision(ratio_target_precision_given_M, target_precision) if plots_effect_M_target_kappa: def plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa): f, ax = plt.subplots() ax.plot(M_space, ratio_target_kappa_given_M) ax.set_xlabel('M') ax.set_ylabel('Optimal ratio') ax.set_title('Optimal Ratio for precison %d' % target_kappa) if savefigs: dataio.save_current_figure('effect_ratio_M_targetkappa%d_{label}_{unique_id}.pdf' % target_kappa) target_kappa = np.array([100, 200, 300, 500, 1000, 3000]) for target_kappa in target_kappa: dist_to_target_kappa = (result_em_fits_kappa - target_kappa)**2. best_dist_to_target_kappa = np.argmin(dist_to_target_kappa, axis=1) ratio_target_kappa_given_M = np.ma.masked_where(dist_to_target_kappa[np.arange(dist_to_target_kappa.shape[0]), best_dist_to_target_kappa] > MAX_DISTANCE, ratio_space[best_dist_to_target_kappa]) # replot plot_ratio_target_kappa(ratio_target_kappa_given_M, target_kappa) if plots_subpopulations_effects: # result_all_precisions_mean for M_tot_selected_i, M_tot_selected in enumerate(M_space[::2]): M_conj_space = ((1.-ratio_space)*M_tot_selected).astype(int) M_feat_space = M_tot_selected - M_conj_space f, axes = plt.subplots(2, 2) axes[0, 0].plot(ratio_space, result_all_precisions_mean[2*M_tot_selected_i]) axes[0, 0].set_xlabel('ratio') axes[0, 0].set_title('Measured precision') axes[1, 0].plot(ratio_space, M_conj_space**2*M_feat_space) axes[1, 0].set_xlabel('M_feat_size') axes[1, 0].set_title('M_c**2*M_f') axes[0, 1].plot(ratio_space, M_conj_space**2.) axes[0, 1].set_xlabel('M') axes[0, 1].set_title('M_c**2') axes[1, 1].plot(ratio_space, M_feat_space) axes[1, 1].set_xlabel('M') axes[1, 1].set_title('M_f') f.suptitle('M_tot %d' % M_tot_selected, fontsize=15) f.set_tight_layout(True) if savefigs: dataio.save_current_figure('scaling_precision_subpop_Mtot%d_{label}_{unique_id}.pdf' % M_tot_selected) plt.close(f) if plots_subpopulations_effects_kappa_fi: # 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_Mratio = cached_data['result_fisherinfo_Mratio'] 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_Mratio = np.empty((M_space.size, ratio_space.size)) # Invert the all_args_i -> M, ratio_conj direction parameters_indirections = data_pbs.loaded_data['parameters_dataset_index'] for M_i, M in enumerate(M_space): 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) try: arg_index = parameters_indirections[(M, 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) (_, _, _, sampler) = launchers.init_everything(curr_args) # Theo Fisher info result_fisherinfo_Mratio[M_i, ratio_conj_i] = sampler.estimate_fisher_info_theocov() # del curr_args['stimuli_generation'] except KeyError: result_fisherinfo_Mratio[M_i, ratio_conj_i] = np.nan # 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_Mratio=result_fisherinfo_Mratio) pickle.dump(data_cache, filecache_out, protocol=2) except IOError: print "Error writing out to caching file ", caching_fisherinfo_filename # result_em_fits_kappa if False: for M_tot_selected_i, M_tot_selected in enumerate(M_space[::2]): M_conj_space = ((1.-ratio_space)*M_tot_selected).astype(int) M_feat_space = M_tot_selected - M_conj_space f, axes = plt.subplots(2, 2) axes[0, 0].plot(ratio_space, result_em_fits_kappa[2*M_tot_selected_i]) axes[0, 0].set_xlabel('ratio') axes[0, 0].set_title('Fitted kappa') axes[1, 0].plot(ratio_space, utils.stddev_to_kappa(1./result_fisherinfo_Mratio[2*M_tot_selected_i]**0.5)) axes[1, 0].set_xlabel('M_feat_size') axes[1, 0].set_title('kappa_FI_mixed') f.suptitle('M_tot %d' % M_tot_selected, fontsize=15) f.set_tight_layout(True) if savefigs: dataio.save_current_figure('scaling_kappa_subpop_Mtot%d_{label}_{unique_id}.pdf' % M_tot_selected) plt.close(f) utils.pcolor_2d_data((result_fisherinfo_Mratio- 2000)**2., log_scale=True, x=M_space, y=ratio_space, xlabel='M', ylabel='ratio', xlabel_format="%d", title='Fisher info') if savefigs: dataio.save_current_figure('dist2000_fi_log_pcolor_{label}_{unique_id}.pdf') all_args = data_pbs.loaded_data['args_list'] variables_to_save = [] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='ratio_scaling_M') plt.show() return locals()
def fit_transform(self, X, verbose=True, report_interval=500, X_true=None): """ Imputes missing values using a batched OT loss Parameters ---------- X : torch.DoubleTensor or torch.cuda.DoubleTensor Contains non-missing and missing data at the indices given by the "mask" argument. Missing values can be arbitrarily assigned (e.g. with NaNs). mask : torch.DoubleTensor or torch.cuda.DoubleTensor mask[i,j] == 1 if X[i,j] is missing, else mask[i,j] == 0. verbose: bool, default=True If True, output loss to log during iterations. X_true: torch.DoubleTensor or None, default=None Ground truth for the missing values. If provided, will output a validation score during training, and return score arrays. For validation/debugging only. Returns ------- X_filled: torch.DoubleTensor or torch.cuda.DoubleTensor Imputed missing data (plus unchanged non-missing data). """ X = X.clone() n, d = X.shape if self.batchsize > n // 2: e = int(np.log2(n // 2)) self.batchsize = 2**e if verbose: logging.info( f"Batchsize larger that half size = {len(X) // 2}. Setting batchsize to {self.batchsize}." ) mask = torch.isnan(X).double() imps = (self.noise * torch.randn(mask.shape).double() + nanmean(X, 0))[mask.bool()] imps.requires_grad = True optimizer = self.opt([imps], lr=self.lr) if verbose: logging.info( f"batchsize = {self.batchsize}, epsilon = {self.eps:.4f}") if X_true is not None: maes = np.zeros(self.niter) rmses = np.zeros(self.niter) for i in range(self.niter): X_filled = X.detach().clone() X_filled[mask.bool()] = imps loss = 0 for _ in range(self.n_pairs): idx1 = np.random.choice(n, self.batchsize, replace=False) idx2 = np.random.choice(n, self.batchsize, replace=False) X1 = X_filled[idx1] X2 = X_filled[idx2] loss = loss + self.sk(X1, X2) if torch.isnan(loss).any() or torch.isinf(loss).any(): ### Catch numerical errors/overflows (should not happen) logging.info("Nan or inf loss") break optimizer.zero_grad() loss.backward() optimizer.step() if X_true is not None: maes[i] = MAE(X_filled, X_true, mask).item() rmses[i] = RMSE(X_filled, X_true, mask).item() if verbose and (i % report_interval == 0): if X_true is not None: logging.info( f'Iteration {i}:\t Loss: {loss.item() / self.n_pairs:.4f}\t ' f'Validation MAE: {maes[i]:.4f}\t' f'RMSE: {rmses[i]:.4f}') else: logging.info( f'Iteration {i}:\t Loss: {loss.item() / self.n_pairs:.4f}' ) X_filled = X.detach().clone() X_filled[mask.bool()] = imps if X_true is not None: return X_filled, maes, rmses else: return X_filled
def plots_fitting_experiments_random(data_pbs, generator_module=None): ''' Reload 2D volume runs from PBS and plot them ''' #### SETUP # savefigs = True savedata = True savemovies = False do_bays09 = True do_gorgo11 = True scatter3d_sumT = False plots_flat_sorted_performance = False plots_memorycurves_fits_best = True nb_best_points = 20 nb_best_points_per_T = nb_best_points/6 size_normal_points = 8 size_best_points = 50 downsampling = 2 # do_relaunch_bestparams_pbs = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", generator_module.dict_parameters_range.keys() # parameters: ratio_conj, sigmax, T # Extract data result_fitexperiments_flat = np.array(data_pbs.dict_arrays['result_fitexperiments']['results_flat']) result_fitexperiments_all_flat = np.array(data_pbs.dict_arrays['result_fitexperiments_all']['results_flat']) result_fitexperiments_noiseconv_flat = np.array(data_pbs.dict_arrays['result_fitexperiments_noiseconv']['results_flat']) result_fitexperiments_noiseconv_all_flat = np.array(data_pbs.dict_arrays['result_fitexperiments_noiseconv_all']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_fitexperiments']['parameters_flat']) all_repeats_completed = data_pbs.dict_arrays['result_fitexperiments']['repeats_completed'] all_args = data_pbs.loaded_data['args_list'] all_args_arr = np.array(all_args) num_repetitions = generator_module.num_repetitions # Extract order of datasets experiment_ids = data_pbs.loaded_data['datasets_list'][0]['fitexperiment_parameters']['experiment_ids'] parameter_names_sorted = data_pbs.dataset_infos['parameters'] T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # filter_data = (result_parameters_flat[:, -1] < 1.0) & (all_repeats_completed == num_repetitions - 1) # filter_data = (all_repeats_completed == num_repetitions - 1) # result_fitexperiments_flat = result_fitexperiments_flat[filter_data] # result_fitexperiments_all_flat = result_fitexperiments_all_flat[filter_data] # result_fitexperiments_noiseconv_flat = result_fitexperiments_noiseconv_flat[filter_data] # result_fitexperiments_noiseconv_all_flat = result_fitexperiments_noiseconv_all_flat[filter_data] # result_parameters_flat = result_parameters_flat[filter_data] # Compute some stuff # Data is summed over all experiments for _flat, contains bic, ll and ll90. # for _all_flat, contains bic, ll and ll90 per experiment. Given that Gorgo11 and Bays09 are incompatible, shouldn't really use the combined version directly! result_fitexperiments_noiseconv_bic_avg_allT = utils.nanmean(result_fitexperiments_noiseconv_flat, axis=-1)[..., 0] result_fitexperiments_noiseconv_allexp_bic_avg_allT = utils.nanmean(result_fitexperiments_noiseconv_all_flat, axis=-1)[:, :, 0] result_fitexperiments_noiseconv_allexp_ll90_avg_allT = -utils.nanmean(result_fitexperiments_noiseconv_all_flat, axis=-1)[:, :, -1] ### BIC # result_fitexperiments_noiseconv_allexp_bic_avg_allT: N x T x exp result_fitexperiments_noiseconv_bays09_bic_avg_allT = result_fitexperiments_noiseconv_allexp_bic_avg_allT[..., 0] result_fitexperiments_noiseconv_gorgo11_bic_avg_allT = result_fitexperiments_noiseconv_allexp_bic_avg_allT[..., 1] result_fitexperiments_noiseconv_dualrecall_bic_avg_allT = result_fitexperiments_noiseconv_allexp_bic_avg_allT[..., 2] # Summed T result_fitexperiments_noiseconv_bays09_bic_avg_sumT = np.nansum(result_fitexperiments_noiseconv_bays09_bic_avg_allT, axis=-1) result_fitexperiments_noiseconv_gorgo11_bic_avg_sumT = np.nansum(result_fitexperiments_noiseconv_gorgo11_bic_avg_allT, axis=-1) result_fitexperiments_noiseconv_dualrecall_bic_avg_sumT = np.nansum(result_fitexperiments_noiseconv_dualrecall_bic_avg_allT, axis=-1) ### LL90 # N x T x exp result_fitexperiments_noiseconv_bays09_ll90_avg_allT = result_fitexperiments_noiseconv_allexp_ll90_avg_allT[..., 0] result_fitexperiments_noiseconv_gorgo11_ll90_avg_allT = result_fitexperiments_noiseconv_allexp_ll90_avg_allT[..., 1] result_fitexperiments_noiseconv_dualrecall_ll90_avg_allT = result_fitexperiments_noiseconv_allexp_ll90_avg_allT[..., 2] # Summed T result_fitexperiments_noiseconv_bays09_ll90_avg_sumT = np.nansum(result_fitexperiments_noiseconv_bays09_ll90_avg_allT, axis=-1) result_fitexperiments_noiseconv_gorgo11_ll90_avg_sumT = np.nansum(result_fitexperiments_noiseconv_gorgo11_ll90_avg_allT, axis=-1) result_fitexperiments_noiseconv_dualrecall_ll90_avg_sumT = np.nansum(result_fitexperiments_noiseconv_dualrecall_ll90_avg_allT, axis=-1) def mask_outliers_array(result_dist_to_use, sigma_outlier=3): ''' Mask outlier datapoints. Compute the mean of the results and assume that points with: result > mean + sigma_outlier*std are outliers. As we want the minimum values, do not mask small values ''' return np.ma.masked_greater(result_dist_to_use, np.mean(result_dist_to_use) + sigma_outlier*np.std(result_dist_to_use)) def best_points_allT(result_dist_to_use): ''' Best points for all T ''' return np.argsort(result_dist_to_use)[:nb_best_points] def str_best_params(best_i, result_dist_to_use): return ' '.join(["%s %.4f" % (parameter_names_sorted[param_i], result_parameters_flat[best_i, param_i]) for param_i in xrange(len(parameter_names_sorted))]) + ' >> %f' % result_dist_to_use[best_i] def plot_scatter(all_vars, result_dist_to_use_name, title='', log_color=True, downsampling=1, label_file='', mask_outliers=True): result_dist_to_use = all_vars[result_dist_to_use_name] result_parameters_flat = all_vars['result_parameters_flat'] # Filter if downsampling filter_downsampling = np.arange(0, result_dist_to_use.size, downsampling) result_dist_to_use = result_dist_to_use[filter_downsampling] result_parameters_flat = result_parameters_flat[filter_downsampling] if mask_outliers: result_dist_to_use = mask_outliers_array(result_dist_to_use) best_points_result_dist_to_use = np.argsort(result_dist_to_use)[:nb_best_points] # Construct all permutations of 3 parameters, for 3D scatters params_permutations = set([tuple(np.sort(np.random.choice(result_parameters_flat.shape[-1], 3, replace=False)).tolist()) for i in xrange(1000)]) for param_permut in params_permutations: fig = plt.figure() ax = Axes3D(fig) # One plot per parameter permutation if log_color: color_points = np.log(result_dist_to_use) else: color_points = result_dist_to_use utils.scatter3d(result_parameters_flat[:, param_permut[0]], result_parameters_flat[:, param_permut[1]], result_parameters_flat[:, param_permut[2]], s=size_normal_points, c=color_points, xlabel=parameter_names_sorted[param_permut[0]], ylabel=parameter_names_sorted[param_permut[1]], zlabel=parameter_names_sorted[param_permut[2]], title=title, ax_handle=ax) utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, param_permut[0]], result_parameters_flat[best_points_result_dist_to_use, param_permut[1]], result_parameters_flat[best_points_result_dist_to_use, param_permut[2]], c='r', s=size_best_points, ax_handle=ax) if savefigs: dataio.save_current_figure('scatter3d_%s_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)) if savemovies: try: utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_%s%s_{label}_{unique_id}.mp4' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)), bitrate=8000, min_duration=8) utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_%s%s_{label}_{unique_id}.gif' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)), nb_frames=30, min_duration=8) except Exception: # Most likely wrong aggregator... print "failed when creating movies for ", result_dist_to_use_name if False and savefigs: ax.view_init(azim=90, elev=10) dataio.save_current_figure('scatter3d_view2_%s_%s%s_{label}_{unique_id}.pdf' % (result_dist_to_use_name, '_'.join([parameter_names_sorted[i] for i in param_permut]), label_file)) # plt.close('all') print "Parameters: %s" % ', '.join(parameter_names_sorted) print "Best points, %s:" % title print '\n'.join([str_best_params(best_i, result_dist_to_use) for best_i in best_points_result_dist_to_use]) if scatter3d_sumT: plot_scatter(locals(), 'result_fitexperiments_noiseconv_bays09_bic_avg_sumT', 'BIC Bays09') plot_scatter(locals(), 'result_fitexperiments_noiseconv_bays09_ll90_avg_sumT', 'LL90 Bays09') plot_scatter(locals(), 'result_fitexperiments_noiseconv_gorgo11_bic_avg_sumT', 'BIC Gorgo11') plot_scatter(locals(), 'result_fitexperiments_noiseconv_gorgo11_ll90_avg_sumT', 'LL90 Gorgo11') plot_scatter(locals(), 'result_fitexperiments_noiseconv_dualrecall_bic_avg_sumT', 'BIC Dual recall') plot_scatter(locals(), 'result_fitexperiments_noiseconv_dualrecall_ll90_avg_sumT', 'LL90 Dual recall') if plots_flat_sorted_performance: result_dist_to_try = [] if do_bays09: result_dist_to_try.extend(['result_fitexperiments_noiseconv_bays09_bic_avg_sumT', 'result_fitexperiments_noiseconv_bays09_ll90_avg_sumT']) if do_gorgo11: result_dist_to_try.extend(['result_fitexperiments_noiseconv_gorgo11_bic_avg_sumT', 'result_fitexperiments_noiseconv_gorgo11_ll90_avg_sumT']) for result_dist in result_dist_to_try: order_indices = np.argsort(locals()[result_dist])[::-1] f, axes = plt.subplots(2, 1) axes[0].plot(np.arange(4) + result_parameters_flat[order_indices]/np.max(result_parameters_flat[order_indices], axis=0)) axes[0].legend(parameter_names_sorted, loc='upper left') axes[0].set_ylabel('Parameters') axes[1].plot(locals()[result_dist][order_indices]) axes[1].set_ylabel(result_dist.split('result_dist_')[-1]) axes[0].set_title('Distance ordered ' + result_dist.split('result_dist_')[-1]) f.canvas.draw() if savefigs: dataio.save_current_figure('plot_sortedperf_full_%s_{label}_{unique_id}.pdf' % (result_dist)) if plots_memorycurves_fits_best: # Alright, will actually reload the data from another set of runs, and find the closest parameter set to the ones found here. data = utils.load_npy('normalisedsigmaxsigmaoutput_random_fitmixturemodels_sigmaxMratiosigmaoutput_repetitions3_280814/outputs/global_plots_fitmixtmodel_random_sigmaoutsigmaxnormMratio-plots_fit_mixturemodels_random-75eb9c74-72e0-4165-8014-92c1ef446f0a.npy') result_em_fits_flat_fitmixture = data['result_em_fits_flat'] result_parameters_flat_fitmixture = data['result_parameters_flat'] all_args_arr_fitmixture = data['all_args_arr'] data_dir = None if not os.environ.get('WORKDIR_DROP'): data_dir = '../experimental_data/' plotting_parameters = launchers_memorycurves_marginal_fi.load_prepare_datasets() def plot_memorycurves_fits_fromexternal(all_vars, result_dist_to_use_name, nb_best_points=10): result_dist_to_use = all_vars[result_dist_to_use_name] result_em_fits_flat_fitmixture = all_vars['result_em_fits_flat_fitmixture'] result_parameters_flat_fitmixture = all_vars['result_parameters_flat_fitmixture'] all_args_arr_fitmixture = all_vars['all_args_arr_fitmixture'] best_point_indices_result_dist = np.argsort(result_dist_to_use)[:nb_best_points] for best_point_index in best_point_indices_result_dist: print "extended plot desired for: " + str_best_params(best_point_index, result_dist_to_use) dist_best_points_fitmixture = np.abs(result_parameters_flat_fitmixture - result_parameters_flat[best_point_index]) dist_best_points_fitmixture -= np.min(dist_best_points_fitmixture, axis=0) dist_best_points_fitmixture /= np.max(dist_best_points_fitmixture, axis=0) best_point_index_fitmixture = np.argmax(np.prod(1-dist_best_points_fitmixture, axis=-1)) print "found closest: " + ' '.join(["%s %.4f" % (parameter_names_sorted[param_i], result_parameters_flat_fitmixture[best_point_index_fitmixture, param_i]) for param_i in xrange(len(parameter_names_sorted))]) # Update arguments all_args_arr_fitmixture[best_point_index_fitmixture].update(dict(zip(parameter_names_sorted, result_parameters_flat_fitmixture[best_point_index_fitmixture]))) packed_data = dict(T_space=T_space, result_em_fits=result_em_fits_flat_fitmixture[best_point_index_fitmixture], all_parameters=all_args_arr_fitmixture[best_point_index_fitmixture]) plotting_parameters['suptitle'] = result_dist_to_use_name plotting_parameters['reuse_axes'] = False if savefigs: packed_data['dataio'] = dataio launchers_memorycurves_marginal_fi.do_memory_plots(packed_data, plotting_parameters) plot_memorycurves_fits_fromexternal(locals(), 'result_external_fitexperiments_noiseconv_bays09_ll90_avg_sumT', nb_best_points=3) plot_memorycurves_fits_fromexternal(locals(), 'result_external_fitexperiments_noiseconv_gorgo11_ll90_avg_sumT', nb_best_points=3) plot_memorycurves_fits_fromexternal(locals(), 'result_external_fitexperiments_noiseconv_dualrecall_ll90_avg_sumT', nb_best_points=3) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['experiment_ids', 'parameter_names_sorted', 'T_space', 'all_args_arr', 'all_repeats_completed', 'filter_data'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='sigmaoutput_normalisedsigmax_random') plt.show() return locals()
def plots_memory_curves(data_pbs, generator_module=None): """ Reload and plot memory curve of a Mixed code. Can use Marginal Fisher Information and fitted Mixture Model as well """ #### SETUP # savefigs = True savedata = True do_error_distrib_fits = True plot_pcolor_fit_precision_to_fisherinfo = True plot_selected_memory_curves = False plot_best_memory_curves = False plot_best_error_distrib = 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(data_pbs.dict_arrays["result_all_precisions"]["results"], axis=-1) result_all_precisions_std = utils.nanstd(data_pbs.dict_arrays["result_all_precisions"]["results"], axis=-1) result_em_fits_mean = utils.nanmean(data_pbs.dict_arrays["result_em_fits"]["results"], axis=-1) result_em_fits_std = utils.nanstd(data_pbs.dict_arrays["result_em_fits"]["results"], axis=-1) result_marginal_inv_fi_mean = utils.nanmean(data_pbs.dict_arrays["result_marginal_inv_fi"]["results"], axis=-1) result_marginal_inv_fi_std = utils.nanstd(data_pbs.dict_arrays["result_marginal_inv_fi"]["results"], axis=-1) result_marginal_fi_mean = utils.nanmean(1.0 / data_pbs.dict_arrays["result_marginal_inv_fi"]["results"], axis=-1) result_marginal_fi_std = utils.nanstd(1.0 / data_pbs.dict_arrays["result_marginal_inv_fi"]["results"], axis=-1) result_responses_all = data_pbs.dict_arrays["result_responses"]["results"] result_target_all = data_pbs.dict_arrays["result_target"]["results"] result_nontargets_all = data_pbs.dict_arrays["result_nontargets"]["results"] M_space = data_pbs.loaded_data["parameters_uniques"]["M"].astype(int) sigmax_space = data_pbs.loaded_data["parameters_uniques"]["sigmax"] T_space = data_pbs.loaded_data["datasets_list"][0]["T_space"] nb_repetitions = result_responses_all.shape[-1] print M_space print sigmax_space print T_space print result_all_precisions_mean.shape, result_em_fits_mean.shape, result_marginal_inv_fi_mean.shape dataio = DataIO.DataIO( output_folder=generator_module.pbs_submission_infos["simul_out_dir"] + "/outputs/", label="global_" + dataset_infos["save_output_filename"], ) ## Load Experimental data experim_datadir = os.environ.get("WORKDIR_DROP", os.path.split(load_experimental_data.__file__)[0]) data_simult = load_experimental_data.load_data_simult( data_dir=os.path.normpath(os.path.join(experim_datadir, "../../experimental_data/")), fit_mixture_model=True ) gorgo11_experimental_precision = data_simult["precision_nitems_theo"] gorgo11_experimental_kappa = np.array([data["kappa"] for _, data in data_simult["em_fits_nitems"]["mean"].items()]) gorgo11_experimental_emfits_mean = np.array( [ [data[key] for _, data in data_simult["em_fits_nitems"]["mean"].items()] for key in ["kappa", "mixt_target", "mixt_nontargets", "mixt_random"] ] ) gorgo11_experimental_emfits_std = np.array( [ [data[key] for _, data in data_simult["em_fits_nitems"]["std"].items()] for key in ["kappa", "mixt_target", "mixt_nontargets", "mixt_random"] ] ) gorgo11_experimental_emfits_sem = gorgo11_experimental_emfits_std / np.sqrt(np.unique(data_simult["subject"]).size) experim_datadir = os.environ.get("WORKDIR_DROP", os.path.split(load_experimental_data.__file__)[0]) data_bays2009 = load_experimental_data.load_data_bays09( data_dir=os.path.normpath(os.path.join(experim_datadir, "../../experimental_data/")), fit_mixture_model=True ) bays09_experimental_mixtures_mean = data_bays2009["em_fits_nitems_arrays"]["mean"] bays09_experimental_mixtures_std = data_bays2009["em_fits_nitems_arrays"]["std"] # add interpolated points for 3 and 5 items emfit_mean_intpfct = spint.interp1d(np.unique(data_bays2009["n_items"]), bays09_experimental_mixtures_mean) bays09_experimental_mixtures_mean_compatible = emfit_mean_intpfct(np.arange(1, 7)) emfit_std_intpfct = spint.interp1d(np.unique(data_bays2009["n_items"]), bays09_experimental_mixtures_std) bays09_experimental_mixtures_std_compatible = emfit_std_intpfct(np.arange(1, 7)) T_space_bays09 = np.arange(1, 6) # Boost non-targets # bays09_experimental_mixtures_mean_compatible[1] *= 1.5 # bays09_experimental_mixtures_mean_compatible[2] /= 1.5 # bays09_experimental_mixtures_mean_compatible /= np.sum(bays09_experimental_mixtures_mean_compatible, axis=0) # Compute some landscapes of fit! # dist_diff_precision_margfi = np.sum(np.abs(result_all_precisions_mean*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) # dist_ratio_precision_margfi = np.sum(np.abs((result_all_precisions_mean*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) # dist_diff_emkappa_margfi = np.sum(np.abs(result_em_fits_mean[..., 0]*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) # dist_ratio_emkappa_margfi = np.sum(np.abs((result_em_fits_mean[..., 0]*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) dist_diff_precision_experim = np.sum( np.abs(result_all_precisions_mean[..., : gorgo11_experimental_kappa.size] - gorgo11_experimental_precision) ** 2.0, axis=-1, ) dist_diff_emkappa_experim = np.sum( np.abs(result_em_fits_mean[..., 0, : gorgo11_experimental_kappa.size] - gorgo11_experimental_kappa) ** 2.0, axis=-1, ) dist_diff_em_mixtures_bays09 = np.sum( np.sum((result_em_fits_mean[..., 1:4] - bays09_experimental_mixtures_mean_compatible[1:].T) ** 2.0, axis=-1), axis=-1, ) dist_diff_modelfits_experfits_bays09 = np.sum( np.sum((result_em_fits_mean[..., :4] - bays09_experimental_mixtures_mean_compatible.T) ** 2.0, axis=-1), axis=-1 ) if do_error_distrib_fits: print "computing error distribution histograms fits" # Now try to fit histograms of errors to target/nontargets bays09_hist_target_mean = data_bays2009["hist_cnts_target_nitems_stats"]["mean"] bays09_hist_target_std = data_bays2009["hist_cnts_target_nitems_stats"]["std"] bays09_hist_nontarget_mean = data_bays2009["hist_cnts_nontarget_nitems_stats"]["mean"] bays09_hist_nontarget_std = data_bays2009["hist_cnts_nontarget_nitems_stats"]["std"] T_space_bays09_filt = np.unique(data_bays2009["n_items"]) angle_space = np.linspace(-np.pi, np.pi, bays09_hist_target_mean.shape[-1] + 1) bins_center = angle_space[:-1] + np.diff(angle_space)[0] / 2 errors_targets = utils.wrap_angles(result_responses_all - result_target_all) hist_targets_all = np.empty( (M_space.size, sigmax_space.size, T_space_bays09_filt.size, angle_space.size - 1, nb_repetitions) ) errors_nontargets = np.nan * np.empty(result_nontargets_all.shape) hist_nontargets_all = np.empty( (M_space.size, sigmax_space.size, T_space_bays09_filt.size, angle_space.size - 1, nb_repetitions) ) for M_i, M in enumerate(M_space): for sigmax_i, sigmax in enumerate(sigmax_space): for T_bays_i, T_bays in enumerate(T_space_bays09_filt): for repet_i in xrange(nb_repetitions): # Could do a nicer indexing but f**k it # Histogram errors to targets hist_targets_all[M_i, sigmax_i, T_bays_i, :, repet_i], x, bins = utils.histogram_binspace( utils.dropnan(errors_targets[M_i, sigmax_i, T_bays - 1, ..., repet_i]), bins=angle_space, norm="density", ) # Compute the error between the responses and nontargets. errors_nontargets[M_i, sigmax_i, T_bays - 1, :, :, repet_i] = utils.wrap_angles( ( result_responses_all[M_i, sigmax_i, T_bays - 1, :, repet_i, np.newaxis] - result_nontargets_all[M_i, sigmax_i, T_bays - 1, :, :, repet_i] ) ) # Histogram it hist_nontargets_all[M_i, sigmax_i, T_bays_i, :, repet_i], x, bins = utils.histogram_binspace( utils.dropnan(errors_nontargets[M_i, sigmax_i, T_bays - 1, ..., repet_i]), bins=angle_space, norm="density", ) hist_targets_mean = utils.nanmean(hist_targets_all, axis=-1).filled(np.nan) hist_targets_std = utils.nanstd(hist_targets_all, axis=-1).filled(np.nan) hist_nontargets_mean = utils.nanmean(hist_nontargets_all, axis=-1).filled(np.nan) hist_nontargets_std = utils.nanstd(hist_nontargets_all, axis=-1).filled(np.nan) # Compute distances to experimental histograms dist_diff_hist_target_bays09 = np.nansum( np.nansum((hist_targets_mean - bays09_hist_target_mean) ** 2.0, axis=-1), axis=-1 ) dist_diff_hist_nontargets_bays09 = np.nansum( np.nansum((hist_nontargets_mean - bays09_hist_nontarget_mean) ** 2.0, axis=-1), axis=-1 ) dist_diff_hist_nontargets_5_6items_bays09 = np.nansum( np.nansum((hist_nontargets_mean[:, :, -2:] - bays09_hist_nontarget_mean[-2:]) ** 2.0, axis=-1), axis=-1 ) if plot_pcolor_fit_precision_to_fisherinfo: # Check fit between precision and Experiments utils.pcolor_2d_data( dist_diff_precision_experim, log_scale=True, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", xlabel_format="%d", ) if savefigs: dataio.save_current_figure("match_precision_exper_log_pcolor_{label}_{unique_id}.pdf") utils.pcolor_2d_data( dist_diff_emkappa_experim, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", xlabel_format="%d" ) if savefigs: dataio.save_current_figure("match_emkappa_model_exper_pcolor_{label}_{unique_id}.pdf") utils.pcolor_2d_data( dist_diff_em_mixtures_bays09, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", log_scale=True, xlabel_format="%d", ) if savefigs: dataio.save_current_figure("match_emmixtures_experbays09_log_pcolor_{label}_{unique_id}.pdf") utils.pcolor_2d_data( dist_diff_modelfits_experfits_bays09, log_scale=True, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", xlabel_format="%d", ) if savefigs: dataio.save_current_figure("match_diff_emfits_experbays09_pcolor_{label}_{unique_id}.pdf") if do_error_distrib_fits: utils.pcolor_2d_data( dist_diff_hist_target_bays09, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", log_scale=True, xlabel_format="%d", ) if savefigs: dataio.save_current_figure("match_hist_targets_experbays09_log_pcolor_{label}_{unique_id}.pdf") utils.pcolor_2d_data( dist_diff_hist_nontargets_bays09, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", log_scale=True, xlabel_format="%d", ) if savefigs: dataio.save_current_figure("match_hist_nontargets_experbays09_log_pcolor_{label}_{unique_id}.pdf") utils.pcolor_2d_data( dist_diff_hist_nontargets_5_6items_bays09, x=M_space, y=sigmax_space, xlabel="M", ylabel="sigmax", log_scale=True, xlabel_format="%d", ) if savefigs: dataio.save_current_figure( "match_hist_nontargets_6items_experbays09_log_pcolor_{label}_{unique_id}.pdf" ) # Macro plot def mem_plot_precision(sigmax_i, M_i, mem_exp_prec): ax = utils.plot_mean_std_area( T_space[: mem_exp_prec.size], mem_exp_prec, np.zeros(mem_exp_prec.size), linewidth=3, fmt="o-", markersize=8, label="Experimental data", ) ax = utils.plot_mean_std_area( T_space[: mem_exp_prec.size], result_all_precisions_mean[M_i, sigmax_i, : mem_exp_prec.size], result_all_precisions_std[M_i, sigmax_i, : mem_exp_prec.size], ax_handle=ax, linewidth=3, fmt="o-", markersize=8, label="Precision of samples", ) # ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][M_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][M_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Marginal Fisher Information') # ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][M_i, sigmax_i], result_em_fits_std[..., 0][M_i, sigmax_i], ax_handle=ax, xlabel='Number of items', ylabel="Inverse variance $[rad^{-2}]$", linewidth=3, fmt='o-', markersize=8, label='Fitted kappa') ax.set_title("M %d, sigmax %.2f" % (M_space[M_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, mem_exp_prec.size + 0.1]) ax.set_xticks(range(1, mem_exp_prec.size + 1)) ax.set_xticklabels(range(1, mem_exp_prec.size + 1)) if savefigs: dataio.save_current_figure( "memorycurves_precision_M%dsigmax%.2f_{label}_{unique_id}.pdf" % (M_space[M_i], sigmax_space[sigmax_i]) ) def mem_plot_kappa(sigmax_i, M_i, exp_kappa_mean, exp_kappa_std=None): ax = utils.plot_mean_std_area( T_space[: exp_kappa_mean.size], exp_kappa_mean, exp_kappa_std, linewidth=3, fmt="o-", markersize=8, label="Experimental data", ) ax = utils.plot_mean_std_area( T_space[: exp_kappa_mean.size], result_em_fits_mean[..., : exp_kappa_mean.size, 0][M_i, sigmax_i], result_em_fits_std[..., : exp_kappa_mean.size, 0][M_i, sigmax_i], xlabel="Number of items", ylabel="Memory error $[rad^{-2}]$", linewidth=3, fmt="o-", markersize=8, label="Fitted kappa", ax_handle=ax, ) # ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][M_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][M_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Marginal Fisher Information') ax.set_title("M %d, sigmax %.2f" % (M_space[M_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, exp_kappa_mean.size + 0.1]) ax.set_xticks(range(1, exp_kappa_mean.size + 1)) ax.set_xticklabels(range(1, exp_kappa_mean.size + 1)) ax.get_figure().canvas.draw() if savefigs: dataio.save_current_figure( "memorycurves_kappa_M%dsigmax%.2f_{label}_{unique_id}.pdf" % (M_space[M_i], sigmax_space[sigmax_i]) ) def em_plot(sigmax_i, M_i): f, ax = plt.subplots() ax2 = ax.twinx() # left axis, kappa ax = utils.plot_mean_std_area( T_space, result_em_fits_mean[..., 0][M_i, sigmax_i], result_em_fits_std[..., 0][M_i, sigmax_i], xlabel="Number of items", 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( T_space, result_em_fits_mean[..., 1][M_i, sigmax_i], result_em_fits_std[..., 1][M_i, sigmax_i], xlabel="Number of items", ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt="o-", markersize=8, label="Target", ) utils.plot_mean_std_area( T_space, result_em_fits_mean[..., 2][M_i, sigmax_i], result_em_fits_std[..., 2][M_i, sigmax_i], xlabel="Number of items", ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt="o-", markersize=8, label="Nontarget", ) utils.plot_mean_std_area( T_space, result_em_fits_mean[..., 3][M_i, sigmax_i], result_em_fits_std[..., 3][M_i, sigmax_i], xlabel="Number of items", 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) ax.set_title("M %d, sigmax %.2f" % (M_space[M_i], sigmax_space[sigmax_i])) ax.set_xlim([0.9, T_space.size]) ax.set_xticks(range(1, T_space.size + 1)) ax.set_xticklabels(range(1, T_space.size + 1)) f.canvas.draw() if savefigs: dataio.save_current_figure( "memorycurves_emfits_M%dsigmax%.2f_{label}_{unique_id}.pdf" % (M_space[M_i], sigmax_space[sigmax_i]) ) def em_plot_paper(sigmax_i, M_i): f, ax = plt.subplots() # Right axis, mixture probabilities utils.plot_mean_std_area( T_space_bays09, result_em_fits_mean[..., 1][M_i, sigmax_i][: T_space_bays09.size], result_em_fits_std[..., 1][M_i, sigmax_i][: T_space_bays09.size], xlabel="Number of items", ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt="o-", markersize=5, label="Target", ) utils.plot_mean_std_area( T_space_bays09, result_em_fits_mean[..., 2][M_i, sigmax_i][: T_space_bays09.size], result_em_fits_std[..., 2][M_i, sigmax_i][: T_space_bays09.size], xlabel="Number of items", ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt="o-", markersize=5, label="Nontarget", ) utils.plot_mean_std_area( T_space_bays09, result_em_fits_mean[..., 3][M_i, sigmax_i][: T_space_bays09.size], result_em_fits_std[..., 3][M_i, sigmax_i][: T_space_bays09.size], xlabel="Number of items", ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt="o-", markersize=5, label="Random", ) ax.legend(prop={"size": 15}) ax.set_title("M %d, sigmax %.2f" % (M_space[M_i], sigmax_space[sigmax_i])) ax.set_xlim([1.0, T_space_bays09.size]) ax.set_ylim([0.0, 1.1]) ax.set_xticks(range(1, T_space_bays09.size + 1)) ax.set_xticklabels(range(1, T_space_bays09.size + 1)) f.canvas.draw() if savefigs: dataio.save_current_figure( "memorycurves_emfits_paper_M%dsigmax%.2f_{label}_{unique_id}.pdf" % (M_space[M_i], sigmax_space[sigmax_i]) ) def hist_errors_targets_nontargets(hists_toplot_mean, hists_toplot_std, title="", M=0, sigmax=0, yaxis_lim="auto"): f1, axes1 = plt.subplots( ncols=hists_toplot_mean.shape[-2], figsize=(hists_toplot_mean.shape[-2] * 6, 6), sharey=True ) for T_bays_i, T_bays in enumerate(T_space_bays09_filt): if not np.all(np.isnan(hists_toplot_mean[T_bays_i])): axes1[T_bays_i].bar( bins_center, hists_toplot_mean[T_bays_i], width=2.0 * np.pi / (angle_space.size - 1), align="center", yerr=hists_toplot_std[T_bays_i], ) axes1[T_bays_i].set_title("N=%d" % T_bays) # axes1[T_{}] axes1[T_bays_i].set_xlim( [bins_center[0] - np.pi / (angle_space.size - 1), bins_center[-1] + np.pi / (angle_space.size - 1)] ) if yaxis_lim == "target": axes1[T_bays_i].set_ylim([0.0, 2.0]) elif yaxis_lim == "nontarget": axes1[T_bays_i].set_ylim([0.0, 0.3]) else: axes1[T_bays_i].set_ylim([0.0, np.nanmax(hists_toplot_mean + hists_toplot_std) * 1.1]) axes1[T_bays_i].set_xticks((-np.pi, -np.pi / 2, 0, np.pi / 2.0, np.pi)) axes1[T_bays_i].set_xticklabels( (r"$-\pi$", r"$-\frac{\pi}{2}$", r"$0$", r"$\frac{\pi}{2}$", r"$\pi$"), fontsize=16 ) f1.canvas.draw() if savefigs: dataio.save_current_figure( "memorycurves_hist_%s_paper_M%dsigmax%.2f_{label}_{unique_id}.pdf" % (title, M, sigmax) ) ################################# if plot_selected_memory_curves: selected_values = [[0.84, 0.23], [0.84, 0.19]] for current_values in selected_values: # Find the indices M_i = np.argmin(np.abs(current_values[0] - M_space)) sigmax_i = np.argmin(np.abs(current_values[1] - sigmax_space)) mem_plot_precision(sigmax_i, M_i) mem_plot_kappa(sigmax_i, M_i) if plot_best_memory_curves: # Best precision fit best_axis2_i_all = np.argmin(dist_diff_precision_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_precision(best_axis2_i, axis1_i, gorgo11_experimental_precision) # Best kappa fit best_axis2_i_all = np.argmin(dist_diff_emkappa_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa( best_axis2_i, axis1_i, gorgo11_experimental_emfits_mean[0], gorgo11_experimental_emfits_std[0] ) # em_plot(best_axis2_i, axis1_i) # Best em parameters fit to Bays09 best_axis2_i_all = np.argmin(dist_diff_modelfits_experfits_bays09, axis=1) # best_axis2_i_all = np.argmin(dist_diff_em_mixtures_bays09, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa( best_axis2_i, axis1_i, bays09_experimental_mixtures_mean_compatible[0, : T_space_bays09.size], bays09_experimental_mixtures_std_compatible[0, : T_space_bays09.size], ) # em_plot(best_axis2_i, axis1_i) em_plot_paper(best_axis2_i, axis1_i) if plot_best_error_distrib and do_error_distrib_fits: # Best target histograms best_axis2_i_all = np.argmin(dist_diff_hist_target_bays09, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): hist_errors_targets_nontargets( hist_targets_mean[axis1_i, best_axis2_i], hist_targets_std[axis1_i, best_axis2_i], "target", M=M_space[axis1_i], sigmax=sigmax_space[best_axis2_i], yaxis_lim="target", ) # Best nontarget histograms best_axis2_i_all = np.argmin(dist_diff_hist_nontargets_bays09, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): hist_errors_targets_nontargets( hist_nontargets_mean[axis1_i, best_axis2_i], hist_nontargets_std[axis1_i, best_axis2_i], "nontarget", M=M_space[axis1_i], sigmax=sigmax_space[best_axis2_i], yaxis_lim="nontarget", ) all_args = data_pbs.loaded_data["args_list"] variables_to_save = [ "gorgo11_experimental_precision", "gorgo11_experimental_kappa", "bays09_experimental_mixtures_mean_compatible", "T_space", ] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder="memory_curves") plt.show() return locals()
def transform(self, X, mask, verbose=True, report_interval=1, X_true=None): """ Impute missing values on new data. Assumes models have been previously fitted on other data. Parameters ---------- X : torch.DoubleTensor or torch.cuda.DoubleTensor, shape (n, d) Contains non-missing and missing data at the indices given by the "mask" argument. Missing values can be arbitrarily assigned (e.g. with NaNs). mask : torch.DoubleTensor or torch.cuda.DoubleTensor, shape (n, d) mask[i,j] == 1 if X[i,j] is missing, else mask[i,j] == 0. verbose: bool, default=True If True, output loss to log during iterations. report_interval : int, default=1 Interval between loss reports (if verbose). X_true: torch.DoubleTensor or None, default=None Ground truth for the missing values. If provided, will output a validation score during training. For debugging only. Returns ------- X_filled: torch.DoubleTensor or torch.cuda.DoubleTensor Imputed missing data (plus unchanged non-missing data). """ assert self.is_fitted, "The model has not been fitted yet." n, d = X.shape normalized_tol = self.tol * torch.max(torch.abs(X[~mask.bool()])) order_ = torch.argsort(mask.sum(0)) X[mask] = nanmean(X) X_filled = X.clone() for i in range(self.max_iter): if self.order == 'random': order_ = np.random.choice(d, d, replace=False) X_old = X_filled.clone().detach() for l in range(d): j = order_[l].item() with torch.no_grad(): X_filled[mask[:, j].bool(), j] = self.models[j]( X_filled[mask[:, j].bool(), :][:, np.r_[0:j, j + 1:d]]).squeeze() if verbose and (i % report_interval == 0): if X_true is not None: logging.info( f'Iteration {i}:\t ' f'Validation MAE: {MAE(X_filled, X_true, mask).item():.4f}\t' f'RMSE: {RMSE(X_filled, X_true, mask).item():.4f}') if torch.norm(X_filled - X_old, p=np.inf) < normalized_tol: break if i == (self.max_iter - 1) and verbose: logging.info('Early stopping criterion not reached') return X_filled
def plots_memory_curves(data_pbs, generator_module=None): ''' Reload and plot memory curve of a Mixed code. Can use Marginal Fisher Information and fitted Mixture Model as well ''' #### SETUP # savefigs = True savedata = True plot_pcolor_fit_precision_to_fisherinfo = False plot_selected_memory_curves = False plot_best_memory_curves = False plot_subplots_persigmax = 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) # ratio_space, sigmax_space, T, 5 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_marginal_inv_fi_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_inv_fi_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_fi_mean = utils.nanmean(np.squeeze(1./data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_fi_std = utils.nanstd(np.squeeze(1./data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) ratioconj_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] print ratioconj_space print sigmax_space print T_space print result_all_precisions_mean.shape, result_em_fits_mean.shape, result_marginal_inv_fi_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) ## Load Experimental data data_simult = load_experimental_data.load_data_simult(data_dir=os.path.normpath(os.path.join(os.path.split(load_experimental_data.__file__)[0], '../../experimental_data/')), fit_mixture_model=True) memory_experimental_precision = data_simult['precision_nitems_theo'] memory_experimental_kappa = np.array([data['kappa'] for _, data in data_simult['em_fits_nitems']['mean'].items()]) memory_experimental_kappa_std = np.array([data['kappa'] for _, data in data_simult['em_fits_nitems']['std'].items()]) experim_datadir = os.environ.get('WORKDIR_DROP', os.path.split(load_experimental_data.__file__)[0]) data_bays2009 = load_experimental_data.load_data_bays09(data_dir=os.path.normpath(os.path.join(experim_datadir, '../../experimental_data/')), fit_mixture_model=True) bays09_experimental_mixtures_mean = data_bays2009['em_fits_nitems_arrays']['mean'] # add interpolated points for 3 and 5 items bays3 = (bays09_experimental_mixtures_mean[:, 2] + bays09_experimental_mixtures_mean[:, 1])/2. bays5 = (bays09_experimental_mixtures_mean[:, -1] + bays09_experimental_mixtures_mean[:, -2])/2. bays09_experimental_mixtures_mean_compatible = np.c_[bays09_experimental_mixtures_mean[:,:2], bays3, bays09_experimental_mixtures_mean[:, 2], bays5] # Boost non-targets # bays09_experimental_mixtures_mean_compatible[1] *= 1.5 # bays09_experimental_mixtures_mean_compatible[2] /= 1.5 # bays09_experimental_mixtures_mean_compatible /= np.sum(bays09_experimental_mixtures_mean_compatible, axis=0) # Compute some landscapes of fit! dist_diff_precision_margfi = np.sum(np.abs(result_all_precisions_mean*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) dist_ratio_precision_margfi = np.sum(np.abs((result_all_precisions_mean*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) dist_diff_emkappa_margfi = np.sum(np.abs(result_em_fits_mean[..., 0]*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) dist_ratio_emkappa_margfi = np.sum(np.abs((result_em_fits_mean[..., 0]*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) dist_diff_precision_experim = np.sum(np.abs(result_all_precisions_mean - memory_experimental_precision)**2., axis=-1) dist_diff_emkappa_experim = np.sum(np.abs(result_em_fits_mean[..., 0] - memory_experimental_kappa)**2., axis=-1) dist_diff_precision_experim_1item = np.abs(result_all_precisions_mean[..., 0] - memory_experimental_precision[0])**2. dist_diff_precision_experim_2item = np.abs(result_all_precisions_mean[..., 1] - memory_experimental_precision[1])**2. dist_diff_precision_margfi_1item = np.abs(result_all_precisions_mean[..., 0]*2. - result_marginal_fi_mean[..., 0, 0])**2. dist_diff_emkappa_experim_1item = np.abs(result_em_fits_mean[..., 0, 0] - memory_experimental_kappa[0])**2. dist_diff_margfi_experim_1item = np.abs(result_marginal_fi_mean[..., 0, 0] - memory_experimental_precision[0])**2. dist_diff_emkappa_mixtures_bays09 = np.sum(np.sum((result_em_fits_mean[..., 1:4] - bays09_experimental_mixtures_mean_compatible[1:].T)**2., axis=-1), axis=-1) dist_diff_modelfits_experfits_bays09 = np.sum(np.sum((result_em_fits_mean[..., :4] - bays09_experimental_mixtures_mean_compatible.T)**2., axis=-1), axis=-1) if plot_pcolor_fit_precision_to_fisherinfo: # Check fit between precision and fisher info utils.pcolor_2d_data(dist_diff_precision_margfi, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_precision_margfi_log_pcolor_{label}_{unique_id}.pdf') # utils.pcolor_2d_data(dist_diff_precision_margfi, x=ratioconj_space, y=sigmax_space[2:], xlabel='ratio conj', ylabel='sigmax') # if savefigs: # dataio.save_current_figure('match_precision_margfi_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_ratio_precision_margfi, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_ratio_precision_margfi_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_margfi, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_diff_emkappa_margfi_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_ratio_emkappa_margfi, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_ratio_emkappa_margfi_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_diff_precision_experim_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_experim, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_diff_emkappa_experim_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_margfi*dist_diff_emkappa_margfi*dist_diff_precision_experim*dist_diff_emkappa_experim, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_bigmultiplication_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_margfi_1item, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_precision_margfi_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim_1item, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_precision_experim_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_experim_1item, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_emkappa_experim_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_margfi_experim_1item, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_margfi_experim_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim_2item, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_precision_experim_2item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_mixtures_bays09, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_mixtures_experbays09_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_modelfits_experfits_bays09, log_scale=True, x=ratioconj_space, y=sigmax_space, xlabel='ratio conj', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_emfits_experbays09_pcolor_{label}_{unique_id}.pdf') # Macro plot def mem_plot_precision(sigmax_i, ratioconj_i): ax = utils.plot_mean_std_area(T_space, memory_experimental_precision, np.zeros(T_space.size), linewidth=3, fmt='o-', markersize=8, label='Experimental data') ax = utils.plot_mean_std_area(T_space, result_all_precisions_mean[ratioconj_i, sigmax_i], result_all_precisions_std[ratioconj_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Precision of samples') # ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][ratioconj_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][ratioconj_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Marginal Fisher Information') # ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][ratioconj_i, sigmax_i], result_em_fits_std[..., 0][ratioconj_i, sigmax_i], ax_handle=ax, xlabel='Number of items', ylabel="Inverse variance $[rad^{-2}]$", linewidth=3, fmt='o-', markersize=8, label='Fitted kappa') ax.set_title('ratio_conj %.2f, sigmax %.2f' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, 5.1]) ax.set_xticks(range(1, 6)) ax.set_xticklabels(range(1, 6)) if savefigs: dataio.save_current_figure('memorycurves_precision_ratioconj%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) def mem_plot_kappa(sigmax_i, ratioconj_i): ax = utils.plot_mean_std_area(T_space, memory_experimental_kappa, memory_experimental_kappa_std, linewidth=3, fmt='o-', markersize=8, label='Experimental data') ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][ratioconj_i, sigmax_i], result_em_fits_std[..., 0][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Memory error $[rad^{-2}]$", ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Fitted kappa') # ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][ratioconj_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][ratioconj_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Marginal Fisher Information') ax.set_title('ratio_conj %.2f, sigmax %.2f' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, 5.1]) ax.set_xticks(range(1, 6)) ax.set_xticklabels(range(1, 6)) if savefigs: dataio.save_current_figure('memorycurves_kappa_ratioconj%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) def em_plot(sigmax_i, ratioconj_i): # TODO finish checking this up. f, ax = plt.subplots() ax2 = ax.twinx() # left axis, kappa ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][ratioconj_i, sigmax_i], result_em_fits_std[..., 0][ratioconj_i, sigmax_i], xlabel='Number of items', 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(T_space, result_em_fits_mean[..., 1][ratioconj_i, sigmax_i], result_em_fits_std[..., 1][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Target') utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 2][ratioconj_i, sigmax_i], result_em_fits_std[..., 2][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Nontarget') utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 3][ratioconj_i, sigmax_i], result_em_fits_std[..., 3][ratioconj_i, sigmax_i], xlabel='Number of items', 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) ax.set_title('ratio_conj %.2f, sigmax %.2f' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) ax.set_xlim([0.9, 5.1]) ax.set_xticks(range(1, 6)) ax.set_xticklabels(range(1, 6)) f.canvas.draw() if savefigs: dataio.save_current_figure('memorycurves_emfits_ratioconj%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) def em_plot_paper(sigmax_i, ratioconj_i): f, ax = plt.subplots() # Right axis, mixture probabilities utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 1][ratioconj_i, sigmax_i], result_em_fits_std[..., 1][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Target') utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 2][ratioconj_i, sigmax_i], result_em_fits_std[..., 2][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Nontarget') utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 3][ratioconj_i, sigmax_i], result_em_fits_std[..., 3][ratioconj_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Random') ax.legend(prop={'size':15}) ax.set_title('ratio_conj %.2f, sigmax %.2f' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) ax.set_xlim([1.0, 5.0]) ax.set_ylim([0.0, 1.1]) ax.set_xticks(range(1, 6)) ax.set_xticklabels(range(1, 6)) f.canvas.draw() if savefigs: dataio.save_current_figure('memorycurves_emfits_paper_ratioconj%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratioconj_space[ratioconj_i], sigmax_space[sigmax_i])) if plot_selected_memory_curves: selected_values = [[0.84, 0.23], [0.84, 0.19]] for current_values in selected_values: # Find the indices ratioconj_i = np.argmin(np.abs(current_values[0] - ratioconj_space)) sigmax_i = np.argmin(np.abs(current_values[1] - sigmax_space)) mem_plot_precision(sigmax_i, ratioconj_i) mem_plot_kappa(sigmax_i, ratioconj_i) if plot_best_memory_curves: # Best precision fit best_axis2_i_all = np.argmin(dist_diff_precision_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_precision(best_axis2_i, axis1_i) # Best kappa fit best_axis2_i_all = np.argmin(dist_diff_emkappa_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa(best_axis2_i, axis1_i) em_plot(best_axis2_i, axis1_i) # Best em parameters fit to Bays09 best_axis2_i_all = np.argmin(dist_diff_modelfits_experfits_bays09, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa(best_axis2_i, axis1_i) # em_plot(best_axis2_i, axis1_i) em_plot_paper(best_axis2_i, axis1_i) if plot_subplots_persigmax: # Do subplots with ratio_conj on x, one plot per T and different figures per sigmax. Looks a bit like reloader_hierarchical_constantMMlower_maxlik_allresponses_221213.py sigmax_selected_indices = np.array([np.argmin((sigmax_space - sigmax_select)**2.) for sigmax_select in [0.1, 0.2, 0.3, 0.4, 0.5]]) for sigmax_select_i in sigmax_selected_indices: # two plots per sigmax. f, axes = plt.subplots(nrows=T_space[-1], ncols=1, sharex=True, figsize=(10, 12)) # all_lines_bis = [] for T_i, T in enumerate(T_space): # Plot EM mixtures utils.plot_mean_std_area(ratioconj_space, result_em_fits_mean[:, sigmax_select_i, T_i, 1], result_em_fits_std[:, sigmax_select_i, T_i, 1], ax_handle=axes[T_i], linewidth=3, fmt='o-', markersize=5) utils.plot_mean_std_area(ratioconj_space, result_em_fits_mean[:, sigmax_select_i, T_i, 2], result_em_fits_std[:, sigmax_select_i, T_i, 2], ax_handle=axes[T_i], linewidth=3, fmt='o-', markersize=5) utils.plot_mean_std_area(ratioconj_space, result_em_fits_mean[:, sigmax_select_i, T_i, 3], result_em_fits_std[:, sigmax_select_i, T_i, 3], ax_handle=axes[T_i], linewidth=3, fmt='o-', markersize=5) # ratio_MMlower_space, result_emfits_filtered[:, i, 1:4], 0*result_emfits_filtered[:, i, 1:4], ax_handle=axes[T_i], linewidth=2) #, color=all_lines[T_i].get_color()) # curr_lines = axes[T_i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[T_i].get_color()) axes[T_i].grid() axes[T_i].set_xticks(np.linspace(0., 1.0, 5)) axes[T_i].set_xlim((0.0, 1.0)) # axes[T_i].set_yticks([]) # axes[T_i].set_ylim((np.min(result_emfits_filtered[:, i, 0]), result_emfits_filtered[max_ind, i, 0]*1.1)) axes[T_i].set_ylim((0.0, 1.05)) axes[T_i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) axes[0].set_title('Sigmax: %.3f' % sigmax_space[sigmax_select_i]) axes[-1].set_xlabel('Proportion of conjunctive units') if savefigs: dataio.save_current_figure('results_subplots_emmixtures_sigmax%.2f_{label}_global_{unique_id}.pdf' % sigmax_space[sigmax_select_i]) f, axes = plt.subplots(nrows=T_space[-1], ncols=1, sharex=True, figsize=(10, 12)) # Kappa kappa for T_i, T in enumerate(T_space): # Plot kappa mixture utils.plot_mean_std_area(ratioconj_space, result_em_fits_mean[:, sigmax_select_i, T_i, 0], result_em_fits_std[:, sigmax_select_i, T_i, 0], ax_handle=axes[T_i], linewidth=3, fmt='o-', markersize=5) # utils.plot_mean_std_area(ratio_MMlower_space, result_emfits_filtered[:, i, 0], 0*result_emfits_filtered[:, i, 0], ax_handle=axes[T_i], linewidth=2) #, color=all_lines[T_i].get_color()) # curr_lines = axes[T_i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[T_i].get_color()) axes[T_i].grid() axes[T_i].set_xticks(np.linspace(0., 1.0, 5)) axes[T_i].set_xlim((0.0, 1.0)) # axes[T_i].set_yticks([]) # axes[T_i].set_ylim((np.min(result_emfits_filtered[:, i, 0]), result_emfits_filtered[max_ind, i, 0]*1.1)) # axes[T_i].set_ylim((0.0, 1.0)) axes[T_i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) axes[0].set_title('Sigmax: %.3f' % sigmax_space[sigmax_select_i]) axes[-1].set_xlabel('Proportion of conjunctive units') # f.subplots_adjust(right=0.75) # plt.figlegend(all_lines_bis, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) if savefigs: dataio.save_current_figure('results_subplots_emkappa_sigmax%.2f_{label}_global_{unique_id}.pdf' % sigmax_space[sigmax_select_i]) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['memory_experimental_precision', 'memory_experimental_kappa', 'bays09_experimental_mixtures_mean_compatible'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='memory_curves') plt.show() return locals()
def postprocess_dualrecall_fitmixturemodel(data_pbs, generator_module=None): ''' Reload runs from PBS To be plotted in Ipython later ''' #### SETUP # savedata = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", data_pbs.dataset_infos['parameters'] # parameters: M, ratio_conj, sigmax # Extract data result_em_fits = np.array(data_pbs.dict_arrays['result_em_fits']['results_flat']) result_dist_dualrecall_angle = np.array(data_pbs.dict_arrays['result_dist_dualrecall_angle']['results_flat']) result_dist_dualrecall_angle_emmixt_KL = np.array(data_pbs.dict_arrays['result_dist_dualrecall_angle_emmixt_KL']['results_flat']) result_dist_dualrecall_colour = np.array(data_pbs.dict_arrays['result_dist_dualrecall_colour']['results_flat']) result_dist_dualrecall_colour_emmixt_KL = np.array(data_pbs.dict_arrays['result_dist_dualrecall_colour_emmixt_KL']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_em_fits']['parameters_flat']) all_repeats_completed = data_pbs.dict_arrays['result_em_fits']['repeats_completed'] all_args_arr = np.array(data_pbs.loaded_data['args_list']) M_space = data_pbs.loaded_data['parameters_uniques']['M'] ratio_conj_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] num_repetitions = generator_module.num_repetitions parameter_names_sorted = data_pbs.dataset_infos['parameters'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Load ground truth data_dualrecall = load_experimental_data.load_data_dualrecall(fit_mixture_model=True) ## Filter everything with repeats_completed == num_repet filter_data = all_repeats_completed == num_repetitions - 1 result_parameters_flat = result_parameters_flat[filter_data] result_em_fits = result_em_fits[filter_data] result_dist_dualrecall_angle = result_dist_dualrecall_angle[filter_data] result_dist_dualrecall_angle_emmixt_KL = result_dist_dualrecall_angle_emmixt_KL[filter_data] result_dist_dualrecall_colour = result_dist_dualrecall_colour[filter_data] result_dist_dualrecall_colour_emmixt_KL = result_dist_dualrecall_colour_emmixt_KL[filter_data] all_args_arr = all_args_arr[filter_data] all_repeats_completed = all_repeats_completed[filter_data] print "Size post-filter: ", result_parameters_flat.shape[0] # Compute lots of averages over the repetitions result_em_fits_avg = utils.nanmean(result_em_fits, axis=-1) result_dist_dualrecall_angle_avg = utils.nanmean(result_dist_dualrecall_angle, axis=-1) result_dist_dualrecall_angle_emmixt_KL_avg = utils.nanmean(result_dist_dualrecall_angle_emmixt_KL, axis=-1) result_dist_dualrecall_colour_avg = utils.nanmean(result_dist_dualrecall_colour, axis=-1) result_dist_dualrecall_colour_emmixt_KL_avg = utils.nanmean(result_dist_dualrecall_colour_emmixt_KL, axis=-1) # all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['parameter_names_sorted', 'all_args_arr', 'all_repeats_completed', 'filter_data'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='dualrecall_fitmixturemodel') plt.show() return locals()
def compute_result_distemfits_dataset(self, all_variables, experiment_id='bays09', cache_array_name='result_dist_bays09', variable_selection_slice=slice( None, 4), variable_selection_slice_cache=slice( None, None), metric='mse'): ''' Result is the distance (sum squared) to experimental data fits Assume that: - result_em_fits exists. Does an average over repetitions_axis and sums over all others. - metric = 'mse' | 'kl' - variable_selection_slice: slice(0, 1) for kappa, slice(1, 4) for mixt proportions, slice(none, 4) for all EM params ''' assert metric == 'mse' or metric == 'kl', "Metric should be {mse, kl}" repetitions_axis = all_variables.get('repetitions_axis', -1) if cache_array_name in all_variables: # If already computed, use it result_dist_allT = utils.nanmean( all_variables[cache_array_name][:, variable_selection_slice_cache], axis=repetitions_axis) elif 'result_em_fits' in all_variables: # Do some annoying slice manipulation slice_valid_indices = variable_selection_slice.indices( all_variables['result_em_fits'].shape[1]) # Create output variables if metric == 'mse': result_dist_allT = np.nan * np.empty( (all_variables['T_space'].size, slice_valid_indices[1] - slice_valid_indices[0])) elif metric == 'kl': result_dist_allT = np.nan * np.empty((all_variables['T_space'].size)) ### Result computation if experiment_id == 'bays09': data_loaded = load_experimental_data.load_data_bays09( fit_mixture_model=True) elif experiment_id == 'gorgo11': data_loaded = load_experimental_data.load_data_gorgo11( fit_mixture_model=True) else: raise ValueError('wrong experiment_id {}'.format(experiment_id)) experimental_mixtures_mean = data_loaded['em_fits_nitems_arrays']['mean'] experimental_T_space = np.unique(data_loaded['n_items']) curr_result = np.nan for T_i, T in enumerate(all_variables['T_space']): if T in experimental_T_space: if metric == 'mse': curr_result = ( experimental_mixtures_mean[variable_selection_slice, experimental_T_space == T] - all_variables['result_em_fits'][T_i, variable_selection_slice] )**2. elif metric == 'kl': curr_result = utils.KL_div( all_variables['result_em_fits'][T_i, variable_selection_slice], experimental_mixtures_mean[variable_selection_slice, experimental_T_space == T], axis=0) result_dist_allT[T_i] = utils.nanmean( curr_result, axis=repetitions_axis) else: raise ValueError( 'array {}/result_em_fits not present, bad'.format(cache_array_name)) print result_dist_allT # return the overall distance, over all parameters and number of items return np.nansum(result_dist_allT)
def plots_3dvolume_hierarchical_M_Mlayerone(data_pbs, generator_module=None): ''' Reload 3D volume runs from PBS and plot them ''' #### SETUP # savefigs = True savedata = False plots_pcolors = False plots_singleaxe = False plots_multipleaxes = True plots_multipleaxes_emfits = True load_fit_mixture_model = True # caching_emfit_filename = None caching_emfit_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_emfit.pickle') plt.rcParams['font.size'] = 16 # #### /SETUP dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) print "Order parameters: ", generator_module.dict_parameters_range.keys() results_precision_constant_M_Mlower = np.squeeze(utils.nanmean(data_pbs.dict_arrays['results_precision_M_T']['results'], axis=-1)) results_precision_constant_M_Mlower_std = np.squeeze(utils.nanstd(data_pbs.dict_arrays['results_precision_M_T']['results'], axis=-1)) results_responses = np.squeeze(data_pbs.dict_arrays['result_responses']['results']) results_targets = np.squeeze(data_pbs.dict_arrays['result_targets']['results']) results_nontargets = np.squeeze(data_pbs.dict_arrays['result_nontargets']['results']) results_emfits_M_T = np.squeeze(data_pbs.dict_arrays['results_emfits_M_T']['results']) M_space = data_pbs.loaded_data['parameters_uniques']['M'] M_layer_one_space = data_pbs.loaded_data['parameters_uniques']['M_layer_one'] ratio_MMlower_space = M_space/generator_module.filtering_function_parameters['target_M_total'] filtering_indices = (np.arange(M_space.size), np.arange(-M_layer_one_space.size, 0)[::-1]) T = results_precision_constant_M_Mlower.shape[-1] T_space = np.arange(T) print M_space print M_layer_one_space print results_precision_constant_M_Mlower.shape # print results_precision_constant_M_Mlower T = results_precision_constant_M_Mlower.shape[-1] results_precision_filtered = results_precision_constant_M_Mlower[filtering_indices] results_precision_filtered_std = results_precision_constant_M_Mlower_std[filtering_indices] results_responses_filtered = results_responses[filtering_indices] results_targets_filtered = results_targets[filtering_indices] results_nontargets_filtered = results_nontargets[filtering_indices] results_emfits_M_T_filtered = results_emfits_M_T[filtering_indices] results_precision_filtered_smoothed = np.apply_along_axis(smooth, 0, results_precision_filtered, *(10, 'bartlett')) if load_fit_mixture_model: # Fit the mixture model on the samples 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_emfits_filtered = cached_data['result_emfits_filtered'] print "Loading from cache file %s" % caching_emfit_filename load_fit_mixture_model = False except IOError: print "Error while loading ", caching_emfit_filename, "falling back to computing the EM fits" load_fit_mixture_model = False if load_fit_mixture_model: result_emfits_filtered = np.nan*np.empty((ratio_MMlower_space.size, T, 5)) # Fit EM model print "fitting EM model" for ratio_MMlower_i, ratio_MMlower in enumerate(ratio_MMlower_space): for T_i in T_space: if np.any(~np.isnan(results_responses_filtered[ratio_MMlower_i, T_i])): print "ratio MM, T:", ratio_MMlower, T_i+1 curr_em_fits = em_circularmixture_allitems_uniquekappa.fit(results_responses_filtered[ratio_MMlower_i, T_i], results_targets_filtered[ratio_MMlower_i, T_i], results_nontargets_filtered[ratio_MMlower_i, T_i, :, :T_i]) curr_em_fits['mixt_nontargets_sum'] = np.sum(curr_em_fits['mixt_nontargets']) result_emfits_filtered[ratio_MMlower_i, T_i] = [curr_em_fits[key] for key in ('kappa', 'mixt_target', 'mixt_nontargets_sum', 'mixt_random', 'train_LL')] # 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_emfit = dict(result_emfits_filtered=result_emfits_filtered) pickle.dump(data_emfit, filecache_out, protocol=2) print "cache file %s written" % caching_emfit_filename except IOError: print "Error writing out to caching file ", caching_emfit_filename if plots_pcolors: utils.pcolor_2d_data(results_precision_filtered, log_scale=True, x=ratio_MMlower_space, y=np.arange(1, T+1), xlabel="$\\frac{M}{M+M_{layer one}}$", ylabel='$T$', ticks_interpolate=10) plt.plot(np.argmax(results_precision_filtered, axis=0), np.arange(results_precision_filtered.shape[-1]), 'ko', markersize=10) if savefigs: dataio.save_current_figure('results_2dlog_{label}_global_{unique_id}.pdf') utils.pcolor_2d_data(results_precision_filtered/np.max(results_precision_filtered, axis=0), x=ratio_MMlower_space, y=np.arange(1, T+1), xlabel="$\\frac{M}{M+M_{layer one}}$", ylabel='$T$', ticks_interpolate=10) plt.plot(np.argmax(results_precision_filtered, axis=0), np.arange(results_precision_filtered.shape[-1]), 'ko', markersize=10) if savefigs: dataio.save_current_figure('results_2dnorm_{label}_global_{unique_id}.pdf') utils.pcolor_2d_data(results_precision_filtered_smoothed/np.max(results_precision_filtered_smoothed, axis=0), x=ratio_MMlower_space, y=np.arange(1, T+1), xlabel="$\\frac{M}{M+M_{layer one}}$", ylabel='$T$', ticks_interpolate=10) plt.plot(np.argmax(results_precision_filtered_smoothed, axis=0), np.arange(results_precision_filtered_smoothed.shape[-1]), 'ko', markersize=10) if savefigs: dataio.save_current_figure('results_2dsmoothnorm_{label}_global_{unique_id}.pdf') if plots_singleaxe: plt.figure() plt.plot(ratio_MMlower_space, results_precision_filtered_smoothed/np.max(results_precision_filtered_smoothed, axis=0), linewidth=2) plt.plot(ratio_MMlower_space[np.argmax(results_precision_filtered_smoothed, axis=0)], np.ones(results_precision_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)) if savefigs: dataio.save_current_figure('results_1dsmoothnormsame_{label}_global_{unique_id}.pdf') plt.figure() moved_results_precision_filtered_smoothed = 1.2*np.arange(results_precision_filtered_smoothed.shape[-1]) + results_precision_filtered_smoothed/np.max(results_precision_filtered_smoothed, axis=0) all_lines = [] for i, max_i in enumerate(np.argmax(results_precision_filtered_smoothed, axis=0)): curr_lines = plt.plot(ratio_MMlower_space, moved_results_precision_filtered_smoothed[:, i], linewidth=2) plt.plot(ratio_MMlower_space[max_i], moved_results_precision_filtered_smoothed[max_i, i], 'o', markersize=10, color=curr_lines[0].get_color()) all_lines.extend(curr_lines) plt.plot(np.linspace(0.0, 1.0, 100), np.outer(np.ones(100), 1.2*np.arange(1, results_precision_filtered_smoothed.shape[-1])), 'k:') plt.grid() plt.legend(all_lines, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='best') plt.ylim((0., moved_results_precision_filtered_smoothed.max()*1.05)) plt.yticks([]) plt.xticks(np.linspace(0, 1.0, 5)) if savefigs: dataio.save_current_figure('results_1dsmoothnorm_{label}_global_{unique_id}.pdf') if plots_multipleaxes: # Plot smooth precisions, all T on multiple subplots. # all_lines = [] f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) for i, max_ind in enumerate(np.argmax(results_precision_filtered_smoothed, axis=0)): curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered_smoothed[:, i], linewidth=2) # , color=all_lines[i].get_color()) axes[i].plot(ratio_MMlower_space[max_ind], results_precision_filtered_smoothed[max_ind, i], 'o', markersize=10, color=curr_lines[0].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) axes[i].set_ylim((np.min(results_precision_filtered_smoothed[:, i]), results_precision_filtered_smoothed[max_ind, i]*1.1)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines.extend(curr_lines) f.subplots_adjust(right=0.75) # plt.figlegend(all_lines, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) # f.tight_layout() if savefigs: dataio.save_current_figure('results_subplots_1dsmoothnorm_{label}_global_{unique_id}.pdf') # Plot precisions with standard deviation around f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) # all_lines_bis = [] for i, max_ind in enumerate(np.argmax(results_precision_filtered, axis=0)): utils.plot_mean_std_area(ratio_MMlower_space, results_precision_filtered[:, i], results_precision_filtered_std[:, i], ax_handle=axes[i], linewidth=2) #, color=all_lines[i].get_color()) # curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[i].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) axes[i].set_ylim((np.min(results_precision_filtered[:, i]), results_precision_filtered[max_ind, i]*1.1)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) f.subplots_adjust(right=0.75) # plt.figlegend(all_lines_bis, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) if savefigs: dataio.save_current_figure('results_subplots_1dnorm_{label}_global_{unique_id}.pdf') if plots_multipleaxes_emfits: f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) all_lines_bis = [] for i, max_ind in enumerate(np.nanargmax(result_emfits_filtered[..., 0], axis=0)): utils.plot_mean_std_area(ratio_MMlower_space, result_emfits_filtered[:, i, 0], 0*result_emfits_filtered[:, i, 0], ax_handle=axes[i], linewidth=2) #, color=all_lines[i].get_color()) # curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[i].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) # axes[i].set_ylim((np.min(result_emfits_filtered[:, i, 0]), result_emfits_filtered[max_ind, i, 0]*1.1)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) f.subplots_adjust(right=0.75) # plt.figlegend(all_lines_bis, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) if savefigs: dataio.save_current_figure('results_subplots_emkappa_{label}_global_{unique_id}.pdf') variables_to_save = [] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='hierarchicalrandomnetwork_characterisation') plt.show() return locals()
def plots_memory_curves(data_pbs, generator_module=None): ''' Reload and plot memory curve of a Mixed code. Can use Marginal Fisher Information and fitted Mixture Model as well ''' #### SETUP # savefigs = True savedata = True plot_pcolor_fit_precision_to_fisherinfo = True plot_selected_memory_curves = False plot_best_memory_curves = 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) ratiohier_space = data_pbs.loaded_data['parameters_uniques']['ratio_hierarchical'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] print ratiohier_space print sigmax_space print T_space print result_all_precisions_mean.shape, result_em_fits_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) ## Load Experimental data data_simult = load_experimental_data.load_data_simult(data_dir=os.path.normpath(os.path.join(os.path.split(load_experimental_data.__file__)[0], '../../experimental_data/')), fit_mixture_model=True) gorgo11_experimental_precision = data_simult['precision_nitems_theo'] gorgo11_experimental_kappa = np.array([data['kappa'] for _, data in data_simult['em_fits_nitems']['mean'].items()]) gorgo11_experimental_kappa_std = np.array([data['kappa'] for _, data in data_simult['em_fits_nitems']['std'].items()]) gorgo11_experimental_emfits_mean = np.array([[data[key] for _, data in data_simult['em_fits_nitems']['mean'].items()] for key in ['kappa', 'mixt_target', 'mixt_nontargets', 'mixt_random']]) gorgo11_experimental_emfits_std = np.array([[data[key] for _, data in data_simult['em_fits_nitems']['std'].items()] for key in ['kappa', 'mixt_target', 'mixt_nontargets', 'mixt_random']]) gorgo11_experimental_emfits_sem = gorgo11_experimental_emfits_std/np.sqrt(np.unique(data_simult['subject']).size) gorgo11_T_space = data_simult['data_to_fit']['n_items'] data_bays2009 = load_experimental_data.load_data_bays09(fit_mixture_model=True) bays09_experimental_mixtures_mean = data_bays2009['em_fits_nitems_arrays']['mean'] bays09_experimental_mixtures_std = data_bays2009['em_fits_nitems_arrays']['std'] # add interpolated points for 3 and 5 items emfit_mean_intpfct = spint.interp1d(np.unique(data_bays2009['n_items']), bays09_experimental_mixtures_mean) bays09_experimental_mixtures_mean_compatible = emfit_mean_intpfct(np.arange(1, 7)) emfit_std_intpfct = spint.interp1d(np.unique(data_bays2009['n_items']), bays09_experimental_mixtures_std) bays09_experimental_mixtures_std_compatible = emfit_std_intpfct(np.arange(1, 7)) bays09_T_space_interp = np.arange(1, 7) bays09_T_space = data_bays2009['data_to_fit']['n_items'] # Compute some landscapes of fit! dist_diff_precision_experim = np.sum(np.abs(result_all_precisions_mean[..., :gorgo11_experimental_precision.size] - gorgo11_experimental_precision)**2., axis=-1) dist_diff_emkappa_experim = np.sum(np.abs(result_em_fits_mean[..., :gorgo11_experimental_precision.size, 0] - gorgo11_experimental_kappa)**2., axis=-1) dist_diff_precision_experim_1item = np.abs(result_all_precisions_mean[..., 0] - gorgo11_experimental_precision[0])**2. dist_diff_precision_experim_2item = np.abs(result_all_precisions_mean[..., 1] - gorgo11_experimental_precision[1])**2. dist_diff_emkappa_experim_1item = np.abs(result_em_fits_mean[..., 0, 0] - gorgo11_experimental_kappa[0])**2. dist_diff_em_mixtures_bays09 = np.sum(np.sum((result_em_fits_mean[..., 1:4] - bays09_experimental_mixtures_mean_compatible[1:].T)**2., axis=-1), axis=-1) dist_diff_modelfits_experfits_bays09 = np.sum(np.sum((result_em_fits_mean[..., :4] - bays09_experimental_mixtures_mean_compatible.T)**2., axis=-1), axis=-1) if plot_pcolor_fit_precision_to_fisherinfo: utils.pcolor_2d_data(dist_diff_precision_experim, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_diff_precision_experim_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_experim, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_diff_emkappa_experim_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim*dist_diff_emkappa_experim, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax', log_scale=True) if savefigs: dataio.save_current_figure('match_bigmultiplication_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim_1item, log_scale=True, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_precision_experim_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_emkappa_experim_1item, log_scale=True, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_emkappa_experim_1item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_precision_experim_2item, log_scale=True, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_precision_experim_2item_log_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_em_mixtures_bays09, log_scale=True, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_mixtures_experbays09_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_diff_modelfits_experfits_bays09, log_scale=True, x=ratiohier_space, y=sigmax_space, xlabel='ratio hier', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_diff_emfits_experbays09_pcolor_{label}_{unique_id}.pdf') # Macro plot def mem_plot_precision(sigmax_i, ratiohier_i, mem_exp_prec): ax = utils.plot_mean_std_area(T_space[:mem_exp_prec.size], mem_exp_prec, np.zeros(mem_exp_prec.size), linewidth=3, fmt='o-', markersize=8, label='Experimental data') ax = utils.plot_mean_std_area(T_space[:mem_exp_prec.size], result_all_precisions_mean[ratiohier_i, sigmax_i, :mem_exp_prec.size], result_all_precisions_std[ratiohier_i, sigmax_i, :mem_exp_prec.size], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Precision of samples') # ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][ratiohier_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][ratiohier_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Marginal Fisher Information') # ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][ratiohier_i, sigmax_i], result_em_fits_std[..., 0][ratiohier_i, sigmax_i], ax_handle=ax, xlabel='Number of items', ylabel="Inverse variance $[rad^{-2}]$", linewidth=3, fmt='o-', markersize=8, label='Fitted kappa') ax.set_title('ratio_hier %.2f, sigmax %.2f' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, mem_exp_prec.size + 0.1]) ax.set_xticks(range(1, mem_exp_prec.size + 1)) ax.set_xticklabels(range(1, mem_exp_prec.size + 1)) if savefigs: dataio.save_current_figure('memorycurves_precision_ratiohier%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) def mem_plot_kappa(sigmax_i, ratiohier_i, T_space_exp, exp_kappa_mean, exp_kappa_std=None): ax = utils.plot_mean_std_area(T_space_exp, exp_kappa_mean, exp_kappa_std, linewidth=3, fmt='o-', markersize=8, label='Experimental data') ax = utils.plot_mean_std_area(T_space[:T_space_exp.max()], result_em_fits_mean[..., :T_space_exp.max(), 0][ratiohier_i, sigmax_i], result_em_fits_std[..., :T_space_exp.max(), 0][ratiohier_i, sigmax_i], xlabel='Number of items', ylabel="Memory error $[rad^{-2}]$", linewidth=3, fmt='o-', markersize=8, label='Fitted kappa', ax_handle=ax) ax.set_title('ratio_hier %.2f, sigmax %.2f' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) ax.legend() ax.set_xlim([0.9, T_space_exp.max()+0.1]) ax.set_xticks(range(1, T_space_exp.max()+1)) ax.set_xticklabels(range(1, T_space_exp.max()+1)) ax.get_figure().canvas.draw() if savefigs: dataio.save_current_figure('memorycurves_kappa_ratiohier%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) # def em_plot(sigmax_i, ratiohier_i): # # TODO finish checking this up. # f, ax = plt.subplots() # ax2 = ax.twinx() # # left axis, kappa # ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][ratiohier_i, sigmax_i], result_em_fits_std[..., 0][ratiohier_i, sigmax_i], xlabel='Number of items', 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(T_space, result_em_fits_mean[..., 1][ratiohier_i, sigmax_i], result_em_fits_std[..., 1][ratiohier_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Target') # utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 2][ratiohier_i, sigmax_i], result_em_fits_std[..., 2][ratiohier_i, sigmax_i], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Nontarget') # utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 3][ratiohier_i, sigmax_i], result_em_fits_std[..., 3][ratiohier_i, sigmax_i], xlabel='Number of items', 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) # ax.set_title('ratio_hier %.2f, sigmax %.2f' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) # ax.set_xlim([0.9, 5.1]) # ax.set_xticks(range(1, 6)) # ax.set_xticklabels(range(1, 6)) # f.canvas.draw() # if savefigs: # dataio.save_current_figure('memorycurves_emfits_ratiohier%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) def em_plot_paper(sigmax_i, ratiohier_i): f, ax = plt.subplots() # mixture probabilities utils.plot_mean_std_area(bays09_T_space_interp, result_em_fits_mean[..., 1][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], result_em_fits_std[..., 1][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Target') utils.plot_mean_std_area(bays09_T_space_interp, result_em_fits_mean[..., 2][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], result_em_fits_std[..., 2][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Nontarget') utils.plot_mean_std_area(bays09_T_space_interp, result_em_fits_mean[..., 3][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], result_em_fits_std[..., 3][ratiohier_i, sigmax_i][:bays09_T_space_interp.size], xlabel='Number of items', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='o-', markersize=5, label='Random') ax.legend(prop={'size':15}) ax.set_title('ratio_hier %.2f, sigmax %.2f' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) ax.set_xlim([1.0, bays09_T_space_interp.size]) ax.set_ylim([0.0, 1.1]) ax.set_xticks(range(1, bays09_T_space_interp.size+1)) ax.set_xticklabels(range(1, bays09_T_space_interp.size+1)) f.canvas.draw() if savefigs: dataio.save_current_figure('memorycurves_emfits_paper_ratiohier%.2fsigmax%.2f_{label}_{unique_id}.pdf' % (ratiohier_space[ratiohier_i], sigmax_space[sigmax_i])) if plot_selected_memory_curves: selected_values = [[0.84, 0.23], [0.84, 0.19]] for current_values in selected_values: # Find the indices ratiohier_i = np.argmin(np.abs(current_values[0] - ratiohier_space)) sigmax_i = np.argmin(np.abs(current_values[1] - sigmax_space)) # mem_plot_precision(sigmax_i, ratiohier_i) # mem_plot_kappa(sigmax_i, ratiohier_i) if plot_best_memory_curves: # Best precision fit best_axis2_i_all = np.argmin(dist_diff_precision_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_precision(best_axis2_i, axis1_i, gorgo11_experimental_precision) # Best kappa fit best_axis2_i_all = np.argmin(dist_diff_emkappa_experim, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa(best_axis2_i, axis1_i, gorgo11_T_space, gorgo11_experimental_emfits_mean[0], gorgo11_experimental_emfits_std[0]) # em_plot(best_axis2_i, axis1_i) # Best em parameters fit to Bays09 best_axis2_i_all = np.argmin(dist_diff_modelfits_experfits_bays09, axis=1) # best_axis2_i_all = np.argmin(dist_diff_em_mixtures_bays09, axis=1) for axis1_i, best_axis2_i in enumerate(best_axis2_i_all): mem_plot_kappa(best_axis2_i, axis1_i, bays09_T_space, bays09_experimental_mixtures_mean[0], bays09_experimental_mixtures_std[0]) # em_plot(best_axis2_i, axis1_i) em_plot_paper(best_axis2_i, axis1_i) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['gorgo11_experimental_precision', 'gorgo11_experimental_kappa', 'bays09_experimental_mixtures_mean_compatible'] if savedata: dataio.save_variables_default(locals(), additional_variables=variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='memory_curves') 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 = 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_3dvolume_hierarchical_M_Mlayerone(data_pbs, generator_module=None): ''' Reload 3D volume runs from PBS and plot them ''' #### SETUP # savefigs = True savedata = False # warning, huge file created... plots_pcolors = False plots_singleaxe = False plots_multipleaxes = True plots_multipleaxes_emfits = True load_fit_mixture_model = True # caching_emfit_filename = None caching_emfit_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_emfit_newshapes.pickle') plt.rcParams['font.size'] = 16 # #### /SETUP dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) print "Order parameters: ", generator_module.dict_parameters_range.keys() results_precision_constant_M_Mlower = np.squeeze(utils.nanmean(data_pbs.dict_arrays['results_precision_M_T']['results'], axis=-1)) results_precision_constant_M_Mlower_std = np.squeeze(utils.nanstd(data_pbs.dict_arrays['results_precision_M_T']['results'], axis=-1)) results_responses = np.squeeze(data_pbs.dict_arrays['result_responses']['results']) results_targets = np.squeeze(data_pbs.dict_arrays['result_targets']['results']) results_nontargets = np.squeeze(data_pbs.dict_arrays['result_nontargets']['results']) # results_emfits_M_T = np.squeeze(data_pbs.dict_arrays['results_emfits_M_T']['results']) M_space = data_pbs.loaded_data['parameters_uniques']['M'] M_layer_one_space = data_pbs.loaded_data['parameters_uniques']['M_layer_one'] ratio_MMlower_space = M_space/generator_module.filtering_function_parameters['target_M_total'] filtering_indices = (np.arange(M_space.size), np.arange(-M_layer_one_space.size, 0)[::-1]) T = results_precision_constant_M_Mlower.shape[-1] T_space = np.arange(T) N = results_nontargets.shape[-2] num_repetitions = data_pbs.loaded_data['args_list'][0]['num_repetitions'] # Fix after @3625585, now the shape are: # results_responses, results_targets: M_space.size, M_layer_one_space.size, T, wrong num_repet, N # results_nontargets: M_space.size, M_layer_one_space.size, T, wrong num_repet, N, T-1 if data_pbs.loaded_data['nb_datasets_per_parameters'] > 1: num_repetitions = data_pbs.loaded_data['nb_datasets_per_parameters'] # Filter and reshape results_responses = results_responses[:, :, :, :num_repetitions, :] results_responses.shape = (M_space.size, M_layer_one_space.size, T, num_repetitions*N) results_targets = results_targets[:, :, :, :num_repetitions, :] results_targets.shape = (M_space.size, M_layer_one_space.size, T, num_repetitions*N) results_nontargets = results_nontargets[:, :, :, :num_repetitions, :, :] results_nontargets.shape = (M_space.size, M_layer_one_space.size, T, num_repetitions*N, T-1) print M_space print M_layer_one_space print results_precision_constant_M_Mlower.shape # print results_precision_constant_M_Mlower results_precision_filtered = results_precision_constant_M_Mlower[filtering_indices] del results_precision_constant_M_Mlower results_precision_filtered_std = results_precision_constant_M_Mlower_std[filtering_indices] del results_precision_constant_M_Mlower_std results_responses_filtered = results_responses[filtering_indices] del results_responses results_targets_filtered = results_targets[filtering_indices] del results_targets results_nontargets_filtered = results_nontargets[filtering_indices] del results_nontargets # results_emfits_M_T_filtered = results_emfits_M_T[filtering_indices] # del results_emfits_M_T results_precision_filtered_smoothed = np.apply_along_axis(smooth, 0, results_precision_filtered, *(10, 'bartlett')) if load_fit_mixture_model: # Fit the mixture model on the samples 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_emfits_filtered = cached_data['result_emfits_filtered'] results_responses_sha1_loaded = cached_data.get('results_responses_sha1', '') # Check that the sha1 is the same, if not recompute! if results_responses_sha1_loaded == hashlib.sha1(results_responses_filtered).hexdigest(): print "Loading from cache file %s" % caching_emfit_filename load_fit_mixture_model = False else: print "Tried loading from cache file %s, but data changed, recomputing..." % caching_emfit_filename load_fit_mixture_model = True except IOError: print "Error while loading ", caching_emfit_filename, "falling back to computing the EM fits" load_fit_mixture_model = False if load_fit_mixture_model: result_emfits_filtered = np.nan*np.empty((ratio_MMlower_space.size, T, 5)) # Fit EM model print "fitting EM model" for ratio_MMlower_i, ratio_MMlower in enumerate(ratio_MMlower_space): for T_i in T_space: if np.any(~np.isnan(results_responses_filtered[ratio_MMlower_i, T_i])): print "ratio MM, T:", ratio_MMlower, T_i+1 curr_em_fits = em_circularmixture_allitems_uniquekappa.fit(results_responses_filtered[ratio_MMlower_i, T_i], results_targets_filtered[ratio_MMlower_i, T_i], results_nontargets_filtered[ratio_MMlower_i, T_i, :, :T_i]) curr_em_fits['mixt_nontargets_sum'] = np.sum(curr_em_fits['mixt_nontargets']) result_emfits_filtered[ratio_MMlower_i, T_i] = [curr_em_fits[key] for key in ('kappa', 'mixt_target', 'mixt_nontargets_sum', 'mixt_random', 'train_LL')] # 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: results_responses_sha1 = hashlib.sha1(results_responses_filtered).hexdigest() data_emfit = dict(result_emfits_filtered=result_emfits_filtered, results_responses_sha1=results_responses_sha1) pickle.dump(data_emfit, filecache_out, protocol=2) print "cache file %s written" % caching_emfit_filename except IOError: print "Error writing out to caching file ", caching_emfit_filename if plots_multipleaxes: # Plot precisions with standard deviation around f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) # all_lines_bis = [] for i, max_ind in enumerate(np.argmax(results_precision_filtered, axis=0)): utils.plot_mean_std_area(ratio_MMlower_space, results_precision_filtered[:, i], results_precision_filtered_std[:, i], ax_handle=axes[i], linewidth=2) #, color=all_lines[i].get_color()) # curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[i].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) axes[i].set_ylim((np.min(results_precision_filtered[:, i]), results_precision_filtered[max_ind, i]*1.1)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) f.subplots_adjust(right=0.75) # plt.figlegend(all_lines_bis, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) if savefigs: dataio.save_current_figure('results_subplots_1dnorm_{label}_global_{unique_id}.pdf') if plots_multipleaxes_emfits: f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) all_lines_bis = [] for i, max_ind in enumerate(np.nanargmax(result_emfits_filtered[..., 0], axis=0)): # Plot Target mixture utils.plot_mean_std_area(ratio_MMlower_space, result_emfits_filtered[:, i, 1:4], 0*result_emfits_filtered[:, i, 1:4], ax_handle=axes[i], linewidth=2) #, color=all_lines[i].get_color()) # curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[i].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) # axes[i].set_ylim((np.min(result_emfits_filtered[:, i, 0]), result_emfits_filtered[max_ind, i, 0]*1.1)) axes[i].set_ylim((0.0, 1.05)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) if savefigs: dataio.save_current_figure('results_subplots_emtarget_{label}_global_{unique_id}.pdf') f, axes = plt.subplots(nrows=T, ncols=1, sharex=True, figsize=(10, 12)) for i, max_ind in enumerate(np.nanargmax(result_emfits_filtered[..., 0], axis=0)): # Plot kappa mixture utils.plot_mean_std_area(ratio_MMlower_space, result_emfits_filtered[:, i, 0], 0*result_emfits_filtered[:, i, 0], ax_handle=axes[i], linewidth=2) #, color=all_lines[i].get_color()) # curr_lines = axes[i].plot(ratio_MMlower_space, results_precision_filtered[:, i], linewidth=2, color=all_lines[i].get_color()) axes[i].grid() axes[i].set_xticks(np.linspace(0., 1.0, 5)) axes[i].set_xlim((0.0, 1.0)) # axes[i].set_yticks([]) # axes[i].set_ylim((np.min(result_emfits_filtered[:, i, 0]), result_emfits_filtered[max_ind, i, 0]*1.1)) # axes[i].set_ylim((0.0, 1.0)) axes[i].locator_params(axis='y', tight=True, nbins=4) # all_lines_bis.extend(curr_lines) # f.subplots_adjust(right=0.75) # plt.figlegend(all_lines_bis, ['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='right', bbox_to_anchor=(1.0, 0.5)) if savefigs: dataio.save_current_figure('results_subplots_emkappa_{label}_global_{unique_id}.pdf') variables_to_save = [] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='hierarchicalrandomnetwork_characterisation') plt.show() return locals()
def plots_fit_mixturemodels_random(data_pbs, generator_module=None): ''' Reload runs from PBS ''' #### SETUP # savefigs = True savedata = True savemovies = False plots_dist_bays09 = True do_scatters_3d = True do_best_points_extended_plots = True # do_relaunch_bestparams_pbs = True colormap = None # or 'cubehelix' plt.rcParams['font.size'] = 16 # #### /SETUP print "Order parameters: ", data_pbs.dataset_infos['parameters'] # parameters: M, ratio_conj, sigmax # Extract data result_em_fits_flat = np.array(data_pbs.dict_arrays['result_em_fits']['results_flat']) result_dist_bays09_flat = np.array(data_pbs.dict_arrays['result_dist_bays09']['results_flat']) result_dist_gorgo11_flat = np.array(data_pbs.dict_arrays['result_dist_gorgo11']['results_flat']) result_parameters_flat = np.array(data_pbs.dict_arrays['result_em_fits']['parameters_flat']) M_space = data_pbs.loaded_data['parameters_uniques']['M'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] ratio_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] num_repetitions = generator_module.num_repetitions parameter_names_sorted = data_pbs.dataset_infos['parameters'] T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] all_args = data_pbs.loaded_data['args_list'] all_repeats_completed = data_pbs.dict_arrays['result_em_fits']['repeats_completed'] dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) # Load bays09 data_bays09 = load_experimental_data.load_data_bays09(fit_mixture_model=True) bays09_nitems = data_bays09['data_to_fit']['n_items'] bays09_em_target = np.nan*np.empty((bays09_nitems.max(), 4)) #kappa, prob_target, prob_nontarget, prob_random bays09_em_target[bays09_nitems - 1] = data_bays09['em_fits_nitems_arrays']['mean'].T bays09_emmixt_target = bays09_em_target[:, 1:] # All parameters info plotting_parameters = launcher_memorycurve.load_prepare_datasets() ## Compute some stuff # result_dist_bays09_kappa_T1_avg = utils.nanmean(result_dist_bays09_flat[:, 0, 0], axis=-1) # result_dist_bays09_kappa_allT_avg = np.nansum(utils.nanmean(result_dist_bays09_flat[:, :, 0], axis=-1), axis=1) # Square distance to kappa result_dist_bays09_allT_avg = utils.nanmean((result_em_fits_flat[:, :, :4] - bays09_em_target[np.newaxis, :, :, np.newaxis])**2, axis=-1) result_dist_bays09_kappa_sum = np.nansum(result_dist_bays09_allT_avg[:, :, 0], axis=-1) result_dist_bays09_kappa_sum_masked = np.ma.masked_greater(result_dist_bays09_kappa_sum, 1e8) result_dist_bays09_kappa_T1_sum = result_dist_bays09_allT_avg[:, 0, 0] result_dist_bays09_kappa_T25_sum = np.nansum(result_dist_bays09_allT_avg[:, 1:, 0], axis=-1) # Square and KL distance for EM Mixtures result_dist_bays09_emmixt_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, :, 1:], axis=-1), axis=-1) result_dist_bays09_emmixt_T1_sum = np.nansum(result_dist_bays09_allT_avg[:, 0, 1:], axis=-1) result_dist_bays09_emmixt_T25_sum = np.nansum(np.nansum(result_dist_bays09_allT_avg[:, 1:, 1:], axis=-1), axis=-1) result_dist_bays09_emmixt_KL = utils.nanmean(utils.KL_div(result_em_fits_flat[:, :, 1:4], bays09_emmixt_target[np.newaxis, :, :, np.newaxis], axis=-2), axis=-1) # KL over dimension of mixtures, then mean over repetitions result_dist_bays09_emmixt_KL_sum = np.nansum(result_dist_bays09_emmixt_KL, axis=-1) # sum over T result_dist_bays09_emmixt_KL_T1_sum = result_dist_bays09_emmixt_KL[:, 0] result_dist_bays09_emmixt_KL_T25_sum = np.nansum(result_dist_bays09_emmixt_KL[:, 1:], axis=-1) result_dist_bays09_both_normalised = result_dist_bays09_emmixt_sum/np.max(result_dist_bays09_emmixt_sum) + result_dist_bays09_kappa_sum/np.max(result_dist_bays09_kappa_sum) result_dist_bays09_kappaKL_normalised_summed = result_dist_bays09_emmixt_KL_sum/np.max(result_dist_bays09_emmixt_KL_sum) + result_dist_bays09_kappa_sum/np.max(result_dist_bays09_kappa_sum) if plots_dist_bays09: nb_best_points = 30 size_normal_points = 8 size_best_points = 50 nb_best_points_extended_plots = 3 def plot_memorycurve(result_em_fits, args_used, suptitle=''): packed_data = dict(T_space=T_space, result_em_fits=result_em_fits, all_parameters=args_used) if suptitle: plotting_parameters['suptitle'] = suptitle if savefigs: packed_data['dataio'] = dataio plotting_parameters['reuse_axes'] = False launcher_memorycurve.do_memory_plots(packed_data, plotting_parameters) def plot_scatter(result_dist_to_use, best_points_result_dist_to_use, result_dist_to_use_name='', title=''): fig = plt.figure() ax = Axes3D(fig) utils.scatter3d(result_parameters_flat[:, 0], result_parameters_flat[:, 1], result_parameters_flat[:, 2], s=size_normal_points, c=np.log(result_dist_to_use), xlabel=parameter_names_sorted[0], ylabel=parameter_names_sorted[1], zlabel=parameter_names_sorted[2], title=title, ax_handle=ax) utils.scatter3d(result_parameters_flat[best_points_result_dist_to_use, 0], result_parameters_flat[best_points_result_dist_to_use, 1], result_parameters_flat[best_points_result_dist_to_use, 2], c='r', s=size_best_points, ax_handle=ax) print "Best points, %s:" % title print '\n'.join(['M %d, ratio %.2f, sigmax %.2f: %f' % (result_parameters_flat[i, 0], result_parameters_flat[i, 1], result_parameters_flat[i, 2], result_dist_to_use[i]) for i in best_points_result_dist_to_use]) if savefigs: dataio.save_current_figure('scatter3d_%s_{label}_{unique_id}.pdf' % result_dist_to_use_name) if savemovies: try: utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_{label}_{unique_id}.mp4' % result_dist_to_use_name), bitrate=8000, min_duration=8) utils.rotate_plot3d(ax, dataio.create_formatted_filename('scatter3d_%s_{label}_{unique_id}.gif' % result_dist_to_use_name), nb_frames=30, min_duration=8) except Exception: # Most likely wrong aggregator... print "failed when creating movies for ", result_dist_to_use_name ax.view_init(azim=90, elev=10) dataio.save_current_figure('scatter3d_%s_view2_{label}_{unique_id}.pdf' % result_dist_to_use_name) return ax def plots_redirects(all_vars, result_dist_to_use_name, log_color=True, title='', avoid_incomplete_repeats=True): result_dist_to_use = all_vars[result_dist_to_use_name] if avoid_incomplete_repeats: result_dist_to_use = np.ma.masked_where(~(all_repeats_completed == num_repetitions-1), result_dist_to_use) if not log_color: result_dist_to_use = np.exp(result_dist_to_use) best_points_result_dist_to_use = np.argsort(result_dist_to_use)[:nb_best_points] # Scatter if do_scatters_3d: plot_scatter(result_dist_to_use, best_points_result_dist_to_use, result_dist_to_use_name, title=title) # Now do the additional plots if required if do_best_points_extended_plots: for best_point_index in best_points_result_dist_to_use[:nb_best_points_extended_plots]: print "extended plot for M %d, ratio %.2f, sigmax %.2f: score %f" % (result_parameters_flat[best_point_index, 0], result_parameters_flat[best_point_index, 1], result_parameters_flat[best_point_index, 2], result_dist_to_use[best_point_index]) plot_memorycurve(result_em_fits_flat[best_point_index], all_args[best_point_index], suptitle=result_dist_to_use_name) # Distance for kappa, all T plots_redirects(locals(), 'result_dist_bays09_kappa_sum', title='kappa all T') # Distance for em fits, all T, Squared distance plots_redirects(locals(), 'result_dist_bays09_emmixt_sum', title='em fits, all T') # Distance for em fits, all T, KL distance plots_redirects(locals(), 'result_dist_bays09_emmixt_KL_sum', title='em fits, all T, KL') # Distance for sum of normalised em fits + normalised kappa, all T plots_redirects(locals(), 'result_dist_bays09_both_normalised', title='summed normalised em mixt + kappa') # Distance kappa T = 1 plots_redirects(locals(), 'result_dist_bays09_kappa_T1_sum', title='Kappa T=1') # Distance kappa T = 2...5 plots_redirects(locals(), 'result_dist_bays09_kappa_T25_sum', title='Kappa T=2/5') # Distance em fits T = 1 plots_redirects(locals(), 'result_dist_bays09_emmixt_T1_sum', title='em fits T=1') # Distance em fits T = 2...5 plots_redirects(locals(), 'result_dist_bays09_emmixt_T25_sum', title='em fits T=2/5') # Distance em fits T = 1, KL plots_redirects(locals(), 'result_dist_bays09_emmixt_KL_T1_sum', title='em fits T=1, KL') # Distance em fits T = 2...5, KL plots_redirects(locals(), 'result_dist_bays09_emmixt_KL_T25_sum', title='em fits T=2/5, KL') # if plots_per_T: # for T in T_space: # currT_indices = result_parameters_flat[:, 2] == T # utils.contourf_interpolate_data_interactive_maxvalue(result_parameters_flat[currT_indices][..., :2], result_fitexperiments_bic_avg[currT_indices], xlabel='Ratio_conj', ylabel='sigma x', title='BIC, T %d' % T, interpolation_numpoints=200, interpolation_method='nearest', log_scale=False) # # Interpolate # if plots_interpolate: # sigmax_target = 0.9 # M_interp_space = np.arange(6, 625, 5) # ratio_interp_space = np.linspace(0.01, 1.0, 50) # # sigmax_interp_space = np.linspace(0.01, 1.0, 50) # sigmax_interp_space = np.array([sigmax_target]) # params_crossspace = np.array(utils.cross(M_interp_space, ratio_interp_space, sigmax_interp_space)) # interpolated_data = rbf_interpolator(params_crossspace[:, 0], params_crossspace[:, 1], params_crossspace[:, 2]).reshape((M_interp_space.size, ratio_interp_space.size)) # utils.pcolor_2d_data(interpolated_data, M_interp_space, ratio_interp_space, 'M', 'ratio', 'interpolated, fixing sigmax= %.2f' % sigmax_target) # points_closeby = ((result_parameters_flat[:, 2] - sigmax_target)**2)< 0.01 # plt.figure() # # plt.imshow(interpolated_data, extent=(M_interp_space.min(), M_interp_space.max(), ratio_interp_space.min(), ratio_interp_space.max())) # plt.imshow(interpolated_data) # plt.scatter(result_parameters_flat[points_closeby, 0], result_parameters_flat[points_closeby, 1], s=100, c=result_fitexperiments_bic_avg[points_closeby], marker='o') # if plot_per_ratio: # # Plot the evolution of loglike as a function of sigmax, with std shown # for ratio_conj_i, ratio_conj in enumerate(ratio_space): # ax = utils.plot_mean_std_area(sigmax_space, result_log_posterior_mean[ratio_conj_i], result_log_posterior_std[ratio_conj_i]) # ax.get_figure().canvas.draw() # if savefigs: # dataio.save_current_figure('results_fitexp_%s_loglike_ratioconj%.2f_{label}_global_{unique_id}.pdf' % (exp_dataset, ratio_conj)) variables_to_save = ['parameter_names_sorted', 'all_repeats_completed', 'T_space'] if savedata: dataio.save_variables_default(locals(), variables_to_save) dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='fit_mixturemodels') plt.show() return locals()
def plots_memory_curves(data_pbs, generator_module=None): ''' Reload and plot memory curve of a feature code. Can use Marginal Fisher Information and fitted Mixture Model as well ''' #### SETUP # savefigs = True savedata = True plot_pcolor_fit_precision_to_fisherinfo = True plot_selected_memory_curves = 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_marginal_inv_fi_mean = utils.nanmean(np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_inv_fi_std = utils.nanstd(np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_fi_mean = utils.nanmean(1./np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) result_marginal_fi_std = utils.nanstd(1./np.squeeze(data_pbs.dict_arrays['result_marginal_inv_fi']['results']), axis=-1) M_space = data_pbs.loaded_data['parameters_uniques']['M'] sigmax_space = data_pbs.loaded_data['parameters_uniques']['sigmax'] T_space = data_pbs.loaded_data['datasets_list'][0]['T_space'] print M_space print sigmax_space print T_space print result_all_precisions_mean.shape, result_em_fits_mean.shape, result_marginal_inv_fi_mean.shape dataio = DataIO.DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename']) ## Load Experimental data data_simult = load_experimental_data.load_data_simult(data_dir=os.path.normpath(os.path.join(os.path.split(load_experimental_data.__file__)[0], '../../experimental_data/'))) memory_experimental = data_simult['precision_nitems_theo'] # Compute some landscapes of fit! dist_diff_precision_margfi = np.sum(np.abs(result_all_precisions_mean*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) dist_diff_precision_margfi_1item = np.abs(result_all_precisions_mean[..., 0]*2. - result_marginal_fi_mean[..., 0, 0])**2. dist_diff_all_precision_margfi = np.abs(result_all_precisions_mean*2. - result_marginal_fi_mean[..., 0])**2. dist_ratio_precision_margfi = np.sum(np.abs((result_all_precisions_mean*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) dist_diff_emkappa_margfi = np.sum(np.abs(result_em_fits_mean[..., 0]*2. - result_marginal_fi_mean[..., 0])**2., axis=-1) dist_ratio_emkappa_margfi = np.sum(np.abs((result_em_fits_mean[..., 0]*2.)/result_marginal_fi_mean[..., 0] - 1.0)**2., axis=-1) dist_diff_precision_experim = np.sum(np.abs(result_all_precisions_mean - memory_experimental)**2., axis=-1) dist_diff_emkappa_experim = np.sum(np.abs(result_em_fits_mean[..., 0] - memory_experimental)**2., axis=-1) if plot_pcolor_fit_precision_to_fisherinfo: # Check fit between precision and fisher info utils.pcolor_2d_data(dist_diff_precision_margfi, log_scale=True, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax') if savefigs: dataio.save_current_figure('match_precision_margfi_log_pcolor_{label}_{unique_id}.pdf') # utils.pcolor_2d_data(dist_diff_precision_margfi, x=M_space, y=sigmax_space[2:], xlabel='M', ylabel='sigmax') # if savefigs: # dataio.save_current_figure('match_precision_margfi_pcolor_{label}_{unique_id}.pdf') utils.pcolor_2d_data(dist_ratio_precision_margfi[4:], x=M_space[4:], y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_diff_emkappa_margfi, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_ratio_emkappa_margfi[4:], x=M_space[4:], y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_diff_precision_experim, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_diff_emkappa_experim, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_diff_precision_margfi*dist_diff_emkappa_margfi*dist_diff_precision_experim*dist_diff_emkappa_experim, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax', log_scale=True) utils.pcolor_2d_data(dist_diff_precision_margfi_1item, log_scale=True, x=M_space, y=sigmax_space, xlabel='M', ylabel='sigmax') if plot_selected_memory_curves: selected_values = [[20, 0.19], [40, 0.15], [100, 0.44], [140, 0.15], [40, 0.17], [60, 0.50]] for current_values in selected_values: # Find the indices M_i = np.argmin(np.abs(current_values[0] - M_space)) sigmax_i = np.argmin(np.abs(current_values[1] - sigmax_space)) ax = utils.plot_mean_std_area(T_space, memory_experimental, np.zeros(T_space.size), linewidth=3, fmt='o-', markersize=8) ax = utils.plot_mean_std_area(T_space, result_all_precisions_mean[M_i, sigmax_i], result_all_precisions_std[M_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8) ax = utils.plot_mean_std_area(T_space, 0.5*result_marginal_fi_mean[..., 0][M_i, sigmax_i], 0.5*result_marginal_fi_std[..., 0][M_i, sigmax_i], ax_handle=ax, linewidth=3, fmt='o-', markersize=8) ax = utils.plot_mean_std_area(T_space, result_em_fits_mean[..., 0][M_i, sigmax_i], result_em_fits_std[..., 0][M_i, sigmax_i], ax_handle=ax, xlabel='Number of items', ylabel='Kappa/Inverse variance', linewidth=3, fmt='o-', markersize=8) ax.set_title('M %d, sigmax %.2f' % (M_space[M_i], sigmax_space[sigmax_i])) plt.legend(['Experimental data', 'Precision of samples', 'Marginal Fisher Information', 'Fitted kappa']) ax.set_xlim([0.9, 5.1]) ax.set_xticks(range(1, 6)) ax.set_xticklabels(range(1, 6)) if savefigs: dataio.save_current_figure('memorycurves_M%dsigmax%.2f_{label}_{unique_id}.pdf' % (M_space[M_i], sigmax_space[sigmax_i])) all_args = data_pbs.loaded_data['args_list'] variables_to_save = ['result_all_precisions_mean', 'result_em_fits_mean', 'result_marginal_inv_fi_mean', 'result_all_precisions_std', 'result_em_fits_std', 'result_marginal_inv_fi_std', 'result_marginal_fi_mean', 'result_marginal_fi_std', 'M_space', 'sigmax_space', 'T_space', 'all_args'] if savedata: dataio.save_variables(variables_to_save, locals()) plt.show() return locals()
def fit_transform(self, X, verbose=True, report_interval=1, X_true=None): """ Fits the imputer on a dataset with missing data, and returns the imputations. Parameters ---------- X : torch.DoubleTensor or torch.cuda.DoubleTensor, shape (n, d) Contains non-missing and missing data at the indices given by the "mask" argument. Missing values can be arbitrarily assigned (e.g. with NaNs). mask : torch.DoubleTensor or torch.cuda.DoubleTensor, shape (n, d) mask[i,j] == 1 if X[i,j] is missing, else mask[i,j] == 0. verbose : bool, default=True If True, output loss to log during iterations. report_interval : int, default=1 Interval between loss reports (if verbose). X_true: torch.DoubleTensor or None, default=None Ground truth for the missing values. If provided, will output a validation score during training. For debugging only. Returns ------- X_filled: torch.DoubleTensor or torch.cuda.DoubleTensor Imputed missing data (plus unchanged non-missing data). """ X = X.clone() n, d = X.shape mask = torch.isnan(X).double() normalized_tol = self.tol * torch.max(torch.abs(X[~mask.bool()])) if self.batchsize > n // 2: e = int(np.log2(n // 2)) self.batchsize = 2**e if verbose: logging.info( f"Batchsize larger that half size = {len(X) // 2}." f" Setting batchsize to {self.batchsize}.") order_ = torch.argsort(mask.sum(0)) optimizers = [ self.opt(self.models[i].parameters(), lr=self.lr, weight_decay=self.weight_decay) for i in range(d) ] imps = (self.noise * torch.randn(mask.shape).double() + nanmean(X, 0))[mask.bool()] X[mask.bool()] = imps X_filled = X.clone() if X_true is not None: maes = np.zeros(self.max_iter) rmses = np.zeros(self.max_iter) for i in range(self.max_iter): if self.order == 'random': order_ = np.random.choice(d, d, replace=False) X_old = X_filled.clone().detach() loss = 0 for l in range(d): j = order_[l].item() n_not_miss = (~mask[:, j].bool()).sum().item() if n - n_not_miss == 0: continue # no missing value on that coordinate for k in range(self.niter): loss = 0 X_filled = X_filled.detach() X_filled[mask[:, j].bool(), j] = self.models[j]( X_filled[mask[:, j].bool(), :][:, np.r_[0:j, j + 1:d]]).squeeze() for _ in range(self.n_pairs): idx1 = np.random.choice(n, self.batchsize, replace=False) X1 = X_filled[idx1] if self.unsymmetrize: n_miss = (~mask[:, j].bool()).sum().item() idx2 = np.random.choice( n_miss, self.batchsize, replace=self.batchsize > n_miss) X2 = X_filled[~mask[:, j].bool(), :][idx2] else: idx2 = np.random.choice(n, self.batchsize, replace=False) X2 = X_filled[idx2] loss = loss + self.sk(X1, X2) optimizers[j].zero_grad() loss.backward() optimizers[j].step() # Impute with last parameters with torch.no_grad(): X_filled[mask[:, j].bool(), j] = self.models[j]( X_filled[mask[:, j].bool(), :][:, np.r_[0:j, j + 1:d]]).squeeze() if X_true is not None: maes[i] = MAE(X_filled, X_true, mask).item() rmses[i] = RMSE(X_filled, X_true, mask).item() if verbose and (i % report_interval == 0): if X_true is not None: logging.info( f'Iteration {i}:\t Loss: {loss.item() / self.n_pairs:.4f}\t' f'Validation MAE: {maes[i]:.4f}\t' f'RMSE: {rmses[i]: .4f}') else: logging.info( f'Iteration {i}:\t Loss: {loss.item() / self.n_pairs:.4f}' ) if torch.norm(X_filled - X_old, p=np.inf) < normalized_tol: break if i == (self.max_iter - 1) and verbose: logging.info('Early stopping criterion not reached') self.is_fitted = True if X_true is not None: return X_filled, maes, rmses else: return X_filled