-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
67 lines (54 loc) · 2.16 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Bunch of file format conversion utilities.
"""
import csv
import mne.io
# Format floats as strings to 6sf. NOTE: maybe not needed?
def sixSF(readings):
return [format(x, '.6f') for x in readings]
# Given an array of positions (x,y,z), noralize them all to length 1 vectors.
def normalPos(positions):
fPos = np.array([[float(p) for p in pos] for pos in positions])
return [row / numpy.linalg.norm(row) for row in fPos]
def edfToCSV(edfPath, csvPath):
"""
Convert an EDF file to the CSV that openVibe can load as signal
"""
print "Loading %s..." % edfPath
raw = mne.io.read_raw_edf(edfPath, preload=True)
readings = raw._data.T
channelNames = raw.info['ch_names']
sampleRate = raw.info['sfreq']
secPerSample = 1.0 / sampleRate
print "# Readings: %d" % readings.shape[0]
print "# Channels: %d" % len(channelNames)
print "Sample rate: %d hz" % sampleRate
print "Writing to %s..." % csvPath
with open(csvPath, 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=';')
writer.writerow(['Time (s)'] + channelNames + ['Sampling Rate'])
# First row has extra rate:
writer.writerow([0.0] + sixSF(readings[0].tolist()) + [sampleRate])
for r in range(1, readings.shape[0]):
writer.writerow([r * secPerSample] + sixSF(readings[r].tolist()))
print "Done!"
def locationsToTxt(inPath, outPath):
"""
Convert the location data provided for the headset to openvibe sensor location file.
"""
print "Loading %s..." % inPath
rows = []
with open(inPath, 'rb') as inFile:
reader = csv.reader(inFile, delimiter='\t')
rows = [row for row in reader][3:67]
channelNames = [row[0].replace('E', 'EEG ') for row in rows]
channelList = " ".join('"' + name + '"' for name in channelNames)
header = '[\n\t[ %s ]\n\t[ "x" "y" "z" ]\n]\n' % channelList
positions = normalPos([row[1:] for row in rows])
print "Writing %s..." % outPath
with open(outPath, 'w') as outFile:
outFile.write(header)
for pos in positions:
row = "[\n\t[ %s %s %s ]\n]\n" % tuple(pos)
outFile.write(row)
print "Done!"