Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
for cell in cellsUpdate:
# Add synMechs, stim and conn NEURON objects
cell.addStimsNEURONObj()
#cell.addSynMechsNEURONObj()
cell.addConnsNEURONObj()
nodeSynapses = sum([len(cell.conns) for cell in sim.net.cells])
if sim.cfg.createPyStruct:
nodeConnections = sum([len(set([conn['preGid'] for conn in cell.conns])) for cell in sim.net.cells])
else:
nodeConnections = nodeSynapses
print((' Number of connections on node %i: %i ' % (sim.rank, nodeConnections)))
if nodeSynapses != nodeConnections:
print((' Number of synaptic contacts on node %i: %i ' % (sim.rank, nodeSynapses)))
sim.pc.barrier()
sim.timing('stop', 'connectTime')
if sim.rank == 0 and sim.cfg.timing: print((' Done; cell connection time = %0.2f s.' % sim.timingData['connectTime']))
return [cell.conns for cell in self.cells]
def createCells (self):
from .. import sim
sim.pc.barrier()
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
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']
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)
if not gapJunctions and 'gapJunction' in connParam: gapJunctions = True
if sim.cfg.printSynsAfterRule:
nodeSynapses = sum([len(cell.conns) for cell in sim.net.cells])
print((' Number of synaptic contacts on node %i after conn rule %s: %i ' % (sim.rank, connParamLabel, nodeSynapses)))
# add presynaptoc gap junctions
if gapJunctions:
# distribute info on presyn gap junctions across nodes
if not getattr(sim.net, 'preGapJunctions', False):
sim.net.preGapJunctions = [] # if doesn't exist, create list to store presynaptic cell gap junctions
data = [sim.net.preGapJunctions]*sim.nhosts # send cells data to other nodes
data[sim.rank] = None
gather = sim.pc.py_alltoall(data) # collect cells data from other nodes (required to generate connections)
sim.pc.barrier()
for dataNode in gather:
if dataNode: sim.net.preGapJunctions.extend(dataNode)
# add gap junctions of presynaptic cells (need to do separately because could be in different ranks)
for preGapParams in getattr(sim.net, 'preGapJunctions', []):
if preGapParams['gid'] in self.gid2lid: # only cells in this rank
cell = self.cells[self.gid2lid[preGapParams['gid']]]
cell.addConn(preGapParams)
# apply subcellular connectivity params (distribution of synaspes)
if self.params.subConnParams:
self.subcellularConn(allCellTags, allPopTags)
sim.cfg.createNEURONObj = origCreateNEURONObj # set to original value
sim.cfg.addSynMechs = origAddSynMechs # set to original value
cellsUpdate = [c for c in sim.net.cells if c.tags['cellModel'] not in ['NetStim', 'VecStim']]
if sim.cfg.createNEURONObj:
# 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
for cell in sim.net.cells:
for conn in cell.conns:
if 'hSTDP' in conn:
sim.allWeights[-1].append(float(conn['hNetcon'].weight[0])) # save weight only for STDP conns
for cell in cellsUpdate:
# Add synMechs, stim and conn NEURON objects
cell.addStimsNEURONObj()
#cell.addSynMechsNEURONObj()
cell.addConnsNEURONObj()
nodeSynapses = sum([len(cell.conns) for cell in sim.net.cells])
if sim.cfg.createPyStruct:
nodeConnections = sum([len(set([conn['preGid'] for conn in cell.conns])) for cell in sim.net.cells])
else:
nodeConnections = nodeSynapses
print((' Number of connections on node %i: %i ' % (sim.rank, nodeConnections)))
if nodeSynapses != nodeConnections:
print((' Number of synaptic contacts on node %i: %i ' % (sim.rank, nodeSynapses)))
sim.pc.barrier()
sim.timing('stop', 'connectTime')
if sim.rank == 0 and sim.cfg.timing: print((' Done; cell connection time = %0.2f s.' % sim.timingData['connectTime']))
return [cell.conns for cell in self.cells]
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)
# check if gap junctions in any of the conn rules
if not gapJunctions and 'gapJunction' in connParam: gapJunctions = True
if sim.cfg.printSynsAfterRule:
nodeSynapses = sum([len(cell.conns) for cell in sim.net.cells])
print((' Number of synaptic contacts on node %i after conn rule %s: %i ' % (sim.rank, connParamLabel, nodeSynapses)))
# add presynaptoc gap junctions
if gapJunctions:
# distribute info on presyn gap junctions across nodes
if not getattr(sim.net, 'preGapJunctions', False):
sim.net.preGapJunctions = [] # if doesn't exist, create list to store presynaptic cell gap junctions
data = [sim.net.preGapJunctions]*sim.nhosts # send cells data to other nodes
data[sim.rank] = None
gather = sim.pc.py_alltoall(data) # collect cells data from other nodes (required to generate connections)
sim.pc.barrier()
for dataNode in gather:
if dataNode: sim.net.preGapJunctions.extend(dataNode)
# add gap junctions of presynaptic cells (need to do separately because could be in different ranks)
for preGapParams in getattr(sim.net, 'preGapJunctions', []):
if preGapParams['gid'] in self.gid2lid: # only cells in this rank
cell = self.cells[self.gid2lid[preGapParams['gid']]]
cell.addConn(preGapParams)
# apply subcellular connectivity params (distribution of synaspes)
if self.params.subConnParams:
self.subcellularConn(allCellTags, allPopTags)
sim.cfg.createNEURONObj = origCreateNEURONObj # set to original value
sim.cfg.addSynMechs = origAddSynMechs # set to original value
cellsUpdate = [c for c in sim.net.cells if c.tags['cellModel'] not in ['NetStim', 'VecStim']]