-
Notifications
You must be signed in to change notification settings - Fork 7
/
stressLabelDB.py
201 lines (125 loc) · 5.09 KB
/
stressLabelDB.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json,csv,sys,os,psycopg2
#-----------------------------------------------------------------------------------------------
#This script is intended to create user specific tables (postgreSQL) containing their reported
#stress level,mood,sleep alongside with the time they did so.
# table name: uXX , XX E [0,59] eg. 'u02', 'u32'
# each row: (timestamp,stress_level), stressed/not stressed = 1/0
#-----------------------------------------------------------------------------------------------
create1= "CREATE TABLE {0} (time_stamp INT, stress_level INT) ; "
insert1="INSERT INTO {0} (time_stamp,stress_level) VALUES ({1},{2}) "
create2= "CREATE TABLE {0} (time_stamp INT, mood INT) ; "
insert2="INSERT INTO {0} (time_stamp,mood) VALUES ({1},{2}) "
create3= "CREATE TABLE {0} (hour FLOAT, rate INT, time_stamp INT) ; "
insert3="INSERT INTO {0} (hour,rate,time_stamp) VALUES ({1},{2},{3});"
drop = "DROP TABLE {0};"
def main(argv):
#connecting to database
try:
con = psycopg2.connect(database='dataset', user='tabrianos')
cur = con.cursor()
except psycopg2.DatabaseError as err:
print('Error %s' % err)
exit()
#depending on what user chose, either inserts, or drops tables
if sys.argv[1]=='-stress':
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/EMA/response/Stress'
#iterating through all json files
for filename in os.listdir(directory):
try:
jsonData=open(os.path.join(directory,filename))
except(IOError, RuntimeError):
print("Could not open file : %s" % filename)
fullfile=json.load(jsonData)
#creating table for each users' reported stress level along with timestamps
uid=(filename.split('_'))[1][0:3]
createQ=create1.format(uid)
print(createQ)
cur.execute(createQ)
#inserting user specific data into table for
for i in range(0,len(fullfile)):
#defending against dirty data
if 'level' in fullfile[i]:
#converting from scale 1-5 to binay 0/1 - not stressed/stressed
if int(fullfile[i]['level'])==5:
stress = 0
elif int(fullfile[i]['level'])==4:
stress = 1
elif int(fullfile[i]['level'])==1:
stress = 2
elif int(fullfile[i]['level'])==2:
stress = 3
elif int(fullfile[i]['level'])==3:
stress = 4
insertQ=insert1.format(uid, fullfile[i]['resp_time'], stress)
print(insertQ)
cur.execute(insertQ)
elif sys.argv[1]=='-mood':
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/EMA/response/PAM'
#iterating through all json files
for filename in os.listdir(directory):
try:
jsonData=open(os.path.join(directory,filename))
except(IOError, RuntimeError):
print("Could not open file : %s" % filename)
fullfile=json.load(jsonData)
#creating table for each users' reported stress level along with timestamps
uid=(filename.split('_'))[1][0:3]+'mood'
createQ=create2.format(uid)
print(createQ)
cur.execute(createQ)
#inserting user specific data into table for
for i in range(0,len(fullfile)):
#defending against dirty data
if 'picture_idx' in fullfile[i]:
#converting from scale 1-5 to binay 0/1 - not stressed/stressed
if int(fullfile[i]['picture_idx'])>=1 and int(fullfile[i]['picture_idx'])<=4:
mood = 0
elif int(fullfile[i]['picture_idx'])>=5 and int(fullfile[i]['picture_idx'])<=8:
mood = 1
elif int(fullfile[i]['picture_idx'])>=9 and int(fullfile[i]['picture_idx'])<=12:
mood = 2
else:
mood = 3
insertQ=insert2.format(uid, fullfile[i]['resp_time'], mood)
print(insertQ)
cur.execute(insertQ)
elif sys.argv[1] == '-sleep':
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/EMA/response/Sleep'
#iterating through all json files
for filename in os.listdir(directory):
try:
jsonData=open(os.path.join(directory,filename))
except(IOError, RuntimeError):
print("Could not open file : %s" % filename)
fullfile=json.load(jsonData)
#creating table for each users' reported stress level along with timestamps
uid=(filename.split('_'))[1][0:3]+'sleep'
createQ=create3.format(uid)
print(createQ)
cur.execute(createQ)
for i in range(0,len(fullfile)):
if 'hour' and 'resp_time' and 'rate' in fullfile[i]:
timeslept = 2.5 +0.5*float(fullfile[i]['hour'])
if timeslept==2.5:
print(fullfile[i]['hour'])
else:
insertQ=insert3.format(uid, timeslept, fullfile[i]['rate'],fullfile[i]['resp_time'])
#print(insertQ)
cur.execute(insertQ)
#destroying all user tables
elif sys.argv[1]=='-drop':
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/EMA/response/Stress'
for filename in os.listdir(directory):
uid=(filename.split('_'))[1][0:3]
dropQ=drop.format(uid)
cur.execute(dropQ)
directory = os.path.dirname(os.path.abspath(__file__)) + '/dataset/EMA/response/Sleep'
for filename in os.listdir(directory):
uid=(filename.split('_'))[1][0:3]+'sleep'
dropQ=drop.format(uid)
cur.execute(dropQ)
print('Tables destroyed successfully!')
con.commit()
con.close()
if __name__ == '__main__':
main(sys.argv[1:])