Ejemplo n.º 1
0
def run_optimisation(prefix,
                     config,
                     level,
                     parameters,
                     max_constraints,
                     min_constraints,
                     weights,
                     target_data,
                     sim_time=500,
                     dt=0.05,
                     analysis_start_time=0,
                     population_size=20,
                     max_evaluations=20,
                     num_selected=10,
                     num_offspring=20,
                     mutation_rate=0.5,
                     num_elites=1,
                     seed=12345,
                     simulator='jNeuroML',
                     nogui=False):

    ref = prefix + config

    run_dir = "NT_%s_%s" % (ref, time.ctime().replace(' ', '_').replace(
        ':', '.'))
    os.mkdir(run_dir)

    my_controller = C302Controller(ref,
                                   level,
                                   config,
                                   sim_time,
                                   dt,
                                   simulator=simulator,
                                   generate_dir=run_dir)

    peak_threshold = -31 if level is 'A' or level is 'B' else (
        -20 if level is 'C1' else 0)

    analysis_var = {
        'peak_delta': 0,
        'baseline': 0,
        'dvdt_threshold': 0,
        'peak_threshold': peak_threshold
    }

    data = ref + '.dat'

    sim_var = OrderedDict()
    for i in range(len(parameters)):
        sim_var[
            parameters[i]] = max_constraints[i] / 2 + min_constraints[i] / 2

    #make an evaluator, using automatic target evaluation:
    my_evaluator = evaluators.NetworkEvaluator(
        controller=my_controller,
        analysis_start_time=analysis_start_time,
        analysis_end_time=sim_time,
        parameters=parameters,
        analysis_var=analysis_var,
        weights=weights,
        targets=target_data)

    #make an optimizer
    my_optimizer = optimizers.CustomOptimizerA(max_constraints,
                                               min_constraints,
                                               my_evaluator,
                                               population_size=population_size,
                                               max_evaluations=max_evaluations,
                                               num_selected=num_selected,
                                               num_offspring=num_offspring,
                                               num_elites=num_elites,
                                               mutation_rate=mutation_rate,
                                               seeds=None,
                                               verbose=False)

    start = time.time()
    #run the optimizer
    best_candidate, fitness = my_optimizer.optimize(do_plot=False,
                                                    seed=seed,
                                                    summary_dir=run_dir)

    secs = time.time() - start

    reportj = {}
    info = "Ran %s evaluations (pop: %s) in %f seconds (%f mins)\n\n" % (
        max_evaluations, population_size, secs, secs / 60.0)
    report = "----------------------------------------------------\n\n" + info

    reportj['comment'] = info
    reportj['time'] = secs

    for key, value in zip(parameters, best_candidate):
        sim_var[key] = value

    best_candidate_t, best_candidate_v = my_controller.run_individual(
        sim_var, show=False)

    best_candidate_analysis = analysis.NetworkAnalysis(
        best_candidate_v,
        best_candidate_t,
        analysis_var,
        start_analysis=analysis_start_time,
        end_analysis=sim_time)

    best_cand_analysis_full = best_candidate_analysis.analyse()
    best_cand_analysis = best_candidate_analysis.analyse(weights.keys())

    report += "---------- Best candidate ------------------------------------------\n"

    report += pp.pformat(best_cand_analysis_full) + "\n"
    report += pp.pformat(best_cand_analysis) + "\n\n"
    report += "FITNESS: %f\n\n" % fitness

    print(report)

    reportj['fitness'] = fitness
    reportj['best_cand_analysis_full'] = best_cand_analysis_full
    reportj['best_cand_analysis'] = best_cand_analysis
    reportj['parameters'] = parameters
    reportj['analysis_var'] = analysis_var
    reportj['target_data'] = target_data
    reportj['weights'] = weights

    reportj['analysis_start_time'] = analysis_start_time

    reportj['population_size'] = population_size
    reportj['max_evaluations'] = max_evaluations
    reportj['num_selected'] = num_selected
    reportj['num_offspring'] = num_offspring
    reportj['mutation_rate'] = mutation_rate
    reportj['num_elites'] = num_elites

    reportj['sim_time'] = sim_time
    reportj['dt'] = dt

    report_file = open("%s/report.json" % run_dir, 'w')
    report_file.write(pp.pformat(reportj))
    report_file.close()

    plot_file = open("%s/plotgens.py" % run_dir, 'w')
    plot_file.write(
        'from neurotune.utils import plot_generation_evolution\nimport os\n')
    plot_file.write('\n')
    plot_file.write('parameters = %s\n' % parameters)
    plot_file.write('\n')
    plot_file.write(
        "curr_dir = os.path.dirname(__file__) if len(os.path.dirname(__file__))>0 else '.'\n"
    )
    plot_file.write(
        "plot_generation_evolution(parameters, individuals_file_name = '%s/ga_individuals.csv'%curr_dir)\n"
    )
    plot_file.close()

    if not nogui:
        added = []
        for wref in weights.keys():
            ref = wref.split(':')[0]
            if not ref in added:
                added.append(ref)
                best_candidate_plot = plt.plot(best_candidate_t,
                                               best_candidate_v[ref],
                                               label="%s - %i evaluations" %
                                               (ref, max_evaluations))

        plt.legend()

        plt.ylim(-80.0, 80.0)
        plt.xlim(0.0, sim_time)
        plt.title("Models")
        plt.xlabel("Time (ms)")
        plt.ylabel("Membrane potential(mV)")

        plt.show()

        utils.plot_generation_evolution(
            sim_var.keys(),
            individuals_file_name='%s/ga_individuals.csv' % run_dir)
        #target_data, v, t = get_target_muscle_cell_data(analysis_var, analysis_start_time, sim_time, cell_ref, weights.keys())
        target_data = {
            cell_ref + ':average_minimum': -70,
            cell_ref + ':mean_spike_frequency': 4,
            cell_ref + ':average_maximum': 40,
            cell_ref + ':max_peak_no': 8
        }

        print("Analysis of experimental data:")
        pp.pprint(target_data)

        #make an evaluator, using automatic target evaluation:
        my_evaluator = evaluators.NetworkEvaluator(
            controller=my_controller,
            analysis_start_time=analysis_start_time,
            analysis_end_time=sim_time,
            parameters=parameters,
            analysis_var=analysis_var,
            weights=weights,
            targets=target_data)

        population_size = 20
        max_evaluations = 50
        num_selected = 15
        num_offspring = 10
        mutation_rate = 0.5
        num_elites = 1

        #make an optimizer
        my_optimizer = optimizers.CustomOptimizerA(
            max_constraints,
            min_constraints,
Ejemplo n.º 3
0
def _run_optimisation(a):

    if isinstance(a.parameters, str):
        a.parameters = parse_list_arg(a.parameters)
    if isinstance(a.min_constraints, str):
        a.min_constraints = parse_list_arg(a.min_constraints)
    if isinstance(a.max_constraints, str):
        a.max_constraints = parse_list_arg(a.max_constraints)
    if isinstance(a.target_data, str):
        a.target_data = parse_dict_arg(a.target_data)
    if isinstance(a.weights, str): a.weights = parse_dict_arg(a.weights)
    if isinstance(a.known_target_values, str):
        a.known_target_values = parse_dict_arg(a.known_target_values)
    if isinstance(a.extra_report_info, str):
        a.extra_report_info = parse_dict_arg(a.extra_report_info)

    pynml.print_comment_v(
        "====================================================================================="
    )
    pynml.print_comment_v("Starting run_optimisation with: ")
    keys = sorted(a.__dict__.keys())

    for key in keys:
        value = a.__dict__[key]
        pynml.print_comment_v("  %s = %s%s" % (key, ' ' *
                                               (30 - len(key)), value))
    pynml.print_comment_v(
        "====================================================================================="
    )

    if a.dry_run:
        pynml.print_comment_v("Dry run; not running optimization...")
        return

    ref = a.prefix

    run_dir = "NT_%s_%s" % (ref, time.ctime().replace(' ', '_').replace(
        ':', '.'))
    os.mkdir(run_dir)

    my_controller = NeuroMLController(
        ref,
        a.neuroml_file,
        a.target,
        a.sim_time,
        a.dt,
        simulator=a.simulator,
        generate_dir=run_dir,
        num_parallel_evaluations=a.num_parallel_evaluations,
        cleanup=a.cleanup)

    peak_threshold = 0

    analysis_var = {
        'peak_delta': 0,
        'baseline': 0,
        'dvdt_threshold': 0,
        'peak_threshold': peak_threshold
    }

    sim_var = OrderedDict()

    #make an evaluator, using automatic target evaluation:
    my_evaluator = evaluators.NetworkEvaluator(
        controller=my_controller,
        analysis_start_time=a.analysis_start_time,
        analysis_end_time=a.sim_time,
        parameters=a.parameters,
        analysis_var=analysis_var,
        weights=a.weights,
        targets=a.target_data)

    #make an optimizer
    my_optimizer = optimizers.CustomOptimizerA(
        a.max_constraints,
        a.min_constraints,
        my_evaluator,
        population_size=a.population_size,
        max_evaluations=a.max_evaluations,
        num_selected=a.num_selected,
        num_offspring=a.num_offspring,
        num_elites=a.num_elites,
        mutation_rate=a.mutation_rate,
        seeds=None,
        verbose=a.verbose)

    start = time.time()
    #run the optimizer
    best_candidate, fitness = my_optimizer.optimize(do_plot=False,
                                                    seed=a.seed,
                                                    summary_dir=run_dir)

    secs = time.time() - start

    reportj = {}
    info = "Ran %s evaluations (pop: %s) in %f seconds (%f mins total; %fs per eval)\n\n" % (
        a.max_evaluations, a.population_size, secs, secs / 60.0,
        (secs / a.max_evaluations))
    report = "----------------------------------------------------\n\n" + info

    reportj['comment'] = info
    reportj['time'] = secs

    for key, value in zip(a.parameters, best_candidate):
        sim_var[key] = value

    best_candidate_t, best_candidate_v = my_controller.run_individual(
        sim_var, show=False, cleanup=False)

    best_candidate_analysis = analysis.NetworkAnalysis(
        best_candidate_v,
        best_candidate_t,
        analysis_var,
        start_analysis=a.analysis_start_time,
        end_analysis=a.sim_time)

    best_cand_analysis_full = best_candidate_analysis.analyse()
    best_cand_analysis = best_candidate_analysis.analyse(a.weights.keys())

    report += "---------- Best candidate ------------------------------------------\n"

    report += pp.pformat(best_cand_analysis_full) + "\n\n"

    report += "TARGETS: \n"
    report += pp.pformat(a.target_data) + "\n\n"
    report += "TUNED VALUES:\n"
    report += pp.pformat(best_cand_analysis) + "\n\n"

    report += "FITNESS: %f\n\n" % fitness
    report += "FITTEST: %s\n\n" % pp.pformat(dict(sim_var))

    pynml.print_comment_v(report)

    reportj['fitness'] = fitness
    reportj['fittest vars'] = dict(sim_var)
    reportj['best_cand_analysis_full'] = best_cand_analysis_full
    reportj['best_cand_analysis'] = best_cand_analysis
    reportj['parameters'] = a.parameters
    reportj['analysis_var'] = analysis_var
    reportj['target_data'] = a.target_data
    reportj['weights'] = a.weights

    reportj['analysis_start_time'] = a.analysis_start_time

    reportj['population_size'] = a.population_size
    reportj['max_evaluations'] = a.max_evaluations
    reportj['num_selected'] = a.num_selected
    reportj['num_offspring'] = a.num_offspring
    reportj['mutation_rate'] = a.mutation_rate
    reportj['num_elites'] = a.num_elites
    reportj['seed'] = a.seed
    reportj['simulator'] = a.simulator

    reportj['sim_time'] = a.sim_time
    reportj['dt'] = a.dt

    reportj['run_directory'] = run_dir
    reportj['reference'] = ref

    if a.extra_report_info:
        for key in a.extra_report_info:
            reportj[key] = a.extra_report_info[key]

    report_file = open("%s/report.json" % run_dir, 'w')
    report_file.write(pp.pformat(reportj))
    report_file.close()

    plot_file = open("%s/plotgens.py" % run_dir, 'w')
    plot_file.write(
        'from neurotune.utils import plot_generation_evolution\nimport os\n')
    plot_file.write('\n')
    plot_file.write('parameters = %s\n' % a.parameters)
    plot_file.write('\n')
    plot_file.write(
        "curr_dir = os.path.dirname(__file__) if len(os.path.dirname(__file__))>0 else '.'\n"
    )
    plot_file.write(
        "plot_generation_evolution(parameters, individuals_file_name = '%s/ga_individuals.csv'%curr_dir)\n"
    )
    plot_file.close()

    if not a.nogui:
        added = []
        #print("Plotting saved data from %s which are relevant for targets: %s"%(best_candidate_v.keys(), a.target_data.keys()))

        fig = plt.figure()
        fig.canvas.set_window_title(
            "Simulation of fittest individual from run: %s" % ref)

        for tref in best_candidate_v.keys(
        ):  ##################a.target_data.keys():
            ref = tref.split(':')[0]
            if not ref in added:
                added.append(ref)
                #pynml.print_comment(" - Adding plot of: %s"%ref)
                plt.plot(best_candidate_t,
                         best_candidate_v[ref],
                         label="%s - %i evaluations" %
                         (ref, a.max_evaluations))

        plt.legend()

        #plt.ylim(-80.0,80.0)
        plt.xlim(0.0, a.sim_time)
        plt.title("Models %s" % a.prefix)
        plt.xlabel("Time (ms)")
        plt.ylabel("Membrane potential(mV)")

        utils.plot_generation_evolution(
            sim_var.keys(),
            individuals_file_name='%s/ga_individuals.csv' % run_dir,
            target_values=a.known_target_values,
            show_plot_already=a.show_plot_already,
            title_prefix=ref)

        if a.show_plot_already:
            plt.show()

    return reportj
Ejemplo n.º 4
0
        'wave_0:mean_spike_frequency': 10,
        'wave_1:average_maximum': 1,
        'wave_1:average_minimum': 1,
        'wave_1:mean_spike_frequency': 10
    }

    surrogate_targets = surrogate_analysis.analyse(weights.keys())
    pp = pprint.PrettyPrinter(indent=4)
    print("Surrogate analysis")
    pp.pprint(surrogate_targets)

    #make an evaluator
    my_evaluator = evaluators.NetworkEvaluator(controller=swc,
                                               analysis_start_time=0,
                                               analysis_end_time=1000,
                                               parameters=sim_vars.keys(),
                                               analysis_var=analysis_var,
                                               weights=weights,
                                               targets=surrogate_targets)

    population_size = 20
    max_evaluations = 600
    num_selected = 10
    num_offspring = 10
    mutation_rate = 0.9
    num_elites = 1

    #make an optimizer
    my_optimizer = optimizers.CustomOptimizerA(max_constraints,
                                               min_constraints,
                                               my_evaluator,