def checkValidityH(A,B): print "A",A.round(2) print "B",B.round(2) Flags = [] Flags.append("SWA") ipctx=dict() ipctx["ip"]=np.zeros((1,2001)) delay = 1 SWARates = psGA.calcRates(Flags,delay,A,B,False,ipctx,iptau=p.params["iptau"]) Flags = [] Flags.append("Act") ActRates = psGA.calcRates(Flags,delay,A,B,False,ipctx,iptau=p.params["iptau"]) Flags = [] Flags.append("Trans") Rates = psGA.calcRates(Flags,delay,A,B,False,ipctx,iptau=p.params["iptau"],ipamp=5.) tests = fs.checkCondsH(SWARates,ActRates) Grades = np.sum(tests) if Grades == 10: print "Valid" else: print "Invalid" print "Grades",Grades return SWARates, ActRates, Rates,Grades
def checkValidityH(A, B): print "A", A.round(2) print "B", B.round(2) paramComb = dict() for i in xrange(len(terms)): if i < 16: paramComb[terms[i]] = A[ids[i][0]][ids[i][1]] else: paramComb[terms[i]] = B[0, ids[i][0]] print "paramComb", paramComb Flags = [] Flags.append("SWA") ipctx = dict() ipctx["ip"] = np.zeros((1, 2001)) delay = 1 SWARates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"]) Flags = [] Flags.append("Act") ActRates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"]) Flags = [] Flags.append("Trans") Rates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"], ipamp=5.) tests = fs.checkCondsH(SWARates, ActRates) Grades = np.sum(tests) if Grades == 10: print "Valid" else: print "Invalid" print "Grades", Grades return SWARates, ActRates, Rates, Grades
def checkValidityPD(A, B): print "A", A.round(2) print "B", B.round(2) paramComb = dict() for i in xrange(len(terms)): if i < 16: paramComb[terms[i]] = A[ids[i][0]][ids[i][1]] else: paramComb[terms[i]] = B[0, ids[i][0]] Flags = [] Flags.append("SWA") ipctx = dict() ipctx["ip"] = np.zeros((1, 2001)) delay = 1 SWADopDepRates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"]) Flags = [] Flags.append("Act") ActDopDepRates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"]) Flags = [] Flags.append("Trans") TransDopDepRates = psGA.calcRates(Flags, delay, A, B, False, ipctx, iptau=p.params["iptau"], ipamp=5.) # Checks Refer Figure 6I and 6J of Mallet 2008 - All the below firing rates are from Abdi 2015 tests = fs.checkCondsPD(SWADopDepRates, ActDopDepRates) Grades = np.sum(tests) if Grades == 13: print "Valid" else: print "Invalid" print "Grades", Grades return SWADopDepRates, ActDopDepRates, TransDopDepRates, Grades
def analFFT(): randSamp = np.random.randint(0, len(AllAs), 1000) fftSpec = dict() ipctx1 = dict() ipctx1["ip"] = np.zeros((1, 1001)) dt = p.params["dt"] fftSpec["samps"] = randSamp ampSpec = [] fftfreq = np.fft.fftfreq( int(len(ipctx1["ip"][0]) / dt) )[:(int(len(ipctx1["ip"][0]) / dt)) / 2] # Had to calculate on local machine, since this version of numpy (1.7.2) doesnt have np.fft.rfftfreq inds = np.where(fftfreq * 1000 * 100 <= 400)[0] Flags = [] Flags.append("TransFreq") for i, samp in enumerate(randSamp): #temp = np.zeros((8,len(freqs),len(fftfreq)/10+1)) temp = np.zeros((8, len(inds))) A = AllAs[samp] B = AllBs[samp] Rates = psGA.calcRates(Flags, 1.0, A, B, False, ipctx1, iptau=p.params["iptau"]) for k, nuc in enumerate(terms1): ffttemp = np.fft.rfft(Rates[nuc] - np.mean(Rates[nuc])) #temp[k][:] = (np.abs(ffttemp)/np.max(np.abs(ffttemp)))[inds] temp[k][:] = np.abs(ffttemp)[inds] ampSpec.append(temp) fftSpec["ampSpec"] = ampSpec pickle.dump(fftSpec, open(path5 + "fftSpec.pickle", "w"))
def calcMean(): pathname = storage_home + '/output/' print "in calcMean" knownparams = p.params["known"] leak = -0.05 Supressed = [] Sup_Ind = [] k = 0 #print Allcombs #PDFeatures = np.zeros((len(Allcombs),2)) # 0= akinesia, 1,2 = gpi amplitude spectrum,dominant frequency, 3,4 = TA amplitude,frequency, 5,6=TI amplitude, frequency # Samples = np.arange(0,len(Allcombs),1) # np.random.shuffle(Samples) #randSamps = pickle.load(open(pathname+"randSamps.pickle","r")) Allcombs = pickle.load(open(pathname + "uniqueCombs.pickle", "r")) randSamps = np.random.randint(0, len(Allcombs), 1000) MeansSWA = [] MeansAct = [] MeansSpon = [] tempSWA = [] #tempSWATI=[] tempAct = [] tempTrans = [] for i, j in enumerate(randSamps): print i ind = Allcombs[j] d1ta = ind[0] d2ta = ind[1] fsita = ind[2] fsiti = ind[3] tata = ind[4] tati = ind[5] tastn = ind[6] tita = ind[7] titi = ind[8] tistn = ind[9] stnta = ind[10] stnti = ind[11] tid2 = ind[12] tad2 = ind[13] d1ti = ind[14] d2ti = ind[15] jc1 = ind[16] jc2 = ind[17] jfsictx = ind[18] jstnctx = ind[19] #A = np.matrix([[knownparams['d1d1'],knownparams['d1d2'],knownparams['d1fsi'],d1ta,knownparams['d1ti'],0.,0.],[knownparams['d2d1'],knownparams['d2d2'],knownparams['d2fsi'],d2ta,knownparams['d2ti'],0.,0.],[0.,0.,knownparams['fsifsi'],fsita,fsiti,0.,0.],[0,tad2,0.,tata,tati,tastn,0],[0.,tid2,0.,tita,titi,tistn,0.],[0.,0.,0.,knownparams['stnta'],stnti,knownparams['stnstn'],0.],[knownparams['gpid1'],0.,0.,knownparams['gpita'],knownparams['gpiti'],knownparams['gpistn'],knownparams['gpigpi']]]) A = np.matrix([[ knownparams['d1d1'], knownparams['d1d2'], knownparams['d1fsi'], d1ta, d1ti, 0., 0. ], [ knownparams['d2d1'], knownparams['d2d2'], knownparams['d2fsi'], d2ta, d2ti, 0., 0. ], [0., 0., knownparams['fsifsi'], fsita, fsiti, 0., 0.], [0, tad2, 0., tata, tati, tastn, 0], [0., tid2, 0., tita, titi, tistn, 0.], [0., 0., 0., stnta, stnti, knownparams['stnstn'], 0.], [ knownparams['gpid1'], 0., 0., knownparams['gpita'], knownparams['gpiti'], knownparams['gpistn'], knownparams['gpigpi'] ]]) B = np.matrix([jc1, jc2, jfsictx, 0, 0, jstnctx, 0]) delay = 1.0 #Calculate Rates for SWA and Control ipctx1 = dict() #Calculate Rates for SWA and lesion(dopamine depletion) Flags = [] Flags.append("SWA") ipctx1["ip"] = np.zeros((1, 2001)) SWADopDepRates = psGA.calcRates(Flags, delay, A, B, False, ipctx1, iptau=p.params["iptau"]) tempSWA.append([ np.mean(SWADopDepRates["d1"]), np.mean(SWADopDepRates["d2"]), np.mean(SWADopDepRates["fsi"]), np.mean(SWADopDepRates["ta"]), np.mean(SWADopDepRates["ti"]), np.mean(SWADopDepRates["stn"]), np.mean(SWADopDepRates["gpi"]) ]) Flags = [] Flags.append("Act") ipctx1["ip"] = np.zeros((1, 2001)) ActDopDepRates = psGA.calcRates(Flags, delay, A, B, False, ipctx1, iptau=p.params["iptau"]) tempAct.append([ np.mean(ActDopDepRates["d1"]), np.mean(ActDopDepRates["d2"]), np.mean(ActDopDepRates["fsi"]), np.mean(ActDopDepRates["ta"]), np.mean(ActDopDepRates["ti"]), np.mean(ActDopDepRates["stn"]), np.mean(ActDopDepRates["gpi"]) ]) Flags = [] Flags.append("Trans") ipctx1["ip"] = np.zeros((1, 2001)) TransRates = psGA.calcRates(Flags, delay, A, B, False, ipctx1, iptau=p.params["iptau"], ipamp=0.0) tempTrans.append([ np.mean(TransRates["d1"]), np.mean(TransRates["d2"]), np.mean(TransRates["fsi"]), np.mean(TransRates["ta"]), np.mean(TransRates["ti"]), np.mean(TransRates["stn"]), np.mean(TransRates["gpi"]) ]) MeansSWA.append(tempSWA) MeansAct.append(tempAct) MeansSpon.append(tempTrans) Means = dict() Means["SWA"] = MeansSWA Means["Act"] = MeansAct Means["Trans"] = MeansSpon pickle.dump(Means, open(pathname + "MeansTight.pickle", "w"))
def classification(params): pathname = storage_home+'/output/' print "in classification" knownparams = p.params["known"] leak = -0.05 Supressed=[] Sup_Ind=[] k = 0 AllAs = pickle.load(open(pathname+"AllAs.pickle","r")) AllBs = pickle.load(open(pathname+"AllBs.pickle","r")) randSamps = np.random.randint(0,len(AllAs),1000) # 0= GS, 1,2 = gpi SO,dominant frequency, 3,4 = TA SO,frequency, 5,6=TI SO, frequency PDFeatures = np.ones((len(randSamps),8)) maxFreq = np.zeros((len(randSamps),3)) perPowerMaxFreq = np.zeros((len(randSamps),3)) ipAmp = params["ipAmp"] print ipAmp baseLineFR = [] for i,j in enumerate(randSamps): print i A = AllAs[j] B = AllBs[j] delay = 1.0 ipctx1=dict() Flags = [] Flags.append("Trans") ipctx1["ip"] = np.zeros((1,2001)) SWADopDepRates = psGA.calcRates(Flags,delay,A,B,False,ipctx1,ipAmp,iptau=p.params["iptau"]) # Record the ratio of Gpi rates between 500-1000/Rates before 500 dt = p.params["dt"] howmuch = (np.mean(SWADopDepRates['gpi'][100/dt:500/dt]) -np.mean(SWADopDepRates['gpi'][600/dt:1400/dt]))/np.mean(SWADopDepRates['gpi'][100/dt:500/dt]) # (Orig - Final)/Orig PDFeatures[i][0] = howmuch # New features Flags=[] Flags.append("Trans") ipctx1["ip"] = np.zeros((1,2001)) scale=1 TransRates = psGA.calcRates(Flags,delay,A*scale,B*scale,False,ipctx1,ipAmp,iptau=p.params["iptau"]) if ipAmp == 0: temp = dict() for x in terms1: temp[x] = np.mean(TransRates[x]) baseLineFR.append(temp) noiseLim = 0.5 timeStart = 510 timeStart2 = 520 timeEnd1 = 1000 timeEnd2 = 650 timeEnd3 = 900 # An alternatiev idea to SE could be , percentage of power contained in beta-band (periodogram)- because you require some value for susceptibility to oscillations which varies between 0 and 1 noise = np.random.uniform(-noiseLim,noiseLim,len(TransRates['gpi'][timeStart/dt:timeEnd1/dt])) time = np.arange(0,len(noise),1) wind = np.exp(-time/10.) noise1 = np.convolve(noise,wind,mode='same') ind = np.where(noise1<0) noise1[ind] = 0. se11,dfreq11,maxFreq11,perMax11 = spec_entropy(TransRates['stn'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00009,0.00017]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft se12,dfreq12,maxFreq12,perMax12 = spec_entropy(TransRates['stn'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00007,0.00017]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft se13,dfreq13,maxFreq13,perMax13 = spec_entropy(TransRates['stn'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00017,0.00036]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft se14,dfreq14,maxFreq14,perMax14 = spec_entropy(TransRates['stn'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00017,0.00036]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft se15,dfreq15,maxFreq15,perMax15 = spec_entropy(TransRates['stn'][timeStart2/dt:timeEnd3/dt],time_range=TransRates["ipctx"][timeStart2/dt:timeEnd3/dt],freq_range=[0.00017,0.00036]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft se16,dfreq16,maxFreq16,perMax16 = spec_entropy(TransRates['stn'][timeEnd1/dt:(timeEnd1+480.)/dt],time_range=TransRates["ipctx"][timeEnd1/dt:(timeEnd1+500.)/dt],freq_range=[0.0001,0.00025]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft , If the oscillation occurs at the end of the stimulation period ans = np.array([se11,se12,se13,se14,se15,se16]) indnan = np.where(np.isnan(ans)==True) ans[indnan] = 1 print ans indmin = np.where(ans==np.min(ans))[0] print indmin PDFeatures[i][2] = np.min(ans) se21,dfreq21,maxFreq21,perMax21 = spec_entropy(TransRates['ta'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00009,0.00017]) se22,dfreq22,maxFreq22,perMax22 = spec_entropy(TransRates['ta'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00007,0.00017]) se23,dfreq23,maxFreq23,perMax23 = spec_entropy(TransRates['ta'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00017,0.00036]) se24,dfreq24,maxFreq24,perMax24 = spec_entropy(TransRates['ta'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00017,0.00036]) se25,dfreq25,maxFreq25,perMax25 = spec_entropy(TransRates['ta'][timeStart2/dt:timeEnd3/dt],time_range=TransRates["ipctx"][timeStart2/dt:timeEnd3/dt],freq_range=[0.00017,0.00036]) se26,dfreq26,maxFreq26,perMax26 = spec_entropy(TransRates['ta'][timeEnd1/dt:(timeEnd1+480.)/dt],time_range=TransRates["ipctx"][timeEnd1/dt:(timeEnd1+500.)/dt],freq_range=[0.0001,0.00025]) # 0.00010 = 10 Hz, since resolution = 10^-5 sec, 1000msec, and dt = 0.01 , to better the resolution of frequency captured by fft , If the oscillation occurs at the end of the stimulation period ans = np.array([se21,se22,se23,se24,se25,se26]) indnan = np.where(np.isnan(ans)==True) ans[indnan] = 1 print ans indmin = np.where(ans==np.min(ans))[0] print indmin PDFeatures[i][3] = np.min(ans) noise = np.random.uniform(-noiseLim,noiseLim,len(TransRates['ti'][timeStart/dt:timeEnd1/dt])) se31,dfreq31,maxFreq31,perMax31 = spec_entropy(TransRates['ti'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00009,0.00017]) se32,dfreq32,maxFreq32,perMax32 = spec_entropy(TransRates['ti'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00007,0.00017]) se33,dfreq33,maxFreq33,perMax33 = spec_entropy(TransRates['ti'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00017,0.00036]) se34,dfreq34,maxFreq34,perMax34 = spec_entropy(TransRates['ti'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00017,0.00036]) se35,dfreq35,maxFreq35,perMax35 = spec_entropy(TransRates['ti'][timeStart2/dt:timeEnd3/dt],time_range=TransRates["ipctx"][timeStart2/dt:timeEnd3/dt],freq_range=[0.00017,0.00036]) se36,dfreq36,maxFreq36,perMax36 = spec_entropy(TransRates['ti'][timeEnd1/dt:(timeEnd1+480.)/dt],time_range=TransRates["ipctx"][timeEnd1/dt:(timeEnd1+500.)/dt],freq_range=[0.0001,0.00025]) ans = np.array([se31,se32,se33,se34,se35,se36]) indnan = np.where(np.isnan(ans)==True) ans[indnan] = 1 print ans indmin = np.where(ans==np.min(ans))[0] print indmin PDFeatures[i][5] = np.min(ans) se41,dfreq41,maxFreq41,perMax41 = spec_entropy(TransRates['gpi'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00009,0.00017]) se42,dfreq42,maxFreq42,perMax42 = spec_entropy(TransRates['gpi'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00007,0.00017]) se43,dfreq43,maxFreq43,perMax43 = spec_entropy(TransRates['gpi'][timeStart/dt:timeEnd1/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd1/dt],freq_range=[0.00017,0.00036]) se44,dfreq44,maxFreq44,perMax44 = spec_entropy(TransRates['gpi'][timeStart/dt:timeEnd2/dt],time_range=TransRates["ipctx"][timeStart/dt:timeEnd2/dt],freq_range=[0.00017,0.00036]) se45,dfreq45,maxFreq45,perMax45 = spec_entropy(TransRates['gpi'][timeStart2/dt:timeEnd3/dt],time_range=TransRates["ipctx"][timeStart2/dt:timeEnd3/dt],freq_range=[0.00017,0.00036]) se46,dfreq46,maxFreq46,perMax46 = spec_entropy(TransRates['gpi'][timeEnd1/dt:(timeEnd1+480.)/dt],time_range=TransRates["ipctx"][timeEnd1/dt:(timeEnd1+500.)/dt],freq_range=[0.0001,0.00025]) ans = np.array([se41,se42,se43,se44,se45,se46]) indnan = np.where(np.isnan(ans)==True) ans[indnan] = 1 print ans indmin = np.where(ans==np.min(ans))[0] print indmin PDFeatures[i][7] = np.min(ans) if i%250==0: pickle.dump(PDFeatures,open(pathname1+"PDFeaturesnew_"+str(ipAmp)+"_"+str(i)+".pickle","w")) pickle.dump(baseLineFR,open(pathname1+"baseLineFR_"+str(ipAmp)+".pickle","w")) pickle.dump(PDFeatures,open(pathname1+"PDFeaturesnew_"+str(ipAmp)+".pickle","w")) pickle.dump(perPowerMaxFreq,open(pathname1+"PercentageInMaxPower_"+str(ipAmp)+".pickle","w")) pickle.dump(maxFreq,open(pathname1+"maxFreq_"+str(ipAmp)+".pickle","w"))
def calcPhase(): ipctx1 = dict() ipctx1["ip"] = np.zeros((1, 2001)) dt = p.params["dt"] randSamp = np.random.randint(0, len(AllAs), 1000) # Phases of three nuclei phasMus = dict() Flags = [] Flags.append("SWA") phasTAvsTA = [] phasTIvsSTN = [] phasTAvsSTN = [] for i, samp in enumerate(randSamp): #temp = np.zeros((8,len(freqs),len(fftfreq)/10+1)) A = AllAs[samp] B = AllBs[samp] Rates = psGA.calcRates(Flags, 1.0, A, B, False, ipctx1, iptau=p.params["iptau"]) # Filter the signals first at SWA frequency, or the phase difference between cortical input signal and TA,TI not calculated correctly due to multiple frequencies in the signal #B, A = sciSig.butter(2,np.array([0.0001,0.0005]),btype='low') B, A = sciSig.butter(2, np.array([0.00005]), btype='low') taFilt = sciSig.filtfilt(B, A, Rates['ta']) #, padlen=150) tiFilt = sciSig.filtfilt(B, A, Rates['ti']) #, padlen=150) stnFilt = sciSig.filtfilt(B, A, Rates['stn']) #, padlen=150) tG = np.arange(0, len(ipctx1["ip"][0]) + dt, dt) fftfreq = np.fft.fftfreq(len(taFilt), d=dt) fftta = np.fft.rfft(taFilt - np.mean(taFilt)) fftti = np.fft.rfft(tiFilt - np.mean(tiFilt)) fftstn = np.fft.rfft(stnFilt - np.mean(stnFilt)) fftip = np.fft.rfft(Rates['ipctx'] - np.mean(Rates['ipctx'])) maxta = np.where(np.abs(fftta) == np.max(np.abs(fftta)))[0] maxti = np.where(np.abs(fftti) == np.max(np.abs(fftti)))[0] maxstn = np.where(np.abs(fftstn) == np.max(np.abs(fftstn)))[0] maxip = np.where(np.abs(fftip) == np.max(np.abs(fftip)))[0] print "maxta,maxti,maxstn,maxip", maxta, maxti, maxstn, maxip phasTAvsTA.append(np.angle(fftta[maxta] / fftta[maxta])) phasTIvsSTN.append( np.mean([ np.angle(fftti[maxti] / fftstn[maxti]), np.angle(fftti[maxstn] / fftstn[maxstn]) ])) phasTAvsSTN.append( np.mean([ np.angle(fftta[maxta] / fftstn[maxta]), np.angle(fftta[maxstn] / fftstn[maxstn]) ])) phasMus["ta_ta"] = phasTAvsTA phasMus["stn_ti"] = phasTIvsSTN phasMus["stn_ta"] = phasTAvsSTN pickle.dump(phasMus, open(path5 + "Phases.pickle", "w"))