Skip to content

Commit 698d362

Browse files
committed
Create output directory for debugging images if it doesn't exist already.
Also log the output paths in the error message for convenience. PiperOrigin-RevId: 188318258
1 parent 6b3457c commit 698d362

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

dm_control/mujoco/testing/image_utils.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import collections
2323
import functools
2424
import os
25+
import sys
2526

2627
# Internal dependencies.
2728
from dm_control import mujoco
@@ -194,17 +195,19 @@ def decorated_method(*args, **kwargs):
194195
try:
195196
test_method(*args, **kwargs)
196197
except ImagesNotClose as e:
198+
_, _, tb = sys.exc_info()
199+
if not os.path.exists(output_dir):
200+
os.makedirs(output_dir)
197201
difference = e.actual.astype(np.double) - e.expected
198202
difference = (0.5 * (difference + 255)).astype(np.uint8)
199-
_save_pixels(e.expected,
200-
os.path.join(output_dir,
201-
'{}-expected.png'.format(method_name)))
202-
_save_pixels(e.actual,
203-
os.path.join(output_dir,
204-
'{}-actual.png'.format(method_name)))
205-
_save_pixels(difference,
206-
os.path.join(output_dir,
207-
'{}-difference.png'.format(method_name)))
208-
raise # Reraise the exception with the original traceback.
203+
base_name = os.path.join(output_dir, method_name)
204+
_save_pixels(e.expected, base_name + '-expected.png')
205+
_save_pixels(e.actual, base_name + '-actual.png')
206+
_save_pixels(difference, base_name + '-difference.png')
207+
msg = ('{}. Debugging images saved to '
208+
'{}-{{expected,actual,difference}}.png.'.format(e, base_name))
209+
new_e = ImagesNotClose(msg, expected=e.expected, actual=e.actual)
210+
# Reraise the exception with the original traceback.
211+
six.reraise(ImagesNotClose, new_e, tb)
209212
return decorated_method
210213
return decorator

dm_control/mujoco/testing/image_utils_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ def test_save_images_on_failure(self):
6161
def func():
6262
raise image_utils.ImagesNotClose(message, image1, image2)
6363

64-
with self.assertRaisesWithLiteralMatch(image_utils.ImagesNotClose, message):
64+
with self.assertRaisesRegexp(image_utils.ImagesNotClose,
65+
'{}.*'.format(message)):
6566
func()
6667

6768
def validate_saved_file(name, expected_contents):

0 commit comments

Comments
 (0)