@@ -431,16 +431,13 @@ def generate_baseline_image(self, item, fig):
431431 """
432432 Generate reference figures.
433433 """
434- compare = get_compare (item )
435- savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
436434
437435 if not os .path .exists (self .generate_dir ):
438436 os .makedirs (self .generate_dir )
439437
440438 baseline_filename = self .generate_filename (item )
441439 baseline_path = (self .generate_dir / baseline_filename ).absolute ()
442- fig .savefig (str (baseline_path ), ** savefig_kwargs )
443-
440+ self .save_figure (item , fig , baseline_path )
444441 close_mpl_figure (fig )
445442
446443 return baseline_path
@@ -450,13 +447,9 @@ def generate_image_hash(self, item, fig):
450447 For a `matplotlib.figure.Figure`, returns the SHA256 hash as a hexadecimal
451448 string.
452449 """
453- compare = get_compare (item )
454- savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
455-
456- ext = self ._file_extension (item )
457450
458451 imgdata = io .BytesIO ()
459- fig . savefig ( imgdata , ** savefig_kwargs )
452+ self . save_figure ( item , fig , imgdata )
460453 out = _hash_file (imgdata )
461454 imgdata .close ()
462455
@@ -475,14 +468,13 @@ def compare_image_to_baseline(self, item, fig, result_dir, summary=None):
475468
476469 compare = get_compare (item )
477470 tolerance = compare .kwargs .get ('tolerance' , 2 )
478- savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
479471
480472 ext = self ._file_extension (item )
481473
482474 baseline_image_ref = self .obtain_baseline_image (item , result_dir )
483475
484476 test_image = (result_dir / f"result.{ ext } " ).absolute ()
485- fig . savefig ( str ( test_image ), ** savefig_kwargs )
477+ self . save_figure ( item , fig , test_image )
486478
487479 if ext in ['png' , 'svg' ]: # Use original file
488480 summary ['result_image' ] = test_image .relative_to (self .results_dir ).as_posix ()
@@ -554,13 +546,55 @@ def load_hash_library(self, library_path):
554546 with open (str (library_path )) as fp :
555547 return json .load (fp )
556548
549+ def save_figure (self , item , fig , filename ):
550+ if isinstance (filename , Path ):
551+ filename = str (filename )
552+ compare = get_compare (item )
553+ savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
554+ deterministic = compare .kwargs .get ('deterministic' , False )
555+
556+ original_source_date_epoch = os .environ .get ('SOURCE_DATE_EPOCH' , None )
557+
558+ extra_rcparams = {}
559+
560+ if deterministic :
561+
562+ # Make sure we don't modify the original dictionary in case is a common
563+ # object used by different tests
564+ savefig_kwargs = savefig_kwargs .copy ()
565+
566+ if 'metadata' not in savefig_kwargs :
567+ savefig_kwargs ['metadata' ] = {}
568+
569+ ext = self ._file_extension (item )
570+
571+ if ext == 'png' :
572+ extra_metadata = {"Software" : None }
573+ elif ext == 'pdf' :
574+ extra_metadata = {"Creator" : None , "Producer" : None , "CreationDate" : None }
575+ elif ext == 'eps' :
576+ extra_metadata = {"Creator" : "test" }
577+ os .environ ['SOURCE_DATE_EPOCH' ] = '1680254601'
578+ elif ext == 'svg' :
579+ extra_metadata = {"Date" : None }
580+ extra_rcparams ["svg.hashsalt" ] = "test"
581+
582+ savefig_kwargs ['metadata' ].update (extra_metadata )
583+
584+ import matplotlib .pyplot as plt
585+
586+ with plt .rc_context (rc = extra_rcparams ):
587+ fig .savefig (filename , ** savefig_kwargs )
588+
589+ if original_source_date_epoch is not None :
590+ os .environ ['SOURCE_DATE_EPOCH' ] = original_source_date_epoch
591+
557592 def compare_image_to_hash_library (self , item , fig , result_dir , summary = None ):
558593 hash_comparison_pass = False
559594 if summary is None :
560595 summary = {}
561596
562597 compare = get_compare (item )
563- savefig_kwargs = compare .kwargs .get ('savefig_kwargs' , {})
564598
565599 ext = self ._file_extension (item )
566600
@@ -601,7 +635,7 @@ def compare_image_to_hash_library(self, item, fig, result_dir, summary=None):
601635
602636 # Save the figure for later summary (will be removed later if not needed)
603637 test_image = (result_dir / f"result.{ ext } " ).absolute ()
604- fig . savefig ( str ( test_image ), ** savefig_kwargs )
638+ self . save_figure ( item , fig , test_image )
605639 summary ['result_image' ] = test_image .relative_to (self .results_dir ).as_posix ()
606640
607641 # Hybrid mode (hash and image comparison)
0 commit comments