diff --git a/newsfragments/XXX.bugfix b/newsfragments/XXX.bugfix new file mode 100644 index 000000000..d1c5abf15 --- /dev/null +++ b/newsfragments/XXX.bugfix @@ -0,0 +1 @@ +``dxtbx.plot_detector_models``: use noninteractive matpotlib backend if using the pdf_file option diff --git a/src/dxtbx/command_line/plot_detector_models.py b/src/dxtbx/command_line/plot_detector_models.py index 4034e7417..b9f228280 100644 --- a/src/dxtbx/command_line/plot_detector_models.py +++ b/src/dxtbx/command_line/plot_detector_models.py @@ -6,7 +6,6 @@ import sys from collections.abc import Sequence -import matplotlib.pyplot as plt import numpy as np from matplotlib.backends.backend_pdf import PdfPages from matplotlib.patches import FancyArrowPatch @@ -170,6 +169,12 @@ def run(args=None): except Exception: raise Sorry("Unrecognized argument %s" % arg) params = phil_scope.fetch(sources=user_phil).extract() + if params.pdf_file: + import matplotlib + + matplotlib.use("Agg") + + import matplotlib.pyplot as plt fig = plt.figure() colormap = plt.cm.gist_ncar diff --git a/tests/command_line/test_plot_detector_models.py b/tests/command_line/test_plot_detector_models.py new file mode 100644 index 000000000..f2289efa0 --- /dev/null +++ b/tests/command_line/test_plot_detector_models.py @@ -0,0 +1,17 @@ +from __future__ import annotations + +import os.path +import shutil +import subprocess + + +def test_plot_models_noninteractive_to_pdf(dials_data, tmp_path): + ssx = dials_data("cunir_serial_processed", pathlib=True) + expts = str(ssx / "imported_no_ref_5.expt") + result = subprocess.run( + [shutil.which("dxtbx.plot_detector_models"), expts, "pdf_file=plot_test.pdf"], + cwd=tmp_path, + capture_output=True, + ) + assert not result.returncode and not result.stderr + assert os.path.exists(tmp_path / "plot_test.pdf")