-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
321 lines (279 loc) · 9.78 KB
/
utils.py
File metadata and controls
321 lines (279 loc) · 9.78 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from __future__ import annotations
import logging
import os
import shutil
import subprocess
import uuid
from typing import List, Sequence, Optional, Dict, Any
import numpy as np
from attrs import define
from pyspark.sql import SparkSession, DataFrame
from pyspark.sql.types import StructType
def load_dataframe(
spark: SparkSession, input_path: str, scheme: Optional[StructType | str] = None
) -> DataFrame:
file_extension = input_path.split(".")[-1].lower()
def infer_delimiter(_first_line):
if "\t" in _first_line:
return "\t"
elif "," in _first_line:
return ","
elif " " in _first_line:
return " "
elif "|" in _first_line:
return "|"
elif ";" in _first_line:
return ";"
else:
return None
if file_extension in ["csv", "tsv", "txt"]:
if file_extension == "csv":
sep = ","
elif file_extension == "tsv":
sep = "\t"
elif file_extension == "txt":
with open(input_path, "r") as file:
first_line = file.readline()
sep = infer_delimiter(first_line)
if sep is None:
raise ValueError(f"Could not infer delimiter for file {input_path}")
df = spark.read.csv(input_path, sep=sep, header=True, schema=scheme)
else:
try:
df = spark.read.load(input_path, scheme=scheme)
except Exception as e:
raise FileNotFoundError(f"File not supported: {e}")
return df
def ensure_created(list_of_path: Sequence[str]) -> None:
for path in list_of_path:
os.makedirs(path, exist_ok=True)
def truncate_paths(paths: Sequence[str]) -> None:
for path in paths:
is_dir = "." not in path.split("/")[-1]
if is_dir:
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
else:
open(path, "w").close()
def get_id(output: bytes) -> int:
return int(output.decode().strip().split(" ")[-1])
def init_logger(
logger_name: str, output_path: str = None, logging_level: str = "INFO"
) -> logging.Logger:
logging.basicConfig(
filename=output_path,
level=logging.getLevelName(logging_level),
format="%(asctime)s - %(levelname)s - %(process)d - %(message)s",
)
return logging.getLogger(logger_name)
def submit_job(submitter_script: str, script: str, *args) -> int:
output = subprocess.check_output(
f"{submitter_script} {script} {' '.join(args)}", shell=True
)
idx = get_id(output)
return idx
def preprocess_dep_ids(ids: List[int | None]) -> List[str]:
return [str(_id) for _id in ids if _id is not None]
_NOT_PROVIDED = "Not provided"
@define
class DownloadedImage:
retry_count: int
error_code: int
error_msg: str
unique_name: str
source_id: int
identifier: str
is_license_full: bool
license: str
source: str
title: str
hashsum_original: str = ""
hashsum_resized: str = ""
# image: np.ndarray = np.ndarray(0)
image: bytes = bytes()
original_size: np.ndarray[np.uint32] = np.ndarray([0, 0], dtype=np.uint32)
resized_size: np.ndarray[np.uint32] = np.ndarray([0, 0], dtype=np.uint32)
start_time: float = 0
end_time: float = 0
@classmethod
def from_row(cls, row: Dict[str, Any]) -> DownloadedImage:
if "EOL content ID" in row.keys() and "EOL page ID" in row.keys():
source_id = row["EOL content ID"] + "_" + row["EOL page ID"]
else:
source_id = "None"
return cls(
retry_count=0,
error_code=0,
error_msg="",
unique_name=row.get("uuid", uuid.uuid4().hex),
source_id=row.get("source_id", source_id),
identifier=row.get("identifier", ""),
is_license_full=all(
[
row.get("license", None),
row.get("source", None),
row.get("title", None),
]
),
license=row.get("license", _NOT_PROVIDED) or _NOT_PROVIDED,
source=row.get("source", _NOT_PROVIDED) or _NOT_PROVIDED,
title=row.get("title", _NOT_PROVIDED) or _NOT_PROVIDED,
)
def init_downloaded_image_entry(
image_entry: np.ndarray, row: Dict[str, Any]
) -> np.ndarray:
image_entry["is_downloaded"] = False
image_entry["retry_count"] = 0
image_entry["error_code"] = 0
image_entry["error_msg"] = ""
image_entry["uuid"] = row.get("UUID", uuid.uuid4().hex)
image_entry["source_id"] = row.get("source_id", 0)
image_entry["identifier"] = row.get("identifier", "")
image_entry["is_license_full"] = all(
[row.get("license", None), row.get("source", None), row.get("title", None)]
)
image_entry["license"] = row.get("license", _NOT_PROVIDED) or _NOT_PROVIDED
image_entry["source"] = row.get("source", _NOT_PROVIDED) or _NOT_PROVIDED
image_entry["title"] = row.get("title", _NOT_PROVIDED) or _NOT_PROVIDED
return image_entry
@define
class SuccessEntry:
uuid: str
source_id: int
identifier: str
is_license_full: bool
license: str
source: str
title: str
hashsum_original: str
hashsum_resized: str
original_size: np.ndarray[np.uint32]
resized_size: np.ndarray[np.uint32]
image: bytes
def __success_dtype(self, img_size: int):
return np.dtype(
[
("uuid", "S32"),
("source_id", "S32"),
("identifier", "S256"),
("is_license_full", "bool"),
("license", "S256"),
("source", "S256"),
("title", "S256"),
("original_size", "(2,)u4"),
("resized_size", "(2,)u4"),
("hashsum_original", "S32"),
("hashsum_resized", "S32"),
("image", f"({img_size},{img_size},3)uint8"),
]
)
@staticmethod
def get_success_spark_scheme():
from pyspark.sql.types import StructType
from pyspark.sql.types import StringType
from pyspark.sql.types import LongType
from pyspark.sql.types import StructField
from pyspark.sql.types import BooleanType
from pyspark.sql.types import ArrayType
from pyspark.sql.types import BinaryType
return StructType(
[
StructField("uuid", StringType(), False),
StructField("source_id", StringType(), False),
StructField("identifier", StringType(), False),
StructField("is_license_full", BooleanType(), False),
StructField("license", StringType(), True),
StructField("source", StringType(), True),
StructField("title", StringType(), True),
StructField("original_size", ArrayType(LongType(), False), False),
StructField("resized_size", ArrayType(LongType(), False), False),
StructField("hashsum_original", StringType(), False),
StructField("hashsum_resized", StringType(), False),
StructField("image", BinaryType(), False),
]
)
@classmethod
def from_downloaded(cls, downloaded: DownloadedImage) -> SuccessEntry:
return cls(
uuid=downloaded.unique_name,
source_id=downloaded.source_id,
identifier=downloaded.identifier,
is_license_full=downloaded.is_license_full,
license=downloaded.license,
source=downloaded.source,
title=downloaded.title,
hashsum_original=downloaded.hashsum_original,
hashsum_resized=downloaded.hashsum_resized,
original_size=downloaded.original_size,
resized_size=downloaded.resized_size,
image=downloaded.image,
)
@staticmethod
def to_list_download(downloaded: DownloadedImage) -> List:
return [
downloaded.unique_name,
downloaded.source_id,
downloaded.identifier,
downloaded.is_license_full,
downloaded.license,
downloaded.source,
downloaded.title,
downloaded.original_size,
downloaded.resized_size,
downloaded.hashsum_original,
downloaded.hashsum_resized,
downloaded.image,
]
@staticmethod
def get_names() -> List[str]:
return [
"uuid",
"source_id",
"identifier",
"is_license_full",
"license",
"source",
"title",
"original_size",
"resized_size",
"hashsum_original",
"hashsum_resized",
"image",
]
def to_list(self) -> List:
return [
self.uuid,
self.source_id,
self.identifier,
self.is_license_full,
self.license,
self.source,
self.title,
self.original_size,
self.resized_size,
self.hashsum_original,
self.hashsum_resized,
self.image,
]
def to_np(self) -> np.ndarray:
np_structure = np.array(
[
(
self.uuid,
self.source_id,
self.identifier,
self.is_license_full,
self.license,
self.source,
self.title,
self.original_size,
self.resized_size,
self.hashsum_original,
self.hashsum_resized,
self.image,
)
],
dtype=self.__success_dtype(np.max(self.resized_size)),
)
return np_structure