-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
55 lines (48 loc) · 1.6 KB
/
load_data.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
def load_data(file='frompost'):
""" Loads the data in a file
:param file: (string) The filename of the file where the ECG data is
:returns: A matrix containing all the data from the file
"""
import numpy as np
import csv
if not file.endswith('.csv'):
raise ValueError("file must be in .csv format")
try:
matrix = np.loadtxt(open(file), delimiter=",", skiprows=1)
dims = np.shape(matrix)
except:
mat_csv = csv.reader(open(file))
count = 0
arr = []
for row in mat_csv:
if row[0] == '' or row[1] == '':
continue
elif row[0][0].isalpha() or row[1][0].isalpha():
continue
else:
arr.append([float(row[0]), float(row[1])])
count += 1
matrix = np.array(arr)
dims = np.shape(matrix)
print(matrix)
if len(dims) < 2 or dims[1] != 2:
raise ValueError("file must have two columns (time and voltage)")
for item in matrix[:, 0]:
if type(item) is not int:
try:
int(item)
except:
raise ValueError("files must only contain numbers")
for item in matrix[:, 1]:
if type(item) is not int:
try:
int(item)
except:
raise ValueError("files must only contain numbers")
for i, item in enumerate(matrix[:, 1]):
if item > 300:
np.delete(matrix, [i][:])
matrix = (matrix[:, 0], matrix[:, 1])
return matrix
if __name__ == "__main__":
load_data('ECGTest.csv')