-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyEdfToCsv.py
executable file
·112 lines (95 loc) · 3.64 KB
/
pyEdfToCsv.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
import pyedflib
import numpy as np
import argparse
import datetime
import locale
# defaults
separator = ';'
locale.setlocale(locale.LC_ALL, '')
decimalpoint = (locale.localeconv()['decimal_point'])
def signalsToCsv(filename, labels, signals):
''' Export all signals to single .csv'''
filename = filename+'.csv'
with open(filename, 'w+') as f:
labels_row = separator.join(labels)
f.write(labels_row)
# Get max samples length
maxLength = 0
for i in range(len(signals)):
maxLength = max(maxLength, len(signals[i]))
# @TODO
def signalsToCsvs(filename, labels, signals, sampleRates, dimensions):
''' Export all signals to multiple .csv'''
for i in range(len(signals)):
filepath = filename+labels[i]+'_'+str(sampleRates[i])+'sps.csv'
with open(filepath, 'w+') as f:
# Labels
f.write('Time[s]%c%s [%s]\n' %
(separator, labels[i], dimensions[i]))
# Prepare time values
if (args.timeAbsolute):
time = startTime
delta = datetime.timedelta(seconds=1.0/sampleRates[i])
else:
time = 0
delta = 1.0/sampleRates[i]
# Samples saving
for sample in signals[i]:
# DateTime to text
if (args.timeAbsolute):
# Absolute time used
text = '%s%c' % (time.strftime(
'%Y-%m-%d %H:%M:%S.%f'), separator)
else:
# Relative time used
text = '%2.4f%c' % (time, separator)
time += delta
# Sample to text
text += str(sample)
# EOL
text += '\n'
# Decimal mark conversion of whole line
if (decimalpoint == ','):
text = text.replace('.', ',')
# Save
f.write(text)
# Arguments and config
# #####################################################
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str,
required=True, help='Input EDF file')
parser.add_argument('-s', '--separator', type=str,
required=False, help='Data CSV separator')
parser.add_argument('-d', '--decimalpoint', type=str,
required=False, help='Decimal point character')
parser.add_argument('-t', '--timeAbsolute', action='store_true',
required=False, help='Default behaviour is relative time printing (First sample is 0s). Absolute time prints time according to record start time.')
args = parser.parse_args()
if (args.separator is not None):
separator = args.separator
if (args.decimalpoint is not None):
decimalpoint = args.decimalpoint
# Open EDF file
f = pyedflib.EdfReader(args.input)
print('(File) Opened', args.input)
n = f.signals_in_file
print('(File) %u signals in file.' % (n))
labels = f.getSignalLabels()
print('(File) Signal labels in file : ', labels)
startTime = f.getStartdatetime()
print('(File) Start of recording', startTime)
sampleRates = f.getSampleFrequencies()
signals = []
dimensions = []
for i in np.arange(n):
print('(Signal) Reading signal %u `%s`, sampling freqeuncy %u, samples %u' % (
i, labels[i], f.getSampleFrequency(i), f.getNSamples()[i]))
print('(Signal) Signal header : ', f.getSignalHeader(i))
print('')
signal = f.readSignal(i)
signals.append(signal)
dimensions.append(f.getPhysicalDimension(i))
# Create .csv
print('Creation of .csv.')
signalsToCsvs(args.input, labels, signals, sampleRates, dimensions)