-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataparser.py
105 lines (100 loc) · 4.2 KB
/
dataparser.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
from os import listdir, remove, path
import numpy as np
def check_id(driver_id):
try:
id_num = float(driver_id)
if id_num > 100:
return False
return True
except ValueError:
return False
read_dir = "rawData2"
write_dir = "procData2"
open_write_files = {}
scenario_titles = {}
files_list = listdir(read_dir)
for files in listdir(write_dir):
remove(write_dir+'/'+files)
print files_list
ParticipantID = 0
for files in files_list:
dataf = open(read_dir + "/" + files, "r")
s = dataf.readline()
s = s.split('\t')
current_ent = int(s[0])
string_list = []
while s:
if (s[len(s)-1] == "(Aspect)") or \
(s[len(s)-1] == "(UnityEngine.GameObject)"):
s.pop()
if current_ent == int(s[0]):
string_list.append(s)
else:
if string_list[1][2] == "Completed":
if check_id(ParticipantID):
if string_list[1][1] == "2Scenario":
string_list[1][1] = "Scenario"
Scenario = string_list[1][3]
value_dict = {}
string_list = string_list[3:]
for lists in string_list:
last = lists.pop()
key = ''.join(lists[1:])
if key == "Other":
key = "Othercollider"
last = "nothing"
if key == "Car":
if (Scenario == "Trust_1") or (Scenario == "Contagion_2"):
key = "Carbehind"
last = "nothing"
else:
key = "Carahead"
last = "nothing"
if key == "Carinadjacent":
key = "Carinadjacentlane"
last = "nothing"
value_dict[key] = last
scenario_file = write_dir + "/" + Scenario + ".csv"
value_dict.pop("time")
not_interesting_values = ["Othercollider", "Othercar", "Carahead", "Carbehind"]
for value in not_interesting_values:
if value in value_dict:
value_dict.pop(value)
if path.exists(scenario_file):
writef = open_write_files[Scenario]
else:
writef = open(scenario_file, "w")
open_write_files[Scenario] = writef
writef.write("ParticipantID")
scenario_titles[Scenario] = []
for column_names in value_dict:
writef.write(";" + column_names)
scenario_titles[Scenario].append(column_names)
writef.write('\n')
writef.write(ParticipantID)
for column_names in value_dict:
column_val = value_dict[column_names].replace(',', '')
if column_val == "True":
column_val = "1"
if column_val == "False":
column_val = "0"
if column_val == "Right":
column_val = "1"
if column_val == "Left":
column_val = "0"
if column_val == "Infinity":
column_val = str(np.Inf)
if Scenario == "Cooperation_2":
if (column_names == "Whowentfirst") or (column_names == "Whoshouldgofirst"):
if column_val.find('Human') != -1:
column_val = "1"
else:
column_val = "0"
writef.write(";" + column_val)
elif current_ent == 2:
ParticipantID = string_list[3][2]
string_list = [s]
current_ent = int(s[0])
s = dataf.readline()
if s:
s = s.split()