def test_opt_1(self): cellmodel = "ADEXP" if cellmodel == "IZHI": model = model_classes.IzhiModel() if cellmodel == "MAT": model = model_classes.MATModel() if cellmodel == "ADEXP": model = model_classes.ADEXPModel() dtc = DataTC() dtc.backend = cellmodel dtc._backend = model._backend dtc.attrs = model.attrs dtc.params = { k: np.mean(v) for k, v in MODEL_PARAMS[cellmodel].items() } other_params = BPO_PARAMS[cellmodel] dtc = dtc_to_rheo(dtc) assert dtc.rheobase is not None self.assertIsNotNone(dtc.rheobase) vm, plt, dtc = inject_and_plot_model(dtc, plotly=False) self.assertIsNotNone(vm) model = dtc.dtc_to_model() self.assertIsNotNone(model)
def model_to_dtc(self): dtc = DataTC() dtc.attrs = self.attrs try: dtc.backend = self.get_backend() except: dtc.backend = self.backend if hasattr(self, 'rheobase'): dtc.rheobase = self.rheobase return dtc
def transform(ind): from neuronunit.optimization.data_transport_container import DataTC dtc = DataTC() print(dtc) param_dict = {} for i, j in enumerate(ind): param_dict[td[i]] = str(j) dtc.attrs = param_dict dtc.evaluated = False return dtc
def model_to_dtc(self, attrs=None): """ Args: self Returns: dtc DTC is a simulator indipendent data transport container object. """ from neuronunit.optimization.data_transport_container import DataTC dtc = DataTC(backend=self.backend) print('gets here model to dtc') dtc.attrs = self.attrs return dtc '''
def transform(ind): import dask.bag as db from neuronunit.optimization.data_transport_container import DataTC dtc = DataTC() import neuronunit LEMS_MODEL_PATH = str(neuronunit.__path__[0])+str('/models/NeuroML2/LEMS_2007One.xml') if backend is not None: dtc.backend = backend else: dtc.backend = 'NEURON' dtc.attrs = {} for i,j in enumerate(ind): dtc.attrs[str(td[i])] = j dtc.evaluated = False return dtc
def transform(xargs): (ind, td, backend) = xargs # The merits of defining a function in a function # is that it yields a semi global scoped variables. # conisider refactoring outer function as a decorator. dtc = DataTC() LEMS_MODEL_PATH = str( neuronunit.__path__[0]) + str('/models/NeuroML2/LEMS_2007One.xml') dtc.backend = 'RAW' dtc.attrs = {} for i, j in enumerate(ind): dtc.attrs[str(td[i])] = j dtc.evaluated = False return dtc
def transform(ind): # The merits of defining a function in a function # is that it yields a semi global scoped variables. # dtc = DataTC() LEMS_MODEL_PATH = str(neuronunit.__path__[0])+str('/models/NeuroML2/LEMS_2007One.xml') if backend is not None: dtc.backend = backend else: dtc.backend = 'NEURON' dtc.attrs = {} if isinstance(ind, Iterable): for i,j in enumerate(ind): dtc.attrs[str(td[i])] = j else: dtc.attrs[str(td[0])] = ind dtc.evaluated = False return dtc
def generate_prediction(self,model = None): if self.prediction is None: dtc = DataTC() dtc.backed = model.backend dtc.attrs = model.attrs dtc.rheobase = model.rheobase dtc.tests = [self] dtc = three_step_protocol(dtc) dtc,ephys0 = allen_wave_predictions(dtc,thirty=True) dtc,ephys1 = allen_wave_predictions(dtc,thirty=False) if self.name in ephys0.keys(): feature = ephys0[self.name] self.prediction = {} self.prediction['value'] = feature #self.prediction['std'] = feature if self.name in ephys1.keys(): feature = ephys1[self.name] self.prediction = {} self.prediction['value'] = feature #self.prediction['std'] = feature return self.prediction
def impute_check(pop, dtcpop, td, tests): delta = len(pop) - len(dtcpop) # if a rheobase value cannot be found for a given set of dtc model more_attributes # delete that model, or rather, filter it out above, and impute # a new model from the mean of the pre-existing model attributes. impute_pop = [] if delta != 0: impute = [] impute_gene = [] # impute individual, not impute index for i in range(0, len(pop[0])): impute_gene.append(np.mean([p[i] for p in pop])) ind = WSListIndividual(impute_gene) # # newest functioning code. # other broken transform should be modelled on this. ## what function transform should consist of. dtc = DataTC() LEMS_MODEL_PATH = str( neuronunit.__path__[0]) + str('/models/NeuroML2/LEMS_2007One.xml') dtc.backend = 'RAW' dtc.attrs = {} for i, j in enumerate(ind): dtc.attrs[str(td[i])] = j ## end function transform xarg = (dtc, tests[0], 'RAW') dtc = dtc_to_rheo(xarg) ind.rheobase = dtc.rheobase print(dtc.attrs, dtc.rheobase, 'still failing') if type(ind.rheobase) is type(None): pass pop.append(ind) dtcpop.append(dtc) return dtcpop, pop
def model_test_eval(self, tests): """ Take a model and some tests Evaluate a test suite over them. """ from sciunit import TestSuite if type(tests) is TestSuite: not_suite = TSD({t.name: t for t in tests.tests}) OM = OptMan(tests, backend=self._backend) dtc = DataTC() dtc.attrs = self.attrs assert set(self._backend.attrs.keys()) in set(self.attrs.keys()) dtc.backend = self._backend dtc.tests = copy.copy(not_suite) dtc = dtc_to_rheo(dtc) if dtc.rheobase is not None: dtc.tests = dtc.format_test() dtc = list(map(OM.elephant_evaluation, [dtc])) model = dtc.dtc_to_model() model.SM = dtc.SM model.obs_preds = dtc.obs_preds return dtc[0], model
def generate_prediction(self, model): def check_fix_range(dtc): ''' Inputs: lookup, A dictionary of previous current injection values used to search rheobase Outputs: A boolean to indicate if the correct rheobase current was found and a dictionary containing the range of values used. If rheobase was actually found then rather returning a boolean and a dictionary, instead logical True, and the rheobase current is returned. given a dictionary of rheobase search values, use that dictionary as input for a subsequent search. ''' steps = [] dtc.rheobase = {} dtc.rheobase['value'] = None sub, supra = get_sub_supra(dtc.lookup) if (len(sub) + len(supra)) == 0: # This assertion would only be occur if there was a bug assert sub.max() <= supra.min() elif len(sub) and len(supra): # Termination criterion steps = np.linspace(sub.max(), supra.min(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA elif len(sub): steps = np.linspace(sub.max(), 2 * sub.max(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA elif len(supra): steps = np.linspace(supra.min() - 100, supra.min(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA dtc.current_steps = steps return dtc def get_sub_supra(lookup): sub, supra = [], [] for current, n_spikes in lookup.items(): if n_spikes < self.observation['value']: sub.append(current) elif n_spikes > self.observation['value']: supra.append(current) delta = n_spikes - self.observation['value'] #print(delta,'difference \n\n\n\nn') sub = np.array(sorted(list(set(sub)))) supra = np.array(sorted(list(set(supra)))) return sub, supra def check_current(dtc): ''' Inputs are an amplitude to test and a virtual model output is an virtual model with an updated dictionary. ''' dtc.boolean = False model = dtc.dtc_to_model() model._backend.attrs = dtc.attrs params = { 'injected_square_current': { 'amplitude': None, 'delay': DELAY, 'duration': DURATION } } ampl = float(dtc.ampl) if ampl not in dtc.lookup or len(dtc.lookup) == 0: uc = { 'amplitude': ampl * pq.pA, 'duration': DURATION, 'delay': DELAY } dtc.run_number += 1 #model.set_attrs(**dtc.attrs) #if model.backend is str("JIT_MATBackend"): # vm = model._backend.inject_square_current(**uc) # n_spikes = len(model._backend.spikes) #print(n_spikes,model._backend.attrs,'gets here?') if str("JIT_") in model.backend: _ = model._backend.inject_square_current(**uc) n_spikes = model._backend.get_spike_count() #print(n_spikes,ampl)#,model._backend.attrs['cm']) if float(ampl) < -10.0: dtc.rheobase = {} dtc.rheobase['value'] = None dtc.boolean = True return dtc target_spk_count = self.observation['value'] if n_spikes == target_spk_count: dtc.lookup[float(ampl)] = n_spikes dtc.rheobase = {} dtc.rheobase['value'] = float(ampl) * pq.pA dtc.target_spk_count = None dtc.target_spk_count = dtc.rheobase['value'] dtc.boolean = True if self.verbose > 2: print('gets here', n_spikes, target_spk_count, n_spikes == target_spk_count) return dtc dtc.lookup[float(ampl)] = n_spikes #print(dtc.lookup) return dtc def init_dtc(dtc): ''' Exploit memory of last model in genes. ''' if dtc.initiated == True: dtc = check_current(dtc) if dtc.boolean: return dtc else: if type(dtc.current_steps) is type(float): dtc.current_steps = [ 0.75 * dtc.current_steps, 1.25 * dtc.current_steps ] elif type(dtc.current_steps) is type(list): dtc.current_steps = [ s * 1.25 for s in dtc.current_steps ] dtc.initiated = True # logically unnecessary but included for readibility if dtc.initiated == False: dtc.boolean = False if str('PYNN') in dtc.backend: steps = np.linspace(100, 1000, 7.0) else: steps = np.linspace(-10.0, 65, 7.0) steps_current = [i * pq.pA for i in steps] dtc.current_steps = steps_current dtc.initiated = True return dtc def find_target_current(self, dtc): # This line should not be necessary: # a class, VeryReducedModel has been made to circumvent this. #if hasattr(dtc,'model_path'): # assert os.path.isfile(dtc.model_path), "%s is not a file" % dtc.model_path # If this it not the first pass/ first generation # then assume the rheobase value found before mutation still holds until proven otherwise. # dtc = check_current(model.rheobase,dtc) # If its not true enter a search, with ranges informed by memory cnt = 0 sub = np.array([0, 0]) supra = np.array([0, 0]) ''' if dtc.backend is 'GLIF': else: big = 50 ''' big = 100 while dtc.boolean == False and cnt < big: # negeative spiker if len(sub): if sub.max() < -1.0: pass dtc_clones = [dtc for i in range(0, len(dtc.current_steps))] for i, s in enumerate(dtc.current_steps): dtc_clones[i] = copy.copy(dtc_clones[i]) dtc_clones[i].ampl = copy.copy(dtc.current_steps[i]) dtc_clones[i].backend = copy.copy(dtc.backend) dtc_clones = [d for d in dtc_clones if not np.isnan(d.ampl)] set_clones = set([float(d.ampl) for d in dtc_clones]) dtc_clone = [] for dtc, sc in zip(dtc_clones, set_clones): dtc = copy.copy(dtc) dtc.ampl = sc * pq.pA dtc = check_current(dtc) dtc_clone.append(dtc) for dtc in dtc_clone: if dtc.boolean == True: return dtc for d in dtc_clone: dtc.lookup.update(d.lookup) dtc = check_fix_range(dtc) sub, supra = get_sub_supra(dtc.lookup) if len(supra) and len(sub): delta = float(supra.min()) - float(sub.max()) if str("GLIF") in dtc.backend: tolerance = 0.0 else: tolerance = 0.0 if self.verbose >= 2: print('not rheobase alg') #print("Try %d: SubMax = %s; SupraMin = %s" % \ #(cnt, sub.max() if len(sub) else None, #supra.min() if len(supra) else None)) cnt += 1 reversed = {v: k for k, v in dtc.lookup.items()} #target_current = reversed[self.observation['value']] #dtc.target_current = target_current return dtc dtc = DataTC() dtc.attrs = {} for k, v in model.attrs.items(): dtc.attrs[k] = v # this is not a perservering assignment, of value, # but rather a multi statement assertion that will be checked. dtc.backend = model.backend #print(dtc,dtc.backend) dtc = init_dtc(dtc) #if hasattr(model,'orig_lems_file_path'): # dtc.model_path = model.orig_lems_file_path # assert os.path.isfile(dtc.model_path), "%s is not a file" % dtc.model_path prediction = {} temp = find_target_current(self, dtc).rheobase if type(temp) is not type(None) and not type(dict): prediction['value'] = float(temp) * pq.pA elif type(temp) is not type(None) and type(dict): if temp['value'] is not None: prediction['value'] = float(temp['value']) * pq.pA else: prediction = None else: prediction = None #print(prediction,'prediction') return prediction
def generate_prediction(self, model): ''' inputs a population of genes/alleles, the population size MU, and an optional argument of a rheobase value guess outputs a population of genes/alleles, a population of individual object shells, ie a pickleable container for gene attributes. Rationale, not every gene value will result in a model for which rheobase is found, in which case that gene is discarded, however to compensate for losses in gene population size, more gene samples must be tested for a successful return from a rheobase search. If the tests return are successful these new sampled individuals are appended to the population, and then their attributes are mapped onto corresponding virtual model objects. ''' def check_fix_range(dtc): ''' Inputs: lookup, A dictionary of previous current injection values used to search rheobase Outputs: A boolean to indicate if the correct rheobase current was found and a dictionary containing the range of values used. If rheobase was actually found then rather returning a boolean and a dictionary, instead logical True, and the rheobase current is returned. given a dictionary of rheobase search values, use that dictionary as input for a subsequent search. ''' import numpy as np import quantities as pq import copy sub=[] supra=[] steps=[] dtc.rheobase = 0.0 for k,v in dtc.lookup.items(): if v == 1: #A logical flag is returned to indicate that rheobase was found. dtc.rheobase = float(k) dtc.current_steps = 0.0 dtc.boolean = True return dtc elif v == 0: sub.append(k) elif v > 0: supra.append(k) sub = np.array(sub) supra = np.array(supra) if 0. in supra and len(sub) == 0: dtc.boolean = True dtc.rheobase = -1 #score = scores.InsufficientDataScore(None) return dtc if len(sub)!=0 and len(supra)!=0: #this assertion would only be occur if there was a bug assert sub.max()<=supra.min() if len(sub) and len(supra): center = list(np.linspace(sub.max(),supra.min(),9.0)) center = [ i for i in center if not i == sub.max() ] center = [ i for i in center if not i == supra.min() ] center[int(len(center)/2)+1]=(sub.max()+supra.min())/2.0 steps = [ i*pq.pA for i in center ] elif len(sub): steps = list(np.linspace(sub.max(),2*sub.max(),9.0)) steps = [ i for i in steps if not i == sub.max() ] steps = [ i*pq.pA for i in steps ] elif len(supra): step = list(np.linspace(-2*(supra.min()),supra.min(),9.0)) steps = [ i for i in steps if not i == supra.min() ] steps = [ i*pq.pA for i in steps ] dtc.current_steps = steps dtc.rheobase = None return copy.copy(dtc) def check_current(ampl,dtc): ''' Inputs are an amplitude to test and a virtual model output is an virtual model with an updated dictionary. ''' import copy import os import quantities from neuronunit.models.reduced import ReducedModel import neuronunit LEMS_MODEL_PATH = str(neuronunit.__path__[0])+str('/models/NeuroML2/LEMS_2007One.xml') dtc.model_path = LEMS_MODEL_PATH model = ReducedModel(dtc.model_path,name='vanilla', backend=(str(dtc.backend), {'DTC':dtc})) import copy model.set_attrs(dtc.attrs) DELAY = 100.0*pq.ms DURATION = 1000.0*pq.ms params = {'injected_square_current': {'amplitude':100.0*pq.pA, 'delay':DELAY, 'duration':DURATION}} dtc = copy.copy(dtc) ampl = float(ampl) #print(dtc.lookup) if ampl not in dtc.lookup or len(dtc.lookup) == 0: current = params.copy()['injected_square_current'] uc = {'amplitude':ampl} current.update(uc) current = {'injected_square_current':current} dtc.run_number += 1 model.set_attrs(dtc.attrs) model.inject_square_current(current) dtc.previous = ampl n_spikes = model.get_spike_count() dtc.lookup[float(ampl)] = n_spikes if n_spikes == 1: dtc.rheobase = float(ampl) dtc.boolean = True return dtc return dtc if float(ampl) in dtc.lookup: return dtc def init_dtc(dtc): import numpy as np import copy if dtc.initiated == True: # expand values in the range to accomodate for mutation. # but otherwise exploit memory of this range. if type(dtc.current_steps) is type(float): dtc.current_steps = [ 0.75 * dtc.current_steps, 1.25 * dtc.current_steps ] elif type(dtc.current_steps) is type(list): dtc.current_steps = [ s * 1.25 for s in dtc.current_steps ] dtc.initiated = True # logically unnecessary but included for readibility if dtc.initiated == False: import quantities as pq import numpy as np dtc.boolean = False steps = np.linspace(0,250,7.0) steps_current = [ i*pq.pA for i in steps ] dtc.current_steps = steps_current dtc.initiated = True return dtc def find_rheobase(self, dtc): import dask.bag as db cnt = 0 assert os.path.isfile(dtc.model_path), "%s is not a file" % dtc.model_path # If this it not the first pass/ first generation # then assume the rheobase value found before mutation still holds until proven otherwise. # dtc = check_current(model.rheobase,dtc) # If its not true enter a search, with ranges informed by memory cnt = 0 while dtc.boolean == False: dtc_clones = [ dtc for s in dtc.current_steps ] b0 = db.from_sequence(dtc.current_steps, npartitions=8) b1 = db.from_sequence(dtc_clones, npartitions=8) dtcpop = list(db.map(check_current,b0,b1).compute()) for dtc_clone in dtcpop: dtc.lookup.update(dtc_clone.lookup) dtc = check_fix_range(dtc) cnt += 1 return dtc dtc = DataTC() dtc.attrs = {} for k,v in model.attrs.items(): dtc.attrs[k] = v dtc = init_dtc(dtc) dtc.model_path = model.orig_lems_file_path dtc.backend = model.backend assert os.path.isfile(dtc.model_path), "%s is not a file" % dtc.model_path prediction = {} prediction['value'] = find_rheobase(self,dtc).rheobase * pq.pA return prediction
import pdb from neuronunit.optimization.data_transport_container import DataTC dtc = DataTC() from neuronunit.tests import np, pq, cap, VmTest, scores, AMPL, DELAY, DURATION opt_keys = ['a', 'b', 'vr'] nparams = len(opt_keys) import numpy as np attrs = { 'a': np.median(mp['a']), 'b': np.median(mp['b']), 'vr': np.median(mp['vr']) } dtc.attrs = attrs model = ReducedModel(path_params['model_path'], name=str('vanilla'), backend=('NEURON', { 'DTC': dtc })) params = { 'injected_square_current': { 'amplitude': 10.0 * pq.pA, 'delay': DELAY, 'duration': DURATION } } model.inject_square_current(params)
for k,v in gd.items(): if k not in explore_ranges: hold_constant_glif[k] = v try: with open('glif_seeds.p','rb') as f: seeds = pickle.load(f) assert seeds is not None except: for local_attrs in grid: store_glif_results[str(local_attrs.values())] = {} dtc = DataTC() dtc.tests = use_test complete_params = {} dtc.attrs = local_attrs dtc.backend = 'GLIF' dtc.cell_name = 'GLIF' for key, use_test in test_frame.items(): dtc.tests = use_test dtc = dtc_to_rheo(dtc) dtc = format_test(dtc) if dtc.rheobase is not None: if dtc.rheobase!=-1.0: dtc = nunit_evaluation(dtc) print(dtc.get_ss()) store_glif_results[str(local_attrs.values())][key] = dtc.get_ss() df = pd.DataFrame(store_glif_results) best_params = {} for index, row in df.iterrows(): best_params[index] = row == row.min()
def test_opt_1(self): specimen_id = self.ids[1] cellmodel = "ADEXP" if cellmodel == "IZHI": model = model_classes.IzhiModel() if cellmodel == "MAT": model = model_classes.MATModel() if cellmodel == "ADEXP": model = model_classes.ADEXPModel() target_num_spikes = 8 dtc = DataTC() dtc.backend = cellmodel dtc._backend = model._backend dtc.attrs = model.attrs dtc.params = { k: np.mean(v) for k, v in MODEL_PARAMS[cellmodel].items() } dtc = dtc_to_rheo(dtc) assert dtc.rheobase is not None self.assertIsNotNone(dtc.rheobase) vm, plt, dtc = inject_and_plot_model(dtc, plotly=False) fixed_current = 122 * qt.pA model, suite, nu_tests, target_current, spk_count = opt_setup( specimen_id, cellmodel, target_num_spikes, provided_model=model, fixed_current=False, cached=True) model = dtc.dtc_to_model() model.seeded_current = target_current['value'] model.allen = True model.seeded_current model.NU = True cell_evaluator, simple_cell = opt_setup_two(model, cellmodel, suite, nu_tests, target_current, spk_count, provided_model=model) NGEN = 15 MU = 12 mapping_funct = dask_map_function final_pop, hall_of_fame, logs, hist = opt_exec(MU, NGEN, mapping_funct, cell_evaluator) opt, target = opt_to_model(hall_of_fame, cell_evaluator, suite, target_current, spk_count) best_ind = hall_of_fame[0] fitnesses = cell_evaluator.evaluate_with_lists(best_ind) assert np.sum(fitnesses) < 0.7 self.assertGreater(0.7, np.sum(fitnesses)) #obnames = [obj.name for obj in cell_evaluator.objectives] gen_numbers = logs.select('gen') min_fitness = logs.select('min') max_fitness = logs.select('max') avg_fitness = logs.select('avg') plt.plot(gen_numbers, max_fitness, label='max fitness') plt.plot(gen_numbers, avg_fitness, label='avg fitness') plt.plot(gen_numbers, min_fitness, label='min fitness') plt.plot(gen_numbers, min_fitness, label='min fitness') plt.semilogy() plt.xlabel('generation #') plt.ylabel('score (# std)') plt.legend() plt.xlim(min(gen_numbers) - 1, max(gen_numbers) + 1) #model = opt.dtc_to_model() plt.plot(opt.vm15.times, opt.vm15) plt.plot(opt.vm15.times, opt.vm15) target.vm15 = suite.traces['vm15'] plt.plot(target.vm15.times, target.vm15) target.vm15 = suite.traces['vm15'] check_bin_vm15(target, opt)
def test_opt(self): ids = [ 324257146, 325479788, 476053392, 623893177, 623960880, 482493761, 471819401 ] specimen_id = ids[1] cellmodel = "MAT" if cellmodel == "IZHI": model = model_classes.IzhiModel() if cellmodel == "MAT": model = model_classes.MATModel() if cellmodel == "ADEXP": model = model_classes.ADEXPModel() specific_filter_list = [ 'ISI_log_slope_1.5x', 'mean_frequency_1.5x', 'adaptation_index2_1.5x', 'first_isi_1.5x', 'ISI_CV_1.5x', 'median_isi_1.5x', 'Spikecount_1.5x', 'all_ISI_values', 'ISI_values', 'time_to_first_spike', 'time_to_last_spike', 'time_to_second_spike', 'spike_times' ] simple_yes_list = specific_filter_list target_num_spikes = 8 dtc = DataTC() dtc.backend = cellmodel dtc._backend = model._backend dtc.attrs = model.attrs dtc.params = { k: np.mean(v) for k, v in MODEL_PARAMS[cellmodel].items() } dtc = dtc_to_rheo(dtc) vm, plt, dtc = inject_and_plot_model(dtc, plotly=False) fixed_current = 122 * qt.pA model, suite, nu_tests, target_current, spk_count = opt_setup( specimen_id, cellmodel, target_num_spikes, provided_model=model, fixed_current=False) model = dtc.dtc_to_model() model.seeded_current = target_current['value'] model.allen = True model.seeded_current model.NU = True cell_evaluator, simple_cell = opt_setup_two(model, cellmodel, suite, nu_tests, target_current, spk_count, provided_model=model) NGEN = 15 MU = 12 mapping_funct = dask_map_function final_pop, hall_of_fame, logs, hist = opt_exec(MU, NGEN, mapping_funct, cell_evaluator) opt, target = opt_to_model(hall_of_fame, cell_evaluator, suite, target_current, spk_count) best_ind = hall_of_fame[0] fitnesses = cell_evaluator.evaluate_with_lists(best_ind) assert np.sum(fitnesses) < 0.7 self.assertGreater(0.7, np.sum(fitnesses)) #obnames = [obj.name for obj in cell_evaluator.objectives] gen_numbers = logs.select('gen') min_fitness = logs.select('min') max_fitness = logs.select('max') avg_fitness = logs.select('avg') plt.plot(gen_numbers, max_fitness, label='max fitness') plt.plot(gen_numbers, avg_fitness, label='avg fitness') plt.plot(gen_numbers, min_fitness, label='min fitness') plt.plot(gen_numbers, min_fitness, label='min fitness') plt.semilogy() plt.xlabel('generation #') plt.ylabel('score (# std)') plt.legend() plt.xlim(min(gen_numbers) - 1, max(gen_numbers) + 1) #model = opt.dtc_to_model() plt.plot(opt.vm15.times, opt.vm15) plt.plot(opt.vm15.times, opt.vm15) target.vm15 = suite.traces['vm15'] plt.plot(target.vm15.times, target.vm15) target.vm15 = suite.traces['vm15'] check_bin_vm15(target, opt)
test_frame.keys() test_frame['olf_mit'].insert(0,test_frame['Cerebellum Purkinje cell'][0]) test_frame #!pip install neo --upgrade df = pd.DataFrame(index=list(test_frame.keys()),columns=list(reduced_cells.keys())) for k,v in reduced_cells.items(): temp = {} temp[str(v)] = {} dtc = DataTC() dtc.tests = use_test dtc.attrs = v dtc.backend = 'RAW' dtc.cell_name = 'vanilla' for key, use_test in test_frame.items(): dtc.tests = use_test dtc = dtc_to_rheo(dtc) dtc = format_test(dtc) if dtc.rheobase is not None: if dtc.rheobase!=-1.0: dtc = nunit_evaluation(dtc) df[k][key] = int(dtc.get_ss())
def generate_prediction(self, model): def check_fix_range(dtc): ''' Inputs: lookup, A dictionary of previous current injection values used to search rheobase Outputs: A boolean to indicate if the correct rheobase current was found and a dictionary containing the range of values used. If rheobase was actually found then rather returning a boolean and a dictionary, instead logical True, and the rheobase current is returned. given a dictionary of rheobase search values, use that dictionary as input for a subsequent search. ''' steps = [] dtc.rheobase = None sub, supra = get_sub_supra(dtc.lookup) if (len(sub) + len(supra)) == 0: # This assertion would only be occur if there was a bug assert sub.max() <= supra.min() elif len(sub) and len(supra): # Termination criterion steps = np.linspace(sub.max(), supra.min(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA elif len(sub): steps = np.linspace(sub.max(), 2 * sub.max(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA elif len(supra): steps = np.linspace(supra.min() - 100, supra.min(), cpucount + 1) * pq.pA steps = steps[1:-1] * pq.pA dtc.current_steps = steps return dtc def get_sub_supra(lookup): sub, supra = [], [] for current, n_spikes in lookup.items(): if n_spikes < self.observation['range'][0]: sub.append(current) elif n_spikes > self.observation['range'][1]: supra.append(current) sub = np.array(sorted(list(set(sub)))) supra = np.array(sorted(list(set(supra)))) return sub, supra ''' def check_current(dtc): #Inputs are an amplitude to test and a virtual model #output is an virtual model with an updated dictionary. dtc.boolean = False model = dtc.dtc_to_model() params = {'injected_square_current': {'amplitude':100.0*pq.pA, 'delay':DELAY, 'duration':DURATION}} ampl = float(dtc.ampl) if ampl not in dtc.lookup or len(dtc.lookup) == 0: uc = {'amplitude':ampl*pq.pA,'duration':DURATION,'delay':DELAY} dtc.run_number += 1 model.set_attrs(dtc.attrs) model.inject_square_current(uc) n_spikes = model.get_spike_count() if float(ampl) < -1.0: dtc.rheobase = None dtc.boolean = True return dtc #target_spk_count = self.observation['range'] if self.observation['range'][0] <= n_spikes <= self.observation['range'][1]: dtc.lookup[float(ampl)] = n_spikes dtc.rheobase = {} dtc.rheobase['value'] = float(ampl)*pq.pA dtc.target_spk_count = None dtc.target_spk_count = dtc.rheobase['value'] dtc.boolean = True if self.verbose >2: print('gets here',n_spikes,target_spk_count,n_spikes == target_spk_count) return dtc dtc.lookup[float(ampl)] = n_spikes return dtc ''' def init_dtc(dtc): ''' Exploit memory of last model in genes. ''' # check for memory and exploit it. if dtc.initiated == True: dtc = check_current(dtc) if dtc.boolean: return dtc else: # Exploit memory of the genes to inform searchable range. # if this model has lineage, assume it didn't mutate that far away from it's ancestor. # using that assumption, on first pass, consult a very narrow range, of test current injection samples: # only slightly displaced away from the ancestors rheobase value. if type(dtc.current_steps) is type(float): dtc.current_steps = [ 0.75 * dtc.current_steps, 1.25 * dtc.current_steps ] elif type(dtc.current_steps) is type(list): dtc.current_steps = [ s * 1.25 for s in dtc.current_steps ] dtc.initiated = True # logically unnecessary but included for readibility if dtc.initiated == False: dtc.boolean = False steps = np.linspace(0, 85, 8.0) steps_current = [i * pq.pA for i in steps] dtc.current_steps = steps_current dtc.initiated = True return dtc def find_target_current(self, dtc): cnt = 0 sub = np.array([0, 0]) supra = np.array([0, 0]) #if dtc.backend is 'GLIF': # big = 100 #else: big = 55 while dtc.boolean == False and cnt < big: # negeative spiker if len(sub): if sub.max() < -1.0: pass #use_diff = True # differentiate the wave to look for spikes #be = dtc.backend dtc_clones = [dtc for i in range(0, len(dtc.current_steps))] for i, s in enumerate(dtc.current_steps): dtc_clones[i] = copy.copy(dtc_clones[i]) dtc_clones[i].ampl = copy.copy(dtc.current_steps[i]) dtc_clones[i].backend = copy.copy(dtc.backend) dtc_clones = [d for d in dtc_clones if not np.isnan(d.ampl)] #try: # b0 = db.from_sequence(dtc_clones, npartitions=npartitions) # dtc_clone = list(b0.map(check_current).compute()) #except: set_clones = set([float(d.ampl) for d in dtc_clones]) dtc_clone = [] for dtc, sc in zip(dtc_clones, set_clones): dtc = copy.copy(dtc) dtc.ampl = sc * pq.pA dtc = check_current(dtc) dtc_clone.append(dtc) for dtc in dtc_clone: if dtc.boolean == True: return dtc for d in dtc_clone: dtc.lookup.update(d.lookup) dtc = check_fix_range(dtc) sub, supra = get_sub_supra(dtc.lookup) if len(supra) and len(sub): delta = float(supra.min()) - float(sub.max()) #if str("GLIF") in dtc.backend: # tolerance = 0.0 #else: tolerance = 0.0 #tolerance = tolerance if delta < tolerance or (str(supra.min()) == str( sub.max())): if self.verbose >= 2: print( delta, 'a neuron, close to the edge! Multi spiking rheobase. # spikes: ', len(supra)) if len(supra) < 100: dtc.rheobase['value'] = float(supra.min()) dtc.boolean = True dtc.lookup[float(supra.min())] = len(supra) else: if type(dtc.rheobase) is type(None): dtc.rheobase = {} dtc.rheobase['value'] = None dtc.boolean = False dtc.lookup[float(supra.min())] = len(supra) return dtc if self.verbose >= 2: print('not rheobase alg') print("Try %d: SubMax = %s; SupraMin = %s" % \ (cnt, sub.max() if len(sub) else None, supra.min() if len(supra) else None)) cnt += 1 return dtc dtc = DataTC() dtc.attrs = {} for k, v in model.attrs.items(): dtc.attrs[k] = v # this is not a perservering assignment, of value, # but rather a multi statement assertion that will be checked. dtc.backend = model.backend dtc = init_dtc(dtc) #if hasattr(model,'orig_lems_file_path'): # dtc.model_path = model.orig_lems_file_path # assert os.path.isfile(dtc.model_path), "%s is not a file" % dtc.model_path prediction = {} temp = find_target_current(self, dtc).rheobase if type(temp) is not type(None): if type(temp) is not type(dict()): prediction['value'] = float(temp) * pq.pA elif type(temp) is type(dict()): if type(temp['value']) is not type(None): prediction['value'] = float(temp['value']) * pq.pA else: prediction['value'] = None elif type(temp) is type(None): prediction['value'] = None # float(temp['value'])* pq.pA else: prediction = None return prediction
model = model_classes.MATModel() if cellmodel == "ADEXP": model = model_classes.ADEXPModel() specific_filter_list = [ 'ISI_log_slope_1.5x', 'mean_frequency_1.5x', 'adaptation_index2_1.5x', 'first_isi_1.5x', 'ISI_CV_1.5x', 'median_isi_1.5x', 'Spikecount_1.5x', 'all_ISI_values', 'ISI_values', 'time_to_first_spike', 'time_to_last_spike', 'time_to_second_spike', 'spike_times' ] simple_yes_list = specific_filter_list target_num_spikes = 8 dtc = DataTC() dtc.backend = cellmodel dtc._backend = model._backend dtc.attrs = model.attrs dtc.params = {k: np.mean(v) for k, v in MODEL_PARAMS[cellmodel].items()} dtc = dtc_to_rheo(dtc) vm, plt, dtc = inject_and_plot_model(dtc, plotly=False) fixed_current = 122 * qt.pA model, suite, nu_tests, target_current, spk_count = opt_setup( specimen_id, cellmodel, target_num_spikes, provided_model=model, fixed_current=False) model = dtc.dtc_to_model() model.seeded_current = target_current['value'] model.allen = True model.seeded_current model.NU = True