def main(dbfile):
    """Run the script given a bigrun file"""
    # Get the db
    db = tables.openFile(dbfile)
    simulations = hm.get_first_level_groups(db.root)
    # Sort simulations according to `rate` and `strength`
    ind_rate_strength = get_ind_rate_strength(simulations)
    n_rate = len(set([i[0] for i in ind_rate_strength]))

    # Selected simus are input like "1,2 1,5 2,10"
    selected_simus = raw_input("Simulations #").split(' ')
    selected_simus = [get_1d_ind_simu(s, n_rate) for s in selected_simus]

    # Plot the histogram for each selected simulation
    for i_sim in selected_simus:
        histogram_peaks(simulations[i_sim])

    plt.show()
    db.close()
def main(dbfile):
    """Run the script given the HDF5 filename"""
    # Get the simulations
    db = tables.openFile(dbfile)
    simus = hm.get_first_level_groups(db.root)

    # Define some function to get specific result values
    def get_strength(simu):
        return hm.get_group_attr(simu, ('paramset', '_v_attrs', 'Common', 'inter_conn_strength', 0, 1))

    def get_mps(simu):
        return hm.get_group_attr(simu, ('results', '_v_attrs', 'MPS', 'whole'))

    def get_sts(simu):
        return hm.get_group_attr(simu, ('results', '_v_attrs', 'STS', 'whole'))

    def get_fftmax(simu):
        return hm.get_group_attr(simu, ('results', '_v_attrs', 'FFTMAX', 'mean'))

    def get_peakdist(simu):
        peakdist = hm.get_group_attr(simu, ('results', '_v_attrs', 'peak_distances'))
        peakdist_means = utils.get_dict_values(peakdist, 2, "mean")
        peakdist_disps = utils.get_dict_values(peakdist, 2, "disp")
        return peakdist_means, peakdist_disps

    def get_peakdist_mean(simu):
        means, _ = get_peakdist(simu)
        return np.mean(means), np.std(means)

    def get_peakdist_mean_mean(simu):
        mean, _ = get_peakdist_mean(simu)
        return mean

    def get_peakdist_mean_disp(simu):
        _, disp = get_peakdist_mean(simu)
        return disp

    def get_peakdist_disp(simu):
        _, disps = get_peakdist(simu)
        return np.mean(disps), np.std(disps)
    
    def get_peakdist_disp_mean(simu):
        mean, _ = get_peakdist_disp(simu)
        return mean

    def get_peakdist_disp_disp(simu):
        _, disp = get_peakdist_disp(simu)
        return disp

    def get_spiking_rate(simu):
        spikes_it = hm.get_group_attr(simu, ('results', 'spikes_it')).read()
        nspikes = spikes_it.shape[1]
        nneurons = hm.get_group_attr(simu, ('paramset', '_v_attrs', 'Common', 'N_mitral')) 
        simu_length = hm.get_group_attr(simu, ('paramset', '_v_attrs', 'Common', 'simu_length'))
        return nspikes/float(simu_length)/nneurons

    get_indexes = (get_strength, get_mps, get_sts, get_fftmax,
                   get_peakdist_mean_mean, get_peakdist_mean_disp,
                   get_peakdist_disp_mean, get_peakdist_disp_disp,
                   get_spiking_rate)
    index_names = ("strength", "MPS", "STS", "FFTMAX",
                   "Peak Dist mean (mean)", "Peak Dist mean (disp)",
                   "Peak Dist disp (mean)", "Peak Dist disp (disp)",
                   "Spiking rate")

    # Get simulation indexes for each simulation
    res_indexes = np.ndarray((len(simus), len(get_indexes)))
    for i_simu, simu in enumerate(simus):
        for i_index, get_index in enumerate(get_indexes):
            res_indexes[i_simu, i_index] = get_index(simu)
    # Sort index array on `strength` index
    indsort = res_indexes[:, 0].argsort()
    res_indexes = res_indexes[indsort, :]
    # Sort simulations accordingly
    sorted_simulations = []
    for sorted_index in indsort:
        sorted_simulations.append(simus[sorted_index])

    # Plot the res_indexes against interconnection strength
    plt.ion()
    plt.figure()
    # Plot standard indexes
    data = {1: "MPS (whole)",
            2: "STS (whole)",
            3: "FFTMAX (mean) (Hz)",
            8: "Spiking rate (spikes/sec/neuron)"}
    for iplot in data:
        plt.plot(res_indexes[:, 0], res_indexes[:, iplot], '.', label=data[iplot])
    # Plot peak dist index
    plt.errorbar(res_indexes[:, 0], res_indexes[:, 4], yerr=res_indexes[:, 5],
                 fmt=".", label="Peak Dist (mean)")
    plt.errorbar(res_indexes[:, 0], res_indexes[:, 6], yerr=res_indexes[:, 7],
                 fmt=".", label="Peak Dist (disp)")
    # Add some plot information
    plt.xlabel("Interconnection strength")
    plt.legend()
    plt.show()

    # Get simulations to plot
    psimu = raw_input("Plot simulation #")
    while psimu != "":
        simu = sorted_simulations[int(psimu)]
        for index_name, index_fun in zip(index_names, get_indexes):
            print index_name, index_fun(simu)
        print "\n"
        hm.plot_simulation(simu)
        psimu = raw_input("Plot simulation #")

    db.close()