46
46
from .kernels import DEFAULT_HAMMING_TOLERANCE , DEFAULT_HASH_SIZE , KERNEL_SHA256 , kernel_factory
47
47
from .summary .html import generate_summary_basic_html , generate_summary_html
48
48
49
- SUPPORTED_FORMATS = {'html' , 'json' , 'basic-html' }
50
-
51
- SHAPE_MISMATCH_ERROR = """Error: Image dimensions did not match.
52
- Expected shape: {expected_shape}
53
- {expected_path}
54
- Actual shape: {actual_shape}
55
- {actual_path}"""
56
-
57
49
#: The default matplotlib backend.
58
50
DEFAULT_BACKEND = "agg"
59
51
72
64
#: The default matplotlib plot style.
73
65
DEFAULT_STYLE = "classic"
74
66
67
+ #: Valid formats for generate summary.
68
+ SUPPORTED_FORMATS = {'html' , 'json' , 'basic-html' }
69
+
70
+ #: Template error message for image shape conformance.
71
+ TEMPLATE_SHAPE_MISMATCH = """Error! Image dimensions did not match.
72
+
73
+ Baseline Shape:
74
+ {baseline_shape}
75
+ Baseline Image:
76
+ {baseline_image}
77
+ Result Shape:
78
+ {result_shape}
79
+ Result Image:
80
+ {result_image}"""
81
+
82
+ TEMPLATE_IMAGE_DIFFERENCE = """Failed! Image files did not match.
83
+
84
+ RMS: {rms}
85
+ Tolerance: {tol}
86
+ Baseline Image:
87
+ {expected}
88
+ Result Image:
89
+ {actual}
90
+ Difference Image:
91
+ {diff}"""
92
+
75
93
76
94
def pathify (path ):
77
95
"""
@@ -501,9 +519,9 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
501
519
502
520
baseline_image_ref = self .obtain_baseline_image (item , result_dir )
503
521
504
- test_image = (result_dir / "result.png" ).absolute ()
505
- fig .savefig (str (test_image ), ** savefig_kwargs )
506
- summary ['result_image' ] = test_image .relative_to (self .results_dir ).as_posix ()
522
+ result_image = (result_dir / "result.png" ).absolute ()
523
+ fig .savefig (str (result_image ), ** savefig_kwargs )
524
+ summary ['result_image' ] = result_image .relative_to (self .results_dir ).as_posix ()
507
525
508
526
if not os .path .exists (baseline_image_ref ):
509
527
summary ['status' ] = 'failed'
@@ -512,7 +530,7 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
512
530
f"{ self .get_baseline_directory (item )} \n "
513
531
"(This is expected for new tests.)\n "
514
532
"Generated Image: \n \t "
515
- f"{ test_image } " )
533
+ f"{ result_image } " )
516
534
summary ['status_msg' ] = error_message
517
535
return error_message
518
536
@@ -525,19 +543,20 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
525
543
# Compare image size ourselves since the Matplotlib
526
544
# exception is a bit cryptic in this case and doesn't show
527
545
# the filenames
528
- expected_shape = imread (str (baseline_image )).shape [:2 ]
529
- actual_shape = imread (str (test_image )).shape [:2 ]
530
- if expected_shape != actual_shape :
546
+ baseline_shape = imread (str (baseline_image )).shape [:2 ]
547
+ result_shape = imread (str (result_image )).shape [:2 ]
548
+ if baseline_shape != result_shape :
531
549
summary ['status' ] = 'failed'
532
550
summary ['image_status' ] = 'diff'
533
- error_message = SHAPE_MISMATCH_ERROR .format (expected_path = baseline_image ,
534
- expected_shape = expected_shape ,
535
- actual_path = test_image ,
536
- actual_shape = actual_shape )
551
+ error_message = TEMPLATE_SHAPE_MISMATCH .format (baseline_image = baseline_image ,
552
+ baseline_shape = baseline_shape ,
553
+ result_image = result_image ,
554
+ result_shape = result_shape )
537
555
summary ['status_msg' ] = error_message
538
556
return error_message
539
557
540
- results = compare_images (str (baseline_image ), str (test_image ), tol = tolerance , in_decorator = True )
558
+ # 'in_decorator=True' ensures that a dictionary of results is returned by 'compare_images'
559
+ results = compare_images (str (baseline_image ), str (result_image ), tol = tolerance , in_decorator = True )
541
560
summary ['tolerance' ] = tolerance
542
561
if results is None :
543
562
summary ['status' ] = 'passed'
@@ -550,13 +569,7 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
550
569
summary ['rms' ] = results ['rms' ]
551
570
diff_image = (result_dir / 'result-failed-diff.png' ).absolute ()
552
571
summary ['diff_image' ] = diff_image .relative_to (self .results_dir ).as_posix ()
553
- template = ['Error: Image files did not match.' ,
554
- 'RMS Value: {rms}' ,
555
- 'Expected: \n {expected}' ,
556
- 'Actual: \n {actual}' ,
557
- 'Difference:\n {diff}' ,
558
- 'Tolerance: \n {tol}' , ]
559
- error_message = '\n ' .join ([line .format (** results ) for line in template ])
572
+ error_message = TEMPLATE_IMAGE_DIFFERENCE .format (** results )
560
573
summary ['status_msg' ] = error_message
561
574
return error_message
562
575
@@ -601,12 +614,12 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
601
614
hash_comparison_pass = True
602
615
summary ['status' ] = 'passed'
603
616
summary ['hash_status' ] = 'match'
604
- summary ['status_msg' ] = 'Test hash matches baseline hash.'
617
+ summary ['status_msg' ] = 'Result hash matches baseline hash.'
605
618
self .kernel .update_summary (summary )
606
619
else : # hash-diff
607
620
summary ['status' ] = 'failed'
608
621
summary ['hash_status' ] = 'diff'
609
- msg = (f"Test hash { test_hash !r} doesn't match baseline hash "
622
+ msg = (f'Result hash { test_hash !r} does not match baseline hash '
610
623
f'{ baseline_hash !r} in library { str (hash_library_filename )!r} '
611
624
f'for test { hash_name !r} .' )
612
625
summary ['status_msg' ] = self .kernel .update_status (msg )
@@ -639,7 +652,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
639
652
# Append the log from image comparison
640
653
r = baseline_comparison or "The comparison to the baseline image succeeded."
641
654
summary ['status_msg' ] += ("\n \n "
642
- "Image comparison test \n "
655
+ "Image Comparison Test \n "
643
656
"---------------------\n " ) + r
644
657
645
658
if hash_comparison_pass : # Return None to indicate test passed
0 commit comments