-
Notifications
You must be signed in to change notification settings - Fork 0
/
iTraceDB.py
112 lines (84 loc) · 5 KB
/
iTraceDB.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
import sqlite3
IDB_TABLES = ["calibration","calibration_point","calibration_sample","files","fixation","fixation_gaze","fixation_run","gaze","ide_context","participant","session","web_context"]
# Exception Class for handling DB matching errors
class IDBDoesNotMatch(Exception):
def __str__(self):
return "The provided database is not an iTrace Toolkit Database"
# Main iTrace Database Class
class iTraceDB:
def __init__(self, path):
self.db = sqlite3.connect(path)
self.cursor = self.db.cursor()
self._verify()
# Verify the Database is a valid iTrace Database
def _verify(self):
tables = self.cursor.execute("""SELECT name FROM sqlite_master WHERE type='table';""").fetchall()
if len(tables) != len(IDB_TABLES):
raise IDBDoesNotMatch
for x in tables:
if x[0] not in IDB_TABLES:
raise IDBDoesNotMatch
# Get all the sessions from the database
def GetSessions(self):
sessions = self.cursor.execute("""SELECT * FROM session""").fetchall()
rtn = []
for session in sessions:
rtn.append(session[9] + " - " + str(session[0]))
return rtn
# Get all the sessions from the database
def GetSessionsWithParticipantID(self):
sessions = self.cursor.execute("""SELECT * FROM session""").fetchall()
rtn = []
for session in sessions:
rtn.append(session[1] + " - " + session[9] + " - " + str(session[0]))
return rtn
# Get all the fixation_runs from the selected session
def GetFixationRuns(self, session_id):
runs = self.cursor.execute("""SELECT * FROM fixation_run WHERE session_id = ?""",(session_id,)).fetchall()
rtn = []
for run in runs:
rtn.append(run[3] + " - " + str(run[0]))
return rtn
# Get all the fixation_runs from the selected session with the session id listed
def GetFixationRunsWithSession(self, session_id):
runs = self.cursor.execute("""SELECT * FROM fixation_run WHERE session_id = ?""",(session_id,)).fetchall()
rtn = []
for run in runs:
rtn.append(f"{run[3]} - {str(run[0])} - {session_id}")
return rtn
# Get the length in seconds of the session
def GetSessionTimeLength(self, session_id):
start = self.cursor.execute("""SELECT MIN(system_time) FROM gaze WHERE session_id = ?""", (session_id,)).fetchall()[0][0]
end = self.cursor.execute("""SELECT MAX(system_time) FROM gaze WHERE session_id = ?""", (session_id,)).fetchall()[0][0]
return (end - start) / 1000
# Get the System time of the session start
def GetSessionStartTime(self,session_id):
return self.cursor.execute("""SELECT session_time FROM session WHERE session_id = ?""", (session_id,)).fetchall()[0][0]
# Returns all the gazes from a session
def GetAllSessionGazes(self, session_id):
return self.cursor.execute("""SELECT * FROM gaze WHERE session_id = ?""", (session_id,)).fetchall()
# Returns all the fixations from a fixation run
def GetAllRunFixations(self, run_id):
return self.cursor.execute("""SELECT * FROM fixation WHERE fixation_run_id = ? ORDER BY fixation_start_event_time""", (run_id,)).fetchall()
# Returns all the fixations from a fixation run and on a certain file
def GetAllRunFixationsTargetingFile(self, run_id,target_file):
return self.cursor.execute("""SELECT * FROM fixation WHERE fixation_run_id = ? and fixation_target = ? ORDER BY fixation_start_event_time""", (run_id,target_file)).fetchall()
# Returns a dictionary of fixation_gazes for the selected fixation_run
def GetAllFixationGazes(self,fixations):
all_fix_gazes = {}
for fixation in fixations:
all_fix_gazes[fixation.fixation_id] = self.cursor.execute("""SELECT * FROM fixation_gaze WHERE fixation_id = ?""", (fixation.fixation_id,)).fetchall()
return all_fix_gazes
# Returns the gaze that happened at a specified event_time
def GetGazeFromEventTime(self,event_time):
return self.cursor.execute("""SELECT * FROM gaze WHERE event_time = ?""", (event_time,)).fetchall()[0]
def GetFilesLookedAtBySession(self,session_id):
return self.cursor.execute("""SELECT DISTINCT fixation_target FROM fixation INNER JOIN gaze ON fixation.fixation_start_event_time = gaze.event_time WHERE session_id = ?""", (session_id,)).fetchall()
def GetParticipantFromSessionID(self,session_id):
return self.cursor.execute("""SELECT participant_id FROM session WHERE session_id = ?""", (session_id,)).fetchall()[0][0]
def GetTaskFromSessionID(self,session_id):
return self.cursor.execute("""SELECT task_name FROM session WHERE session_id = ?""", (session_id,)).fetchall()[0][0]
# Gets the session from a fixation run
#def GetSessionOfFixationRun(self,run_id):
# return self.cursor.execute("""SELECT session_id FROM fixation_run WHERE fixation_run_id = ?""", (run_id,)).fetchall()[0][0]
# Calculates and returns the saccades of a fixation_run