In [1]:
%matplotlib inline

In [2]:
import numpy as np, pandas as pd
import matplotlib.pyplot as plt
from scipy import signal

In [3]:
resp = pd.read_csv('sampleData/run1/1484601268080.txt', header=None)
resp.columns = ['time', 'resp', 'tone']

In [4]:
bci = pd.read_csv('sampleData/run1/OpenBCI-RAW-2017-01-16_16-12-31-oddball.txt', sep=', ', skiprows=4, header=None)
chan_columns = [str(i) for i in range(1,9)]
bci.columns = ['tix'] + chan_columns + ['a', 'b', 'c', 'time']


/home/mike/ve/erp/lib/python3.5/site-packages/ipykernel/__main__.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators (separators > 1 char and different from '\s+' are interpreted as regex); you can avoid this warning by specifying engine='python'.
  if __name__ == '__main__':

In [5]:
resp.sort_values('time', inplace=True)

In [6]:
t0 = bci.loc[0, 'time'] 
tmid_bci = bci.loc[len(bci) // 2, 'time']
tmid_resp = resp.loc[len(resp) // 2, 'time']
delta_t = tmid_resp-tmid_bci
print(tmid_bci, tmid_resp, delta_t, t0)

resp['time'] += tmid_bci-tmid_resp # stupid hack to get the times to line up right
resp['time'] -= t0 
bci['time'] -= t0


1.48460121965e+12 1484601256426 36776.0 1.48460116664e+12

In [7]:
tonef = resp['tone'].replace({'C4':1, 'C5':2, 'C3': 0})
resp['tonef'] = pd.Series(tonef, dtype='float64')
# resp['tonef'].dtype

In [8]:
resp.dtypes


Out[8]:
time     float64
resp       int64
tone      object
tonef    float64
dtype: object

In [9]:
resp


Out[9]:
time resp tone tonef
0 -236582.0 0 C5 2.0
1 -235082.0 0 C4 1.0
2 -233582.0 0 C4 1.0
3 -232082.0 0 C4 1.0
4 -230582.0 0 C4 1.0
5 -229082.0 0 C5 2.0
6 -227582.0 0 C4 1.0
7 -226082.0 0 C4 1.0
8 -224582.0 0 C4 1.0
9 -223082.0 0 C4 1.0
10 -221582.0 0 C5 2.0
11 -220082.0 0 C4 1.0
12 -218582.0 0 C4 1.0
13 -217082.0 0 C5 2.0
14 -215582.0 0 C4 1.0
15 -214082.0 0 C3 0.0
16 -984.0 0 C4 1.0
60 -404.0 2 0 0.0
17 514.0 0 C4 1.0
61 1004.0 2 0 0.0
18 2014.0 0 C4 1.0
62 2512.0 2 0 0.0
19 3514.0 0 C4 1.0
63 4006.0 2 0 0.0
20 5014.0 0 C4 1.0
64 5559.0 2 0 0.0
21 6514.0 0 C4 1.0
65 7026.0 2 0 0.0
22 8014.0 0 C5 2.0
66 8470.0 1 0 0.0
... ... ... ... ...
45 42514.0 0 C4 1.0
89 43301.0 2 0 0.0
46 44014.0 0 C4 1.0
90 44573.0 2 0 0.0
47 45514.0 0 C4 1.0
91 45957.0 2 0 0.0
48 47014.0 0 C4 1.0
92 47441.0 2 0 0.0
49 48514.0 0 C5 2.0
93 48924.0 1 0 0.0
50 50014.0 0 C4 1.0
94 50626.0 2 0 0.0
51 51514.0 0 C4 1.0
95 51914.0 2 0 0.0
52 53014.0 0 C4 1.0
96 53418.0 2 0 0.0
53 54514.0 0 C4 1.0
97 54987.0 2 0 0.0
54 56014.0 0 C5 2.0
98 56562.0 1 0 0.0
55 57514.0 0 C4 1.0
99 58042.0 2 0 0.0
56 59014.0 0 C4 1.0
100 59508.0 2 0 0.0
57 60514.0 0 C5 2.0
101 61076.0 1 0 0.0
58 62014.0 0 C4 1.0
102 62566.0 2 0 0.0
59 63514.0 0 C3 0.0
103 64664.0 2 0 0.0

104 rows × 4 columns


In [10]:
len(bci)


Out[10]:
26492

In [11]:
resp['time'] = pd.Series(resp['time'], dtype='int64') # Converting to int to allow for indexing safely
bci['time'] = pd.Series(bci['time'], dtype='int64')

In [12]:
bci


Out[12]:
tix 1 2 3 4 5 6 7 8 a b c time
0 0 65908.52 -60502.75 18753.72 -59711.32 14425.12 -59723.81 7026.90 163415.34 0.01 -0.02 1.04 0
1 1 65885.30 -60182.50 18726.98 -59706.56 14396.18 -59617.51 6997.95 163411.45 0.00 0.00 0.00 4
2 2 65868.48 -59552.22 18706.76 -59696.95 14383.55 -59398.33 6982.93 163407.78 0.00 0.00 0.00 9
3 3 65887.27 -59397.10 18729.62 -59701.13 14406.44 -59497.21 7008.08 163401.73 0.00 0.00 0.00 13
4 4 65899.29 -59907.30 18741.24 -59714.11 14422.69 -59732.11 7026.11 163394.59 0.00 0.00 0.00 17
5 5 65880.67 -60146.84 18719.29 -59710.71 14401.36 -59654.68 7004.39 163389.61 0.00 0.00 0.00 19
6 6 65859.93 -60248.99 18698.73 -59697.75 14381.47 -59378.84 6987.02 163385.92 0.00 0.00 0.00 24
7 7 65879.38 -60861.23 18724.50 -59697.98 14402.44 -59415.70 7005.62 163379.33 0.01 -0.02 1.04 28
8 8 65904.52 -61150.82 18745.24 -59709.55 14424.14 -59689.53 7026.52 163371.67 0.00 0.00 0.00 31
9 9 65882.75 -60967.60 18720.61 -59714.96 14403.64 -59720.08 7006.42 163367.47 0.00 0.00 0.00 36
10 10 65855.97 -60510.21 18694.04 -59703.61 14377.00 -59474.55 6982.28 163365.23 0.00 0.00 0.00 40
11 11 65874.41 -60289.13 18715.85 -59696.48 14393.88 -59467.13 6992.72 163362.30 0.00 0.00 0.00 44
12 12 65898.08 -60350.09 18743.44 -59709.00 14419.94 -59712.66 7016.48 163355.30 0.00 0.00 0.00 48
13 13 65889.21 -60233.37 18736.10 -59707.12 14411.80 -59763.60 7008.50 163351.11 0.00 0.00 0.00 52
14 14 65861.63 -59730.25 18701.75 -59695.11 14381.65 -59560.85 6985.10 163349.63 0.00 0.00 0.00 55
15 15 65874.45 -59172.08 18710.29 -59687.40 14394.17 -59473.25 6993.91 163343.38 0.00 0.00 0.00 59
16 16 65904.81 -59478.01 18740.86 -59698.24 14422.64 -59649.18 7021.91 163336.23 0.00 0.00 0.00 65
17 17 65894.30 -59920.06 18741.85 -59705.40 14416.16 -59710.67 7016.35 163332.44 0.01 -0.02 1.04 69
18 18 65863.59 -60010.01 18708.59 -59695.81 14392.60 -59518.16 6990.42 163328.22 0.00 0.00 0.00 71
19 19 65862.07 -60383.91 18709.88 -59684.52 14392.91 -59356.96 6994.29 163324.09 0.00 0.00 0.00 76
20 20 65887.04 -60909.08 18734.09 -59697.55 14408.85 -59533.40 7018.43 163315.88 0.00 0.00 0.00 80
21 21 65887.93 -60935.66 18731.97 -59709.09 14409.81 -59614.71 7020.84 163310.31 0.00 0.00 0.00 84
22 22 65854.25 -60570.36 18700.83 -59700.75 14381.94 -59522.22 6988.03 163307.78 0.00 0.00 0.00 88
23 23 65853.27 -60173.78 18695.29 -59690.04 14377.29 -59349.24 6982.48 163302.47 0.00 0.00 0.00 92
24 24 65882.46 -60131.09 18722.07 -59696.46 14404.31 -59476.80 7006.91 163296.56 0.00 0.00 0.00 95
25 25 65889.59 -60170.47 18733.20 -59709.75 14414.26 -59511.65 7013.62 163290.69 0.00 0.00 0.00 99
26 26 65859.84 -59865.95 18704.36 -59709.80 14384.78 -59488.70 6988.41 163288.25 0.00 0.00 0.00 104
27 27 65843.37 -59260.35 18687.15 -59697.75 14368.93 -59300.52 6971.93 163284.98 0.01 -0.02 1.04 108
28 28 65868.45 -59037.97 18716.97 -59700.48 14393.81 -59381.79 6996.05 163278.23 0.00 0.00 0.00 112
29 29 65889.56 -59532.77 18737.29 -59709.91 14413.25 -59504.68 7015.77 163273.14 0.00 0.00 0.00 115
... ... ... ... ... ... ... ... ... ... ... ... ... ...
26462 61 19379.23 0.00 15610.46 -14193.04 13679.51 0.00 6582.99 0.00 0.00 0.00 0.00 105838
26463 62 19356.01 0.00 15587.37 -14171.21 13655.66 0.00 6557.06 0.00 0.00 0.00 0.00 105841
26464 63 19350.78 0.00 15581.80 -14164.86 13661.34 0.00 6541.62 0.00 0.00 0.00 0.00 105846
26465 64 19375.86 0.00 15606.43 -14188.13 13683.20 0.00 6570.12 0.00 0.00 0.00 0.00 105850
26466 65 19386.79 0.00 15616.69 -14199.37 13680.70 0.00 6589.25 0.00 0.00 0.00 0.00 105856
26467 66 19362.51 0.00 15593.11 -14177.29 13654.81 0.00 6568.04 0.00 0.00 0.00 0.00 105859
26468 67 19350.73 0.00 15581.92 -14165.06 13657.97 0.00 6545.06 0.00 0.00 0.00 0.00 105863
26469 68 19374.80 0.00 15605.18 -14186.83 13685.88 0.00 6566.14 0.00 0.01 -0.02 1.04 105865
26470 69 19390.27 0.00 15620.09 -14202.66 13691.02 0.00 6592.67 0.00 0.00 0.00 0.00 105870
26471 70 19369.33 0.00 15599.33 -14184.08 13665.59 0.00 6572.71 0.00 0.00 0.00 0.00 105874
26472 71 19351.76 0.00 15581.85 -14166.24 13662.01 0.00 6550.45 0.00 0.00 0.00 0.00 105878
26473 72 19371.56 0.00 15602.14 -14184.15 13680.34 0.00 6568.02 0.00 0.00 0.00 0.00 105883
26474 73 19389.33 0.00 15620.56 -14201.83 13685.79 0.00 6591.98 0.00 0.00 0.00 0.00 105887
26475 74 19371.79 0.00 15603.24 -14186.58 13664.47 0.00 6576.78 0.00 0.00 0.00 0.00 105889
26476 75 19352.59 0.00 15583.19 -14168.12 13652.74 0.00 6555.03 0.00 0.00 0.00 0.00 105894
26477 76 19367.29 0.00 15597.23 -14180.80 13673.03 0.00 6564.39 0.00 0.00 0.00 0.00 105897
26478 77 19389.65 0.00 15619.80 -14201.94 13687.34 0.00 6589.54 0.00 0.00 0.00 0.00 105902
26479 78 19378.58 0.00 15609.38 -14192.96 13663.98 0.00 6586.75 0.00 0.01 -0.03 1.04 105907
26480 79 19352.01 0.00 15583.01 -14167.38 13651.42 0.00 6550.76 0.00 0.00 0.00 0.00 105910
26481 80 19359.31 0.00 15590.34 -14172.55 13672.61 0.00 6547.38 0.00 0.00 0.00 0.00 105915
26482 81 19387.37 0.00 15618.04 -14199.01 13692.86 0.00 6587.13 0.00 0.00 0.00 0.00 105918
26483 82 19382.02 0.00 15612.25 -14195.46 13676.67 0.00 6593.07 0.00 0.00 0.00 0.00 105922
26484 83 19357.62 0.00 15587.50 -14172.59 13661.70 0.00 6559.83 0.00 0.00 0.00 0.00 105925
26485 84 19358.20 0.00 15588.82 -14171.97 13675.65 0.00 6548.84 0.00 0.00 0.00 0.00 105931
26486 85 19383.72 0.00 15614.37 -14196.17 13691.99 0.00 6582.05 0.00 0.00 0.00 0.00 105934
26487 86 19385.06 0.00 15616.00 -14197.98 13681.01 0.00 6590.28 0.00 0.00 0.00 0.00 105938
26488 87 19358.71 0.00 15589.76 -14172.82 13664.74 0.00 6562.67 0.00 0.00 0.00 0.00 105943
26489 88 19356.14 0.00 15586.41 -14169.49 13672.11 0.00 6552.59 0.00 0.00 -0.02 1.04 105946
26490 89 19379.10 0.00 15609.21 -14191.26 13689.95 0.00 6576.29 0.00 0.00 0.00 0.00 105949
26491 90 19386.29 0.00 15616.56 -14198.92 13683.36 0.00 6596.92 0.00 0.00 0.00 0.00 105953

26492 rows × 13 columns


In [13]:
mid = len(bci) // 2
bci2 = bci.iloc[mid-100:mid+100]

In [14]:
# bci2.merge(resp, 'outer', on='time', sort=True)

In [15]:
# bci.merge(resp, 'outer', on='time', sort=True).to_csv('testmerge.csv')
mframe = pd.merge_asof(bci, resp, on='time', allow_exact_matches=False)

In [16]:
mframe


Out[16]:
tix 1 2 3 4 5 6 7 8 a b c time resp tone tonef
0 0 65908.52 -60502.75 18753.72 -59711.32 14425.12 -59723.81 7026.90 163415.34 0.01 -0.02 1.04 0 2 0 0.0
1 1 65885.30 -60182.50 18726.98 -59706.56 14396.18 -59617.51 6997.95 163411.45 0.00 0.00 0.00 4 2 0 0.0
2 2 65868.48 -59552.22 18706.76 -59696.95 14383.55 -59398.33 6982.93 163407.78 0.00 0.00 0.00 9 2 0 0.0
3 3 65887.27 -59397.10 18729.62 -59701.13 14406.44 -59497.21 7008.08 163401.73 0.00 0.00 0.00 13 2 0 0.0
4 4 65899.29 -59907.30 18741.24 -59714.11 14422.69 -59732.11 7026.11 163394.59 0.00 0.00 0.00 17 2 0 0.0
5 5 65880.67 -60146.84 18719.29 -59710.71 14401.36 -59654.68 7004.39 163389.61 0.00 0.00 0.00 19 2 0 0.0
6 6 65859.93 -60248.99 18698.73 -59697.75 14381.47 -59378.84 6987.02 163385.92 0.00 0.00 0.00 24 2 0 0.0
7 7 65879.38 -60861.23 18724.50 -59697.98 14402.44 -59415.70 7005.62 163379.33 0.01 -0.02 1.04 28 2 0 0.0
8 8 65904.52 -61150.82 18745.24 -59709.55 14424.14 -59689.53 7026.52 163371.67 0.00 0.00 0.00 31 2 0 0.0
9 9 65882.75 -60967.60 18720.61 -59714.96 14403.64 -59720.08 7006.42 163367.47 0.00 0.00 0.00 36 2 0 0.0
10 10 65855.97 -60510.21 18694.04 -59703.61 14377.00 -59474.55 6982.28 163365.23 0.00 0.00 0.00 40 2 0 0.0
11 11 65874.41 -60289.13 18715.85 -59696.48 14393.88 -59467.13 6992.72 163362.30 0.00 0.00 0.00 44 2 0 0.0
12 12 65898.08 -60350.09 18743.44 -59709.00 14419.94 -59712.66 7016.48 163355.30 0.00 0.00 0.00 48 2 0 0.0
13 13 65889.21 -60233.37 18736.10 -59707.12 14411.80 -59763.60 7008.50 163351.11 0.00 0.00 0.00 52 2 0 0.0
14 14 65861.63 -59730.25 18701.75 -59695.11 14381.65 -59560.85 6985.10 163349.63 0.00 0.00 0.00 55 2 0 0.0
15 15 65874.45 -59172.08 18710.29 -59687.40 14394.17 -59473.25 6993.91 163343.38 0.00 0.00 0.00 59 2 0 0.0
16 16 65904.81 -59478.01 18740.86 -59698.24 14422.64 -59649.18 7021.91 163336.23 0.00 0.00 0.00 65 2 0 0.0
17 17 65894.30 -59920.06 18741.85 -59705.40 14416.16 -59710.67 7016.35 163332.44 0.01 -0.02 1.04 69 2 0 0.0
18 18 65863.59 -60010.01 18708.59 -59695.81 14392.60 -59518.16 6990.42 163328.22 0.00 0.00 0.00 71 2 0 0.0
19 19 65862.07 -60383.91 18709.88 -59684.52 14392.91 -59356.96 6994.29 163324.09 0.00 0.00 0.00 76 2 0 0.0
20 20 65887.04 -60909.08 18734.09 -59697.55 14408.85 -59533.40 7018.43 163315.88 0.00 0.00 0.00 80 2 0 0.0
21 21 65887.93 -60935.66 18731.97 -59709.09 14409.81 -59614.71 7020.84 163310.31 0.00 0.00 0.00 84 2 0 0.0
22 22 65854.25 -60570.36 18700.83 -59700.75 14381.94 -59522.22 6988.03 163307.78 0.00 0.00 0.00 88 2 0 0.0
23 23 65853.27 -60173.78 18695.29 -59690.04 14377.29 -59349.24 6982.48 163302.47 0.00 0.00 0.00 92 2 0 0.0
24 24 65882.46 -60131.09 18722.07 -59696.46 14404.31 -59476.80 7006.91 163296.56 0.00 0.00 0.00 95 2 0 0.0
25 25 65889.59 -60170.47 18733.20 -59709.75 14414.26 -59511.65 7013.62 163290.69 0.00 0.00 0.00 99 2 0 0.0
26 26 65859.84 -59865.95 18704.36 -59709.80 14384.78 -59488.70 6988.41 163288.25 0.00 0.00 0.00 104 2 0 0.0
27 27 65843.37 -59260.35 18687.15 -59697.75 14368.93 -59300.52 6971.93 163284.98 0.01 -0.02 1.04 108 2 0 0.0
28 28 65868.45 -59037.97 18716.97 -59700.48 14393.81 -59381.79 6996.05 163278.23 0.00 0.00 0.00 112 2 0 0.0
29 29 65889.56 -59532.77 18737.29 -59709.91 14413.25 -59504.68 7015.77 163273.14 0.00 0.00 0.00 115 2 0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
26462 61 19379.23 0.00 15610.46 -14193.04 13679.51 0.00 6582.99 0.00 0.00 0.00 0.00 105838 2 0 0.0
26463 62 19356.01 0.00 15587.37 -14171.21 13655.66 0.00 6557.06 0.00 0.00 0.00 0.00 105841 2 0 0.0
26464 63 19350.78 0.00 15581.80 -14164.86 13661.34 0.00 6541.62 0.00 0.00 0.00 0.00 105846 2 0 0.0
26465 64 19375.86 0.00 15606.43 -14188.13 13683.20 0.00 6570.12 0.00 0.00 0.00 0.00 105850 2 0 0.0
26466 65 19386.79 0.00 15616.69 -14199.37 13680.70 0.00 6589.25 0.00 0.00 0.00 0.00 105856 2 0 0.0
26467 66 19362.51 0.00 15593.11 -14177.29 13654.81 0.00 6568.04 0.00 0.00 0.00 0.00 105859 2 0 0.0
26468 67 19350.73 0.00 15581.92 -14165.06 13657.97 0.00 6545.06 0.00 0.00 0.00 0.00 105863 2 0 0.0
26469 68 19374.80 0.00 15605.18 -14186.83 13685.88 0.00 6566.14 0.00 0.01 -0.02 1.04 105865 2 0 0.0
26470 69 19390.27 0.00 15620.09 -14202.66 13691.02 0.00 6592.67 0.00 0.00 0.00 0.00 105870 2 0 0.0
26471 70 19369.33 0.00 15599.33 -14184.08 13665.59 0.00 6572.71 0.00 0.00 0.00 0.00 105874 2 0 0.0
26472 71 19351.76 0.00 15581.85 -14166.24 13662.01 0.00 6550.45 0.00 0.00 0.00 0.00 105878 2 0 0.0
26473 72 19371.56 0.00 15602.14 -14184.15 13680.34 0.00 6568.02 0.00 0.00 0.00 0.00 105883 2 0 0.0
26474 73 19389.33 0.00 15620.56 -14201.83 13685.79 0.00 6591.98 0.00 0.00 0.00 0.00 105887 2 0 0.0
26475 74 19371.79 0.00 15603.24 -14186.58 13664.47 0.00 6576.78 0.00 0.00 0.00 0.00 105889 2 0 0.0
26476 75 19352.59 0.00 15583.19 -14168.12 13652.74 0.00 6555.03 0.00 0.00 0.00 0.00 105894 2 0 0.0
26477 76 19367.29 0.00 15597.23 -14180.80 13673.03 0.00 6564.39 0.00 0.00 0.00 0.00 105897 2 0 0.0
26478 77 19389.65 0.00 15619.80 -14201.94 13687.34 0.00 6589.54 0.00 0.00 0.00 0.00 105902 2 0 0.0
26479 78 19378.58 0.00 15609.38 -14192.96 13663.98 0.00 6586.75 0.00 0.01 -0.03 1.04 105907 2 0 0.0
26480 79 19352.01 0.00 15583.01 -14167.38 13651.42 0.00 6550.76 0.00 0.00 0.00 0.00 105910 2 0 0.0
26481 80 19359.31 0.00 15590.34 -14172.55 13672.61 0.00 6547.38 0.00 0.00 0.00 0.00 105915 2 0 0.0
26482 81 19387.37 0.00 15618.04 -14199.01 13692.86 0.00 6587.13 0.00 0.00 0.00 0.00 105918 2 0 0.0
26483 82 19382.02 0.00 15612.25 -14195.46 13676.67 0.00 6593.07 0.00 0.00 0.00 0.00 105922 2 0 0.0
26484 83 19357.62 0.00 15587.50 -14172.59 13661.70 0.00 6559.83 0.00 0.00 0.00 0.00 105925 2 0 0.0
26485 84 19358.20 0.00 15588.82 -14171.97 13675.65 0.00 6548.84 0.00 0.00 0.00 0.00 105931 2 0 0.0
26486 85 19383.72 0.00 15614.37 -14196.17 13691.99 0.00 6582.05 0.00 0.00 0.00 0.00 105934 2 0 0.0
26487 86 19385.06 0.00 15616.00 -14197.98 13681.01 0.00 6590.28 0.00 0.00 0.00 0.00 105938 2 0 0.0
26488 87 19358.71 0.00 15589.76 -14172.82 13664.74 0.00 6562.67 0.00 0.00 0.00 0.00 105943 2 0 0.0
26489 88 19356.14 0.00 15586.41 -14169.49 13672.11 0.00 6552.59 0.00 0.00 -0.02 1.04 105946 2 0 0.0
26490 89 19379.10 0.00 15609.21 -14191.26 13689.95 0.00 6576.29 0.00 0.00 0.00 0.00 105949 2 0 0.0
26491 90 19386.29 0.00 15616.56 -14198.92 13683.36 0.00 6596.92 0.00 0.00 0.00 0.00 105953 2 0 0.0

26492 rows × 16 columns


In [17]:
mframe['dtone'] = mframe['tonef'].diff().fillna(0)
mframe['oddball'] = mframe['dtone'] == 2
mframe.drop(['a', 'b', 'c', 'dtone'], axis=1, inplace=True)

In [18]:
mframe.to_csv('testmerge.csv')
print(mframe.shape, bci.shape)


(26492, 14) (26492, 13)

In [19]:
resp['tone'].dtype


Out[19]:
dtype('O')

In [20]:
import os
orig = os.getcwd()

In [21]:
erp_index = mframe[mframe['oddball']].index
for el in erp_index:
    print(el)


1987
2738
4990
5740
7617
8368
10244
12121
13997
15123

In [ ]:


In [22]:
sampleRate = 250 # Hz
erpTime = 1000 # sec
erp_window = 1000 # ms

In [23]:
mframe.iloc[erp_index[0]:erp_index[0]+erp_window]


Out[23]:
tix 1 2 3 4 5 6 7 8 time resp tone tonef oddball
1987 161 32920.27 0.0 18622.11 -27635.79 14320.90 0.0 6911.16 0.0 8015 0 C5 2.0 True
1988 162 32910.57 0.0 18633.84 -27625.41 14328.23 0.0 6918.98 0.0 8020 0 C5 2.0 False
1989 163 32858.29 0.0 18606.35 -27575.21 14299.33 0.0 6889.57 0.0 8024 0 C5 2.0 False
1990 164 32801.56 0.0 18603.09 -27519.04 14292.17 0.0 6881.36 0.0 8028 0 C5 2.0 False
1991 165 32786.63 0.0 18627.14 -27501.76 14319.44 0.0 6910.62 0.0 8032 0 C5 2.0 False
1992 166 32779.79 0.0 18629.15 -27494.21 14328.63 0.0 6918.45 0.0 8035 0 C5 2.0 False
1993 167 32734.58 0.0 18602.78 -27451.43 14307.37 0.0 6896.45 0.0 8039 0 C5 2.0 False
1994 168 32675.12 0.0 18595.51 -27393.00 14291.91 0.0 6884.72 0.0 8055 0 C5 2.0 False
1995 169 32652.46 0.0 18620.41 -27368.30 14310.23 0.0 6904.07 0.0 8055 0 C5 2.0 False
1996 170 32649.84 0.0 18630.58 -27364.30 14327.22 0.0 6922.51 0.0 8056 0 C5 2.0 False
1997 171 32612.07 0.0 18614.71 -27327.98 14311.60 0.0 6905.30 0.0 8056 0 C5 2.0 False
1998 172 32549.08 0.0 18597.75 -27266.42 14293.83 0.0 6883.87 0.0 8059 0 C5 2.0 False
1999 173 32520.74 0.0 18613.91 -27236.76 14308.98 0.0 6898.26 0.0 8063 0 C5 2.0 False
2000 174 32516.98 0.0 18637.11 -27231.08 14333.79 0.0 6924.32 0.0 8067 0 C5 2.0 False
2001 175 32486.49 0.0 18624.66 -27201.67 14321.08 0.0 6907.87 0.0 8071 0 C5 2.0 False
2002 176 32424.69 0.0 18603.22 -27141.79 14299.55 0.0 6883.24 0.0 8074 0 C5 2.0 False
2003 177 32386.40 0.0 18612.25 -27102.58 14313.45 0.0 6894.75 0.0 8079 0 C5 2.0 False
2004 178 32383.34 0.0 18639.19 -27097.58 14334.62 0.0 6917.02 0.0 8084 0 C5 2.0 False
2005 179 32363.60 0.0 18642.45 -27078.38 14326.98 0.0 6909.15 0.0 8088 0 C5 2.0 False
2006 180 32300.46 0.0 18606.40 -27017.45 14299.26 0.0 6881.07 0.0 8091 0 C5 2.0 False
2007 181 32254.42 0.0 18598.97 -26971.25 14302.46 0.0 6884.96 0.0 8095 0 C5 2.0 False
2008 182 32249.14 0.0 18628.37 -26964.32 14327.83 0.0 6912.99 0.0 8099 0 C5 2.0 False
2009 183 32234.16 0.0 18623.92 -26949.21 14323.65 0.0 6911.07 0.0 8102 0 C5 2.0 False
2010 184 32177.14 0.0 18583.60 -26894.56 14290.95 0.0 6879.84 0.0 8107 0 C5 2.0 False
2011 185 32127.12 0.0 18587.75 -26844.49 14291.44 0.0 6878.17 0.0 8112 0 C5 2.0 False
2012 186 32116.53 0.0 18620.90 -26831.64 14320.76 0.0 6905.55 0.0 8116 0 C5 2.0 False
2013 187 32108.82 0.0 18631.70 -26822.94 14331.25 0.0 6915.94 0.0 8118 0 C5 2.0 False
2014 188 32057.16 0.0 18604.12 -26773.57 14305.21 0.0 6887.87 0.0 8123 0 C5 2.0 False
2015 189 31998.64 0.0 18593.66 -26715.94 14290.79 0.0 6873.56 0.0 8127 0 C5 2.0 False
2016 190 31985.61 0.0 18618.51 -26700.77 14311.46 0.0 6897.26 0.0 8130 0 C5 2.0 False
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2957 107 19896.79 0.0 16075.13 -14625.46 14467.77 0.0 6790.01 0.0 11893 1 0 0.0 False
2958 108 19875.48 0.0 16055.28 -14604.72 14471.03 0.0 6796.85 0.0 11897 1 0 0.0 False
2959 109 19891.93 0.0 16071.53 -14619.25 14495.51 0.0 6820.12 0.0 11900 1 0 0.0 False
2960 110 19899.58 0.0 16078.30 -14626.62 14494.79 0.0 6817.30 0.0 11904 1 0 0.0 False
2961 111 19867.39 0.0 16046.07 -14596.65 14467.55 0.0 6790.15 0.0 11909 1 0 0.0 False
2962 112 19837.53 0.0 16016.83 -14566.94 14470.76 0.0 6793.03 0.0 11912 1 0 0.0 False
2963 113 19851.34 0.0 16031.10 -14578.30 14507.62 0.0 6822.06 0.0 11916 1 0 0.0 False
2964 114 19867.24 0.0 16046.56 -14593.30 14517.23 0.0 6830.16 0.0 11919 1 0 0.0 False
2965 115 19839.92 0.0 16019.11 -14568.11 14492.92 0.0 6804.85 0.0 11925 1 0 0.0 False
2966 116 19807.31 0.0 15986.39 -14536.17 14484.80 0.0 6796.43 0.0 11929 1 0 0.0 False
2967 117 19813.64 0.0 15992.90 -14540.28 14510.44 0.0 6823.54 0.0 11933 1 0 0.0 False
2968 118 19831.74 0.0 16011.31 -14557.13 14521.06 0.0 6832.44 0.0 11936 1 0 0.0 False
2969 119 19812.88 0.0 15992.40 -14539.83 14503.02 0.0 6815.61 0.0 11941 1 0 0.0 False
2970 120 19772.60 0.0 15951.68 -14501.27 14485.70 0.0 6797.70 0.0 11943 1 0 0.0 False
2971 121 19769.29 0.0 15948.39 -14496.36 14501.57 0.0 6813.12 0.0 11947 1 0 0.0 False
2972 122 19793.18 0.0 15972.73 -14518.82 14521.44 0.0 6830.16 0.0 11952 1 0 0.0 False
2973 123 19785.14 0.0 15964.91 -14511.91 14505.05 0.0 6811.36 0.0 11958 1 0 0.0 False
2974 124 19743.16 0.0 15922.85 -14471.44 14487.37 0.0 6795.22 0.0 11960 1 0 0.0 False
2975 125 19734.71 0.0 15914.08 -14461.71 14505.83 0.0 6813.48 0.0 11963 1 0 0.0 False
2976 126 19755.79 0.0 15934.56 -14480.85 14524.16 0.0 6831.56 0.0 11968 1 0 0.0 False
2977 127 19753.04 0.0 15931.99 -14478.99 14506.15 0.0 6816.97 0.0 11972 1 0 0.0 False
2978 128 19711.58 0.0 15890.97 -14440.03 14483.33 0.0 6792.36 0.0 11976 1 0 0.0 False
2979 129 19696.00 0.0 15875.13 -14423.51 14493.18 0.0 6792.20 0.0 11981 1 0 0.0 False
2980 130 19716.41 0.0 15894.88 -14441.71 14519.04 0.0 6819.87 0.0 11984 1 0 0.0 False
2981 131 19720.81 0.0 15899.31 -14446.96 14507.73 0.0 6807.60 0.0 11988 1 0 0.0 False
2982 132 19684.24 0.0 15863.12 -14413.12 14476.64 0.0 6779.91 0.0 11992 1 0 0.0 False
2983 133 19659.48 0.0 15838.76 -14388.24 14482.57 0.0 6789.52 0.0 11996 1 0 0.0 False
2984 134 19673.85 0.0 15853.11 -14400.31 14510.30 0.0 6816.03 0.0 12000 1 0 0.0 False
2985 135 19685.02 0.0 15864.11 -14411.51 14507.31 0.0 6815.18 0.0 12004 1 0 0.0 False
2986 136 19652.86 0.0 15832.39 -14381.69 14482.61 0.0 6786.19 0.0 12008 1 0 0.0 False

1000 rows × 14 columns


In [24]:
erps = []
for idx in erp_index:
    erps.append(mframe[chan_columns].iloc[idx:idx+erp_window])

In [25]:
erps[0].as_matrix()


Out[25]:
array([[ 32920.27,      0.  ,  18622.11, ...,      0.  ,   6911.16,      0.  ],
       [ 32910.57,      0.  ,  18633.84, ...,      0.  ,   6918.98,      0.  ],
       [ 32858.29,      0.  ,  18606.35, ...,      0.  ,   6889.57,      0.  ],
       ..., 
       [ 19673.85,      0.  ,  15853.11, ...,      0.  ,   6816.03,      0.  ],
       [ 19685.02,      0.  ,  15864.11, ...,      0.  ,   6815.18,      0.  ],
       [ 19652.86,      0.  ,  15832.39, ...,      0.  ,   6786.19,      0.  ]])

In [26]:
meta_frame = np.zeros((len(erps), erps[0].shape[0], erps[1].shape[1]))
meta_frame.shape


Out[26]:
(10, 1000, 8)

In [27]:
for i, item in enumerate(erps):
    meta_frame[i] = np.array(item)

In [28]:
meta_frame.shape


Out[28]:
(10, 1000, 8)

In [29]:
erp_mean = meta_frame.mean(axis=0)
erp_mean.shape


Out[29]:
(1000, 8)

In [30]:
erp_mean.mean(axis=1).shape


Out[30]:
(1000,)

In [31]:
p300 = erp_mean.mean(axis=1)
plt.plot(p300)


Out[31]:
[<matplotlib.lines.Line2D at 0x7f40d19de860>]

In [32]:
def butter_lowpass_filter(data, cutoff, fs=1., order=1, axis=0, analog=False):
    # todo: add option to filtfilt or lfilter
    """
    Apply a digital Butterworth low-pass filter.
    :param data: array-like
    :param cutoff: Critical frequency, Hz
    :param fs: Sampling freqency, Hz
    :param order: Order of
    :param axis: ndarray axis, 0='long' axis, 1='row' axis
    :return:
    """
    nyquistFreqInRads = (2*np.pi*fs)/2
    Wn = 2*np.pi*cutoff / (nyquistFreqInRads)
    b, a = signal.butter(order, Wn, btype='low', analog=analog)
    y = signal.filtfilt(b, a, data, axis=axis)
    return y

In [33]:
p300f = butter_lowpass_filter(p300, 0.25, 250)
plt.plot(p300)
plt.plot(p300f)


Out[33]:
[<matplotlib.lines.Line2D at 0x7f40abde0780>]

In [34]:
p300_detrend = p300-p300f

In [35]:
plt.plot(p300_detrend)


Out[35]:
[<matplotlib.lines.Line2D at 0x7f40d08e0128>]

In [36]:
plt.plot(butter_lowpass_filter(p300_detrend, 10, 250))


Out[36]:
[<matplotlib.lines.Line2D at 0x7f40d083ae48>]

In [37]:
meta_p3s = meta_frame.mean(axis=0)

In [38]:
meta_detrend = meta_p3s - butter_lowpass_filter(meta_p3s, .25, 250, axis=0)
plt.plot(meta_detrend)


Out[38]:
[<matplotlib.lines.Line2D at 0x7f40d0825160>,
 <matplotlib.lines.Line2D at 0x7f40d0825400>,
 <matplotlib.lines.Line2D at 0x7f40d08255f8>,
 <matplotlib.lines.Line2D at 0x7f40d08257f0>,
 <matplotlib.lines.Line2D at 0x7f40d08259e8>,
 <matplotlib.lines.Line2D at 0x7f40d0825be0>,
 <matplotlib.lines.Line2D at 0x7f40d0825dd8>,
 <matplotlib.lines.Line2D at 0x7f40d0825fd0>]

In [39]:
meta_detrend[:,3] *= -1

In [40]:
plt.plot(meta_detrend)


Out[40]:
[<matplotlib.lines.Line2D at 0x7f40d0794908>,
 <matplotlib.lines.Line2D at 0x7f40d0794ba8>,
 <matplotlib.lines.Line2D at 0x7f40d0794da0>,
 <matplotlib.lines.Line2D at 0x7f40d0794f98>,
 <matplotlib.lines.Line2D at 0x7f40d07981d0>,
 <matplotlib.lines.Line2D at 0x7f40d07983c8>,
 <matplotlib.lines.Line2D at 0x7f40d07985c0>,
 <matplotlib.lines.Line2D at 0x7f40d07987b8>]

In [46]:
plt.plot(butter_lowpass_filter(meta_detrend, 10, 250))
chan=7
plt.plot(butter_lowpass_filter(meta_detrend[:,chan], 10, 250), 'c')
plt.plot(butter_lowpass_filter(meta_detrend[:,chan], 10, 250), 'k--')
plt.legend(range(8))


Out[46]:
<matplotlib.legend.Legend at 0x7f40d04eb5c0>

In [ ]:


In [45]:
plt.plot(butter_lowpass_filter(meta_detrend.mean(axis=1), 15, 250))
plt.plot(meta_detrend[:,0]*0)


Out[45]:
[<matplotlib.lines.Line2D at 0x7f40d05be5c0>]

In [ ]: