def run_and_save(opt_keys, tests): nparams = len(opt_keys) it = timeit.default_timer() grid_results = run_grid(npoints, tests, provided_keys=opt_keys) ft = timeit.default_timer() elapsed_grid = ft - it with open('dim_{0}_errs{1}_grid.p', 'wb') as f: # pickle.dump([grid_results, opt_keys, tests, elapsed_grid], f) it = timeit.default_timer() ga_out = run_ga(model_params, nparams, npoints, tests, provided_keys=opt_keys, use_cache=True, cache_name='simple') ft = timeit.default_timer() elapsed_ga = ft - it with open('dim_{0}_errs{1}_ga.p'.format(len(opt_keys), len(tests)), 'wb') as f: pickle.dump([ga_out, opt_keys, tests, elapsed_ga], f) file_name = 'dim_{0}_errs{1}_ga.ipynb'.format(len(opt_keys), len(tests)) shutil.chown(file_name, user='******', group=None) return ga_out
def pre_run(tests): dim = len(hof[0].dtc.attrs.keys()) flat_iter = [(i, ki, j, kj) for i, ki in enumerate(hof[0].dtc.attrs.keys()) for j, kj in enumerate(hof[0].dtc.attrs.keys())] cnt = 0 for i, ki, j, kj in flat_iter: free_param = set([ ki, kj ]) # construct a small-set out of the indexed keys 2. If both keys are # are the same, this set will only contain one index bs = set( hof[0].dtc.attrs.keys() ) # construct a full set out of all of the keys available, including ones not indexed here. diff = bs.difference( free_param) # diff is simply the key that is not indexed. # BD is the dictionary of parameters to be held constant # if the plot is 1D then two parameters should be held constant. hc = {} for d in diff: hc[d] = hof[0].dtc.attrs[d] if i == j: assert len(free_param) == len(hc) - 1 assert len(hc) == len(free_param) + 1 from neuronunit.models.NeuroML2 import model_parameters as modelp mp = copy.copy(modelp.model_params) gr = run_grid(3, tests, provided_keys=free_param, hold_constant=hc, mp_in=mp) # make a psuedo test, that still depends on input Parametersself. # each test evaluates a normal PDP. line = [np.sum(list(g.dtc.scores.values())) for g in gr] nr = {str(list(free_param)[0]): None} newrange, range_adj = check_line(line, gr, nr) while range_adj == True: mp = mp_process(newrange) gr = run_grid(3, tests, provided_keys=newrange, hold_constant=hc, mp_in=mp) # make a psuedo test, that still depends on input Parametersself. # each test evaluates a normal PDP. nr = {} line = [np.sum(list(g.dtc.scores.values())) for g in gr] newrange, range_adj = check_line(line, gr, newrange) mp = mp_process(newrange) with open('parameter_bf_ranges.p', 'wb') as f: pickle.dump(mp, f) import pdb pdb.set_trace() package = run_ga( mp, nparams * 2, 12, tests_, provided_keys=opt_keys) #, use_cache = True, cache_name='simple') return package
def run_and_save(opt_keys, tests): nparams = len(opt_keys) it = timeit.default_timer() grid_results = run_grid(nparams, npoints, tests, provided_keys=opt_keys) ft = timeit.default_timer() elapsed_grid = ft - it with open('dim_{0}_errs{1}_grid.p', 'wb') as f: # pickle.dump([grid_results, opt_keys, tests, elapsed_grid], f) it = timeit.default_timer() ga_out = run_ga(model_params, nparams, npoints, tests, provided_keys=opt_keys, use_cache=True, cache_name='simple') ft = timeit.default_timer() elapsed_ga = ft - it with open('dim_{0}_errs{1}_ga.p'.format(len(opt_keys), len(tests)), 'wb') as f: pickle.dump([ga_out, opt_keys, tests, elapsed_ga], f) file_name = 'dim_{0}_errs{1}_ga.ipynb'.format(len(opt_keys), len(tests)) os.system("ipython nbconvert --to html --execute agreement.ipynb") os.system("cp agreement_df.ipynb " + file_name) os.system("ipython nbconvert --to html --execute " + file_name) #shutil.chown('dim_{0}_errs{1}_ga.p', user='******', group='user') with open('dim_{0}_errs{1}_ga.p'.format(len(opt_keys), len(tests)), 'wb') as f: pickle.dump([ga_out, opt_keys, tests, elapsed_ga], f) file_name = 'dim_{0}_errs{1}_ga.ipynb'.format(len(opt_keys), len(tests)) os.system("cp agreement_df.ipynb " + file_name) os.system("ipython nbconvert --to html --execute " + file_name) return ga_out
reduced_dict[k].append(v[i]) explore_param = {k: (np.min(v), np.max(v)) for k, v in reduced_dict.items()} cnt = 0 for (test, observation) in flat_iter: dic_key = str(list(pipe[cnt].values())[0]) init_time = timeit.default_timer() free_params = ['a', 'b', 'vr', 'k', 'vt', 'd'] hc = ['C', 'c'] #DO = SciUnitOptimization(error_criterion = test, selection = sel, provided_dict = model_params, elite_size = 3) start_time = timeit.default_timer() #ga_out, DO = om.run_ga(explore_param,5,TC_tests,free_params=free_params,hc = hc, NSGA = True, MU = 8) ga_out, DO = om.run_ga(explore_param, 17, test, free_params=free_params, hc=hc, NSGA=True) elapsed = timeit.default_timer() - start_time ga_out['time_length'] = elapsed #package = DO.run(offspring_size = MU, max_ngen = 6, cp_frequency=1,cp_filename=str(dic_key)+'.p') #pop, hof_py, pf, log, history, td_py, gen_vs_hof = package with open('dump_all_cells' + str(pipe[cnt]) + str(cnt), 'wb') as f: pickle.dump(ga_out, f) cnt += 1 print('entire duration', elapsed) ''' model_to_write = pipe_results[dic_key]['gen_vs_hof'][-1].dtc.attrs optimization_management.write_opt_to_nml(file_name,model_to_write)
MU = 6 NGEN = 150 try: with open('multi_objective_glif.p','rb') as f: test_opt = pickle.load(f) except: for key, use_test in test_frame.items(): seed = seeds[key] print(seed) ga_out, _ = om.run_ga(explore_ranges,NGEN,use_test,free_params=explore_ranges.keys(), NSGA = True, MU = MU, model_type = str('GLIF'),seed_pop=seed) test_opt = {str('multi_objective_glif')+str(seed):ga_out} with open('multi_objective_glif.p','wb') as f: pickle.dump(test_opt,f) import pdb; pdb.set_trace() model = ReducedModel(LEMS_MODEL_PATH,name = str('vanilla'),backend = (str('HH'))) explore_ranges = {'E_Na' : (40,70), 'g_Na':(100.0,140.0), 'C_m':(0.5,1.5)} attrs_hh = { 'g_K' : 36.0, 'g_Na' : 120.0, 'g_L' : 0.3, \ 'C_m' : 1.0, 'E_L' : -54.387, 'E_K' : -77.0, 'E_Na' : 50.0, 'vr':-65.0 } try:
fc, mp = explore_ranges.pre_run(tests_, opt_keys) with open('ranges.p', 'wb') as f: pickle.dump([fc, mp], f) # update and simplify model parameter dictionary. # this is probably unnecessary for k, v in mp.items(): if type(v) is type(tuple((0, 0))): mp[k] = np.linspace(v[0], v[1], 7) # get a genetic algorithm that operates on this new parameter range. try: assert 1 == 2 with open('package.p', 'rb') as f: package = pickle.load(f) except: package = run_ga(mp, 6, tests_, provided_keys=opt_keys) with open('package.p', 'wb') as f: pickle.dump(package, f) pop = package[0] history = package[4] gen_vs_pop = package[6] hof = package[1] df, matrix = grids(hof, tests_, mp) with open('surfaces.p', 'wb') as f: pickle.dump([df, matrix], f)
assert os.path.isfile(electro_path) == True with open(electro_path, 'rb') as f: electro_tests = pickle.load(f) electro_tests = get_neab.replace_zero_std(electro_tests) electro_tests = get_neab.substitute_parallel_for_serial(electro_tests) test, observation = electro_tests[0] npoints = 10 opt_keys = [str('a'), str('C'), str('b')] nparams = len(opt_keys) ga_out = run_ga(model_params, nparams, npoints, tests, provided_keys=opt_keys, use_cache=True, cache_name='simple') ''' fname = 'dim_{0}_errs{1}_ga.p'.format(len(opt_keys),len(tests)) with open(fname,'wb') as f: pickle.dump([ga_out,opt_keys,tests,elapsed_ga],f) with open(fname,'rb') as f: package = pickle.load(f) ''' pop = ga_out[0]
for k, v in cells['TC'].items(): if k not in free_params: hc[k] = v #print(hc) TC_tests = pickle.load(open('thalamo_cortical_tests.p', 'rb')) #run_ga(model_params, max_ngen, test, free_params = None, hc = None) #ga_out, DO = om.run_ga(explore_param,10,TC_tests,free_params=free_params,hc = hc, NSGA = False, MU = 10) try: #assert 1==2 ga_out_nsga = pickle.load(open('contents.p', 'rb')) except: ga_out_nsga, _ = om.run_ga(explore_param, 25, TC_tests, free_params=free_params, hc=hc, NSGA=True) pickle.dump(ga_out_nsga, open('contents.p', 'wb')) hof = ga_out_nsga['hof'] history = ga_out_nsga['history'] attr_keys = list(hof[0].dtc.attrs.keys()) us = {} # GA utilized_space for key in attr_keys: temp = [v.dtc.attrs[key] for k, v in history.genealogy_history.items()] us[key] = (np.min(temp), np.max(temp)) if 1 == 2: with open('surfaces.p', 'rb') as f:
tests_ += tests[4:7] #import pdb #pdb.set_trace() try: #assert 1 == 2 with open('ga_run.p','rb') as f: package = pickle.load(f) pop = package[0] print(pop[0].dtc.attrs.items()) history = package[4] gen_vs_pop = package[6] hof = package[1] except: print(mp) nparams = len(opt_keys) package = run_ga(mp,nparams*2,12,tests_,provided_keys = opt_keys)#, use_cache = True, cache_name='simple') pop = package[0] history = package[4] gen_vs_pop = package[6] hof = package[1] with open('ga_run.p','wb') as f: pickle.dump(package,f) grid_results = {} #import seaborn as sns from itertools import product import matplotlib.pyplot as plt
#with open('pre_grid_reports.p','rb') as f: # [grid_results,nparams] = pickle.load(f) >>>>>>> 13fe3fde8c76de2d719acdf82b3a71b76cd9f9b4 #grid_results = None <<<<<<< HEAD with open('pre_grid_reports.p','rb') as f: [grid_results,nparams] = pickle.load(f) opt_keys = list(grid_results[0].dtc.attrs.keys()) nparams = 2 tests = [electro_tests[0][0][0],electro_tests[0][0][3]] ga_out = run_ga(model_params,nparams,npoints,tests,provided_keys = opt_keys) pop = ga_out[0] new_report = make_report(grid_results, pop, nparams) reports.update(new_report) #ga_out = run_ga(model_params,nparams,npoints,tests) #grid_results = run_grid(nparams,npoints,tests) with open('pre_ga_reports.p','wb') as f: pickle.dump([ga_out,nparams],f) with open('reports.p','wb') as f: pickle.dump(reports,f) ======= nparams = 2 #tests = electro_tests[0][0][0:2]
if dtc.rheobase is not None: if dtc.rheobase!=-1.0: dtc = nunit_evaluation(dtc) df[k][key] = int(dtc.get_ss()) # A sparse grid sampling over the parameter space, using the published and well corrobarated parameter points, from Izhikitch publications, and the Open Source brain, shows that without optimization, using off the shelf parameter sets to fit real-life biological cell data, does not work so well. MU = 6 NGEN = 150 for key, use_test in test_frame.items(): ga_out, _ = om.run_ga(explore_param,NGEN,use_test,free_params=free_params, NSGA = True, MU = MU) test_opt = {str('multi_objective')+str(ga_out):ga_out} with open('multi_objective.p','wb') as f: pickle.dump(test_opt,f) ''' MU = 6 NGEN = 200 import pickle import numpy as np try: with open('multi_objective.p','rb') as f: test_opt = pickle.load(f) except: