In [1]:
%matplotlib inline

In [13]:
import numpy as np, pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
import os
import re
from datetime import datetime
import pytz
from tabulate import tabulate

In [271]:
path_resp = '/home/mike/w/bci_erp/python/sampleData/run9/oddball_run_2017-02-05_15-02-45.csv'
# path_2 = '/home/mike/w/bci_erp/python/sampleData/oddball_run_2017-02-04_13-02-08.txt'
resp = pd.read_csv(path_resp)
# resp.columns = ['time', 'unixtime', 'resp', 'tone', 'ecode', 'label']
resp['unixtime'] = pd.to_numeric(resp['unixtime'])
resp.head()


Out[271]:
isotime unixtime resp tone type label
0 2017-02-05T15:02:45.234-05:00 1486324965234 0 0 254 start experiment
1 2017-02-05T15:02:48.243-05:00 1486324968243 0 F4 4 stimulus_freq
2 2017-02-05T15:02:48.720-05:00 1486324968720 1 0 8 response_freq
3 2017-02-05T15:02:50.241-05:00 1486324970241 0 G5 6 stimulus_infreq
4 2017-02-05T15:02:50.839-05:00 1486324970839 2 0 10 response_infreq

In [272]:
# pd.to_datetime(resp['unixtime']/1000, unit='s')

In [273]:
path_bci = '/home/mike/w/bci_erp/python/sampleData/run9/OpenBCI-RAW-ob_2017-02-05_15-00-49.txt'
bci = pd.read_csv(path_bci, sep=', ', skiprows=6, header=None)
chan_columns = [str(i) for i in range(1,9)]
bci.columns = ['ticks'] + chan_columns + ['a', 'b', 'c', 'unixtime']
bci['unixtime'] = pd.to_numeric(bci['unixtime'])
bci.head()


/home/mike/ve/erp/lib/python3.5/site-packages/ipykernel/__main__.py:2: 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'.
  from ipykernel import kernelapp as app
Out[273]:
ticks 1 2 3 4 5 6 7 8 a b c unixtime
0 2 -38.74 -43254.51 -5822.72 21606.78 -13614.67 567.69 -73030.12 -50432.22 0.0 0.0 0.0 1.486325e+12
1 3 -104.29 -43376.00 -5807.01 21603.34 -13660.63 556.92 -73994.53 -50476.85 0.0 0.0 0.0 1.486325e+12
2 4 -112.47 -43370.86 -5852.40 21568.18 -13696.54 520.84 -72999.34 -50487.52 0.0 0.0 0.0 1.486325e+12
3 5 -45.26 -43233.19 -5872.09 21553.85 -13671.31 509.87 -71929.90 -50443.57 0.0 0.0 0.0 1.486325e+12
4 6 -28.34 -43218.15 -5820.48 21590.76 -13627.68 545.72 -72789.57 -50428.13 0.0 0.0 0.0 1.486325e+12

watch out for stupid windows backslashes


In [3]:
example_header = r"""
#  Non-editable header begin --------------------------------------------------------------------------------
# 
#  data format...............: continuous
#  setname...................: S1_EEG_elist
#  filename..................: S1_EEG.set
#  filepath..................: C:\Users\mike\Downloads\Test_Data\Test_Data\S1\
#  nchan.....................: 16
#  pnts......................: 1069520
#  srate.....................: 500
#  nevents...................: 2557
#  generated by (bdf)........: 
#  generated by (set)........: S1_EEG_elist
#  reported in ..............: 
#  prog Version..............: 6.1
#  creation date.............: 03-Feb-2017 10:51:12
#  user Account..............: 
# 
#  Non-editable header end --------------------------------------------------------------------------------




# item	 bepoch	  ecode	            label	      onset	          diff	     dura	b_flags	   a_flags	  enable	    bin
#                                                 (sec)           (msec)     (msec)    (binary)   (binary)
"""

In [10]:
header_meta = '''
#  Non-editable header begin --------------------------------------------------------------------------------
# 
#  data format...............: {dataFormat}
#  setname...................: {setname}
#  filename..................: {filename}
#  filepath..................: {filepath}
#  nchan.....................: {nchan}
#  pnts......................: {pnts}
#  srate.....................: {srate}
#  nevents...................: {nevents}
#  generated by (bdf)........: python
#  generated by (set)........: python
#  reported in ..............: 
#  prog Version..............: 6.1
#  creation date.............: {createdDate}
#  user Account..............: {user}
# 
#  Non-editable header end --------------------------------------------------------------------------------
'''

In [11]:
header_cols = """




# item	 bepoch	  ecode	            label	      onset	          diff	     dura	b_flags	   a_flags	  enable	    bin
#                                                 (sec)           (msec)     (msec)    (binary)   (binary)
"""

In [12]:
print(header_meta + header_cols)


#  Non-editable header begin --------------------------------------------------------------------------------
# 
#  data format...............: {dataFormat}
#  setname...................: {setname}
#  filename..................: {filename}
#  filepath..................: {filepath}
#  nchan.....................: {nchan}
#  pnts......................: {pnts}
#  srate.....................: {srate}
#  nevents...................: {nevents}
#  generated by (bdf)........: python
#  generated by (set)........: python
#  reported in ..............: 
#  prog Version..............: 6.1
#  creation date.............: {createdDate}
#  user Account..............: {user}
# 
#  Non-editable header end --------------------------------------------------------------------------------





# item	 bepoch	  ecode	            label	      onset	          diff	     dura	b_flags	   a_flags	  enable	    bin
#                                                 (sec)           (msec)     (msec)    (binary)   (binary)

legend

Format is fixed-width

  • item = (int) ordinal index, starts with 1

  • bepoch = (int) ?bin epoch? typically 0

  • ecode = (uint8) numerical code [0-255] which codes for type of event (aka type)

  • label = (str) given label, empty is ""

  • onset = (float) time from start to event (seconds) aka latency

  • diff = (float) time since prior event (millis)

  • dura = (bin8) no idea, default=00000000

  • b_flags = (bin8) bin flags, default=00000000

  • a_flags = (bin8) artefact flags, default=00000000

  • enable = (bit) 1/0 for true/false

  • bin = (list) List of bin numbers, empty is [ ] (7 spaces)


In [14]:
eventlist_name = '/media/mike/tera/data/erplab/Test_Data/S1/event_export02.txt'

In [37]:
eventlist_df = pd.read_fwf(eventlist_name, skiprows=28, header=None)

In [38]:
eventlist_df.head()


Out[38]:
0 1
0 3 \t0 \t 12\t ""\t ... ]
1 4 \t0 \t 9\t ""\t ... ]
2 5 \t0 \t 22\t ""\t ... ]
3 6 \t0 \t 9\t ""\t ... ]
4 7 \t0 \t 22\t ""\t ... ]

In [42]:
# well, that failed, because it's formatted horribly
with open(eventlist_name, 'r') as infile:
    eventlist_raw = infile.readlines()

In [46]:
data = eventlist_raw[26:]

In [47]:
data[0]


Out[47]:
'1     \t0     \t     22\t              ""\t     13.0600\t      0.00\t    0.0\t    00000000     00000000\t   1\t[       ]\n'

In [48]:
re_inquotes = '\"([\w _\./\\-]*)\"'

In [ ]: