def mutual_information(self, X, Y, title=None, nbins_X=50, nbins_Y=50, noise_sigma='all'): #import pdb; pdb.set_trace() no_nans_idx = np.logical_not(np.logical_or(np.isnan(X), np.isnan(Y))) Xq, _, _ = pyentropy.quantise(X[no_nans_idx], nbins_X) Yq, _, _ = pyentropy.quantise(Y[no_nans_idx], nbins_Y) s = pyentropy.DiscreteSystem(Yq, (1, nbins_Y), Xq, (1, nbins_X)) s.calculate_entropies() # MINE mine = MINE() mine.compute_score(X.flatten(), Y.flatten()) # Linear regression slope, intercept, r, p, stderr = \ scipy.stats.linregress(X[no_nans_idx], Y[no_nans_idx]) #import pdb; pdb.set_trace() if title is not None: print(title) print(" MIC/MI/r^2/p/slope for %s:\t%.3f\t%.3f\t%s\t%s\t%s" % (noise_sigma, mine.mic(), s.I(), r**2, p, slope))
def getFitness(self, smMatrix): #Store the sm state into memory fit = 0 #Fitness function (4) ****************************************** #Mutual Information between motor command and predicted sensory state. sp = smMatrix[1:, 3] mot = smMatrix[1:, 2] spQ = ent.quantise(sp, 10) motQ = ent.quantise(mot, 10) s = ent.DiscreteSystem(spQ[0], (1, 10), motQ[0], (1, 10)) print(str(spQ[0]) + "\n" + str(motQ[0])) s.calculate_entropies(method='plugin', calc=['HX', 'HXY']) mutInf = s.I() fit = mutInf print(fit) return fit
def getFitness(self, smMatrix): #Store the sm state into memory fit = 0 #Fitness function (4) ****************************************** #Mutual Information between motor command and predicted sensory state. sp = smMatrix[1:,3] mot = smMatrix[1:,2] spQ = ent.quantise(sp,10) motQ = ent.quantise(mot,10) s = ent.DiscreteSystem(spQ[0],(1,10), motQ[0],(1,10)) print(str(spQ[0]) + "\n" + str(motQ[0])) s.calculate_entropies(method='pt', calc=['HX', 'HXY']) mutInf = s.I() fit = mutInf print(fit) return fit
def ent_analyses(blk, X_disc=128, Y_disc=64): CP = neoUtils.get_var(blk, 'CP') S = float(blk.annotations['s'][2:-1]) CP /= S CP = CP.magnitude idx = np.all(np.isfinite(CP), axis=1) s = np.empty_like(CP) s[:] = np.nan s[idx, :] = pye.quantise(CP[idx, :], X_disc, uniform='bins')[0] FR = neoUtils.get_rate_b(blk, unit_num=unit_num, sigma=2 * pq.ms)[0] FR = pye.quantise(FR, Y_disc, uniform='bins')[0] idx = np.all(np.isfinite(s), axis=1) X = s.astype('int64').T[:, idx] Y = FR[np.newaxis, idx] DS = pye.DiscreteSystem(X, (X.shape[0], bins), Y, (1, bins)) DS.calculate_entropies() #TODO: I have created a discrete FR and Stimulus, now I need to perform the actual entropy calcs if True: raise Exception('This is not done')
def plot_calc(x,y,ax=None, xlim=[-4, 4], ylim=[-4,4]): if ax is None: f = plt.figure() ax = f.add_subplot(111) # correlation cor = np.corrcoef(x,y)[0,1] # information m = 8 qx = quantise(x, m, uniform='sampling')[0] qy = quantise(y, m, uniform='sampling')[0] s = DiscreteSystem(qx, (1,m), qy, (1,m)) s.calculate_entropies(method='plugin', calc=['HX','HXY']) I = s.I() # p-values? Nplot = 500 plotidx = np.random.permutation(len(x))[:Nplot] ax.scatter(x[plotidx],y[plotidx], s=5, edgecolors='none') #ax.set_title("r=%.1f I=%.2f" % (cor, I)) ax.set_xlim(xlim) ax.set_ylim(ylim) for sp in ['left','right','bottom','top']: ax.spines[sp].set_color('none') ax.xaxis.set_ticks_position('none') ax.yaxis.set_ticks_position('none') ax.set_xticklabels([]) if np.abs(cor)<1e-3: cor = 0.0 if np.isnan(cor): cor = '-' else: cor = "%.2f"%cor ax.text(0.2, 1, cor, horizontalalignment='center', verticalalignment='bottom', transform = ax.transAxes, color='#663300', fontsize=12, fontweight='bold') ax.text(0.8, 1, "%.2f"%I, horizontalalignment='center', verticalalignment='bottom', transform = ax.transAxes, color='#b20000', fontsize=12, fontweight='bold') return ax
def calc_ent(var,sp,use_flags,nbins=100,sigma=5*pq.ms): b = elephant.conversion.binarize(sp,sampling_rate=pq.kHz)[:-1] K = elephant.kernels.GaussianKernel(sigma=sigma) r = elephant.statistics.instantaneous_rate(sp,sampling_period=pq.ms,kernel=K).magnitude idx = np.logical_and(use_flags.ravel(), np.isfinite(var).ravel()) X = pye.quantise(var[idx],nbins,uniform='bins')[0].ravel() # Y = b[idx].astype('int').ravel() Y = r[idx].astype('int').ravel() S = pye.DiscreteSystem(X,(1,nbins),Y,(1,np.max(Y)+1)) S.calculate_entropies(calc=['HX','HY','HXY','SiHXi','HiX','HshX','HiXY','HshXY','ChiX']) return(S.H['HX'],S.H['HXY'],S.I(),S)
def getFitnessH(self, smMatrix): #Store the sm state into memory fit = 0 #Fitness function (1) ****************************************** #Mutual Information between two motors and the first prediced sensor dimension sp = smMatrix[1:,2] #Only predicts the first sensory outcome. mot1 = smMatrix[1:,6] mot2 = smMatrix[1:,7] spQ = ent.quantise(sp,10) motQ1 = ent.quantise(mot1,10) motQ2 = ent.quantise(mot2,10) # print(motQ1[0]) # print(motQ2[0]) motQ = np.row_stack((motQ1[0],motQ2[0])) # print(motQ) s = ent.DiscreteSystem(motQ,(2,10), spQ[0],(1,10)) print(str(spQ[0]) + "\n" + str(motQ)) s.calculate_entropies(method='pt', calc=['HX', 'HXY']) mutInf = s.I() fit = mutInf print(fit) return fit
def getFitnessH(self, smMatrix): #Store the sm state into memory fit = 0 #Fitness function (1) ****************************************** #Mutual Information between two motors and the first prediced sensor dimension sp = smMatrix[1:, 2] #Only predicts the first sensory outcome. mot1 = smMatrix[1:, 6] mot2 = smMatrix[1:, 7] spQ = ent.quantise(sp, 10) motQ1 = ent.quantise(mot1, 10) motQ2 = ent.quantise(mot2, 10) # print(motQ1[0]) # print(motQ2[0]) motQ = np.row_stack((motQ1[0], motQ2[0])) # print(motQ) s = ent.DiscreteSystem(motQ, (2, 10), spQ[0], (1, 10)) print(str(spQ[0]) + "\n" + str(motQ)) s.calculate_entropies(method='pt', calc=['HX', 'HXY']) mutInf = s.I() fit = mutInf print(fit) return fit
def quantize_TxN(z_TxN, M, uniform_code): #z2d_reshaped=z2d.reshape(z2d.size) #z2d_reshaped_q,bin_bounds, bin_centers = pyentropy.quantise(z2d_reshaped, M, uniform='bins') #z2d_q = z2d_reshaped_q.reshape(z2d.shape) #z2d: # [nlen x ntr] z_NxT=z_TxN.transpose() z_NxT_shape=z_NxT.shape #print z2d[0:10,0] # [ntr x nlen ] z_Arr=z_NxT.reshape([z_NxT.size]) #straighted. z2d.size=total elements #print z2d_reshaped[0:10] #if not trsnsposed, tr1,tr2,tr3, tr1,tr2,tr3 zq_Arr,bin_bounds, bin_centers = pyentropy.quantise(z_Arr, M, uniform=uniform_code) zq_NxT = zq_Arr.reshape(z_NxT_shape) return zq_NxT.transpose()
def _run(n, n_b, t, Iosc, f, g, Istim, Sstim, Ipri, dt, back_type, stim_seed=None): # rate = 1.0 / dt # -- SIM --------------------------------------------------------------------- # Init spikers backspikes = neurons.Spikes(n_b, t, dt=dt) pacspikes = neurons.Spikes(n, t, dt=dt, private_stdev=Ipri) drivespikes = neurons.Spikes(n, t, dt=dt, private_stdev=Ipri) times = pacspikes.times # brevity # -- # Create biases d_bias = {} if back_type == 'constant': d_bias['back'] = rates.constant(times, 2) elif back_type == 'stim': d_bias['back'] = rates.stim(times, Istim, Sstim, seed=stim_seed) else: raise ValueError("pac_type not understood") # Drive and osc d_bias['osc'] = rates.osc(times, Iosc, f) d_bias['stim'] = rates.stim(times, Istim, Sstim, seed=stim_seed) # PAC math d_bias['gain'] = d_bias['stim'] * (g * d_bias['osc']) d_bias['summed'] = d_bias['stim'] + (g * d_bias['osc']) d_bias['silenced'] = d_bias['stim'] - (g * d_bias['osc']) # -- # Simulate spiking # Create the background pool. b_spks = backspikes.poisson(d_bias['back']) # Create a non-PAC stimulus pattern for MI inqualities stim_sp = np.hstack([ drivespikes.poisson(d_bias['stim']), b_spks ]) # and then create PAC spikes. d_spikes = {} for k in d_bias.keys(): d_spikes[k + "_p"] = np.hstack([ pacspikes.poisson(d_bias[k]), b_spks ]) # -- LFP ------------------------------------------------------------------ d_lfps = {} for k in d_spikes.keys(): d_lfps[k] = lfp.create_synaptic_lfps(d_spikes[k]) stim_lfps = lfp.create_synaptic_lfps(stim_sp) # -- I -------------------------------------------------------------------- to_calc = ('HX', 'HY', 'HXY') m = 8 # Per Ince's advice d_infos = {} for k in d_lfps.keys(): stim_lfps_q, _, _ = en.quantise(stim_lfps, m) lfp_q, _, _ = en.quantise(d_lfps[k], m) d_infos[k] = en.DiscreteSystem( stim_lfps_q, (1, m), lfp_q, (1, m) ) d_infos[k].calculate_entropies(method='pt', calc=to_calc) # MI d_mis = {} for k, mi in d_infos.items(): d_mis[k] = mi.I() # H d_hs = {} for k, mi in d_infos.items(): d_hs[k] = mi.H # -- PAC ------------------------------------------------------------------ low_f = (f-2, f+2) high_f = (80, 250) d_pacs = {} for k in d_lfps.keys(): d_pacs[k] = pacfn(d_lfps[k], d_lfps[k], low_f, high_f) return { 'MI' : d_mis, 'H' : d_hs, 'PAC' : d_pacs, 'spikes' : d_spikes, 'times' : times }
def run(n, t, Iosc, f, Istim, Sstim, dt, k_spikes, excitability, pac_type='plv'): rate = 1.0 / dt # -- SIM --------------------------------------------------------------------- # Init spikers modspikes = pac.Spikes(n, t, dt=dt) drivespikes = pac.Spikes(n, t, dt=dt) times = modspikes.times # brevity # Create biases d_bias = {} d_bias['osc'] = pac.osc(times, Iosc, f) d_bias['stim'] = pac.stim(times, Istim, Sstim) d_bias['gain'] = d_bias['osc'] * d_bias['stim'] d_bias['gain_silenced'] = (d_bias['osc'] * d_bias['stim']) - d_bias['osc'] d_bias['summed'] = d_bias['osc'] + d_bias['stim'] d_bias['silenced'] = d_bias['stim'] - d_bias['osc'] # Simulate spiking # Create a fixed noiseless stimulus pattern, then.... stim_sp = drivespikes.poisson(d_bias['stim']) # study how different modulation schemes interact # with it d_spikes = {} d_spikes['drive_p'] = stim_sp for k in d_bias.keys(): d_spikes[k + "_p"] = modspikes.poisson(d_bias[k]) if k_spikes > 0: d_spikes['gain_bp'] = modspikes.poisson_binary( d_bias['stim'], d_bias['osc'], k=k_spikes, excitability=excitability ) # -- CREATE LFP -------------------------------------------------------------- d_lfps = {} for k in d_spikes.keys(): d_lfps[k] = lfp.create_synaptic_lfps(d_spikes[k]) # -- I ----------------------------------------------------------------------- to_calc = ('HX', 'HY', 'HXY') m = 20 # Per Ince's advice d_infos = {} for k in d_lfps.keys(): d_infos[k] = en.DiscreteSystem( en.quantise(d_lfps['drive_p'], m)[0], (1, m), en.quantise(d_lfps[k], m)[0], (1, m) ) d_infos[k].calculate_entropies(method='pt', calc=to_calc) # MI d_mis = {} for k, mi in d_infos.items(): d_mis[k] = mi.I() # H d_hs = {} for k, mi in d_infos.items(): d_hs[k] = mi.H # -- PAC OF LFP -------------------------------------------------------------- low_f = (f-2, f+2) high_f = (80, 250) method = pac_type filt = 'eegfilt' kwargs = {'trans' : .15} # for eegfilt d_pacs = {} for k in d_lfps.keys(): d_pacs[k] = scpac(d_lfps[k], low_f, high_f, rate, method, filt, **kwargs) return { 'MI' : d_mis, 'H' : d_hs, 'PAC' : d_pacs, 'spikes' : d_spikes }
# extract result x_in = np.asarray([stim(t, 0) for t in times]) x_e = ys[:, idx['r_E']].flatten() # drop burn in time x_in = x_in[times > drop_before] x_e = x_e[times > drop_before] # normalize x_in = norm(x_in) x_e = norm(x_e) # prep MI variables to_calc = ('HX', 'HY', 'HXY') q_in, _, _ = en.quantise(x_in, m) q_e, _, _ = en.quantise(x_e, m) # MI info = en.DiscreteSystem(q_in, (1, m), q_e, (1, m)) info.calculate_entropies(method='pt', calc=to_calc) mi_no = info.I() # --- ys, layers, phi, rate, stim, params = run(None, times, pac, sigma=s, loc=loc, stim_seed=j)
#!/usr/bin/env python import numpy as np import pyentropy from pyentropy import DiscreteSystem import matplotlib.pyplot as plt N = 10 Nq = 50 Nx = 1 noise = 1 ntrials = 10 for trial in xrange(ntrials): x = N * np.random.rand(Nx, 10000) y = np.zeros(10000) xq = np.empty((Nx, 10000), dtype=int) for n in range(Nx): xq[n, :] , _, _ = pyentropy.quantise(x[n, :], Nq) y += x[n, :] y += noise*np.random.randn(10000) yq, _, _ = pyentropy.quantise(y, Nq) s = DiscreteSystem(xq, (Nx, Nq), yq, (1, Nq)) s.calculate_entropies(method='plugin', calc=['HX', 'HXY']) print(s.I()) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(xq, yq, s=2) ax.set_title('MI: %s' % s.I()) fig.savefig('mi_%d.png' % trial) plt.close()