-
Notifications
You must be signed in to change notification settings - Fork 0
/
inserter.py
167 lines (143 loc) · 4.91 KB
/
inserter.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
import os
import re
from dotenv import load_dotenv, find_dotenv
from tqdm import tqdm
from connector import Connector
from psycopg2.extras import execute_batch
load_dotenv(find_dotenv())
def read_all_users(db: Connector, max_users: int):
"""
Read all users, or up to a specified max number of users (for testing).
All user data will be inserted into the database.
"""
folder = os.environ.get("DATASET_PATH")
subfolders = os.listdir(folder)
folder_count = len(subfolders)
if max_users > folder_count:
max_users = folder_count
progress_bar = tqdm(total=max_users)
tqdm.write("Truncating tables before starting insert stage")
query = "TRUNCATE TABLE users CASCADE"
db.cursor.execute(query)
db.connection.commit()
users = 0
for subfolder in subfolders:
read_user(db, folder, subfolder)
progress_bar.update(1)
users += 1
if users >= max_users:
break
def read_user(db: Connector, data_folder: str, user: str):
user_folder = os.path.join(data_folder, user)
# Find labels (if any)
labels = None
has_labels = False
label_file = os.path.join(user_folder, "labels.txt")
if os.path.exists(label_file):
labels = read_labels(label_file)
has_labels = True
# Add user to database (must escape ID to keep it as string)
query = f"INSERT INTO users(id, has_labels) VALUES ('{user}', {has_labels});"
db.cursor.execute(query)
db.connection.commit()
# Find and insert all activities for this user
tqdm.write(f"Inserting activities for user {user}")
activity_folder = os.path.join(user_folder, "Trajectory")
for root, _, files in os.walk(activity_folder):
for file in files:
plt_file = os.path.join(root, file)
parse_trackpoints(db, user, plt_file, labels)
def read_labels(label_file: str) -> list:
"""
Get all labels for a given user.
Returns a list of labels, where each label is a list on the format
[start_time, end_time, transportation_mode]
"""
labels = []
with open(label_file) as label_reader:
next(label_reader) # skips header
file = label_reader.read()
for row in file.splitlines():
item = re.split("\t|\s", row.strip())
# Parse dates to datetime
item = [l.replace("/", "-") for l in item]
start = f"{item[0]} {item[1]}"
end = f"{item[2]} {item[3]}"
mode = item[4]
label = [start, end, mode]
labels.append(label)
return labels
def parse_trackpoints(db: Connector, user: str, file_path: str, labels: list):
"""
Parse and insert a single .plt file.
Data columns:
0. Latitude
1. Longitude
2. 0 (skip)
3. Altitude (-777 for invalid values)
4. Date in days (skip)
5. Date as string
6. Time as string
"""
# Data to insert
activity = {}
activity["user"] = user
trackpoints = []
"""
Read and parse .plt file.
Skip the first 6 lines, remove whitespace and linebreaks, and split
comma-separated values into a list.
Skip the file if it has more than 2500 entries.
"""
file = open(file_path, "r")
rows = file.readlines()
rows = [x.strip().split(",") for x in rows[6:]]
if len(rows) > 2500:
return
file.close()
"""
Find a matching label (if any).
If the user has labels, we check if the first and last trackpoint in
the activity matches one of those labels. If we don't find a match,
we don't label the activity.
"""
activity["label"] = None
first = rows[0]
last = rows[-1]
activity["start"] = f"{first[5]} {first[6]}"
activity["end"] = f"{last[5]} {last[6]}"
if labels:
for item in labels:
start, end, mode = item
if start == activity["start"] and end == activity["end"]:
activity["label"] = mode
break
"""
Insert the activity into the database and get its ID in return.
"""
query = f"""
INSERT INTO activities
(user_id, transportation_mode, start_date_time, end_date_time)
VALUES (%(user)s, %(label)s, %(start)s, %(end)s)
RETURNING id;
"""
db.cursor.execute(query, activity)
db.connection.commit()
# ID of the activity record that was just inserted
activity_id = db.cursor.fetchone()[0]
for row in rows:
point = {}
point["activity_id"] = activity_id
point["lat"] = float(row[0])
point["lon"] = float(row[1])
point["alt"] = float(row[3])
if point["alt"] == -777:
point["alt"] = None
point["date_time"] = f"{row[5]} {row[6]}"
trackpoints.append(point)
query = f"""
INSERT INTO trackpoints (activity_id, lat, lon, altitude, date_time)
VALUES (%(activity_id)s, %(lat)s, %(lon)s, %(alt)s, %(date_time)s);
"""
execute_batch(db.cursor, query, trackpoints, 1000)
db.connection.commit()