Exemplo n.º 1
0
    def compute_average_histograms(self):
        '''
            Do per subject and nitems, get average histogram
        '''

        angle_space = np.linspace(-np.pi, np.pi, 51)

        self.dataset['hist_cnts_target_subject_nitems'] = np.empty((self.dataset['subject_size'], self.dataset['n_items_size'], angle_space.size - 1))*np.nan
        self.dataset['hist_cnts_nontarget_subject_nitems'] = np.empty((self.dataset['subject_size'], self.dataset['n_items_size'], angle_space.size - 1))*np.nan
        self.dataset['pvalue_nontarget_subject_nitems'] = np.empty((self.dataset['subject_size'], self.dataset['n_items_size']))*np.nan

        for subject_i, subject in enumerate(np.unique(self.dataset['subject'])):
            for n_items_i, n_items in enumerate(np.unique(self.dataset['n_items'])):
                self.dataset['hist_cnts_target_subject_nitems'][subject_i, n_items_i], x, bins = utils.histogram_binspace(utils.dropnan(self.dataset['errors_subject_nitems'][subject_i, n_items_i]), bins=angle_space, norm='density')

                self.dataset['hist_cnts_nontarget_subject_nitems'][subject_i, n_items_i], x, bins = utils.histogram_binspace(utils.dropnan(self.dataset['errors_nontarget_subject_nitems'][subject_i, n_items_i]), bins=angle_space, norm='density')

                if n_items > 1:
                    self.dataset['pvalue_nontarget_subject_nitems'][subject_i, n_items_i] = utils.V_test(utils.dropnan(self.dataset['errors_nontarget_subject_nitems'][subject_i, n_items_i]).flatten())['pvalue']

        self.dataset['hist_cnts_target_nitems_stats'] = dict(mean=np.mean(self.dataset['hist_cnts_target_subject_nitems'], axis=0), std=np.std(self.dataset['hist_cnts_target_subject_nitems'], axis=0), sem=np.std(self.dataset['hist_cnts_target_subject_nitems'], axis=0)/np.sqrt(self.dataset['subject_size']))

        self.dataset['hist_cnts_nontarget_nitems_stats'] = dict(mean=np.mean(self.dataset['hist_cnts_nontarget_subject_nitems'], axis=0), std=np.std(self.dataset['hist_cnts_nontarget_subject_nitems'], axis=0), sem=np.std(self.dataset['hist_cnts_nontarget_subject_nitems'], axis=0)/np.sqrt(self.dataset['subject_size']))
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 compute_bootstrap_samples(dataset, nb_bootstrap_samples, angle_space):
    responses_resampled = np.empty(
        (np.unique(dataset['n_items']).size,
         nb_bootstrap_samples),
        dtype=np.object)
    error_nontargets_resampled = np.empty(
        (np.unique(dataset['n_items']).size,
         nb_bootstrap_samples),
        dtype=np.object)
    error_targets_resampled = np.empty(
        (np.unique(dataset['n_items']).size,
         nb_bootstrap_samples),
        dtype=np.object)
    hist_cnts_nontarget_bootstraps_nitems = np.empty(
        (np.unique(dataset['n_items']).size,
         nb_bootstrap_samples,
         angle_space.size - 1))*np.nan
    hist_cnts_target_bootstraps_nitems = np.empty(
        (np.unique(dataset['n_items']).size,
         nb_bootstrap_samples,
         angle_space.size - 1))*np.nan

    bootstrap_data = {
        'responses_resampled': responses_resampled,
        'error_nontargets_resampled': error_nontargets_resampled,
        'error_targets_resampled': error_targets_resampled,
        'hist_cnts_nontarget_bootstraps_nitems': hist_cnts_nontarget_bootstraps_nitems,
        'hist_cnts_target_bootstraps_nitems': hist_cnts_target_bootstraps_nitems,
    }
    for n_items_i, n_items in enumerate(np.unique(dataset['n_items'])):
        # Data collapsed accross subjects
        ids_filtered = (dataset['n_items'] == n_items).flatten()

        if n_items > 1:
            # Get random bootstrap nontargets
            bootstrap_nontargets = utils.sample_angle(
                dataset['item_angle'][ids_filtered, 1:n_items].shape + (nb_bootstrap_samples, ))

            # Compute associated EM fits
            # bootstrap_results = []
            for bootstrap_i in progress.ProgressDisplay(np.arange(nb_bootstrap_samples), display=progress.SINGLE_LINE):

                em_fit = em_circularmixture.fit(
                    dataset['response'][ids_filtered, 0],
                    dataset['item_angle'][ids_filtered, 0],
                    bootstrap_nontargets[..., bootstrap_i])

                # bootstrap_results.append(em_fit)

                # Get EM samples
                responses_resampled[n_items_i, bootstrap_i] = (
                    em_circularmixture.sample_from_fit(
                        em_fit,
                        dataset['item_angle'][ids_filtered, 0],
                        bootstrap_nontargets[..., bootstrap_i]))

                # Compute the errors
                error_nontargets_resampled[n_items_i, bootstrap_i] = (
                    utils.wrap_angles(
                        responses_resampled[n_items_i, bootstrap_i][:, np.newaxis] - bootstrap_nontargets[..., bootstrap_i]))
                error_targets_resampled[n_items_i, bootstrap_i] = (
                    utils.wrap_angles(
                        responses_resampled[n_items_i, bootstrap_i] - dataset['item_angle'][ids_filtered, 0]))

                # Bin everything
                (hist_cnts_nontarget_bootstraps_nitems[n_items_i, bootstrap_i],
                 _, _) = (
                    utils.histogram_binspace(
                        utils.dropnan(
                            error_nontargets_resampled[n_items_i, bootstrap_i]),
                        bins=angle_space,
                        norm='density'))
                (hist_cnts_target_bootstraps_nitems[n_items_i, bootstrap_i],
                 _, _) = (
                    utils.histogram_binspace(
                        utils.dropnan(
                            error_targets_resampled[n_items_i, bootstrap_i]),
                        bins=angle_space,
                        norm='density'))
    return bootstrap_data
