Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def saveData (include = None, filename = None):
from .. import sim
if sim.rank == 0 and not getattr(sim.net, 'allCells', None): needGather = True
else: needGather = False
if needGather: gather.gatherData()
if filename: sim.cfg.filename = filename
if sim.rank == 0:
sim.timing('start', 'saveTime')
import os
# copy source files
if isinstance(sim.cfg.backupCfgFile, list) and len(sim.cfg.backupCfgFile) == 2:
simName = sim.cfg.simLabel if sim.cfg.simLabel else os.path.basename(sim.cfg.filename)
print(('Copying cfg file %s ... ' % simName))
source = sim.cfg.backupCfgFile[0]
targetFolder = sim.cfg.backupCfgFile[1]
# make dir
def associateGid (self, threshold = None):
from .. import sim
if sim.cfg.createNEURONObj:
sim.pc.set_gid2node(self.gid, sim.rank) # this is the key call that assigns cell gid to a particular node
if 'vref' in self.tags:
nc = h.NetCon(getattr(self.hPointp, '_ref_'+self.tags['vref']), None)
else:
nc = h.NetCon(self.hPointp, None)
threshold = threshold if threshold is not None else sim.net.params.defaultThreshold
nc.threshold = threshold
sim.pc.cell(self.gid, nc, 1) # associate a particular output stream of events
del nc # discard netcon
sim.net.gid2lid[self.gid] = len(sim.net.gid2lid)
def checkMemory ():
from .. import sim
# print memory diagnostic info
if sim.rank == 0: # and checkMemory:
import resource
print('\nMEMORY -----------------------')
print('Sections: ')
print(h.topology())
print('NetCons: ')
print(len(h.List("NetCon")))
print('NetStims:')
print(len(h.List("NetStim")))
print('\n Memory usage: %s \n' % resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
# import objgraph
# objgraph.show_most_common_types()
print('--------------------------------\n')
def associateGid (self, threshold = None):
from .. import sim
if self.secs:
if sim.cfg.createNEURONObj:
sim.pc.set_gid2node(self.gid, sim.rank) # this is the key call that assigns cell gid to a particular node
sec = next((secParams for secName,secParams in self.secs.items() if 'spikeGenLoc' in secParams), None) # check if any section has been specified as spike generator
if sec:
loc = sec['spikeGenLoc'] # get location of spike generator within section
else:
#sec = self.secs['soma'] if 'soma' in self.secs else self.secs[self.secs.keys()[0]] # use soma if exists, otherwise 1st section
sec = next((sec for secName, sec in self.secs.items() if len(sec['topol']) == 0), self.secs[list(self.secs.keys())[0]]) # root sec (no parents)
loc = 0.5
nc = None
if 'pointps' in sec: # if no syns, check if point processes with 'vref' (artificial cell)
for pointpName, pointpParams in sec['pointps'].items():
if 'vref' in pointpParams:
nc = h.NetCon(getattr(sec['pointps'][pointpName]['hObj'], '_ref_'+pointpParams['vref']), None, sec=sec['hObj'])
break
if not nc: # if still haven't created netcon
nc = h.NetCon(sec['hObj'](loc)._ref_v, None, sec=sec['hObj'])
if 'threshold' in sec: threshold = sec['threshold']
def intervalCreateSimulateAnalyze (netParams=None, simConfig=None, output=False, interval=None, distribute=False):
''' Sequence of commands create, simulate and analyse network '''
import os
from .. import sim
(pops, cells, conns, stims, rxd, simData) = sim.create(netParams, simConfig, output=True)
try:
if sim.rank==0:
if os.path.exists('temp'):
for f in os.listdir('temp'):
os.unlink('temp/{}'.format(f))
else:
os.mkdir('temp')
sim.intervalSimulate(interval)
except:
print('Error running with interval')
return
sim.pc.barrier()
sim.analyze()
if output: return (pops, cells, conns, stims, simData)
sim.setGlobals()
# set h.dt
h.dt = sim.cfg.dt
# set v_init if doesn't exist
if 'v_init' not in sim.cfg.hParams: sim.cfg.hParams['v_init'] = -65.0
# parallelcontext vars
sim.pc.set_maxstep(10)
mindelay = sim.pc.allreduce(sim.pc.set_maxstep(10), 2) # flag 2 returns minimum value
if sim.rank==0 and sim.cfg.verbose: print(('Minimum delay (time-step for queue exchange) is %.2f'%(mindelay)))
sim.pc.setup_transfer() # setup transfer of source_var to target_var
# handler for printing out time during simulation run
if sim.rank == 0 and sim.cfg.printRunTime:
def printRunTime():
for i in range(int(sim.cfg.printRunTime*1000.0), int(sim.cfg.duration), int(sim.cfg.printRunTime*1000.0)):
sim.cvode.event(i, 'print ' + str(i/1000.0) + ',"s"')
sim.printRunTime = printRunTime
sim.fih.append(h.FInitializeHandler(1, sim.printRunTime))
# set global index used by all instances of the Random123 instances of Random
if sim.cfg.rand123GlobalIndex is not None:
rand = h.Random()
rand.Random123_globalindex(int(sim.cfg.rand123GlobalIndex))
# reset all netstim randomizers so runs are always equivalent
for cell in sim.net.cells:
if cell.tags.get('cellModel') == 'NetStim':
#cell.hRandom.Random123(sim.hashStr('NetStim'), cell.gid, cell.params['seed'])
def distributedSaveHDF5():
from .. import sim
import h5py
if sim.rank == 0: sim.timing('start', 'saveTimeHDF5')
sim.compactConnFormat()
conns = [[cell.gid]+conn for cell in sim.net.cells for conn in cell.conns]
conns = sim.copyRemoveItemObj(conns, keystart='h', newval=[])
connFormat = ['postGid']+sim.cfg.compactConnFormat
with h5py.File(sim.cfg.filename+'.h5', 'w') as hf:
hf.create_dataset('conns', data = conns)
hf.create_dataset('connsFormat', data = connFormat)
if sim.rank == 0: sim.timing('stop', 'saveTimeHDF5')
def runArm(t):
# turn off RL and explor movs for last testing trial
if t >= sim.trainTime:
sim.useRL = False
sim.explorMovs = False
if sim.useArm:
sim.arm.run(t, sim) # run virtual arm apparatus (calculate command, move arm, feedback)
if sim.useRL and (t - sim.timeoflastRL >= sim.RLinterval): # if time for next RL
sim.timeoflastRL = h.t
vec = h.Vector()
if sim.rank == 0:
critic = sim.arm.RLcritic(h.t) # get critic signal (-1, 0 or 1)
sim.pc.broadcast(vec.from_python([critic]), 0) # convert python list to hoc vector for broadcast data received from arm
else: # other workers
sim.pc.broadcast(vec, 0)
critic = vec.to_python()[0]
if critic != 0: # if critic signal indicates punishment (-1) or reward (+1)
print 't=',t,'- adjusting weights based on RL critic value:', critic
for cell in sim.net.cells:
for conn in cell.conns:
STDPmech = conn.get('hSTDP') # check if has STDP mechanism
if STDPmech: # run stdp.mod method to update syn weights based on RLprint cell.gid
STDPmech.reward_punish(float(critic))
# store weight changes
sim.allWeights.append([]) # Save this time
sim.timing('start', 'createTime')
if sim.rank==0:
print(("\nCreating network of %i cell populations on %i hosts..." % (len(self.pops), sim.nhosts)))
for ipop in list(self.pops.values()): # For each pop instantiate the network cells (objects of class 'Cell')
newCells = ipop.createCells() # create cells for this pop using Pop method
self.cells.extend(newCells) # add to list of cells
sim.pc.barrier()
if sim.rank==0 and sim.cfg.verbose: print(('Instantiated %d cells of population %s'%(len(newCells), ipop.tags['pop'])))
if self.params.defineCellShapes: self.defineCellShapes()
print((' Number of cells on node %i: %i ' % (sim.rank,len(self.cells))))
sim.pc.barrier()
sim.timing('stop', 'createTime')
if sim.rank == 0 and sim.cfg.timing: print((' Done; cell creation time = %0.2f s.' % sim.timingData['createTime']))
return self.cells
def createParallelContext ():
from .. import sim
sim.pc = h.ParallelContext() # MPI: Initialize the ParallelContext class
sim.pc.done()
sim.nhosts = int(sim.pc.nhost()) # Find number of hosts
sim.rank = int(sim.pc.id()) # rank or node number (0 will be the master)
if sim.rank==0:
sim.pc.gid_clear()