-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmake_mapping.py
188 lines (141 loc) · 5.08 KB
/
make_mapping.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
"""
Reads all the files and makes a master mapping to specific files in EvoBio-10M. All files in the 10M have a UUID as the key. This script:
1. Creates the initial mapping (without reading the file contents)
2. Writes all these details to a sqlite database.
"""
import argparse
import logging
import multiprocessing
import os
import tarfile
import uuid
from imageomics import disk, eol, evobio10m, helpers
db_write_frequency = 10_000
log_format = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
logging.basicConfig(level=logging.INFO, format=log_format)
def get_global_id():
return str(uuid.uuid4())
def get_logger():
return logging.getLogger(f"p{os.getpid()}")
######################
# Encyclopedia of Life
######################
def read_eol_from_tar(imgset_path):
"""
Reads all filenames from an imgset (.tar.gz file), assigns a uuid, then inserts it into a sqlite database.
"""
eol_insert_stmt = """
INSERT INTO eol
(content_id, page_id, evobio10m_id)
VALUES
(?, ?, ?);
"""
db = evobio10m.get_db(db_path)
insert_values = []
# r|gz indcates reading from a gzipped file, streaming only
with tarfile.open(imgset_path, "r|gz") as tar:
for i, member in enumerate(tar):
eol_img = eol.ImageFilename.from_filename(member.name)
global_id = get_global_id()
insert_values.append((eol_img.content_id, eol_img.page_id, global_id))
if i % db_write_frequency == 0:
helpers.executerobustly(db, eol_insert_stmt, insert_values)
insert_values = []
# flush any leftover values
helpers.executerobustly(db, eol_insert_stmt, insert_values)
db.close()
#########
# BIOSCAN
#########
def read_bioscan_from_part(part):
bioscan_insert_stmt = """
INSERT INTO bioscan
(part, filename, evobio10m_id)
VALUES
(?, ?, ?);
"""
# each process get its own db connection.
db = evobio10m.get_db(db_path)
insert_values = []
partdir = os.path.join(disk.bioscan_root_dir, f"part{part}")
for i, filename in enumerate(os.listdir(partdir)):
global_id = get_global_id()
insert_values.append((part, filename, global_id))
if i % db_write_frequency == 0:
helpers.executerobustly(db, bioscan_insert_stmt, insert_values)
insert_values = []
# flush any leftover values
helpers.executerobustly(db, bioscan_insert_stmt, insert_values)
db.close()
########
# INAT21
########
def read_inat21_from_clsdir(clsdir):
inat21_insert_stmt = """
INSERT INTO inat21
(filename, cls_name, cls_num, evobio10m_id)
VALUES
(?, ?, ?, ?);
"""
# each process get its own db connection.
db = evobio10m.get_db(db_path)
insert_values = []
for i, filename in enumerate(
os.listdir(os.path.join(disk.inat21_root_dir, clsdir))
):
global_id = get_global_id()
index, *taxa = clsdir.split("_")
index = int(index)
taxon = "_".join(taxa)
insert_values.append((filename, taxon, index, global_id))
if i % db_write_frequency == 0:
helpers.executerobustly(db, inat21_insert_stmt, insert_values)
insert_values = []
# flush any leftover values
helpers.executerobustly(db, inat21_insert_stmt, insert_values)
db.close()
def worker(queue):
logger = logging.getLogger(f"p{os.getpid()}")
for func, args in iter(queue.get, sentinel):
logger.info(f"Started {func.__name__}({', '.join(map(str, args))})")
func(*args)
logger.info(f"Finished {func.__name__}({', '.join(map(str, args))})")
sentinel = "STOP"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--tag", default="dev", help="The suffix for the directory.")
parser.add_argument(
"--workers", type=int, default=32, help="Number of processes to use."
)
args = parser.parse_args()
# Set up some global variables that depend on CLI args.
outdir = evobio10m.get_outdir(args.tag)
os.makedirs(outdir, exist_ok=True)
db_path = os.path.join(outdir, "mapping.sqlite")
print(f"Writing to {db_path}.")
# 1. Create an initial mapping of all images in a sqlite database.
task_queue = multiprocessing.Queue()
# Submit all tasks
# EOL
for imgset_name in sorted(os.listdir(disk.eol_root_dir)):
assert imgset_name.endswith(".tar.gz")
imgset_path = os.path.join(disk.eol_root_dir, imgset_name)
task_queue.put((read_eol_from_tar, (imgset_path,)))
# Bioscan
# 113 parts in bioscan
for i in range(1, 114):
task_queue.put((read_bioscan_from_part, (i,)))
# iNat
for clsdir in os.listdir(disk.inat21_root_dir):
task_queue.put((read_inat21_from_clsdir, (clsdir,)))
processes = []
# Start worker processes
for i in range(args.workers):
p = multiprocessing.Process(target=worker, args=(task_queue,))
processes.append(p)
p.start()
# Stop worker processes
for i in range(args.workers):
task_queue.put(sentinel)
for p in processes:
p.join()