-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_csvs.py
92 lines (77 loc) · 2.73 KB
/
parse_csvs.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
import os
import csv
import numpy as np
import json
default_multiplier = 12
def parse_prime_csvs(in_path, multiplier=default_multiplier, limit=np.inf):
if limit < 0:
limit = np.inf
i = 0
ids = []
data = {}
for root, directories, filenames in os.walk(in_path):
for filename in filenames:
ext = filename.split('.')[-1]
if not ext == 'csv':
continue
path = os.path.join(root, filename)
id = filename.split('.')[0]
ids.append(id)
with open(path) as f:
reader = csv.reader(f)
entry = []
for row in reader:
time = int(float(row[0]) * multiplier)
note = int(row[1])
entry.append([time, note])
data[id] = np.array(entry)
i += 1
if i >= limit:
break
return ids, data
def parse_PPDD(PPDD='PPDD', limit=1000, mult=default_multiplier):
i = 0
data = {}
for root, directories, filenames in os.walk(f'{PPDD}/descriptor'):
for filename in filenames:
ext = filename.split('.')[-1]
if not ext == 'json':
continue
path = os.path.join(root, filename)
with open(path) as json_file:
entry = json.load(json_file)
data[entry['id']] = entry
i += 1
if i >= limit:
break
ids = list(data.keys())
for i in ids:
path = f'{PPDD}/cont_true_csv/{i}.csv'
with open(path) as file:
cont = []
reader = csv.reader(file, delimiter=',')
for row in reader:
cont.append(np.array(row, dtype='float'))
cont = np.array(cont)
path = f'{PPDD}/prime_csv/{i}.csv'
with open(path) as file:
prime = []
reader = csv.reader(file, delimiter=',')
for row in reader:
prime.append(np.array(row, dtype='float'))
prime = np.array(prime)
prime[:, 0] = np.round(prime[:, 0] * mult)
prime[:, 3] = np.round(prime[:, 3] * mult)
cont[:, 0] = np.round(cont[:, 0] * mult)
cont[:, 3] = np.round(cont[:, 3] * mult)
data[i]['prime'] = prime.astype('int16')
data[i]['cont'] = cont.astype('int16')
# multiply and round to get integer values for time
return ids, data
def write_to_csv(prediction, path, multiplier=default_multiplier, round_to=5):
with open(path, 'w', newline='') as f:
w = csv.writer(f)
for pt in prediction:
time = np.round(pt[0] / float(multiplier), round_to)
note = int(pt[1])
w.writerow([time, note])