test_determinism.py 4.5 KB

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