-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.py
More file actions
executable file
·292 lines (237 loc) · 9.93 KB
/
Copy pathimport.py
File metadata and controls
executable file
·292 lines (237 loc) · 9.93 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
# Import data from the old tvnews database. This takes a very long time...
#
# Don't forget to set postgres performance flags
#
# shared_buffers = 1GB
# work_mem = 2GB
# max_worker_processes = 24
# max_parallel_workers_per_gather = 12
# max_parallel_workers = 12
import argparse
import csv
import os
import shutil
import tempfile
from multiprocessing import Pool
import psycopg2
import sqlalchemy
from tqdm import tqdm
import schema
from util import get_or_create, parse_video_name
VIDEO_EXT = '.mp4'
def load_videos(session, video_csv, show_to_canonical_show_csv):
print('Importing video, show, canonical_show')
canonical_show_dict = {}
with open(show_to_canonical_show_csv) as fp:
fp.readline() # Skip headers
reader = csv.reader(fp)
for row in reader:
show, canonical_show, is_recurring = row
is_recurring = is_recurring.upper() == 'TRUE'
canonical_show_dict[show] = (canonical_show, is_recurring)
with open(video_csv) as fp:
fp.readline() # Skip headers
reader = csv.reader(fp)
for row in tqdm(reader):
(
vid, name, num_frames, fps, width, height,
is_duplicate, is_corrupt
) = row
vid = int(vid)
assert name.endswith(VIDEO_EXT)
name = name[:-len(VIDEO_EXT)]
extension = VIDEO_EXT
num_frames = int(num_frames)
fps = float(fps)
width = int(width)
height = int(height)
is_duplicate = is_duplicate[0].upper() == 'T'
is_corrupt = is_corrupt[0].upper() == 'T'
channel, show, timestamp = parse_video_name(name)
canonical_show, is_recurring = canonical_show_dict.get(show, (None, False))
# create Channel if needed
channel_object = get_or_create(session, schema.Channel, name=channel)
# create CanonicalShow if needed
if canonical_show is None:
print('Missing canonical show for show:', show)
canonical_show = show
canonical_show_object = get_or_create(
session, schema.CanonicalShow,
name=canonical_show, is_recurring=is_recurring,
channel_id=channel_object.id)
# create Show if needed
show_object = get_or_create(
session, schema.Show, name=show,
channel_id=channel_object.id,
canonical_show_id=canonical_show_object.id)
# create the Video
session.add(schema.Video(
id=vid, name=name, extension=extension, num_frames=num_frames,
fps=fps, width=width, height=height, time=timestamp,
show_id=show_object.id, is_duplicate=is_duplicate,
is_corrupt=is_corrupt))
session.commit()
def load_hosts_staff(session, host_staff_csv):
print('Importing channel_host, canonical_show_host')
with open(host_staff_csv) as fp:
fp.readline() # Skip headers
reader = csv.reader(fp)
for row in tqdm(reader):
channel_name, canonical_show_name, identity_name = row
identity_id = session.query(schema.Identity).filter_by(
name=identity_name).one().id
channel_id = session.query(schema.Channel).filter_by(
name=channel_name).one().id
if canonical_show_name == '':
# create new host_staff_row
session.add(
schema.ChannelHosts(
identity_id=identity_id, channel_id=channel_id))
else:
canonical_show_id = session.query(schema.CanonicalShow).filter_by(
name=canonical_show_name, channel_id=channel_id).one().id
# create new host_staff_row
session.add(
schema.CanonicalShowHosts(
identity_id=identity_id,
canonical_show_id=canonical_show_id))
session.commit()
def load_via_copy(conn, import_path, table):
cur = conn.cursor()
source_csv = os.path.join(import_path, '{}.csv'.format(table))
with open(source_csv) as fp:
headers = fp.readline() # Skip the headers
print('Importing {} with columns ({})'.format(table, headers[:-1]))
# TODO disable triggers for constraints
cur.copy_expert('copy {}({}) from stdin (format csv)'.format(table, headers), fp)
conn.commit()
def init_worker(function, conn_args):
function.conn = psycopg2.connect(**conn_args)
def copy_worker(args):
source_csv, table = args
cur = copy_worker.conn.cursor()
with open(source_csv) as fp:
headers = fp.readline() # Skip the headers
cur.copy_expert('copy {}({}) from stdin (format csv)'.format(table, headers), fp)
copy_worker.conn.commit()
os.remove(source_csv)
copy_worker.conn = None
def split_csv(source_csv, n, out_dir):
chunks = []
with open(source_csv, 'r') as fp:
headers = fp.readline()
ofp = None
for i, line in enumerate(fp):
if ofp is None:
chunk_path = os.path.join(out_dir, '{}.csv'.format(i))
chunks.append(chunk_path)
ofp = open(chunk_path, 'w')
ofp.write(headers)
ofp.write(line)
if i % n == n - 1:
ofp.close()
ofp = None
if ofp is not None:
ofp.close()
return chunks, headers
def parallel_load_via_copy(conn_args, import_path, table, n=100000):
source_csv = os.path.join(import_path, '{}.csv'.format(table))
tmp_dir = None
try:
tmp_dir = tempfile.mkdtemp(prefix='db-import-{}-'.format(table))
print('Splitting {} csv: {}'.format(table, tmp_dir))
chunks, headers = split_csv(source_csv, n, tmp_dir)
print('Importing {} with columns ({})'.format(table, headers[:-1]))
with Pool(initializer=init_worker, initargs=(copy_worker, conn_args)) as p:
for _ in tqdm(
p.imap_unordered(copy_worker, [(c, table) for c in chunks]),
total=len(chunks)
):
pass
finally:
if tmp_dir and os.path.isdir(tmp_dir):
shutil.rmtree(tmp_dir)
def set_id_sequence(conn, table):
cur = conn.cursor()
cur.execute('SELECT MAX(id) FROM {}'.format(table))
max_id = cur.fetchone()[0]
print('Resetting id sequence for {} to {}'.format(table, max_id))
cur.execute('ALTER SEQUENCE {}_id_seq RESTART WITH {}'.format(table, max_id + 1))
conn.commit()
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('import_path', type=str)
parser.add_argument('--db-name', type=str, default='tvnews')
parser.add_argument('--db-user', type=str, default='admin')
return parser.parse_args()
def main(import_path, db_name, db_user):
password = os.getenv('POSTGRES_PASSWORD')
engine = sqlalchemy.create_engine(
'postgresql://{}:{}@localhost/{}'.format(db_user, password, db_name))
conn_args = {
'dbname': db_name, 'user': db_user, 'host': 'localhost',
'password': password
}
conn = psycopg2.connect(**conn_args)
schema.Face.metadata.create_all(engine)
Session = sqlalchemy.orm.sessionmaker(bind=engine, expire_on_commit=False)
session = Session()
# # Load the rest of the tables, that don't require ETL, with COPY.
# This order matters. It must obey dependencies.
load_via_copy(conn, import_path, 'labeler')
load_via_copy(conn, import_path, 'frame_sampler')
load_via_copy(conn, import_path, 'gender')
load_via_copy(conn, import_path, 'identity')
# videos, channel, show, and canonical_show require special pre-processing
load_videos(session, os.path.join(import_path, 'video.csv'),
os.path.join(import_path, 'show_to_canonical_show.csv'))
# hosts_and_staff requires special processing as well. It depends on the
# identity table.
load_hosts_staff(session, os.path.join(import_path, 'hosts_and_staff.csv'))
# These tables depend on video, and must be loaded after it
load_via_copy(conn, import_path, 'commercial')
parallel_load_via_copy(conn_args, import_path, 'frame')
parallel_load_via_copy(conn_args, import_path, 'face')
parallel_load_via_copy(conn_args, import_path, 'face_gender')
parallel_load_via_copy(conn_args, import_path, 'face_identity')
# Rename commercial labeler
print('Renaming commercial labeler')
session.query(schema.Labeler).filter_by(name='haotian-commercials').update({
schema.Labeler.name: 'commercials'
})
session.commit()
# Set sequence numbers
conn = psycopg2.connect(dbname=db_name, user=db_user, host='localhost',
password=password)
set_id_sequence(conn, 'labeler')
set_id_sequence(conn, 'frame_sampler')
set_id_sequence(conn, 'gender')
set_id_sequence(conn, 'identity')
set_id_sequence(conn, 'video')
set_id_sequence(conn, 'commercial')
set_id_sequence(conn, 'frame')
set_id_sequence(conn, 'face')
# Set up missing rows
print('Set up new rows')
session.add(schema.Labeler(name='handlabeled-gender', is_handlabel=True))
session.add(schema.Labeler(name='commercials-1s', is_handlabel=False))
session.add(schema.FrameSampler(name='1s'))
# Drop extra mtcnn labeler
print('Dropping extra mtcnn labeler')
mtcnn_labeler_id = session.query(schema.Labeler).filter_by(
name='mtcnn'
).one().id
duplicate_mtcnn_labeler_id = session.query(schema.Labeler).filter_by(
name='mtcnn:july-25-2019'
).one().id
session.query(schema.Face).filter_by(
labeler_id=duplicate_mtcnn_labeler_id).update({
schema.Face.labeler_id: mtcnn_labeler_id})
session.query(schema.Labeler).filter_by(
id=duplicate_mtcnn_labeler_id
).delete()
session.commit()
print('Done!')
if __name__ == '__main__':
main(**vars(get_args()))