def store_in_table(self): # Compute and store in table Path the paths of all the # avatars of all the relevant sessions # cleaning and rebuilding the table con = lite.connect('everscape.db') with con: cur = con.cursor() try: cur.execute("\ DROP TABLE Path;\ ") except: print('could not drop table Path') cur.execute("\ CREATE TABLE Path(\ session_id INTEGER,\ avatar_no INTEGER,\ path_going INTEGER,\ path_returning INTEGER,\ PRIMARY KEY (session_id, avatar_no)\ );\ ") global sessions print(sessions) avatar_llist = [] path_hashes = [] for session_index in range(len(sessions)): # iterating over the sessions avatar_llist.append(\ hp.Avatar_of_session(session_index)\ ) path_hashes.append({}) logs = hp.train_logs(session_index) # taking the index of the avatar avatar_index = 0 for avatar in avatar_llist[-1]: #iterating over the avatars in one session # Find the way an avatar took on the way on # and the way back, if he took a car. print("") path = self.classify(session_index, avatar_index) print(path) if hp.granted_train(logs, avatar): # reads the train logs to check if the avatar # took the train on the way back path['after']='C' # organizes all the info in a hash path_hashes[-1][avatar]=\ [path['before'], path['after']] # updating the index of the avatar avatar_index += 1 con = lite.connect('everscape.db') with con: # Stores the hash containing all the info in the # database cur = con.cursor() for session_index in range(len(sessions)): # Iterating over sessions for avatar in avatar_llist[session_index]: # Iterating over the avatars in a session before = path_hashes[session_index][avatar][0] after = path_hashes[session_index][avatar][1] if before == 'A': before = 1 elif before == 'B': before = 2 if after == 'A': after = 1 elif after == 'B': after = 2 elif after == 'C': after = 3 cur.execute("\ INSERT INTO Path\ VALUES (%d, %d, %d, %d);"%(\ sessions[session_index],\ avatar,\ before,\ after\ )\ )
def __init__(self, nb_trees = 4): #builds the KD-tree extracting people from choice #'train' on one hand, on choice 'car' (A or B) on the #other hand, concatening the points on each side, and #then concatening the two sides remembering the #junction indice. self.nb_trees = nb_trees car_array = [] train_array = [] choice_hashes = {} con = lite.connect('everscape.db') with con: # Loading the choices of avatars cur = con.cursor() cur.execute("\ SELECT * FROM Path\ ") rows = cur.fetchall() for row in rows: # Stores and sorts those choices in hashes try: # Ensures the hash corresponding to the sessions # is created, else creates it. choice_hashes[row[0]] except: choice_hashes[row[0]] = {} # For each session hash, the key is the avatar, # the value is its choice. # Here all the sessions and avatar numbers are # real numbers, not the indices choice_hashes[row[0]][row[1]]=row[3] for session in choice_hashes: # Iterating over the sessions, here the session # variable is a hash (iterating over an array of # hashes # Getting the index of the session in order to be # able to get the hash containing the choices made # by the avatars during the session session_index = sessions.index(session) # Train logs. Not only people who choose train # went to the train station. Some other people # choose to go to the station but were not granted # a seat. In the scope of this analysis we # consider they made the choice to go to the # station. logs = hp.train_logs(session_index) # Getting the hash containing the choices of the # avatars in the session. avatars_choice = choice_hashes[session] # Usefull to retrieve the index of the avatars avatar_array = hp.Avatar_of_session(session_index) for avatar in avatars_choice: # Iterating in this hash # Usefull to call the method from helpers file avatar_index = avatar_array.index(avatar) # Getting the trajectory of the considered # avatar. trajectory = hp.Trajectory_points(session_index,\ avatar_index) # We only take the part of the trajectory # after the earthquake. If the avatar went by # car and returned by train, mingling the two # parts of the trajectory is problematic. time_earthquake =\ hp.earthquake_time(session_index) if (avatars_choice[avatar] == 1 or \ avatars_choice[avatar] == 2) and\ not hp.requested_train(logs, avatar): # Carefull, some people who took a car # went first to the train station. We # don't want to take those people into # account to build a tree against which we # will look up trajectories. for point in trajectory: if point[3] > time_earthquake: # Only the part of trajectory after # earthquake is usefull. car_array.append([point[0],\ point[1]]) # all_array.append([point[0],\ # point[1]]) elif avatars_choice[avatar] == 3: # print("classified train") for point in trajectory: if point[3] > time_earthquake: # Only the part of trajectory after # earthquake is usefull. train_array.append (\ [point[0], point[1]]) # When I will ask the tree for neighbours, the tree # will give back indices of the data points # corresponding to neighbours. As I will concatene # the data points from car user with data points from # train users, I have to keep track of the limit # indice between both dataset in order to trace which # indices correspond to which dataset self.limit = len(car_array) # Here we concatene the to datasets data = car_array for position in train_array: data.append(position) # Building a KDTree is recursive and here we have some # volume sys.setrecursionlimit(10000) # Building many times the same tree (it is fast) so # that later, for slow parts of the program we can # make parallel computing (to compute the time of # decision). self.trees = [] for i in range(self.nb_trees): self.trees.append(-1) tree = sp.KDTree(data) for i in range(self.nb_trees): self.trees[i] = tree