Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def instantiateNetPyNEModel(self):
with redirect_stdout(sys.__stdout__):
saveData = sim.allSimData if hasattr(sim, 'allSimData') and 'spkt' in sim.allSimData.keys() and len(sim.allSimData['spkt'])>0 else False
sim.create(self.netParams, self.simConfig)
sim.net.defineCellShapes() # creates 3d pt for cells with stylized geometries
sim.gatherData(gatherLFP=False)
if saveData: sim.allSimData = saveData # preserve data from previous simulation
return sim
gather = sim.pc.py_alltoall(data)
sim.pc.barrier()
if sim.rank == 0:
allCells = []
allPops = ODict()
for popLabel,pop in sim.net.pops.items(): allPops[popLabel] = pop.__getstate__() # can't use dict comprehension for OrderedDict
allPopsCellGids = {popLabel: [] for popLabel in netPopsCellGids} ####################
# fill in allSimData taking into account if data is dict of h.Vector (code needs improvement to be more generic)
for node in gather: # concatenate data from each node
allCells.extend(node['netCells']) # extend allCells list
for popLabel,popCellGids in node['netPopsCellGids'].items():
allPopsCellGids[popLabel].extend(popCellGids)
if len(sim.allSimData['spkt']) > 0:
sim.allSimData['spkt'], sim.allSimData['spkid'] = zip(*sorted(zip(sim.allSimData['spkt'], sim.allSimData['spkid']))) # sort spks
sim.allSimData['spkt'], sim.allSimData['spkid'] = list(sim.allSimData['spkt']), list(sim.allSimData['spkid'])
sim.net.allCells = sorted(allCells, key=lambda k: k['gid'])
for popLabel,pop in allPops.items():
pop['cellGids'] = sorted(allPopsCellGids[popLabel])
sim.net.allPops = allPops
# clean to avoid mem leaks
for node in gather:
if node:
node.clear()
del node
for item in data:
if item:
item.clear()
sim.allSimData[key] = list(nodeData['simData'][key])
# fill in allSimData taking into account if data is dict of h.Vector (code needs improvement to be more generic)
for node in gather: # concatenate data from each node
for key,val in node['simData'].items(): # update simData dics of dics of h.Vector
if key in simDataVecs: # simData dicts that contain Vectors
if isinstance(val, dict):
for cell,val2 in val.items():
if isinstance(val2,dict):
sim.allSimData[key].update(Dict({cell:Dict()}))
for stim,val3 in val2.items():
sim.allSimData[key][cell].update({stim:list(val3)}) # udpate simData dicts which are dicts of dicts of Vectors (eg. ['stim']['cell_1']['backgrounsd']=h.Vector)
else:
sim.allSimData[key].update({cell:list(val2)}) # udpate simData dicts which are dicts of Vectors (eg. ['v']['cell_1']=h.Vector)
else:
sim.allSimData[key] = list(sim.allSimData[key])+list(val) # udpate simData dicts which are Vectors
elif gatherLFP and key == 'LFP':
sim.allSimData[key] += np.array(val)
elif key not in singleNodeVecs:
sim.allSimData[key].update(val) # update simData dicts which are not Vectors
if len(sim.allSimData['spkt']) > 0:
sim.allSimData['spkt'], sim.allSimData['spkid'] = zip(*sorted(zip(sim.allSimData['spkt'], sim.allSimData['spkid']))) # sort spks
sim.allSimData['spkt'], sim.allSimData['spkid'] = list(sim.allSimData['spkt']), list(sim.allSimData['spkid'])
sim.net.allPops = ODict() # pops
for popLabel,pop in sim.net.pops.items(): sim.net.allPops[popLabel] = pop.__getstate__() # can't use dict comprehension for OrderedDict
sim.net.allCells = [c.__dict__ for c in sim.net.cells]
# gather cells, pops and sim data
def doIhaveInstOrSimData(self): # return [bool, bool] telling if we have an instance and simulated data
with redirect_stdout(sys.__stdout__):
out = [False, False]
if hasattr(sim, 'net'):
if hasattr(sim.net, 'cells') and hasattr(sim.net, 'pops'):
if len(sim.net.cells)>0 and len(sim.net.pops.keys())>0:
out[0] = True
if hasattr(sim, 'allSimData'):
if 'spkt' in sim.allSimData.keys() and 'spkid' in sim.allSimData.keys():
if len(sim.allSimData['spkt'])>0 and len(sim.allSimData['spkid'])>0:
out[1] = True
return {'haveInstance': out[0], 'haveSimData': out[1]}
simDataVecs = ['spkt','spkid','stims']+list(sim.cfg.recordTraces.keys())
singleNodeVecs = ['t']
netPopsCellGids = {popLabel: list(pop.cellGids) for popLabel,pop in sim.net.pops.items()}
# gather only sim data
nodeData = {'simData': sim.simData}
data = [None]*sim.nhosts
data[0] = {}
for k,v in nodeData.items():
data[0][k] = v
gather = sim.pc.py_alltoall(data)
sim.pc.barrier()
if sim.rank == 0: # simData
print(' Gathering only sim data in master...')
sim.allSimData = Dict()
for k in list(gather[0]['simData'].keys()): # initialize all keys of allSimData dict
if gatherLFP and k == 'LFP':
sim.allSimData[k] = np.zeros((gather[0]['simData']['LFP'].shape))
else:
sim.allSimData[k] = {}
for key in singleNodeVecs: # store single node vectors (eg. 't')
sim.allSimData[key] = list(nodeData['simData'][key])
# fill in allSimData taking into account if data is dict of h.Vector (code needs improvement to be more generic)
for node in gather: # concatenate data from each node
for key,val in node['simData'].items(): # update simData dics of dics of h.Vector
if key in simDataVecs: # simData dicts that contain Vectors
if isinstance(val, dict):
for cell,val2 in val.items():
if isinstance(val2,dict):
sim.allSimData[key].update(Dict({cell:Dict()}))
for stim,val3 in val2.items():
data = []
lenData = []
for key in list(fullTrace.keys()):
t.append(np.array(sim.allSimData[trace]['cell_time_'+str(gid)][key]))
t_indexes.append(t[-1].__ge__(timeRange[0]).__and__(t[-1].__le__(timeRange[1])))
data.append(np.array(fullTrace[key])[t_indexes[-1]])
lenData = len(data[-1])
t[-1] = t[-1][t_indexes[-1]]
else:
data = [fullTrace[key][int(timeRange[0]/recordStep):int(timeRange[1]/recordStep)] for key in list(fullTrace.keys())]
lenData = len(data[0])
data = np.transpose(np.array(data))
t = np.arange(timeRange[0], timeRange[1]+recordStep, recordStep)
else:
if recordStep == 'adaptive':
t = np.array(sim.allSimData[trace]['cell_time_'+str(gid)])
t_indexes = t.__ge__(timeRange[0]).__and__(t.__le__(timeRange[1]))
data = np.array(sim.allSimData[trace]['cell_'+str(gid)])[t_indexes]
lenData = len(data)
t = t[t_indexes]
else:
data = fullTrace[int(timeRange[0]/recordStep):int(timeRange[1]/recordStep)]
lenData = len(data)
t = np.arange(timeRange[0], timeRange[1]+recordStep, recordStep)
tracesData.append({'t': t, 'cell_'+str(gid)+'_'+trace: data})
color = colorList2[itrace%len(colorList2)]
if not overlay:
plt.subplot(len(tracesList),1,itrace+1)
color = 'blue'
if recordStep == 'adaptive':
if isinstance(data, list) and isinstance(data[0], (list, np.array)):
for tl,dl in zip(t,data):
cellsPost, _, _ = getCellsInclude(includePost)
for postCell in cellsPost:
print postCell['gid']
preGidsAll = [conn[preGidIndex] for conn in postCell['conns'] if isinstance(conn[preGidIndex], Number) and conn[preGidIndex] in cellsPreGids+cellsPrePreGids]
preGids = [gid for gid in preGidsAll if gid in cellsPreGids]
for preGid in preGids:
preCell = sim.net.allCells[preGid]
prePreGids = [conn[preGidIndex] for conn in preCell['conns'] if conn[preGidIndex] in cellsPrePreGids]
totCon += 1
if not set(prePreGids).isdisjoint(preGidsAll):
numDis += 1
print ' Total disynaptic connections: %d / %d (%.2f%%)' % (numDis, totCon, float(numDis)/float(totCon)*100 if totCon>0 else 0.0)
try:
sim.allSimData['disynConns'] = numDis
except:
pass
print ' time ellapsed (s): ', time() - start
return numDis
# Select cells to include
if len(cellGids) > 0:
try:
spkinds,spkts = list(zip(*[(spkgid,spkt) for spkgid,spkt in zip(sim.allSimData['spkid'],sim.allSimData['spkt']) if spkgid in cellGids]))
except:
spkinds,spkts = [],[]
else:
spkinds,spkts = [],[]
# Add NetStim spikes
spkts, spkinds = list(spkts), list(spkinds)
numNetStims = 0
if 'stims' in sim.allSimData:
for netStimLabel in netStimLabels:
netStimSpks = [spk for cell,stims in sim.allSimData['stims'].items() \
for stimLabel,stimSpks in stims.items() for spk in stimSpks if stimLabel == netStimLabel]
if len(netStimSpks) > 0:
lastInd = max(spkinds) if len(spkinds)>0 else 0
spktsNew = netStimSpks
spkindsNew = [lastInd+1+i for i in range(len(netStimSpks))]
spkts.extend(spktsNew)
spkinds.extend(spkindsNew)
numNetStims += 1
if peakBin:
histo = np.histogram(spkts, bins = np.arange(timeRange[0], timeRange[1], peakBin))
histoT = histo[1][:-1]+peakBin/2
histoCount = histo[0]
histData.append(histoCount)