-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_reader.py
144 lines (104 loc) · 3.79 KB
/
csv_reader.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'''
Created on May 18, 2019
@author: fmoya
'''
import numpy as np
import csv
import sys
#def reader_data(path: str) -> np.array:
def reader_data(path):
'''
gets data from csv file
data contains 134 columns
the first column corresponds to sample
the second column corresponds to class label
the rest 132 columns corresponds to all of the joints (x,y,z) measurements
returns a numpy array
@param path: path to file
'''
data = np.loadtxt(path, delimiter=',', skiprows=1)
return data
def reader_data_2(path):
'''
gets data from csv file
data contains 134 columns
the first column corresponds to sample
the second column corresponds to class label
the rest 132 columns corresponds to all of the joints (x,y,z) measurements
returns a numpy array
@param path: path to file
'''
counter = 0
data = np.empty((0,134))
with open(path, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
try:
if spamreader.line_num == 1:
print('\n')
print(', '.join(row))
else:
frame = list(map(float, row))
frame = np.array(frame)
frame = frame[:134]
frame = np.reshape(frame, newshape = (1, 134))
data = np.append(data, frame, axis = 0)
sys.stdout.write('\r' + 'In {} Number of seq {}'.format(path, len(data)) )
sys.stdout.flush()
#if counter == 5000:
# break
counter += 1
except KeyboardInterrupt:
print('\nYou cancelled the operation.')
return data
def reader_labels(path):
'''
gets labels and attributes from csv file
data contains 20 columns
the first column corresponds to class label
the rest 19 columns corresponds to all of the attributes
returns a numpy array
@param path: path to file
'''
data = np.loadtxt(path, delimiter=',', skiprows=1)
return data
def reader_labels_2(path):
'''
gets labels and attributes from csv file
data contains 20 columns
the first column corresponds to class label
the rest 19 columns corresponds to all of the attributes
returns a numpy array
@param path: path to file
'''
counter = 0
data = np.empty((0,20))
with open(path, 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
try:
if spamreader.line_num == 1:
print('\n')
print(', '.join(row))
else:
frame = list(map(float, row))
frame = np.array(frame)
frame = frame[:20]
frame = np.reshape(frame, newshape = (1, 20))
data = np.append(data, frame, axis = 0)
sys.stdout.write('\r' + 'In {} Number of seq {}'.format(path, len(data)) )
sys.stdout.flush()
#if counter == 5000:
# break
counter += 1
except KeyboardInterrupt:
print('\nYou cancelled the operation.')
return data
if __name__ == '__main__':
#pathFile = '/vol/corpora/har/DFG_Project/2019/MoCap/recordings_2019_06/14_Annotated_Dataset/P01/' +\
# 'S01_P01_R01_A17_N01_labels.csv'
#labels = reader_labels(pathFile)
#pathFile = '/vol/corpora/har/DFG_Project/2019/MoCap/recordings_2019_06/14_Annotated_Dataset/P01/' +\
# 'S01_P01_R01_A17_N01_norm_data.csv'
#data = reader_data(pathFile)
print("Done")