def timefn(f, what, eol='\n'): """Time the execution of a function and display the results to the status""" tstart=posix.times() start=time.time() rv=f() tend=posix.times() end=time.time() ut=tend[0] - tstart[0] st=tend[1] - tstart[1] status("Finished %s in %.2fr/%.2fu/%.2fs%s" \ % (what, end-start, ut, st, eol)) return rv
def updateUsage(self): now = time.time() t = posix.times() curCpuVal = t[0] + t[1] dt = now - self.lastCalcTime dv = curCpuVal - self.lastCpuVal self.cpuUsage = (dv / dt) * 100.0 self.lastCalcTime = now self.lastCpuVal = curCpuVal
def run(self, t_max, downsample=1, record_from_syns=False, record_from_iclamps=False, record_from_vclamps=False, record_from_channels=False, record_v_deriv=False, record_concentrations=[], pprint=False): """ Run the NEURON simulation. Records at all locations stored under the name 'rec locs' on `self` (see `MorphTree.storeLocs()`) Parameters ---------- t_max: float Duration of the simulation downsample: int (> 0) Records the state of the model every `downsample` time-steps record_from_syns: bool (default ``False``) Record currents of synapstic point processes (in `self.syns`). Accessible as `np.ndarray` in the output dict under key 'i_syn' record_from_iclamps: bool (default ``False``) Record currents of iclamps (in `self.iclamps`) Accessible as `np.ndarray` in the output dict under key 'i_clamp' record_from_vclamps: bool (default ``False``) Record currents of vclamps (in `self.vclamps`) Accessible as `np.ndarray` in the output dict under key 'i_vclamp' record_from_channels: bool (default ``False``) Record channel state variables from `neat` defined channels in `self`, at locations stored under 'rec locs' Accessible as `np.ndarray` in the output dict under key 'chan' record_v_deriv: bool (default ``False``) Record voltage derivative at locations stored under 'rec locs' Accessible as `np.ndarray` in the output dict under key 'dv_dt' record_from_concentrations: bool (default ``False``) Record ion concentration at locations stored under 'rec locs' Accessible as `np.ndarray` in the output dict with as key the ion's name Returns ------- dict Dictionary with the results of the simulation. Contains time and voltage as `np.ndarray` at locations stored under the name ' rec locs', respectively with keys 't' and 'v_m'. Also contains traces of other recorded variables if the option to record them was set to ``True`` """ assert isinstance(downsample, int) and downsample > 0 # simulation time recorder res = {'t': h.Vector()} res['t'].record(h._ref_t) # voltage recorders res['v_m'] = [] for loc in self.getLocs('rec locs'): res['v_m'].append(h.Vector()) res['v_m'][-1].record(self.sections[loc['node']](loc['x'])._ref_v) # synapse current recorders if record_from_syns: res['i_syn'] = [] for syn in self.syns: res['i_syn'].append(h.Vector()) res['i_syn'][-1].record(syn._ref_i) # current clamp current recorders if record_from_iclamps: res['i_clamp'] = [] for iclamp in self.iclamps: res['i_clamp'].append(h.Vector()) res['i_clamp'][-1].record(iclamp._ref_i) # voltage clamp current recorders if record_from_vclamps: res['i_vclamp'] = [] for vclamp in self.vclamps: res['i_vclamp'].append(h.Vector()) res['i_vclamp'][-1].record(vclamp._ref_i) # channel state variable recordings if record_from_channels: res['chan'] = {} channel_names = self.getChannelsInTree() for channel_name in channel_names: res['chan'][channel_name] = { str(var): [] for var in self.channel_storage[channel_name].statevars } for loc in self.getLocs('rec locs'): for ind, varname in enumerate( self.channel_storage[channel_name].statevars): var = str(varname) # assure xcoordinate is refering to proper neuron section (not endpoint) xx = loc['x'] if xx < 1e-3: xx += 1e-3 elif xx > 1. - 1e-3: xx -= 1e-3 # create the recorder try: rec = h.Vector() exec('rec.record(self.sections[loc[0]](xx).' + mechname[channel_name] + '._ref_' + str(var) + ')') res['chan'][channel_name][var].append(rec) except AttributeError: # the channel does not exist here res['chan'][channel_name][var].append([]) if len(record_concentrations) > 0: for c_ion in record_concentrations: res[c_ion] = [] for loc in self.getLocs('rec locs'): res[c_ion].append(h.Vector()) exec( 'res[c_ion][-1].record(self.sections[loc[\'node\']](loc[\'x\'])._ref_' + c_ion + 'i)') # record voltage derivative if record_v_deriv: res['dv_dt'] = [] for ii, loc in enumerate(self.getLocs('rec locs')): res['dv_dt'].append(h.Vector()) # res['dv_dt'][-1].deriv(res['v_m'][ii], self.dt) # initialize # neuron.celsius=37. h.finitialize(self.v_init) h.dt = self.dt # simulate if pprint: print('>>> Simulating the NEURON model for ' + str(t_max) + ' ms. <<<') start = posix.times()[0] neuron.run(t_max + self.t_calibrate) stop = posix.times()[0] if pprint: print('>>> Elapsed time: ' + str(stop - start) + ' seconds. <<<') runtime = stop - start # compute derivative if 'dv_dt' in res: for ii, loc in enumerate(self.getLocs('rec locs')): res['dv_dt'][ii].deriv(res['v_m'][ii], h.dt, 2) res['dv_dt'][ii] = np.array( res['dv_dt'][ii])[self.indstart:][::downsample] res['dv_dt'] = np.array(res['dv_dt']) # cast recordings into numpy arrays res['t'] = np.array( res['t'])[self.indstart:][::downsample] - self.t_calibrate for key in set(res.keys()) - {'t', 'chan', 'dv_dt'}: if key in res and len(res[key]) > 0: res[key] = np.array([np.array(reslist)[self.indstart:][::downsample] \ for reslist in res[key]]) if key in ('i_syn', 'i_clamp', 'i_vclamp'): res[key] *= -1. # cast channel recordings into numpy arrays if 'chan' in res: for channel_name in channel_names: channel = self.channel_storage[channel_name] for ind0, varname in enumerate(channel.statevars): var = str(varname) for ind1 in range(len(self.getLocs('rec locs'))): res['chan'][channel_name][var][ind1] = \ np.array(res['chan'][channel_name][var][ind1])[self.indstart:][::downsample] if len(res['chan'][channel_name][var][ind1]) == 0: res['chan'][channel_name][var][ ind1] = np.zeros_like(res['t']) res['chan'][channel_name][var] = \ np.array(res['chan'][channel_name][var]) # compute P_open # sv = np.zeros((len(channel.statevars), len(self.getLocs('rec locs')), len(res['t']))) sv = {} for varname in channel.statevars: var = str(varname) sv[var] = res['chan'][channel_name][var] res['chan'][channel_name]['p_open'] = channel.computePOpen( res['v_m'], **sv) return res
acts.addAction(funcExit,AccActionsContainer.EXIT) lattice.initialize() print "Total length=",lattice.getLength() nodes = lattice.getNodes() for node in nodes: print "node=",node.getName()," s start,stop = %4.3f %4.3f "%lattice.getNodePositionsDict()[node] d = {"print":True} lattice.trackActions(acts,d) print "Total number of nodes=",nElems[0] #========Speed test========================== count = 1 while(True): #lattice.initialize() lattice.trackActions(acts) if( count % 10000 == 0): print "i=",count, " time= %9.8f "%(posix.times()[0]/count) count += 1 print "====STOP===" sys.exit(1)
acts.addAction(funcEntrance, AccActionsContainer.ENTRANCE) acts.addAction(funcTrack, AccActionsContainer.BODY) acts.addAction(funcExit, AccActionsContainer.EXIT) lattice.initialize() print "Total length=", lattice.getLength() nodes = lattice.getNodes() for node in nodes: print "node=", node.getName( ), " s start,stop = %4.3f %4.3f " % lattice.getNodePositionsDict()[node] d = {"print": True} lattice.trackActions(acts, d) print "Total number of nodes=", nElems[0] #========Speed test========================== count = 1 while (count < 100000): #lattice.initialize() lattice.trackActions(acts) if (count % 10000 == 0): print "i=", count, " time= %9.8f " % (posix.times()[0] / count) count += 1 print "====STOP===" sys.exit(1)
def run(self, t_max, downsample=1, record_from_syns=False, record_from_iclamps=False, record_from_vclamps=False, pprint=False): # simulation time recorder res = {'t': h.Vector()} res['t'].record(h._ref_t) # voltage recorders res['v_m'] = [] for loc in self.getLocs('rec locs'): res['v_m'].append(h.Vector()) res['v_m'][-1].record(self.sections[loc['node']](loc['x'])._ref_v) # synapse current recorders if record_from_syns: res['i_syn'] = [] for syn in self.syns: res['i_syn'].append(h.Vector()) res['i_syn'][-1].record(syn._ref_i) # current clamp current recorders if record_from_iclamps: res['i_clamp'] = [] for iclamp in self.iclamps: res['i_clamp'].append(h.Vector()) res['i_clamp'][-1].record(iclamp._ref_i) # voltage clamp current recorders if record_from_vclamps: res['i_vclamp'] = [] for vclamp in self.vclamps: res['i_vclamp'].append(h.Vector()) res['i_vclamp'][-1].record(vclamp._ref_i) # initialize # neuron.celsius=37. h.finitialize(self.v_eq) h.dt = self.dt # simulate if pprint: print '>>> Simulating the NEURON model for ' + str( t_max) + ' ms. <<<' start = posix.times()[0] neuron.run(t_max + self.t_calibrate) stop = posix.times()[0] if pprint: print '>>> Elapsed time: ' + str(stop - start) + ' seconds. <<<' runtime = stop - start # cast recordings into numpy arrays res['t'] = np.array( res['t'])[self.indstart:][::downsample] - self.t_calibrate for key in 'v_m', 'i_syn', 'i_clamp', 'i_vclamp': if key in res and len(res[key]) > 0: res[key] = np.array([np.array(reslist)[self.indstart:][::downsample] \ for reslist in res[key]]) if key in ('i_syn', 'i_clamp', 'i_vclamp'): res[key] *= -1. return res