Skip to content

Commit 8830e1d

Browse files
committed
Be a bit more robust to errors
1 parent ac37d3d commit 8830e1d

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

gbmi/utils/images.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,14 @@ def forward_output(
8383

8484
def batch_run(
8585
args: Iterable[str],
86-
*images: str,
86+
*images: str | Path,
8787
batchsize: int = 64,
8888
post_args: list[str] = [],
8989
check: bool = True,
9090
trim_printout: Optional[Callable[[str], Optional[str]]] = None,
9191
stdout_write: Optional[Callable] = None,
9292
stderr_write: Optional[Callable] = None,
93+
wrap_errs: Optional[list[Exception]] = None,
9394
**kwargs,
9495
):
9596
if len(images) > batchsize:
@@ -100,13 +101,14 @@ def batch_run(
100101
batchsize=batchsize,
101102
post_args=post_args,
102103
check=check,
104+
wrap_errs=wrap_errs,
103105
**kwargs,
104106
)
105107
for i in range(0, len(images), batchsize)
106108
]
107109

108110
process = subprocess.Popen(
109-
[*args, *images, *post_args],
111+
[*args, *map(str, images), *post_args],
110112
stdout=subprocess.PIPE,
111113
stderr=subprocess.PIPE,
112114
**kwargs,
@@ -133,7 +135,12 @@ def batch_run(
133135
process.wait()
134136

135137
if check and process.returncode != 0:
136-
raise subprocess.CalledProcessError(process.returncode, process.args)
138+
exn = subprocess.CalledProcessError(process.returncode, process.args)
139+
if wrap_errs is not None:
140+
stderr_write(f"Error: {exn}")
141+
wrap_errs.append(exn)
142+
else:
143+
raise exn
137144

138145
return process
139146

@@ -154,6 +161,7 @@ def ect(
154161
trim_printout: bool = False,
155162
stdout_write: Optional[Callable] = None,
156163
stderr_write: Optional[Callable] = None,
164+
wrap_errs: Optional[list[Exception]] = None,
157165
):
158166
if not images:
159167
return
@@ -173,6 +181,7 @@ def ect(
173181
trim_printout=trim_ect if trim_printout else (lambda x: x),
174182
stdout_write=stdout_write,
175183
stderr_write=stderr_write,
184+
wrap_errs=wrap_errs,
176185
)
177186

178187

@@ -195,6 +204,7 @@ def optipng(
195204
trim_printout: bool = False,
196205
stdout_write: Optional[Callable] = None,
197206
stderr_write: Optional[Callable] = None,
207+
wrap_errs: Optional[list[Exception]] = None,
198208
):
199209
if not images:
200210
return
@@ -210,6 +220,7 @@ def optipng(
210220
trim_printout=trim_optipng if trim_printout else None,
211221
stdout_write=stdout_write,
212222
stderr_write=stderr_write,
223+
wrap_errs=wrap_errs,
213224
)
214225

215226

@@ -225,6 +236,7 @@ def pngcrush(
225236
trim_printout: bool = False,
226237
stdout_write: Optional[Callable] = None,
227238
stderr_write: Optional[Callable] = None,
239+
wrap_errs: Optional[list[Exception]] = None,
228240
):
229241
if not images:
230242
return
@@ -259,6 +271,7 @@ def pngcrush(
259271
trim_printout=trim_pngcrush if trim_printout else None,
260272
stdout_write=stdout_write,
261273
stderr_write=stderr_write,
274+
wrap_errs=wrap_errs,
262275
)
263276

264277
# Replace original images with crushed images if they are smaller
@@ -283,6 +296,7 @@ def optimize(
283296
tqdm_leave: Optional[bool] = None,
284297
stdout_write: Optional[Callable] = None,
285298
stderr_write: Optional[Callable] = None,
299+
wrap_errs: Optional[list[Exception]] = None,
286300
):
287301
cur_images = images
288302
cur_sizes = [Path(image).stat().st_size for image in cur_images]
@@ -297,13 +311,15 @@ def optimize(
297311
trim_printout=trim_printout,
298312
stdout_write=partial(tqdm.write, file=sys.stdout),
299313
stderr_write=partial(tqdm.write, file=sys.stderr),
314+
wrap_errs=wrap_errs,
300315
)
301316
optipng(
302317
*cur_images,
303318
exhaustive=exhaustive,
304319
trim_printout=trim_printout,
305320
stdout_write=stdout_write,
306321
stderr_write=stderr_write,
322+
wrap_errs=wrap_errs,
307323
)
308324
pngcrush(
309325
*cur_images,
@@ -313,6 +329,7 @@ def optimize(
313329
trim_printout=trim_printout,
314330
stdout_write=stdout_write,
315331
stderr_write=stderr_write,
332+
wrap_errs=wrap_errs,
316333
)
317334
new_sizes = [Path(image).stat().st_size for image in cur_images]
318335
cur_images = [

notebooks_jason/max_of_K_all_models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,15 @@ def optimize_pngs(errs: list[Exception] = []):
329329
# wrap_err(image_utils.pngcrush, f)
330330
# wrap_err(image_utils.optipng, f)
331331

332-
opt_success = wrap_err(
333-
image_utils.optimize,
332+
new_errs = []
333+
image_utils.optimize(
334334
*LATEX_FIGURE_PATH.glob("*.png"),
335335
exhaustive=True,
336-
return_bool=True,
337336
trim_printout=COMPACT_IMAGE_OPTIMIZE_OUTPUT,
337+
wrap_errs=new_errs,
338338
)
339+
opt_success = not new_errs
340+
errs.extend(new_errs)
339341

340342
if not opt_success:
341343
for f in tqdm(

0 commit comments

Comments
 (0)