-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_png_cache.py
147 lines (121 loc) · 5.13 KB
/
make_png_cache.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
import logging
import json
import os
import requests
import time
import multiprocessing
from functools import partial
from Scantensus.datasets.unity import UnityO
SOURCE = "b"
NUM_PROCESSES = 6
# Use the direct ip so you dont get rate limited
MAGIQUANT_ADDRESS = "89.39.141.131"
# MAGIQUANT_ADDRESS = "files.magiquant.com"
if SOURCE == "a":
SOURCE_TYPE = "json"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/labels/unity/labels.json"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "b":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/imp-echo-validation100-a4c.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "c":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/A4CH_LV.csv"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache-a4c/"
elif SOURCE == "d":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/imp-echo-stowell-a4c-lv-systole.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "e":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/unity-plax-1.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "f":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/imp-echo-shunshin-plax-measure-100.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "g":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/unity-plax-2.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "h":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/imp-echo-stowell-plax-measure-train-b.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "i":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/a4c_zoom_all.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache/"
elif SOURCE == "j":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/unity-a4c-1.txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/test_agg1.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache-3d/"
elif SOURCE == "k":
SOURCE_TYPE = "txt"
LABELS_PATH = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/validation/02-validation100_a4c_filehash.txt"
OUTPUT_ROOT_DIR = "/Volumes/Matt-Data/Projects-Clone/scantensus-data/png-cache-02-a4c/"
else:
print(f"No Source Set")
def download_hash(file, output_root_dir):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info(f"{file} Starting")
if len(file) == 67:
hash = file
ALL_FRAMES = True
print(f"{file} All Frames")
elif len(file) == 76:
hash = file[:-9]
ALL_FRAMES = False
frame_num = int(file[-8:-4])
frame_nums = [frame_num-5, frame_num, frame_num+5]
elif len(file) == 72:
hash = file[:-5]
ALL_FRAMES = False
frame_num = int(file[-4:])
frame_nums = [frame_num-5, frame_num, frame_num+5]
else:
return
if ALL_FRAMES:
frame_nums = range(1000)
failed_count = 0
for frame_num in frame_nums:
if failed_count > 5:
break
sub_a = hash[:2]
sub_b = hash[3:5]
sub_c = hash[5:7]
file_name = f"{hash}-{frame_num:04}.png"
location = f"http://{MAGIQUANT_ADDRESS}/scantensus-database-png-flat/{sub_a}/{sub_b}/{sub_c}/{file_name}"
output_dir = os.path.join(output_root_dir, sub_a, sub_b, sub_c)
output_path = os.path.join(output_dir, file_name)
os.makedirs(output_dir, exist_ok=True)
logger.warning(f"{location} Downloading")
response = requests.get(location)
if response.status_code == 200:
with open(output_path, 'wb') as outfile:
outfile.write(response.content)
else:
failed_count = failed_count + 1
logger.warning(f"{location} Fail {response.status_code}")
time.sleep(0.5)
def main():
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if SOURCE_TYPE == "json":
with open(LABELS_PATH, 'r') as json_f:
db_raw = json.load(json_f)
hashes = list(db_raw.keys())
if SOURCE_TYPE == "txt":
hashes = []
with open(LABELS_PATH, 'r') as labels_f:
for line in labels_f:
hashes.append(line[:-1])
pool = multiprocessing.Pool(NUM_PROCESSES)
pool.map(partial(download_hash, output_root_dir=OUTPUT_ROOT_DIR), hashes)
pool.close()
pool.join()
if __name__ == "__main__":
main()