Esempio n. 1
0
def MCMCMC(starting_trees,
           posterior_function,
           summaries,
           temperature_scheme,
           printing_schemes,
           iteration_scheme,
           overall_thinnings,
           proposal_scheme,
           cores=4,
           no_chains=None,
           numpy_seeds=None,
           multiplier=None,
           result_file=None,
           store_permuts=False,
           stop_criteria=None,
           make_outfile_stills=[],
           save_only_coldest_chain=False,
           posterior_function_list=[]):
    '''
    this function runs a MC3 using the basic_chain_unpacker. Let no_chains=number of chains. The inputs are
        starting_trees: a list of one or more trees that the chains should be started with
        posterior_function: one 'initialized'(see MCMC.py) unnormalized posterior function, that should be simulated from.
        summaries: a list of instances of realization of concrete classes of the superclass Summary. It is closely linked to printing_scheme, 
                   because they are only meaningful if specified in that.
        temperature_scheme: one instance of a class that has the functions:
                                    - get_temp(i): returns the the temperature for the i'th chain
                                    - update_temp(permut): updates the temperatures for each chain using the permutation, permut
        printing_scheme: a list of either one or no_chains dictionaries, where each dictionary gives the sample_verbose_scheme of basic_chain
        iteration_scheme: a list that sums to the total number of iterations, where each entry is the number of MH-steps before the next flipping
        overall_thinnings: an integer indicating how much should be skipped before each summary statistics is calculated
        proposal_scheme: a list of instances of classes that handles proposals and updates of said proposals. It has the basic functions
                                    - prop(x,pks): proposes the next tree(and returns proposal densities and statistics in pks)
                                    - adapt(mhr): updates the proposals based on the mhr ratio, mhr
                                    - extract_new_values(): after two chains have changed position in the flipping, this function gets the information that should be changed_later
                                    - wear_new_values(information): the new values from extract_new_values replaces the old values.
        #ps:     - a list of doubles in the interval [0,1] indicating the parameter of the geometrically distributed prior on the number of admixture events for each chain. Or:
        #        - a double in the interval [0,1] indicating the same parameter for all geometrically distributed prior on the number of admixture events.
                                    
    '''

    #h=hpy()

    if no_chains is None:
        no_chains = cores

    #if isinstance(ps, int):
    #   ps=[ps]*no_chains

    if len(printing_schemes) == 1:
        printing_schemes = [printing_schemes[0]] * no_chains

    df_result = None
    total_permutation = range(no_chains)
    permuts = []
    xs = starting_trees

    freeze_support()

    if numpy_seeds is None:
        numpy_seeds = [None] * no_chains

    if posterior_function_list:
        pool = basic_chain_pool(summaries, None, proposal_scheme, numpy_seeds,
                                posterior_function_list)
        rs = [posterior_f.base_r for posterior_f in posterior_function_list]
        ps = [posterior_f.p for posterior_f in posterior_function_list]
        posteriors = [
            posterior_f(x)
            for x, posterior_f in zip(xs, posterior_function_list)
        ]
    else:
        rs = []
        ps = [posterior_function.p]
        pool = basic_chain_pool(summaries, posterior_function, proposal_scheme,
                                numpy_seeds)
        posteriors = [posterior_function(x) for x in xs]

    proposal_updates = [
        proposal.get_exportable_state() for proposal in proposal_scheme
    ]

    cum_iterations = 0
    start_time = time.time()
    for no_iterations in iteration_scheme:
        #letting each chain run for no_iterations:
        #print h.heap()
        iteration_object = _pack_everything(xs, posteriors, temperature_scheme,
                                            printing_schemes,
                                            overall_thinnings, no_iterations,
                                            cum_iterations, proposal_updates,
                                            multiplier)
        if no_chains == 1:  #debugging purposes
            new_state = [_basic_chain_unpacker(iteration_object.next())]
        else:
            new_state = pool.order_calculation(iteration_object)
        xs, posteriors, df_add, proposal_updates = _unpack_everything(
            new_state, summaries, total_permutation)
        df_result = _update_results(df_result, df_add)
        if result_file is not None:
            if cum_iterations == 0:
                start_data_frame(
                    df_result,
                    result_file,
                    save_only_coldest_chain=save_only_coldest_chain)
            elif df_result.shape[0] > 1000:
                add_to_data_frame(
                    df_result,
                    result_file,
                    save_only_coldest_chain=save_only_coldest_chain)
                df_result = df_result[0:0]
        #making the mc3 flips and updating:
        if not posterior_function_list:
            xs, posteriors, permut, proposal_updates = flipping(
                xs, posteriors, temperature_scheme, proposal_updates, rs, ps,
                [posterior_function
                 ])  # trees, posteriors, range(len(trees)),[None]*len(trees)#
        else:
            xs, posteriors, permut, proposal_updates = flipping(
                xs, posteriors, temperature_scheme, proposal_updates, rs, ps,
                posterior_function_list
            )  #trees, posteriors, range(len(trees)),[None]*len(trees)#
        if store_permuts:
            permuts.append(permut)
        temperature_scheme.update_temps(permut)
        #proposal_updates=_handle_flipping_of_proposals(proposal_updates, permut)
        total_permutation = _update_permutation(total_permutation, permut)
        cum_iterations += no_iterations
        if stop_criteria is not None:
            if stop_criteria(cum_iterations, result_file):
                break
        if make_outfile_stills:
            if make_outfile_stills[0] * 60 * 60 < time.time() - start_time:
                cp_command = [
                    'cp', result_file,
                    result_file + str(make_outfile_stills[0])
                ]
                subprocess.call(cp_command)
                make_outfile_stills.pop(0)

    pool.terminate()
    if result_file is None and store_permuts:
        return df_result, permuts
    elif result_file is None:
        return df_result
    elif store_permuts:
        return permuts
    # lowdown = open(folder_string + 'lowdown.txt', 'w')
    # lowdown.write('Round 2: ')

    run_specs['folder'] = folder_string

    # csv_stats = folder_string + 'performance_measures' + '.csv'
    # statistics = open(csv_stats, 'ab')
    # stat_writer = csv.writer(statistics)

    # stat_writer.writerow(('seed', 'blue rel', 'red rel'))

    seeds = []
    for u in xrange(31):
        seeds.append(random.randint(0, 10000))

    seed_index = int(raw_input("Input the experiment seed (1-30): "))

    for i in xrange(number_of_experiments):
        round_three_allvbest.run(seeds[seed_index], i, run_specs, test_set)
        # stat_writer.writerow(stats)
        seed_index += 1

    # statistics.close()


if __name__ == "__main__":
    freeze_support()
    main()
    # cProfile.run("main()")
Esempio n. 3
0
    button = tk.Button(master=root, text='Quit', command=_quit)
    button.pack(side=tk.BOTTOM)
    F1 = tk.Frame(root)
    F1.pack()
    L0 = tk.Label(F1, text='Input Data File')
    L0.pack(side='left')
    E0text = tk.StringVar()
    E0text.set('4520')
    E0 = tk.Entry(F1, textvariable=E0text)
    E0.pack(side='left')
    B0 = tk.Button(F1, text='Read Data', command=clickReadData)
    B0.pack(side='left')
    root.mainloop()


if __name__ == '__main__':
    mp.freeze_support()
    __spec__ = "ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>)"

    pool = mp.Pool(1)
    temp = pool.apply_async(maindo, (
        images,
        noise_img,
        testImg,
    ))
    #    print(temp.get())

    pool.close()  # 将进程池关闭,不再接受新的进程
    pool.join()  # 主进程阻