-
Notifications
You must be signed in to change notification settings - Fork 2
/
db_export.py
executable file
·255 lines (214 loc) · 11.5 KB
/
db_export.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# -*- coding: utf-8 -*-
from datetime import time
import pandas as pd
import json
import mysql.connector
from crypt import load_key, decrypt
database = "alienzoo"
user_name = "user_alienzoo"
user_pw = "useralienzoopw123456"
class DataMgr():
def __init__(self):
self.db = mysql.connector.connect(host="localhost", user=user_name, password=user_pw, database=database)
# Create dictionary where userId is mapped to group
self.user_groups = {}
self._get_users_groups()
def _get_users_groups(self):
try:
cur = self.db.cursor()
cur.execute("SELECT userId, controlGroup FROM users")
for row in cur.fetchall():
self.user_groups[str(row[0])] = int(row[1])
return True
except Exception as ex:
print(ex)
return False
def export_performance(self, file_out="performance.csv"):
try:
data_userId = []
data_group = []
data_blockNo = []
data_trialNo = []
data_plant1 = [];data_plant2 = [];data_plant3 = [];data_plant4 = [];data_plant5 = []
data_CFplant1 = [];data_CFplant2 = [];data_CFplant3 = [];data_CFplant4 = [];data_CFplant5 = []
data_shubNoOld = []
data_shubNoNew = []
cur = self.db.cursor()
cur.execute("SELECT userId, data FROM logs")
for row in cur.fetchall():
d = json.loads(row[1])
if "cfPlants" in d:
data_userId.append(str(row[0]))
data_group.append(self.user_groups[str(row[0])])
data_blockNo.append(int(d["blockCount"]))
data_trialNo.append(int(d["trialCount"]))
data_shubNoOld.append(int(d["shubNoOld"]))
data_shubNoNew.append(int(d["shubNoNew"]))
plants = d["plants"];data_plant1.append(plants[0]);data_plant2.append(plants[1]);data_plant3.append(plants[2]);data_plant4.append(plants[3]);data_plant5.append(plants[4])
cfPlants = d["cfPlants"];data_CFplant1.append(cfPlants[0]);data_CFplant2.append(cfPlants[1]);data_CFplant3.append(cfPlants[2]);data_CFplant4.append(cfPlants[3]);data_CFplant5.append(cfPlants[4])
df = pd.DataFrame({"userId": data_userId, "group": data_group, "blockNo": data_blockNo, "trialNo": data_trialNo,
"plant1": data_plant1, "plant2": data_plant2, "plant3": data_plant3, "plant4": data_plant4, "plant5": data_plant5,
"CFplant1": data_CFplant1, "CFplant2": data_CFplant2, "CFplant3": data_CFplant3, "CFplant4": data_CFplant4, "CFplant5": data_CFplant5,
"shubNoOld": data_shubNoOld, "shubNoNew": data_shubNoNew})
df.to_csv(file_out, index=False)
return True
except Exception as ex:
print(ex)
return False
def export_reactionTimes(self, file_out="reactionTime.csv"):
try:
data = {}
data_userId = []
data_group = []
data_BlockNr = []
data_TrialNr = []
data_timeAgreementScene = []
data_timeStartScene = []
data_timeStableUntilFeeding = []
data_timeFeedbackScene = []
cur = self.db.cursor()
cur.execute("SELECT userId, eventId, timeElapsed, blockId, trialId FROM elapsedtime_logs")
for row in cur.fetchall():
user_id = str(row[0])
event_id = int(row[1])
time_elapsed = int(row[2])
block_id = int(row[3])
trial_id = int(row[4])
if user_id not in data: # Create structure
data[user_id] = {"timeAgreementScene": -1, "timeStartScene": -1, "blocks": {}}
if block_id != -1:
if block_id not in data[user_id]["blocks"]:
data[user_id]["blocks"][block_id] = {"trialNr": {}, "timeFeedbackScene": -1}
if block_id - 1 not in data[user_id]["blocks"]:
data[user_id]["blocks"][block_id-1] = {"trialNr": {}, "timeFeedbackScene": -1}
if trial_id != -1:
if trial_id not in data[user_id]["blocks"][block_id]["trialNr"]:
data[user_id]["blocks"][block_id]["trialNr"][trial_id] = {"timeStableUntilFeeding": -1}
# Process event
if event_id == 0: # timeAgreementScene
data[user_id]["timeAgreementScene"] = time_elapsed
elif event_id == 1:
pass
elif event_id == 2: # timeStartScene
data[user_id]["timeStartScene"] = time_elapsed
elif event_id == 3: # timeStableUntilFeeding
data[user_id]["blocks"][block_id]["trialNr"][trial_id]["timeStableUntilFeeding"] = time_elapsed
elif event_id == 4: # timeStableClickFeedbackScene
pass
elif event_id == 5: # timeFeedbackScene
data[user_id]["blocks"][block_id-1]["timeFeedbackScene"] = time_elapsed
elif event_id == 6: # timeAttentionScene
pass
else:
print("Unknown event_id")
# Create final data frame
for user_id in data.keys():
for block_id in data[user_id]["blocks"]:
for trial_id in data[user_id]["blocks"][block_id]["trialNr"]:
if data[user_id]["blocks"][block_id]["timeFeedbackScene"] == -1 or data[user_id]["blocks"][block_id]["trialNr"][trial_id]["timeStableUntilFeeding"] == -1:
continue
data_userId.append(user_id)
data_group.append(self.user_groups[user_id])
data_BlockNr.append(block_id)
data_TrialNr.append(trial_id)
data_timeAgreementScene.append(data[user_id]["timeAgreementScene"])
data_timeStartScene.append(data[user_id]["timeStartScene"])
data_timeStableUntilFeeding.append(data[user_id]["blocks"][block_id]["trialNr"][trial_id]["timeStableUntilFeeding"])
data_timeFeedbackScene.append(data[user_id]["blocks"][block_id]["timeFeedbackScene"])
df = pd.DataFrame({"userId": data_userId, "group": data_group, "BlockNr": data_BlockNr, "TrialNr": data_TrialNr, "timeAgreementScene": data_timeAgreementScene, "timeStartScene": data_timeStartScene, "timeStableUntilFeeding": data_timeStableUntilFeeding, "timeFeedbackScene": data_timeFeedbackScene})
df.to_csv(file_out, index=False)
return True
except Exception as ex:
print(ex)
return False
def export_survey(self, file_out_survey="survey.csv", file_out_demographics="demographics.csv"):
try:
# Survey
data_userId = []
data_group = []
data_itemNo = []
data_reponseNo = []
data_checked = []
cur = self.db.cursor()
cur.execute("SELECT userId, questionId, var1, var2, var3, var4, var5, var6 FROM questionnaire_logs")
for row in cur.fetchall():
for i in range(2, 8):
data_userId.append(str(row[0]))
data_group.append(self.user_groups[str(row[0])])
data_itemNo.append(int(row[1]))
data_reponseNo.append(i - 1)
data_checked.append(int(row[i]))
df = pd.DataFrame({"userId": data_userId, "group": data_group, "itemNo": data_itemNo, "responseNo": data_reponseNo, "checked": data_checked})
df.to_csv(file_out_survey, index=False)
# Demographics
data_demographics_userId = []
data_demographics_group = []
data_demographics_item = []
data_demographics_responseNo = []
data_demographics_checked = []
cur = self.db.cursor()
cur.execute("SELECT userId, varAge1, varAge2, varAge3, varAge4, varAge5, varAge6, varAge7, varGender1, varGender2, varGender3, varGender4, varGender5, varGender6, varGender7 FROM demographics")
for row in cur.fetchall():
for i in range(1, 8):
data_demographics_userId.append(str(row[0]))
data_demographics_group.append(self.user_groups[str(row[0])])
data_demographics_item.append("age")
data_demographics_responseNo.append(i)
data_demographics_checked.append(int(row[i]))
for i in range(8, 15):
data_demographics_userId.append(str(row[0]))
data_demographics_group.append(self.user_groups[str(row[0])])
data_demographics_item.append("gender")
data_demographics_responseNo.append(i - 7)
data_demographics_checked.append(int(row[i]))
df_demographics = pd.DataFrame({"userId": data_demographics_userId, "group": data_demographics_group, "item": data_demographics_item, "responseNo": data_demographics_responseNo, "checked": data_demographics_checked})
df_demographics.to_csv(file_out_demographics, index=False)
return True
except Exception as ex:
print(ex)
return False
def export_attentionCheck(self, file_out="attentionCheck.csv"):
try:
data_userId = []
data_group = []
data_trialNo = []
data_userInput = []
data_shubNo = []
cur = self.db.cursor()
cur.execute("SELECT userId, data FROM logs")
for row in cur.fetchall():
d = json.loads(row[1])
if "userPrediction" in d:
data_userId.append(str(row[0]))
data_group.append(self.user_groups[str(row[0])])
data_trialNo.append(int(d["trialCount"]))
data_userInput.append(int(d["userPrediction"]))
data_shubNo.append(int(d["n_shubs"]))
df = pd.DataFrame({"userId": data_userId, "group": data_group, "trialNo": data_trialNo, "userInput": data_userInput, "shubNo": data_shubNo})
df.to_csv(file_out, index=False)
return True
except Exception as ex:
print(ex)
return False
def export_decrypted_payment_ids(self, file_out="decryptedPaymentIds.csv"):
try:
data_userId = []
data_paymentId = []
cur = self.db.cursor()
cur.execute("SELECT userId, paymentId FROM users_payout")
for row in cur.fetchall():
data_userId.append(str(row[0]))
data_paymentId.append(str(row[1]))
private_key = load_key("private_key.bin")
data_paymentId = [decrypt(pid, private_key) for pid in data_paymentId]
df = pd.DataFrame({"userId": data_userId, "paymentId": data_paymentId})
df.to_csv(file_out, index=False)
return True
except Exception as ex:
print(ex)
return False
def export_everything(self):
return self.export_performance() and self.export_reactionTimes() and self.export_attentionCheck() and self.export_survey() and self.export_decrypted_payment_ids()
if __name__ == "__main__":
dbmgr = DataMgr()
print(dbmgr.export_everything())