#print allpopMUAs #print "\n" normte = np.zeros(len(popsizes)) # For each population, obtain its MUA against all other populations for popnum in range(len(allpopMUAs)): allotherpops = copy.deepcopy(allpopMUAs) thispop = allotherpops.pop( popnum ) # Gives us this population, AND removes it from 'remaining' list thispopvec = h.Vector(thispop) # Convert thispop to a hoc Vector # Now, for each 'other' population, compare it to 'thispop' for otherpop in allotherpops: # Convert otherpop to a hoc Vector otherpopvec = h.Vector(otherpop) normtevec = h.normte(thispopvec, otherpopvec, 30) # Try 30 shuffles #normtevec.printf("%8.4f\n") #print "using %f, popnum %d" % (normtevec.x[2], popnum) normte[popnum] += normtevec.x[2] #print normte #print "\n" allnormtes[runnum] = normte print "allnormtes" print allnormtes xaxis = range(len(base_popsizes)) meanperpop = np.mean(allnormtes, axis=0) stdperpop = np.std(allnormtes, axis=0) print meanperpop print stdperpop
def nTE(cells1=[], cells2=[], spks1=None, spks2=None, timeRange=None, binSize=20, numShuffle=30): ''' Calculate normalized transfer entropy - cells1 (['all',|'allCells','allNetStims',|,120,|,'E1'|,('L2', 56)|,('L5',[4,5,6])]): Subset of cells from which to obtain spike train 1 (default: []) - cells2 (['all',|'allCells','allNetStims',|,120,|,'E1'|,('L2', 56)|,('L5',[4,5,6])]): Subset of cells from which to obtain spike train 1 (default: []) - spks1 (list): Spike train 1; list of spike times; if omitted then obtains spikes from cells1 (default: None) - spks2 (list): Spike train 2; list of spike times; if omitted then obtains spikes from cells2 (default: None) - timeRange ([min, max]): Range of time to calculate nTE in ms (default: [0,cfg.duration]) - binSize (int): Bin size used to convert spike times into histogram - numShuffle (int): Number of times to shuffle spike train 1 to calculate TEshuffled; note: nTE = (TE - TEShuffled)/H(X2F|X2P) - Returns nTE (float): normalized transfer entropy ''' from neuron import h import netpyne from .. import sim import os root = os.path.dirname(netpyne.__file__) if 'nte' not in dir(h): try: print( ' Warning: support/nte.mod not compiled; attempting to compile from %s via "nrnivmodl support"' % (root)) os.system('cd ' + root + '; nrnivmodl support') from neuron import load_mechanisms load_mechanisms(root) print(' Compilation of support folder mod files successful') except: print(' Error compiling support folder mod files') return h.load_file(root + '/support/nte.hoc') # nTE code (also requires support/net.mod) if not spks1: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells1) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks1 = list(spkts) if not spks2: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells2) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks2 = list(spkts) # time range if getattr(sim, 'cfg', None): timeRange = [0, sim.cfg.duration] else: timeRange = [0, max(spks1 + spks2)] inputVec = h.Vector() outputVec = h.Vector() histo1 = np.histogram(spks1, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount1 = histo1[0] histo2 = np.histogram(spks2, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount2 = histo2[0] inputVec.from_python(histoCount1) outputVec.from_python(histoCount2) out = h.normte(inputVec, outputVec, numShuffle) TE, H, nTE, _, _ = out.to_python() return nTE
def nTE(cells1=[], cells2=[], spks1=None, spks2=None, timeRange=None, binSize=20, numShuffle=30): """Calculate normalized transfer entropy. Parameters ---------- cells1 : list Subset of cells from which to obtain spike train 1. **Default:** ``[]`` **Options:** ``['all']`` plots all cells and stimulations, ``['allNetStims']`` plots just stimulations, ``['popName1']`` plots a single population, ``['popName1', 'popName2']`` plots multiple populations, ``[120]`` plots a single cell, ``[120, 130]`` plots multiple cells, ``[('popName1', 56)]`` plots a cell from a specific population, ``[('popName1', [0, 1]), ('popName2', [4, 5, 6])]``, plots cells from multiple populations cells2 : list Subset of cells from which to obtain spike train 2. **Default:** ``[]`` **Options:** same as for `cells1` spks1 : list Spike train 1; list of spike times; if omitted then obtains spikes from cells1. **Default:** ``None`` spks2 : list Spike train 2; list of spike times; if omitted then obtains spikes from cells2. **Default:** ``None`` timeRange : list [min, max] Range of time to calculate nTE in ms. **Default:** ``None`` uses the entire simulation time range binSize : int Bin size used to convert spike times into histogram. **Default:** ``20`` numShuffle : int Number of times to shuffle spike train 1 to calculate TEshuffled; note: nTE = (TE - TEShuffled)/H(X2F|X2P). **Default:** ``30`` Returns ------- float Normalized transfer entropy See Also -------- granger : Examples -------- >>> import netpyne, netpyne.examples.example >>> out = netpyne.analysis.nTE() """ from neuron import h import netpyne from .. import sim import os root = os.path.dirname(netpyne.__file__) if 'nte' not in dir(h): try: print( ' Warning: support/nte.mod not compiled; attempting to compile from %s via "nrnivmodl support"' % (root)) os.system('cd ' + root + '; nrnivmodl support') from neuron import load_mechanisms load_mechanisms(root) print(' Compilation of support folder mod files successful') except: print(' Error compiling support folder mod files') return h.load_file(root + '/support/nte.hoc') # nTE code (also requires support/net.mod) if not spks1: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells1) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks1 = list(spkts) if not spks2: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells2) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks2 = list(spkts) # time range if getattr(sim, 'cfg', None): timeRange = [0, sim.cfg.duration] else: timeRange = [0, max(spks1 + spks2)] inputVec = h.Vector() outputVec = h.Vector() histo1 = np.histogram(spks1, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount1 = histo1[0] histo2 = np.histogram(spks2, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount2 = histo2[0] inputVec.from_python(histoCount1) outputVec.from_python(histoCount2) out = h.normte(inputVec, outputVec, numShuffle) TE, H, nTE, _, _ = out.to_python() return nTE
def nTE(cells1=[], cells2=[], spks1=None, spks2=None, timeRange=None, binSize=20, numShuffle=30): """ Function that calculates the Normalized Transfer Entropy (nTE) between two spike train signals. Transfer entropy is a model-free statistic that is able to measure the time-directed transfer of information between stochastic variables and therefore provides an asymmetric method to measure information transfer. In simple words, the nTE represents the fraction of information in X explained by its own past which is not explained by the past of Y. Kale, P. et al (2018, July). Normalized Transfer Entropy as a Tool to Identify Multisource Functional Epileptic Networks IEEE Engineering in Medicine and Biology Society (EMBC) https://doi.org/10.1109/embc.2018.8512532 Parameters ---------- cells1 : list Subset of cells from which to obtain spike train 1. **Default:** ``[]`` **Options:** ``['all']`` plots all cells and stimulations, ``['allNetStims']`` plots just stimulations, ``['popName1']`` plots a single population, ``['popName1', 'popName2']`` plots multiple populations, ``[120]`` plots a single cell, ``[120, 130]`` plots multiple cells, ``[('popName1', 56)]`` plots a cell from a specific population, ``[('popName1', [0, 1]), ('popName2', [4, 5, 6])]``, plots cells from multiple populations cells2 : list Subset of cells from which to obtain spike train 2. **Default:** ``[]`` **Options:** same as for `cells1` spks1 : list Spike train 1; list of spike times; if omitted then obtains spikes from cells1. **Default:** ``None`` **Options:** ``<option>`` <description of option> spks2 : list Spike train 2; list of spike times; if omitted then obtains spikes from cells2. **Default:** ``None`` **Options:** ``<option>`` <description of option> timeRange : list [min, max] Range of time to calculate nTE in ms. **Default:** ``None`` uses the entire simulation time range **Options:** ``<option>`` <description of option> binSize : int Bin size used to convert spike times into histogram. **Default:** ``20`` **Options:** ``<option>`` <description of option> numShuffle : int Number of times to shuffle spike train 1 to calculate TEshuffled; note: nTE = (TE - TEShuffled)/H(X2F|X2P). **Default:** ``30`` **Options:** ``<option>`` <description of option> Returns ------- """ from neuron import h import netpyne from .. import sim import os root = os.path.dirname(netpyne.__file__) if 'nte' not in dir(h): try: print( ' Warning: support/nte.mod not compiled; attempting to compile from %s via "nrnivmodl support"' % (root)) os.system('cd ' + root + '; nrnivmodl support') from neuron import load_mechanisms load_mechanisms(root) print(' Compilation of support folder mod files successful') except: print(' Error compiling support folder mod files') return h.load_file(root + '/support/nte.hoc') # nTE code (also requires support/net.mod) if not spks1: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells1) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks1 = list(spkts) if not spks2: # if doesnt contain a list of spk times, obtain from cells specified cells, cellGids, netStimPops = getCellsInclude(cells2) numNetStims = 0 # Select cells to include if len(cellGids) > 0: try: spkts = [ spkt for spkgid, spkt in zip(sim.allSimData['spkid'], sim.allSimData['spkt']) if spkgid in cellGids ] except: spkts = [] else: spkts = [] # Add NetStim spikes spkts = list(spkts) numNetStims = 0 for netStimPop in netStimPops: if 'stims' in sim.allSimData: cellStims = [ cellStim for cell, cellStim in sim.allSimData['stims'].items() if netStimPop in cellStim ] if len(cellStims) > 0: spktsNew = [ spkt for cellStim in cellStims for spkt in cellStim[netStimPop] ] spkts.extend(spktsNew) numNetStims += len(cellStims) spks2 = list(spkts) # time range if getattr(sim, 'cfg', None): timeRange = [0, sim.cfg.duration] else: timeRange = [0, max(spks1 + spks2)] inputVec = h.Vector() outputVec = h.Vector() histo1 = np.histogram(spks1, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount1 = histo1[0] histo2 = np.histogram(spks2, bins=np.arange(timeRange[0], timeRange[1], binSize)) histoCount2 = histo2[0] inputVec.from_python(histoCount1) outputVec.from_python(histoCount2) out = h.normte(inputVec, outputVec, numShuffle) TE, H, nTE, _, _ = out.to_python() return nTE