def setupRecordLFP(): from .. import sim from netpyne.support.recxelectrode import RecXElectrode nsites = len(sim.cfg.recordLFP) saveSteps = int(np.ceil(sim.cfg.duration / sim.cfg.recordStep)) sim.simData['LFP'] = np.zeros((saveSteps, nsites)) if sim.cfg.saveLFPCells: for c in sim.net.cells: sim.simData['LFPCells'][c.gid] = np.zeros((saveSteps, nsites)) if not sim.net.params.defineCellShapes: sim.net.defineCellShapes( ) # convert cell shapes (if not previously done already) sim.net.calcSegCoords() # calculate segment coords for each cell sim.net.recXElectrode = RecXElectrode( sim) # create exctracellular recording electrode if sim.cfg.createNEURONObj: for cell in sim.net.compartCells: nseg = cell._segCoords['p0'].shape[1] sim.net.recXElectrode.calcTransferResistance( cell.gid, cell._segCoords) # transfer resistance for each cell cell.imembPtr = h.PtrVector(nseg) # pointer vector cell.imembPtr.ptr_update_callback( cell.setImembPtr ) # used for gathering an array of i_membrane values from the pointer vector cell.imembVec = h.Vector(nseg) sim.cvode.use_fast_imem(1) # make i_membrane_ a range variable
def loadSimData(filename, data=None): """ Function to load simulation data from a file Parameters ---------- filename : str The path and name of the file where the data is stored. **Default:** *required* data : dict A dictionary containing a `simData` key. **Default:** ``None`` """ from .. import sim if not data: data = _loadFile(filename) print('Loading simData...') if 'simData' in data: sim.allSimData = data['simData'] else: print((' simData not found in file %s' % (filename))) if 'net' in data: if 'recXElectrode' in data['net']: from netpyne.support.recxelectrode import RecXElectrode xElectrode = data['net']['recXElectrode'] if False == isinstance(xElectrode, RecXElectrode): xElectrode = RecXElectrode.fromJSON(xElectrode) sim.net.recXElectrode = xElectrode
def setupRecordLFP(): """ Function for/to <short description of `netpyne.sim.setup.setupRecordLFP`> """ from .. import sim from netpyne.support.recxelectrode import RecXElectrode nsites = len(sim.cfg.recordLFP) saveSteps = int(np.ceil(sim.cfg.duration / sim.cfg.recordStep)) sim.simData['LFP'] = np.zeros((saveSteps, nsites)) if sim.cfg.saveLFPCells: if sim.cfg.saveLFPCells == True: cellsRecordLFP = utils.getCellsList(['all']) # record all cells elif isinstance(sim.cfg.saveLFPCells, list): cellsRecordLFP = utils.getCellsList(sim.cfg.saveLFPCells) for c in cellsRecordLFP: sim.simData['LFPCells'][c.gid] = np.zeros((saveSteps, nsites)) if sim.cfg.saveLFPPops: if sim.cfg.saveLFPPops == True: popsRecordLFP = list(sim.net.pops.keys()) # record all pops elif isinstance(sim.cfg.saveLFPPops, list): popsRecordLFP = [ p for p in sim.cfg.saveLFPPops if p in list(sim.net.pops.keys()) ] # only pops that exist sim.net.popForEachGid = {} for pop in popsRecordLFP: sim.net.popForEachGid.update( {gid: pop for gid in sim.net.pops[pop].cellGids}) for pop in popsRecordLFP: sim.simData['LFPPops'][pop] = np.zeros((saveSteps, nsites)) if not sim.net.params.defineCellShapes: sim.net.defineCellShapes( ) # convert cell shapes (if not previously done already) sim.net.calcSegCoords() # calculate segment coords for each cell sim.net.recXElectrode = RecXElectrode.fromConfig( sim.cfg) # create exctracellular recording electrode if sim.cfg.createNEURONObj: for cell in sim.net.compartCells: nseg = cell._segCoords['p0'].shape[1] sim.net.recXElectrode.calcTransferResistance( cell.gid, cell._segCoords) # transfer resistance for each cell cell.imembPtr = h.PtrVector(nseg) # pointer vector cell.imembPtr.ptr_update_callback( cell.setImembPtr ) # used for gathering an array of i_membrane values from the pointer vector cell.imembVec = h.Vector(nseg) sim.cvode.use_fast_imem(True) # make i_membrane_ a range variable sim.cfg.use_fast_imem = True
def gatherDataFromFiles(gatherLFP=True, saveFolder=None, simLabel=None, sim=None, fileType='pkl', saveMerged=False): """ Function to gather data from multiple files (from distributed or interval saving) Parameters ---------- gatherLFP : bool Whether or not to gather LFP data. **Default:** ``True`` gathers LFP data if available. **Options:** ``False`` does not gather LFP data. saveFolder : str Name of the directory where data files are located. **Default:** ``None`` attempts to auto-locate the data directory. """ import os if not sim: from netpyne import sim if getattr(sim, 'rank', None) is None: sim.initialize() sim.timing('start', 'gatherTime') if sim.rank == 0: fileType = fileType.lower() if fileType not in ['pkl', 'json']: print( f"Could not gather data from '.{fileType}' files. Only .pkl and .json are supported so far." ) return False if not simLabel: simLabel = sim.cfg.simLabel if not saveFolder: saveFolder = sim.cfg.saveFolder nodeDataDir = os.path.join(saveFolder, simLabel + '_node_data') print(f"\nSearching for .{fileType} node files in {nodeDataDir} ...") simLabels = [ f.replace(f'_node_0.{fileType}', '') for f in os.listdir(nodeDataDir) if f.endswith(f'_node_0.{fileType}') ] if len(simLabels) == 0: print(f"Could not gather data from files. No node files found.") return False mergedFiles = [] for simLabel in simLabels: allSimData = Dict() allCells = [] allPops = ODict() print('\nGathering data from files for simulation: %s ...' % (simLabel)) simDataVecs = ['spkt', 'spkid', 'stims'] + list( sim.cfg.recordTraces.keys()) singleNodeVecs = ['t'] if sim.cfg.recordDipolesHNN: _aggregateDipoles() simDataVecs.append('dipole') fileData = {'simData': sim.simData} fileList = sorted([ f for f in os.listdir(nodeDataDir) if (f.startswith(simLabel + '_node') and f.endswith(f'.{fileType}')) ]) for ifile, file in enumerate(fileList): print(' Merging data file: %s' % (file)) with open(os.path.join(nodeDataDir, file), 'rb') as openFile: if fileType == 'pkl': data = pickle.load(openFile) elif fileType == 'json': import json data = json.load(openFile) if 'cells' in data.keys(): if fileType == 'pkl': allCells.extend([ cell.__getstate__() for cell in data['cells'] ]) else: allCells.extend(data['cells']) if 'pops' in data.keys(): loadedPops = data['pops'] if fileType == 'pkl': for popLabel, pop in loadedPops.items(): allPops[popLabel] = pop['tags'] elif fileType == 'json': # if populations order is not preserved (which is inherently the case for JSON), need to sort them again loadedPops = list(loadedPops.items()) def sort(popKeyAndValue): # the assumption while sorting is that populations order corresponds to cell gids in this population cellGids = popKeyAndValue[1]['cellGids'] if len(cellGids) > 0: return cellGids[0] else: return -1 loadedPops.sort(key=sort) for popLabel, pop in loadedPops: allPops[popLabel] = pop['tags'] if 'simConfig' in data.keys(): setup.setSimCfg(data['simConfig']) if 'net' in data and gatherLFP: if 'recXElectrode' in data['net']: xElectrode = data['net']['recXElectrode'] if False == isinstance(xElectrode, RecXElectrode): xElectrode = RecXElectrode.fromJSON(xElectrode) sim.net.recXElectrode = xElectrode nodePopsCellGids = { popLabel: list(pop['cellGids']) for popLabel, pop in data['pops'].items() } if ifile == 0 and gatherLFP and 'LFP' in data['simData']: lfpData = data['simData']['LFP'] if False == isinstance(lfpData, np.ndarray): lfpData = np.array(lfpData) data['simData']['LFP'] = lfpData allSimData['LFP'] = np.zeros(lfpData.shape) if 'LFPPops' in data['simData']: allSimData['LFPPops'] = { p: np.zeros(lfpData.shape) for p in data['simData']['LFPPops'].keys() } for key, value in data['simData'].items(): if key in simDataVecs: if isinstance(value, dict): for key2, value2 in value.items(): if isinstance(value2, dict): allSimData[key].update( Dict({key2: Dict()})) for stim, value3 in value2.items(): allSimData[key][key2].update( {stim: list(value3)}) elif key == 'dipole': allSimData[key][key2] = np.add( allSimData[key][key2], value2.as_numpy()) else: allSimData[key].update( {key2: list(value2)}) else: allSimData[key] = list( allSimData[key]) + list(value) elif gatherLFP and key == 'LFP': allSimData['LFP'] += np.array(value) elif gatherLFP and key == 'LFPPops': for p in value: allSimData['LFPPops'][p] += np.array(value[p]) elif key == 'dipoleSum': if key not in allSimData.keys(): allSimData[key] = value else: allSimData[key] += value elif key not in singleNodeVecs: allSimData[key].update(value) if file == fileList[0]: for key in singleNodeVecs: allSimData[key] = list(fileData['simData'][key]) allPopsCellGids = { popLabel: [] for popLabel in nodePopsCellGids } else: for popLabel, popCellGids in nodePopsCellGids.items(): allPopsCellGids[popLabel].extend(popCellGids) mergedFiles.append(file) if len(allSimData['spkt']) > 0: allSimData['spkt'], allSimData['spkid'] = zip( *sorted(zip(allSimData['spkt'], allSimData['spkid']))) allSimData['spkt'], allSimData['spkid'] = list( allSimData['spkt']), list(allSimData['spkid']) sim.allSimData = allSimData 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 ## Print statistics sim.pc.barrier() if sim.rank != 0: sim.pc.barrier() else: sim.timing('stop', 'gatherTime') if sim.cfg.timing: print((' Done; gather time = %0.2f s.' % sim.timingData['gatherTime'])) if saveMerged: print('\nSaving merged data into single file ...') saved = sim.saveData() if len(saved) > 0: # if single file saved successfully, clean up node data for file in mergedFiles: path = os.path.join(nodeDataDir, file) os.remove(path) print('\nAnalyzing...') sim.totalSpikes = len(sim.allSimData['spkt']) sim.totalSynapses = sum( [len(cell['conns']) for cell in sim.net.allCells]) if sim.cfg.createPyStruct: if sim.cfg.compactConnFormat: preGidIndex = sim.cfg.compactConnFormat.index( 'preGid') if 'preGid' in sim.cfg.compactConnFormat else 0 sim.totalConnections = sum([ len(set([conn[preGidIndex] for conn in cell['conns']])) for cell in sim.net.allCells ]) else: sim.totalConnections = sum([ len(set([conn['preGid'] for conn in cell['conns']])) for cell in sim.net.allCells ]) else: sim.totalConnections = sim.totalSynapses sim.numCells = len(sim.net.allCells) if sim.totalSpikes > 0: sim.firingRate = float( sim.totalSpikes) / sim.numCells / sim.cfg.duration * 1e3 else: sim.firingRate = 0 if sim.numCells > 0: sim.connsPerCell = sim.totalConnections / float(sim.numCells) sim.synsPerCell = sim.totalSynapses / float(sim.numCells) else: sim.connsPerCell = 0 sim.synsPerCell = 0 print((' Cells: %i' % (sim.numCells))) print((' Connections: %i (%0.2f per cell)' % (sim.totalConnections, sim.connsPerCell))) if sim.totalSynapses != sim.totalConnections: print((' Synaptic contacts: %i (%0.2f per cell)' % (sim.totalSynapses, sim.synsPerCell))) print((' Spikes: %i (%0.2f Hz)' % (sim.totalSpikes, sim.firingRate))) if 'runTime' in sim.timingData: print((' Simulated time: %0.1f s; %i workers' % (sim.cfg.duration / 1e3, sim.nhosts))) print((' Run time: %0.2f s' % (sim.timingData['runTime']))) if sim.cfg.printPopAvgRates and not sim.cfg.gatherOnlySimData: trange = sim.cfg.printPopAvgRates if isinstance( sim.cfg.printPopAvgRates, list) else None sim.allSimData['popRates'] = sim.analysis.popAvgRates( tranges=trange) if 'plotfI' in sim.cfg.analysis: sim.analysis.calculatefI() sim.allSimData['avgRate'] = sim.firingRate