def loaddframebyarband (lcol,skipbipolar=True,skipcsd=False,FoctTH=1.5,ERPscoreTH=0.8,ERPDurTH=[75,300]): # loads the pandas data frames split up by frequency band lar = ['A1', 'Thal'] based = 'data/nhpdat/spont/oscout/' ddf = {'A1':{'s2':{},'g':{},'i1':{}},'Thal':{'Th':{}}} for ar,lschan in zip(lar,[['s2','g','i1'],['Th']]): for schan in lschan: for b in lband: ddf[ar][schan][b]={k:[] for k in lcol} for ar in lar: for fn in os.listdir(based+ar): if getorigsampr('data/nhpdat/spont/'+ar+'/'+fn.split('_')[0]) != 44e3: continue if not fn.endswith('dframe.pkl'): continue if skipbipolar and fn.count('bipolar_True')>0: continue if skipcsd and fn.count('bipolar_False')>0: continue df = pickle.load(open(based+ar+'/'+fn,'rb')) print(fn) lchan = list(set(df['chan'])) lchan.sort() if ar == 'A1': s2,g,i1 = lchan lschan = ['s2','g','i1'] else: th = lchan[0] lschan = ['Th'] for band in lband: for chan,schan in zip(lchan,lschan): dfs = df[(df.band==band) & (df.Foct<FoctTH) & (df.chan==chan) & ((df.ERPscore<ERPscoreTH)|(df.dur<ERPDurTH[0])|(df.dur>ERPDurTH[1]))] for k in lcol: lx = dfs[k] for x in lx: ddf[ar][schan][band][k].append(x) return ddf
def avgERPOverChan(dd, noiseth=0.75): from nhpdat import getflayers, getdownsampr, getorigsampr, getTriggerIDs, closestfile, hasBBNStim, IsCortex, IsThal from nhpdat import getStimIntensity, getTriggerTimes ddx = {'s2': [], 'g': [], 'i1': [], 'tt': None} for k in dd: if getorigsampr('data/nhpdat/bbn/' + k) != 44000.0: continue ddf = dd[k] lsc = ddf['lchan'] for idx, c in enumerate([ddf[lsc[0]], ddf[lsc[1]], ddf[lsc[2]]]): if max(abs(ddf['avgCSD'][c, :])) > noiseth: continue ddx[lsc[idx]].append(ddf['avgCSD'][c, :]) if ddx['tt'] is None: ddx['tt'] = ddf['ttavg'] for c in lsc: ddx[c + 'avg'] = mean(np.array(ddx[c]), axis=0) s = std(np.array(ddx[c]), axis=0) s /= sqrt(len(ddx[c])) ddx[c + 'stderr'] = s return ddx
def loaddlcoh (lband = ['delta','theta','alpha','beta','gamma','hgamma'], skipbipolar = True,\ ar='A1', bdir='data/nhpdat/spont/laggedcoh/A1',origdir='data/nhpdat/spont/A1/',lschan=['s2','g','i1']): # loads lagged coherence output into dictionaries from nhpdat import getorigsampr ddlcoh = {} ddlcoh[ar] = {} lfn = os.listdir(bdir) for fn in lfn: if skipbipolar and fn.count('bipolar_True') > 0: continue origfn = origdir+fn.split('_')[0] if ar == 'A1' and getorigsampr(origfn) < 44e3: continue if fn.endswith('.pkl'): ddlcoh[ar][fn] = pickle.load(open(bdir+'/'+fn,'rb')) dlcoh = {ar:{schan:{} for schan in lschan}} for c in lschan: for b in lband: dlcoh[ar][c][b]=[] for k in ddlcoh[ar].keys(): for chan,schan in zip(ddlcoh[ar][k].keys(),lschan): for b in lband: for x in ddlcoh[ar][k][chan][b]: dlcoh[ar][schan][b].append(x) return ddlcoh,dlcoh
def getAvgERPInDir (based, stimIntensity, needBBN, needCX, needThal,\ swindowms=0, ewindowms=150, dbpath='data/nhpdat/spont/A1/19apr4_A1_spont_LayersForSam.csv', useBIP=False): from nhpdat import getflayers, getdownsampr, getorigsampr, getTriggerIDs, closestfile, hasBBNStim, IsCortex, IsThal from nhpdat import getStimIntensity, getTriggerTimes dd = {} for fn in os.listdir(based): if not fn.endswith('.mat'): continue FN = os.path.join(based, fn) if stimIntensity > 0 and getStimIntensity(FN) != stimIntensity: continue if needBBN and not hasBBNStim(FN): continue if needCX and not IsCortex(FN): continue if needThal and not IsThal(FN): continue s2, g, i1 = -1, -1, -1 lchan = [] if IsCortex(FN): s2, g, i1 = getflayers(closestfile(fn, dbpath=dbpath)[0], abbrev=True) if s2 < 0: continue # no layer/channel information lchan = [s2, g, i1] samprds = getdownsampr(FN) divby = getorigsampr(FN) / samprds trigtimes = [ int(round(x)) for x in np.array(getTriggerTimes(FN)) / divby ] trigIDs = getTriggerIDs(FN) if useBIP: sampr, dat, dt, tt, CSD, MUA, BIP = loadfile(FN, samprds, getbipolar=True) else: sampr, dat, dt, tt, CSD, MUA = loadfile(FN, samprds) ttrigtimes = [index2ms(t, sampr) for t in trigtimes] if useBIP: ttavg, avgBIP = getAvgERP(BIP, sampr, trigtimes, swindowms, ewindowms) ddf = {'fn': fn, 'ttavg': ttavg, 'avgBIP': avgBIP, 'sampr': sampr} else: ttavg, avgCSD = getAvgERP(CSD, sampr, trigtimes, swindowms, ewindowms) ddf = {'fn': fn, 'ttavg': ttavg, 'avgCSD': avgCSD, 'sampr': sampr} print(fn, lchan) if s2 >= 0: if useBIP: s2 += 1 g += 1 i1 += 1 ddf['s2'] = s2 ddf['g'] = g ddf['i1'] = i1 else: th = int(CSD.shape[0] / 2) lchan = [th] if useBIP: th += 1 ddf['th'] = th if useBIP: ddf['lchan'] = [x + 1 for x in lchan] else: ddf['lchan'] = lchan dd[fn] = ddf return dd