|
| 1 | +import cfg |
| 2 | +import h5py |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +from tqdm import tqdm |
| 6 | + |
| 7 | +pd.options.mode.chained_assignment = None # to be fixed |
| 8 | + |
| 9 | + |
| 10 | +rat_auto = pd.DataFrame() |
| 11 | +rat = pd.read_hdf(cfg.velocity_fname, 'Rat_Behavior').set_index('index') |
| 12 | +stim = pd.read_hdf(cfg.relational_fname, 'Events').set_index('index') |
| 13 | +stim.drop(labels=['MotiveExpTimeSecs'], axis=1, inplace=True) |
| 14 | + |
| 15 | +rat_s = pd.merge(rat, stim, on=['Frame', 'Time', 'session_id']) |
| 16 | +rat_sf = rat_s[rat_s.Filtered == 1] |
| 17 | + |
| 18 | +limits = pd.DataFrame(cfg.LIMITS) |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +# create limit columns |
| 23 | +rat_sf['Dmin'], rat_sf['Dmax'] = 0, 0 |
| 24 | +rat_sf['change'] = False |
| 25 | +for index, row in tqdm(limits.iterrows()): |
| 26 | + rat_sf['change'] = (rat_sf['Dmin']==0) & (rat_sf['speed']==row['speed']) |
| 27 | + rat_sf['Dmin'] = np.where(rat_sf['change']==True, row['min'], rat_sf['Dmin']) |
| 28 | + rat_sf['Dmax'] = np.where(rat_sf['change']==True, row['max'], rat_sf['Dmax']) |
| 29 | + |
| 30 | +rat_sf['SRB'] = (rat_sf['U'] > rat_sf['Dmin']) & (rat_sf['U'] < rat_sf['Dmax']) |
| 31 | + |
| 32 | +# dropping unused columns |
| 33 | +rat_sf.drop(['Filtered', 'change'], axis=1, inplace=True) |
| 34 | + |
| 35 | + |
| 36 | +# MERGING SRB THAT HAVE REALLY SHORT BREAKS |
| 37 | +rat_up2 = pd.DataFrame() |
| 38 | +for name, dd in rat_sf.groupby('session_id'): |
| 39 | + # calculating dtime - time difference between srb |
| 40 | + dfSRBtemp = dd[dd['SRB']==True] |
| 41 | + dfSRBtemp['dtime'] = dfSRBtemp['Time'].diff(1) |
| 42 | + df2 = pd.merge(dd, dfSRBtemp[['dtime', 'Frame', 'session_id','Time']], on=['Frame', 'session_id','Time'], how='outer') |
| 43 | + df2.fillna(0, inplace=True) |
| 44 | + |
| 45 | + #special time cases |
| 46 | + dftemp = dfSRBtemp[(dfSRBtemp['dtime']>0.005) & (dfSRBtemp['dtime']<0.1)] |
| 47 | + dftemp = dftemp.reset_index(drop=True) |
| 48 | + |
| 49 | + maxF, minF = {}, {} |
| 50 | + minF={} |
| 51 | + df2['SRBall'] = df2['SRB'] |
| 52 | + df2['SRBtt'] = False |
| 53 | + # merge super close events |
| 54 | + for i in np.arange(0,len(dftemp)): |
| 55 | + maxF[i] = dftemp.Time[i] |
| 56 | + minF[i] = maxF[i]-dftemp.dtime[i] |
| 57 | + |
| 58 | + df2['SRBtt'] = (df2['Time']>minF[i]) & (df2['Time']<maxF[i]) | (df2['SRBtt']==True) |
| 59 | + df2['SRBall'] = (df2['SRBtt']==True) | (df2['SRBall']==True) |
| 60 | + |
| 61 | + |
| 62 | + df2['dtimeA'] = df2['dtime'] |
| 63 | + df2.drop('dtime', inplace=True, axis=1) |
| 64 | + |
| 65 | + # DELETING SHORT SRB |
| 66 | + # calculating dtime - time of srb |
| 67 | + dfSRBtemp2 = df2[df2['SRBall']==False] |
| 68 | + dfSRBtemp2['dtime'] = dfSRBtemp2['Time'].diff(1) |
| 69 | + df22 = pd.merge(df2, dfSRBtemp2[['dtime', 'Frame', 'session_id','Time']], on=['Frame', 'session_id','Time'], how='outer') |
| 70 | + df22.fillna(0, inplace=True) |
| 71 | + |
| 72 | + #special time cases |
| 73 | + dftemp2 = dfSRBtemp2[(dfSRBtemp2['dtime']<0.4) & (dfSRBtemp2['dtime']>0.0049)] |
| 74 | + dftemp2 = dftemp2.reset_index(drop=True) |
| 75 | + maxF2, minF2 = {}, {} |
| 76 | + df22['SRBall2'] = df22['SRBall'] |
| 77 | + df22['SRBtt2'] = False |
| 78 | + |
| 79 | + # delete short events |
| 80 | + for i in np.arange(0,len(dftemp2)): |
| 81 | + maxF2[i] = dftemp2.Time[i] |
| 82 | + minF2[i] = dftemp2.Time[i] - dftemp2.dtime[i] |
| 83 | + df22['SRBtt2'] = ((df22['Time']>=minF2[i]) & (df22['Time']<=maxF2[i])) | (df22['SRBtt2']==True) |
| 84 | + df22['SRBall2'] = (df22['SRBtt2']==False) & (df22['SRBall2'] == True) |
| 85 | + |
| 86 | + rat_auto = pd.concat([rat_auto, df22], axis=0, ignore_index=True) |
| 87 | + |
| 88 | + |
| 89 | +with h5py.File(cfg.SRBauto_fname, 'w') as f: |
| 90 | + f.create_dataset('Rat_Behavior', data=rat_auto.to_records()) |
0 commit comments