Skip to content

Commit 071bfca

Browse files
sbryngelsonclaude
andcommitted
Guard LogNorm against NaN data, harden frame cleanup
- Add np.isfinite() checks to LogNorm guards in both 2D and 3D renderers so all-NaN data doesn't crash matplotlib - Wrap stale-frame and post-encode cleanup in try/except OSError so locked or missing files don't abort rendering Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 376963e commit 071bfca

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

toolchain/mfc/viz/renderer.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def render_2d(x_cc, y_cc, data, varname, step, output, **opts): # pylint: disab
4747
if log_scale:
4848
lo = vmin if vmin is not None else np.nanmin(data[data > 0]) if np.any(data > 0) else 1e-10
4949
hi = vmax if vmax is not None else np.nanmax(data)
50-
if hi <= 0:
50+
if not np.isfinite(hi) or hi <= 0:
5151
hi = 1.0
52-
if lo <= 0 or lo >= hi:
52+
if not np.isfinite(lo) or lo <= 0 or lo >= hi:
5353
lo = hi * 1e-10
5454
norm = LogNorm(vmin=lo, vmax=hi)
5555
vmin = None
@@ -117,9 +117,9 @@ def render_3d_slice(assembled, varname, step, output, slice_axis='z', # pylint:
117117
if log_scale:
118118
lo = vmin if vmin is not None else np.nanmin(sliced[sliced > 0]) if np.any(sliced > 0) else 1e-10
119119
hi = vmax if vmax is not None else np.nanmax(sliced)
120-
if hi <= 0:
120+
if not np.isfinite(hi) or hi <= 0:
121121
hi = 1.0
122-
if lo <= 0 or lo >= hi:
122+
if not np.isfinite(lo) or lo <= 0 or lo >= hi:
123123
lo = hi * 1e-10
124124
norm = LogNorm(vmin=lo, vmax=hi)
125125
vmin = None
@@ -204,7 +204,10 @@ def render_mp4(varname, steps, output, fps=10, # pylint: disable=too-many-argum
204204
if os.path.isdir(viz_dir):
205205
for stale in os.listdir(viz_dir):
206206
if stale.endswith('.png'):
207-
os.remove(os.path.join(viz_dir, stale))
207+
try:
208+
os.remove(os.path.join(viz_dir, stale))
209+
except OSError:
210+
pass
208211
os.makedirs(viz_dir, exist_ok=True)
209212

210213
try:
@@ -260,8 +263,10 @@ def render_mp4(varname, steps, output, fps=10, # pylint: disable=too-many-argum
260263
if success:
261264
for fname in frame_files:
262265
fpath = os.path.join(viz_dir, fname)
263-
if os.path.isfile(fpath):
266+
try:
264267
os.remove(fpath)
268+
except OSError:
269+
pass
265270
try:
266271
os.rmdir(viz_dir)
267272
except OSError:

0 commit comments

Comments
 (0)