def plots_misbinding_logposterior(data_pbs, generator_module=None):
    '''
        Reload 3D volume runs from PBS and plot them

    '''


    #### SETUP
    #
    savedata = False
    savefigs = True

    plot_logpost = False
    plot_error = False
    plot_mixtmodel = True
    plot_hist_responses_fisherinfo = True
    compute_plot_bootstrap = False
    compute_fisher_info_perratioconj = True

    # mixturemodel_to_use = 'original'
    mixturemodel_to_use = 'allitems'
    # mixturemodel_to_use = 'allitems_kappafi'

    caching_fisherinfo_filename = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'cache_fisherinfo.pickle')


    #
    #### /SETUP

    print "Order parameters: ", generator_module.dict_parameters_range.keys()

    result_all_log_posterior = np.squeeze(data_pbs.dict_arrays['result_all_log_posterior']['results'])
    result_all_thetas = np.squeeze(data_pbs.dict_arrays['result_all_thetas']['results'])

    ratio_space = data_pbs.loaded_data['parameters_uniques']['ratio_conj']

    print ratio_space
    print result_all_log_posterior.shape

    N = result_all_thetas.shape[-1]

    result_prob_wrong = np.zeros((ratio_space.size, N))
    result_em_fits = np.empty((ratio_space.size, 6))*np.nan

    all_args = data_pbs.loaded_data['args_list']

    fixed_means = [-np.pi*0.6, np.pi*0.6]
    all_angles = np.linspace(-np.pi, np.pi, result_all_log_posterior.shape[-1])

    dataio = DataIO(output_folder=generator_module.pbs_submission_infos['simul_out_dir'] + '/outputs/', label='global_' + dataset_infos['save_output_filename'])


    plt.rcParams['font.size'] = 18


    if plot_hist_responses_fisherinfo:

        # From cache
        if caching_fisherinfo_filename is not None:
            if os.path.exists(caching_fisherinfo_filename):
                # Got file, open it and try to use its contents
                try:
                    with open(caching_fisherinfo_filename, 'r') as file_in:
                        # Load and assign values
                        cached_data = pickle.load(file_in)
                        result_fisherinfo_ratio = cached_data['result_fisherinfo_ratio']
                        compute_fisher_info_perratioconj = False

                except IOError:
                    print "Error while loading ", caching_fisherinfo_filename, "falling back to computing the Fisher Info"

        if compute_fisher_info_perratioconj:
            # We did not save the Fisher info, but need it if we want to fit the mixture model with fixed kappa. So recompute them using the args_dicts

            result_fisherinfo_ratio = np.empty(ratio_space.shape)

            # Invert the all_args_i -> ratio_conj direction
            parameters_indirections = data_pbs.loaded_data['parameters_dataset_index']

            for ratio_conj_i, ratio_conj in enumerate(ratio_space):
                # Get index of first dataset with the current ratio_conj (no need for the others, I think)
                arg_index = parameters_indirections[(ratio_conj,)][0]

                # Now using this dataset, reconstruct a RandomFactorialNetwork and compute the fisher info
                curr_args = all_args[arg_index]

                curr_args['stimuli_generation'] = lambda T: np.linspace(-np.pi*0.6, np.pi*0.6, T)

                (random_network, data_gen, stat_meas, sampler) = launchers.init_everything(curr_args)

                # Theo Fisher info
                result_fisherinfo_ratio[ratio_conj_i] = sampler.estimate_fisher_info_theocov()

                del curr_args['stimuli_generation']

            # Save everything to a file, for faster later plotting
            if caching_fisherinfo_filename is not None:
                try:
                    with open(caching_fisherinfo_filename, 'w') as filecache_out:
                        data_cache = dict(result_fisherinfo_ratio=result_fisherinfo_ratio)
                        pickle.dump(data_cache, filecache_out, protocol=2)
                except IOError:
                    print "Error writing out to caching file ", caching_fisherinfo_filename

        # Now plots. Do histograms of responses (around -pi/6 and pi/6), add Von Mises derived from Theo FI on top, and vertical lines for the correct target/nontarget angles.
        for ratio_conj_i, ratio_conj in enumerate(ratio_space):
            # Histogram
            ax = utils.hist_angular_data(result_all_thetas[ratio_conj_i], bins=100, title='ratio %.2f, fi %.0f' % (ratio_conj, result_fisherinfo_ratio[ratio_conj_i]))
            bar_heights, _, _ = utils.histogram_binspace(result_all_thetas[ratio_conj_i], bins=100, norm='density')

            # Add Fisher info prediction on top
            x = np.linspace(-np.pi, np.pi, 1000)
            if result_fisherinfo_ratio[ratio_conj_i] < 700:
                # Von Mises PDF
                utils.plot_vonmises_pdf(x, utils.stddev_to_kappa(1./result_fisherinfo_ratio[ratio_conj_i]**0.5), mu=fixed_means[-1], ax_handle=ax, linewidth=3, color='r', scale=np.max(bar_heights), fmt='-')
            else:
                # Switch to Gaussian instead
                utils.plot_normal_pdf(x, mu=fixed_means[-1], std=1./result_fisherinfo_ratio[ratio_conj_i]**0.5, ax_handle=ax, linewidth=3, color='r', scale=np.max(bar_heights), fmt='-')

            # ax.set_xticks([])
            # ax.set_yticks([])

            # Add vertical line to correct target/nontarget
            ax.axvline(x=fixed_means[0], color='g', linewidth=2)
            ax.axvline(x=fixed_means[1], color='r', linewidth=2)

            ax.get_figure().canvas.draw()

            if savefigs:
                # plt.tight_layout()
                dataio.save_current_figure('results_misbinding_histresponses_vonmisespdf_ratioconj%.2f{label}_{unique_id}.pdf' % (ratio_conj))



    if plot_logpost:
        for ratio_conj_i, ratio_conj in enumerate(ratio_space):
            # ax = utils.plot_mean_std_area(all_angles, nanmean(result_all_log_posterior[ratio_conj_i], axis=0), nanstd(result_all_log_posterior[ratio_conj_i], axis=0))

            # ax.set_xlim((-np.pi, np.pi))
            # ax.set_xticks((-np.pi, -np.pi / 2, 0, np.pi / 2., np.pi))
            # ax.set_xticklabels((r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'))
            # ax.set_yticks(())

            # ax.get_figure().canvas.draw()

            # if savefigs:
            #     dataio.save_current_figure('results_misbinding_logpost_ratioconj%.2f_{label}_global_{unique_id}.pdf' % ratio_conj)


            # Compute the probability of answering wrongly (from fitting mixture distrib onto posterior)
            for n in xrange(result_all_log_posterior.shape[1]):
                result_prob_wrong[ratio_conj_i, n], _, _ = utils.fit_gaussian_mixture_fixedmeans(all_angles, np.exp(result_all_log_posterior[ratio_conj_i, n]), fixed_means=fixed_means, normalise=True, return_fitted_data=False, should_plot=False)

        # ax = utils.plot_mean_std_area(ratio_space, nanmean(result_prob_wrong, axis=-1), nanstd(result_prob_wrong, axis=-1))
        plt.figure()
        plt.plot(ratio_space, utils.nanmean(result_prob_wrong, axis=-1))

        # ax.get_figure().canvas.draw()
        if savefigs:
            dataio.save_current_figure('results_misbinding_probwrongpost_allratioconj_{label}_global_{unique_id}.pdf')

    if plot_error:

        ## Compute Standard deviation/precision from samples and plot it as a function of ratio_conj
        stats = utils.compute_mean_std_circular_data(utils.wrap_angles(result_all_thetas - fixed_means[1]).T)

        f = plt.figure()
        plt.plot(ratio_space, stats['std'])
        plt.ylabel('Standard deviation [rad]')

        if savefigs:
            dataio.save_current_figure('results_misbinding_stddev_allratioconj_{label}_global_{unique_id}.pdf')

        f = plt.figure()
        plt.plot(ratio_space, utils.compute_angle_precision_from_std(stats['std'], square_precision=False), linewidth=2)
        plt.ylabel('Precision [$1/rad$]')
        plt.xlabel('Proportion of conjunctive units')
        plt.grid()

        if savefigs:
            dataio.save_current_figure('results_misbinding_precision_allratioconj_{label}_global_{unique_id}.pdf')

        ## Compute the probability of misbinding
        # 1) Just count samples < 0 / samples tot
        # 2) Fit a mixture model, average over mixture probabilities
        prob_smaller0 = np.sum(result_all_thetas <= 1, axis=1)/float(result_all_thetas.shape[1])

        em_centers = np.zeros((ratio_space.size, 2))
        em_covs = np.zeros((ratio_space.size, 2))
        em_pk = np.zeros((ratio_space.size, 2))
        em_ll = np.zeros(ratio_space.size)
        for ratio_conj_i, ratio_conj in enumerate(ratio_space):
            cen_lst, cov_lst, em_pk[ratio_conj_i], em_ll[ratio_conj_i] = pygmm.em(result_all_thetas[ratio_conj_i, np.newaxis].T, K = 2, max_iter = 400, init_kw={'cluster_init':'fixed', 'fixed_means': fixed_means})

            em_centers[ratio_conj_i] = np.array(cen_lst).flatten()
            em_covs[ratio_conj_i] = np.array(cov_lst).flatten()

        # print em_centers
        # print em_covs
        # print em_pk

        f = plt.figure()
        plt.plot(ratio_space, prob_smaller0)
        plt.ylabel('Misbound proportion')
        if savefigs:
            dataio.save_current_figure('results_misbinding_countsmaller0_allratioconj_{label}_global_{unique_id}.pdf')

        f = plt.figure()
        plt.plot(ratio_space, np.max(em_pk, axis=-1), 'g', linewidth=2)
        plt.ylabel('Mixture proportion, correct')
        plt.xlabel('Proportion of conjunctive units')
        plt.grid()
        if savefigs:
            dataio.save_current_figure('results_misbinding_emmixture_allratioconj_{label}_global_{unique_id}.pdf')


        # Put everything on one figure
        f = plt.figure(figsize=(10, 6))
        norm_for_plot = lambda x: (x - np.min(x))/np.max((x - np.min(x)))
        plt.plot(ratio_space, norm_for_plot(stats['std']), ratio_space, norm_for_plot(utils.compute_angle_precision_from_std(stats['std'], square_precision=False)), ratio_space, norm_for_plot(prob_smaller0), ratio_space, norm_for_plot(em_pk[:, 1]), ratio_space, norm_for_plot(em_pk[:, 0]))
        plt.legend(('Std dev', 'Precision', 'Prob smaller 1', 'Mixture proportion correct', 'Mixture proportion misbinding'))
        # plt.plot(ratio_space, norm_for_plot(compute_angle_precision_from_std(stats['std'], square_precision=False)), ratio_space, norm_for_plot(em_pk[:, 1]), linewidth=2)
        # plt.legend(('Precision', 'Mixture proportion correct'), loc='best')
        plt.grid()
        if savefigs:
            dataio.save_current_figure('results_misbinding_allmetrics_allratioconj_{label}_global_{unique_id}.pdf')


    if plot_mixtmodel:
        # Fit Paul's model
        target_angle = np.ones(N)*fixed_means[1]
        nontarget_angles = np.ones((N, 1))*fixed_means[0]

        for ratio_conj_i, ratio_conj in enumerate(ratio_space):
            print "Ratio: ", ratio_conj

            responses = result_all_thetas[ratio_conj_i]

            if mixturemodel_to_use == 'allitems_kappafi':
                curr_params_fit = em_circularmixture_allitems_kappafi.fit(responses, target_angle, nontarget_angles, kappa=result_fisherinfo_ratio[ratio_conj_i])
            elif mixturemodel_to_use == 'allitems':
                curr_params_fit = em_circularmixture_allitems_uniquekappa.fit(responses, target_angle, nontarget_angles)
            else:
                curr_params_fit = em_circularmixture.fit(responses, target_angle, nontarget_angles)

            result_em_fits[ratio_conj_i] = [curr_params_fit['kappa'], curr_params_fit['mixt_target']] + utils.arrnum_to_list(curr_params_fit['mixt_nontargets']) + [curr_params_fit[key] for key in ('mixt_random', 'train_LL', 'bic')]

            print curr_params_fit


        if False:
            f, ax = plt.subplots()
            ax2 = ax.twinx()

            # left axis, kappa
            ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 0], 0*result_em_fits[:, 0], xlabel='Proportion of conjunctive units', ylabel="Inverse variance $[rad^{-2}]$", ax_handle=ax, linewidth=3, fmt='o-', markersize=8, label='Fitted kappa', color='k')

            # Right axis, mixture probabilities
            utils.plot_mean_std_area(ratio_space, result_em_fits[:, 1], 0*result_em_fits[:, 1], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Target')
            utils.plot_mean_std_area(ratio_space, result_em_fits[:, 2], 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Nontarget')
            utils.plot_mean_std_area(ratio_space, result_em_fits[:, 3], 0*result_em_fits[:, 3], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax2, linewidth=3, fmt='o-', markersize=8, label='Random')

            lines, labels = ax.get_legend_handles_labels()
            lines2, labels2 = ax2.get_legend_handles_labels()
            ax.legend(lines + lines2, labels + labels2, fontsize=12, loc='right')

            # ax.set_xlim([0.9, 5.1])
            # ax.set_xticks(range(1, 6))
            # ax.set_xticklabels(range(1, 6))
            plt.grid()

            f.canvas.draw()

        if True:
            # Mixture probabilities
            ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 1], 0*result_em_fits[:, 1], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", linewidth=3, fmt='-', markersize=8, label='Target')
            utils.plot_mean_std_area(ratio_space, result_em_fits[:, 2], 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='-', markersize=8, label='Nontarget')
            utils.plot_mean_std_area(ratio_space, result_em_fits[:, 3], 0*result_em_fits[:, 3], xlabel='Proportion of conjunctive units', ylabel="Mixture probabilities", ax_handle=ax, linewidth=3, fmt='-', markersize=8, label='Random')

            ax.legend(loc='right')

            # ax.set_xlim([0.9, 5.1])
            # ax.set_xticks(range(1, 6))
            # ax.set_xticklabels(range(1, 6))
            plt.grid()

            if savefigs:
                dataio.save_current_figure('results_misbinding_emmixture_allratioconj_{label}_global_{unique_id}.pdf')

        if True:
            # Kappa
            # ax = utils.plot_mean_std_area(ratio_space, result_em_fits[:, 0], 0*result_em_fits[:, 0], xlabel='Proportion of conjunctive units', ylabel="$\kappa [rad^{-2}]$", linewidth=3, fmt='-', markersize=8, label='Kappa')
            ax = utils.plot_mean_std_area(ratio_space, utils.kappa_to_stddev(result_em_fits[:, 0]), 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Standard deviation [rad]", linewidth=3, fmt='-', markersize=8, label='Mixture model $\kappa$')

            # Add Fisher Info theo
            ax = utils.plot_mean_std_area(ratio_space, utils.kappa_to_stddev(result_fisherinfo_ratio), 0*result_em_fits[:, 2], xlabel='Proportion of conjunctive units', ylabel="Standard deviation [rad]", linewidth=3, fmt='-', markersize=8, label='Fisher Information', ax_handle=ax)

            ax.legend(loc='best')

            # ax.set_xlim([0.9, 5.1])
            # ax.set_xticks(range(1, 6))
            # ax.set_xticklabels(range(1, 6))
            plt.grid()

            if savefigs:
                dataio.save_current_figure('results_misbinding_kappa_allratioconj_{label}_global_{unique_id}.pdf')

    if compute_plot_bootstrap:
        ## Compute the bootstrap pvalue for each ratio
        #       use the bootstrap CDF from mixed runs, not the exact current ones, not sure if good idea.

        bootstrap_to_load = 1
        if bootstrap_to_load == 1:
            cache_bootstrap_fn = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_bootstrap_mixed_from_bootstrapnontargets.pickle')
            bootstrap_ecdf_sum_label = 'bootstrap_ecdf_allitems_sum_sigmax_T'
            bootstrap_ecdf_all_label = 'bootstrap_ecdf_allitems_all_sigmax_T'
        elif bootstrap_to_load == 2:
            cache_bootstrap_fn = os.path.join(generator_module.pbs_submission_infos['simul_out_dir'], 'outputs', 'cache_bootstrap_misbinding_mixed.pickle')
            bootstrap_ecdf_sum_label = 'bootstrap_ecdf_allitems_sum_ratioconj'
            bootstrap_ecdf_all_label = 'bootstrap_ecdf_allitems_all_ratioconj'

        try:
            with open(cache_bootstrap_fn, 'r') as file_in:
                # Load and assign values
                cached_data = pickle.load(file_in)
                assert bootstrap_ecdf_sum_label in cached_data
                assert bootstrap_ecdf_all_label in cached_data
                should_fit_bootstrap = False

        except IOError:
            print "Error while loading ", cache_bootstrap_fn

        # Select the ECDF to use
        if bootstrap_to_load == 1:
            sigmax_i = 3    # corresponds to sigmax = 2, input here.
            T_i = 1         # two possible targets here.
            bootstrap_ecdf_sum_used = cached_data[bootstrap_ecdf_sum_label][sigmax_i][T_i]['ecdf']
            bootstrap_ecdf_all_used = cached_data[bootstrap_ecdf_all_label][sigmax_i][T_i]['ecdf']
        elif bootstrap_to_load == 2:
            ratio_conj_i = 4
            bootstrap_ecdf_sum_used = cached_data[bootstrap_ecdf_sum_label][ratio_conj_i]['ecdf']
            bootstrap_ecdf_all_used = cached_data[bootstrap_ecdf_all_label][ratio_conj_i]['ecdf']


        result_pvalue_bootstrap_sum = np.empty(ratio_space.size)*np.nan
        result_pvalue_bootstrap_all = np.empty((ratio_space.size, nontarget_angles.shape[-1]))*np.nan

        for ratio_conj_i, ratio_conj in enumerate(ratio_space):
            print "Ratio: ", ratio_conj

            responses = result_all_thetas[ratio_conj_i]

            bootstrap_allitems_nontargets_allitems_uniquekappa = em_circularmixture_allitems_uniquekappa.bootstrap_nontarget_stat(responses, target_angle, nontarget_angles,
                sumnontargets_bootstrap_ecdf=bootstrap_ecdf_sum_used,
                allnontargets_bootstrap_ecdf=bootstrap_ecdf_all_used)

            result_pvalue_bootstrap_sum[ratio_conj_i] = bootstrap_allitems_nontargets_allitems_uniquekappa['p_value']
            result_pvalue_bootstrap_all[ratio_conj_i] = bootstrap_allitems_nontargets_allitems_uniquekappa['allnontarget_p_value']

        ## Plots
        # f, ax = plt.subplots()
        # ax.plot(ratio_space, result_pvalue_bootstrap_all, linewidth=2)

        # if savefigs:
        #     dataio.save_current_figure("pvalue_bootstrap_all_ratioconj_{label}_{unique_id}.pdf")

        f, ax = plt.subplots()
        ax.plot(ratio_space, result_pvalue_bootstrap_sum, linewidth=2)
        plt.grid()

        if savefigs:
            dataio.save_current_figure("pvalue_bootstrap_sum_ratioconj_{label}_{unique_id}.pdf")


    # plt.figure()
    # plt.plot(ratio_MMlower, results_filtered_smoothed/np.max(results_filtered_smoothed, axis=0), linewidth=2)
    # plt.plot(ratio_MMlower[np.argmax(results_filtered_smoothed, axis=0)], np.ones(results_filtered_smoothed.shape[-1]), 'ro', markersize=10)
    # plt.grid()
    # plt.ylim((0., 1.1))
    # plt.subplots_adjust(right=0.8)
    # plt.legend(['%d item' % i + 's'*(i>1) for i in xrange(1, T+1)], loc='center right', bbox_to_anchor=(1.3, 0.5))
    # plt.xticks(np.linspace(0, 1.0, 5))

    variables_to_save = ['target_angle', 'nontarget_angles']

    if savedata:
        dataio.save_variables_default(locals(), variables_to_save)
        dataio.make_link_output_to_dropbox(dropbox_current_experiment_folder='misbindings')


    plt.show()

    return locals()
Exemplo n.º 5
0
def plot_bootstrap_randomsamples():
    '''
        Do histograms with random samples from bootstrap nontarget estimates
    '''

    dataio = DataIO(label='plotpaper_bootstrap_randomized')

    nb_bootstrap_samples = 200
    use_precomputed = True

    angle_space = np.linspace(-np.pi, np.pi, 51)
    bins_center = angle_space[:-1] + np.diff(angle_space)[0]/2

    data_bays2009 = load_experimental_data.load_data_bays09(fit_mixture_model=True)

    ## Super long simulation, use precomputed data maybe?
    if use_precomputed:
        data = pickle.load(open('/Users/loicmatthey/Dropbox/UCL/1-phd/Work/Visual_working_memory/code/git-bayesian-visual-working-memory/Data/cache_randomized_bootstrap_samples_plots_paper_theo_plotbootstrapsamples/bootstrap_histo_katz.npy', 'r'))

        responses_resampled = data['responses_resampled']
        error_nontargets_resampled = data['error_nontargets_resampled']
        error_targets_resampled = data['error_targets_resampled']
        hist_cnts_nontarget_bootstraps_nitems = data['hist_cnts_nontarget_bootstraps_nitems']
        hist_cnts_target_bootstraps_nitems = data['hist_cnts_target_bootstraps_nitems']
    else:
        responses_resampled = np.empty((np.unique(data_bays2009['n_items']).size, nb_bootstrap_samples), dtype=np.object)
        error_nontargets_resampled = np.empty((np.unique(data_bays2009['n_items']).size, nb_bootstrap_samples), dtype=np.object)
        error_targets_resampled = np.empty((np.unique(data_bays2009['n_items']).size, nb_bootstrap_samples), dtype=np.object)
        hist_cnts_nontarget_bootstraps_nitems = np.empty((np.unique(data_bays2009['n_items']).size, nb_bootstrap_samples, angle_space.size - 1))*np.nan
        hist_cnts_target_bootstraps_nitems = np.empty((np.unique(data_bays2009['n_items']).size, nb_bootstrap_samples, angle_space.size - 1))*np.nan

        for n_items_i, n_items in enumerate(np.unique(data_bays2009['n_items'])):
            # Data collapsed accross subjects
            ids_filtered = (data_bays2009['n_items'] == n_items).flatten()

            if n_items > 1:

                # Get random bootstrap nontargets
                bootstrap_nontargets = utils.sample_angle(data_bays2009['item_angle'][ids_filtered, 1:n_items].shape + (nb_bootstrap_samples, ))

                # Compute associated EM fits
                bootstrap_results = []
                for bootstrap_i in progress.ProgressDisplay(np.arange(nb_bootstrap_samples), display=progress.SINGLE_LINE):

                    em_fit = em_circularmixture_allitems_uniquekappa.fit(data_bays2009['response'][ids_filtered, 0], data_bays2009['item_angle'][ids_filtered, 0], bootstrap_nontargets[..., bootstrap_i])

                    bootstrap_results.append(em_fit)

                    # Get EM samples
                    responses_resampled[n_items_i, bootstrap_i] = em_circularmixture_allitems_uniquekappa.sample_from_fit(em_fit, data_bays2009['item_angle'][ids_filtered, 0], bootstrap_nontargets[..., bootstrap_i])

                    # Compute the errors
                    error_nontargets_resampled[n_items_i, bootstrap_i] = utils.wrap_angles(responses_resampled[n_items_i, bootstrap_i][:, np.newaxis] - bootstrap_nontargets[..., bootstrap_i])
                    error_targets_resampled[n_items_i, bootstrap_i] = utils.wrap_angles(responses_resampled[n_items_i, bootstrap_i] - data_bays2009['item_angle'][ids_filtered, 0])

                    # Bin everything
                    hist_cnts_nontarget_bootstraps_nitems[n_items_i, bootstrap_i], x, bins = utils.histogram_binspace(utils.dropnan(error_nontargets_resampled[n_items_i, bootstrap_i]), bins=angle_space, norm='density')
                    hist_cnts_target_bootstraps_nitems[n_items_i, bootstrap_i], x, bins = utils.histogram_binspace(utils.dropnan(error_targets_resampled[n_items_i, bootstrap_i]), bins=angle_space, norm='density')

    # Now show average histogram
    hist_cnts_target_bootstraps_nitems_mean = np.mean(hist_cnts_target_bootstraps_nitems, axis=-2)
    hist_cnts_target_bootstraps_nitems_std = np.std(hist_cnts_target_bootstraps_nitems, axis=-2)
    hist_cnts_target_bootstraps_nitems_sem = hist_cnts_target_bootstraps_nitems_std/np.sqrt(hist_cnts_target_bootstraps_nitems.shape[1])

    hist_cnts_nontarget_bootstraps_nitems_mean = np.mean(hist_cnts_nontarget_bootstraps_nitems, axis=-2)
    hist_cnts_nontarget_bootstraps_nitems_std = np.std(hist_cnts_nontarget_bootstraps_nitems, axis=-2)
    hist_cnts_nontarget_bootstraps_nitems_sem = hist_cnts_nontarget_bootstraps_nitems_std/np.sqrt(hist_cnts_target_bootstraps_nitems.shape[1])

    f1, axes1 = plt.subplots(ncols=np.unique(data_bays2009['n_items']).size-1, figsize=((np.unique(data_bays2009['n_items']).size-1)*6, 6), sharey=True)
    for n_items_i, n_items in enumerate(np.unique(data_bays2009['n_items'])):
        if n_items>1:
            utils.plot_mean_std_area(bins_center, hist_cnts_nontarget_bootstraps_nitems_mean[n_items_i], hist_cnts_nontarget_bootstraps_nitems_sem[n_items_i], ax_handle=axes1[n_items_i-1], color='k')

            # Now add the Data histograms
            axes1[n_items_i-1].bar(bins_center, data_bays2009['hist_cnts_nontarget_nitems_stats']['mean'][n_items_i], width=2.*np.pi/(angle_space.size-1), align='center', yerr=data_bays2009['hist_cnts_nontarget_nitems_stats']['sem'][n_items_i])
            # axes4[n_items_i-1].set_title('N=%d' % n_items)
            axes1[n_items_i-1].set_xlim([bins_center[0]-np.pi/(angle_space.size-1), bins_center[-1]+np.pi/(angle_space.size-1)])

            # axes3[n_items_i-1].set_ylim([0., 2.0])
            axes1[n_items_i-1].set_xticks((-np.pi, -np.pi/2, 0, np.pi/2., np.pi))
            axes1[n_items_i-1].set_xticklabels((r'$-\pi$', r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'), fontsize=16)

            # axes1[n_items_i-1].bar(bins_center, hist_cnts_nontarget_bootstraps_nitems_mean[n_items_i], width=2.*np.pi/(angle_space.size-1), align='center', yerr=hist_cnts_nontarget_bootstraps_nitems_std[n_items_i])
            axes1[n_items_i-1].get_figure().canvas.draw()

    if dataio is not None:
        plt.tight_layout()
        dataio.save_current_figure("hist_error_nontarget_persubj_{label}_{unique_id}.pdf")


    if False:
        f2, axes2 = plt.subplots(ncols=np.unique(data_bays2009['n_items']).size-1, figsize=((np.unique(data_bays2009['n_items']).size-1)*6, 6), sharey=True)
        for n_items_i, n_items in enumerate(np.unique(data_bays2009['n_items'])):
            utils.plot_mean_std_area(bins_center, hist_cnts_target_bootstraps_nitems_mean[n_items_i], hist_cnts_target_bootstraps_nitems_std[n_items_i], ax_handle=axes2[n_items_i-1])
            # axes2[n_items_i-1].bar(bins_center, hist_cnts_target_bootstraps_nitems_mean[n_items_i], width=2.*np.pi/(angle_space.size-1), align='center', yerr=hist_cnts_target_bootstraps_nitems_std[n_items_i])

    return locals()