test_determinism.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. Test output reproducibility.
  3. """
  4. import os
  5. import subprocess
  6. import sys
  7. import pytest
  8. import matplotlib as mpl
  9. import matplotlib.testing.compare
  10. from matplotlib import pyplot as plt
  11. from matplotlib.testing._markers import needs_ghostscript, needs_usetex
  12. def _save_figure(objects='mhi', fmt="pdf", usetex=False):
  13. mpl.use(fmt)
  14. mpl.rcParams.update({'svg.hashsalt': 'asdf', 'text.usetex': usetex})
  15. fig = plt.figure()
  16. if 'm' in objects:
  17. # use different markers...
  18. ax1 = fig.add_subplot(1, 6, 1)
  19. x = range(10)
  20. ax1.plot(x, [1] * 10, marker='D')
  21. ax1.plot(x, [2] * 10, marker='x')
  22. ax1.plot(x, [3] * 10, marker='^')
  23. ax1.plot(x, [4] * 10, marker='H')
  24. ax1.plot(x, [5] * 10, marker='v')
  25. if 'h' in objects:
  26. # also use different hatch patterns
  27. ax2 = fig.add_subplot(1, 6, 2)
  28. bars = (ax2.bar(range(1, 5), range(1, 5)) +
  29. ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5)))
  30. ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
  31. patterns = ('-', '+', 'x', '\\', '*', 'o', 'O', '.')
  32. for bar, pattern in zip(bars, patterns):
  33. bar.set_hatch(pattern)
  34. if 'i' in objects:
  35. # also use different images
  36. A = [[1, 2, 3], [2, 3, 1], [3, 1, 2]]
  37. fig.add_subplot(1, 6, 3).imshow(A, interpolation='nearest')
  38. A = [[1, 3, 2], [1, 2, 3], [3, 1, 2]]
  39. fig.add_subplot(1, 6, 4).imshow(A, interpolation='bilinear')
  40. A = [[2, 3, 1], [1, 2, 3], [2, 1, 3]]
  41. fig.add_subplot(1, 6, 5).imshow(A, interpolation='bicubic')
  42. x = range(5)
  43. ax = fig.add_subplot(1, 6, 6)
  44. ax.plot(x, x)
  45. ax.set_title('A string $1+2+\\sigma$')
  46. ax.set_xlabel('A string $1+2+\\sigma$')
  47. ax.set_ylabel('A string $1+2+\\sigma$')
  48. stdout = getattr(sys.stdout, 'buffer', sys.stdout)
  49. fig.savefig(stdout, format=fmt)
  50. @pytest.mark.parametrize(
  51. "objects, fmt, usetex", [
  52. ("", "pdf", False),
  53. ("m", "pdf", False),
  54. ("h", "pdf", False),
  55. ("i", "pdf", False),
  56. ("mhi", "pdf", False),
  57. ("mhi", "ps", False),
  58. pytest.param(
  59. "mhi", "ps", True, marks=[needs_usetex, needs_ghostscript]),
  60. ("mhi", "svg", False),
  61. pytest.param("mhi", "svg", True, marks=needs_usetex),
  62. ]
  63. )
  64. def test_determinism_check(objects, fmt, usetex):
  65. """
  66. Output three times the same graphs and checks that the outputs are exactly
  67. the same.
  68. Parameters
  69. ----------
  70. objects : str
  71. Objects to be included in the test document: 'm' for markers, 'h' for
  72. hatch patterns, 'i' for images.
  73. fmt : {"pdf", "ps", "svg"}
  74. Output format.
  75. """
  76. plots = [
  77. subprocess.check_output(
  78. [sys.executable, "-R", "-c",
  79. f"from matplotlib.tests.test_determinism import _save_figure;"
  80. f"_save_figure({objects!r}, {fmt!r}, {usetex})"],
  81. env={**os.environ, "SOURCE_DATE_EPOCH": "946684800",
  82. "MPLBACKEND": "Agg"})
  83. for _ in range(3)
  84. ]
  85. for p in plots[1:]:
  86. if fmt == "ps" and usetex:
  87. if p != plots[0]:
  88. pytest.skip("failed, maybe due to ghostscript timestamps")
  89. else:
  90. assert p == plots[0]
  91. @pytest.mark.parametrize(
  92. "fmt, string", [
  93. ("pdf", b"/CreationDate (D:20000101000000Z)"),
  94. # SOURCE_DATE_EPOCH support is not tested with text.usetex,
  95. # because the produced timestamp comes from ghostscript:
  96. # %%CreationDate: D:20000101000000Z00\'00\', and this could change
  97. # with another ghostscript version.
  98. ("ps", b"%%CreationDate: Sat Jan 01 00:00:00 2000"),
  99. ]
  100. )
  101. def test_determinism_source_date_epoch(fmt, string):
  102. """
  103. Test SOURCE_DATE_EPOCH support. Output a document with the environment
  104. variable SOURCE_DATE_EPOCH set to 2000-01-01 00:00 UTC and check that the
  105. document contains the timestamp that corresponds to this date (given as an
  106. argument).
  107. Parameters
  108. ----------
  109. fmt : {"pdf", "ps", "svg"}
  110. Output format.
  111. string : bytes
  112. Timestamp string for 2000-01-01 00:00 UTC.
  113. """
  114. buf = subprocess.check_output(
  115. [sys.executable, "-R", "-c",
  116. f"from matplotlib.tests.test_determinism import _save_figure; "
  117. f"_save_figure('', {fmt!r})"],
  118. env={**os.environ, "SOURCE_DATE_EPOCH": "946684800",
  119. "MPLBACKEND": "Agg"})
  120. assert string in buf