コード例 #1
0
ファイル: NeuroMLTuner.py プロジェクト: RokasSt/pyNeuroML
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)
    
    pynml.print_comment_v("=====================================================================================")
    pynml.print_comment_v("Starting run_optimisation with: ")
    for key,value in a.__dict__.items():
        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)

    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)

    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
    
    
    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)
        
        if a.show_plot_already:
            plt.show()
    
    return reportj
コード例 #2
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.9,
                     num_elites=1,
                     seed=12345,
                     simulator='jNeuroML',
                     nogui=False,
                     num_local_procesors_to_use=4):

    print("Running optimisation...")
    print("parameters: %s" % parameters)
    print("max_constraints: %s" % max_constraints)
    print("min_constraints: %s" % min_constraints)
    print("simulator: %s" % simulator)
    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,
        num_local_procesors_to_use=num_local_procesors_to_use)

    peak_threshold = -31 if level.startswith('A') or level.startswith(
        'B') else (-40)

    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 = c302Evaluators.EnhancedNetworkEvaluator(
        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 total; %fs per eval)\n\n" % (
        max_evaluations, population_size, secs, secs / 60.0,
        (secs / max_evaluations))
    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_results = my_controller.last_results

    best_candidate_analysis = c302Analysis.Data_Analyser(
        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(target_data)

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

    report += pp.pformat(best_candidate) + "\n\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['fittest vars'] = dict(sim_var)
    reportj['best_cand_details'] = best_candidate
    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['seed'] = seed
    reportj['simulator'] = simulator

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

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

    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 and not "phase_offset" in ref:
                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)")

        utils.plot_generation_evolution(
            sim_var.keys(),
            individuals_file_name='%s/ga_individuals.csv' % run_dir)

        c302_utils.plot_c302_results(best_candidate_results,
                                     config,
                                     level,
                                     directory=run_dir,
                                     save=True)
コード例 #3
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)
コード例 #4
0
ファイル: c302tuner.py プロジェクト: Jueast/CElegansNeuroML
        
        
        data_plot = plt.plot(t,v[cell_ref], label="Original data")
        best_candidate_plot = plt.plot(best_candidate_t,best_candidate_v[cell_ref], label="Best model - %i evaluations"%max_evaluations)

        plt.legend()

        plt.ylim(-80.0,80.0)
        plt.xlim(0.0,1000.0)
        plt.title("Models optimized from data")
        plt.xlabel("Time (ms)")
        plt.ylabel("Membrane potential(mV)")
        plt.savefig("data_vs_candidate.png",bbox_inches='tight',format='png')
        plt.show()

        utils.plot_generation_evolution(sim_var.keys())

    else:


        sim_var = OrderedDict([('leak_cond_density', 0.05), 
                                ('k_slow_cond_density', 0.5), 
                                ('k_fast_cond_density', 0.05), 
                                ('ca_boyle_cond_density', 0.5), 
                                ('specific_capacitance', 1.05), 
                                ('leak_erev', -50), 
                                ('k_slow_erev', -60), 
                                ('k_fast_erev', -60), 
                                ('ca_boyle_erev', 40)])
        
        
コード例 #5
0
    num_selected =     10
    num_offspring =    5
    mutation_rate =    0.5
    num_elites =       1

    #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=True)

    #run the optimizer
    best_candidate, fitness = my_optimizer.optimize(do_plot=False, seed=1234567)


    keys = list(sim_vars.keys())
    for i in range(len(best_candidate)):
        sim_vars[keys[i]] = best_candidate[i]        

    fit_times, fit_volts = swc.run_individual(sim_vars, showPlots, False)

    if showPlots:
        utils.plot_generation_evolution(sim_vars.keys(), sim_vars)
コード例 #6
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
コード例 #7
0
        best_candidate_plot = plt.plot(best_candidate_t,
                                       best_candidate_v[cell_ref],
                                       label="Best model - %i evaluations" %
                                       max_evaluations)

        plt.legend()

        plt.ylim(-80.0, 80.0)
        plt.xlim(0.0, 1000.0)
        plt.title("Models optimized from data")
        plt.xlabel("Time (ms)")
        plt.ylabel("Membrane potential(mV)")
        plt.savefig("data_vs_candidate.png", bbox_inches='tight', format='png')
        plt.show()

        utils.plot_generation_evolution(sim_var.keys())

    else:

        sim_var = OrderedDict({
            "muscle_k_slow_cond_density": .22,
            "muscle_ca_simple_cond_density": .15,
            "muscle_leak_cond_density": 0.002,
            "k_slow_erev": -93.,
            "ca_simple_erev": 60.,
            "unphysiological_offset_current": 5
        })

        example_run_t, example_run_v = my_controller.run_individual(sim_var,
                                                                    show=True)
コード例 #8
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,
                     nogui =               False):  

    ref = prefix+config

    my_controller = C302Controller(ref, level, config, sim_time, dt)

    peak_threshold = -31 if level is 'A' or level is 'B' 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=123456)

    secs = time.time()-start
    print("----------------------------------------------------\n\n"
          +"Ran %s evaluations (pop: %s) in %f seconds (%f mins)\n"%(max_evaluations, population_size, secs, secs/60.0))

    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())

    print("---------- Best candidate ------------------------------------------")
    pp.pprint(best_cand_analysis_full)
    pp.pprint(best_cand_analysis)
    print("Fitness: %f"%fitness)

    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())
コード例 #9
0
def run_optimisation(
    prefix,
    neuroml_file,
    target,
    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

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

    my_controller = NeuroMLController(
        ref, neuroml_file, target, sim_time, dt, simulator=simulator, generate_dir=run_dir
    )

    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=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
    report += "FITTEST: %s\n\n" % sim_var

    print(report)

    reportj["fitness"] = fitness
    reportj["fittest vars"] = sim_var
    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)