From 372f9f430bb0edb901b3265ecd0cddf8ee4d1ffb Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Thu, 3 May 2018 21:11:58 -0400 Subject: [PATCH] EKF pytest save plots to pdf --- .gitignore | 1 + EKF/tests/pytest/CMakeLists.txt | 8 ++++++-- EKF/tests/pytest/plot_utils.py | 27 ++++++++++++++++++--------- EKF/tests/pytest/requirements.txt | 1 - Jenkinsfile | 1 + Makefile | 3 +++ 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index ea2b51b57b..04fe264b6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.DS_Store *~ .cache/ +.pytest_cache/ build/ EKF/tests/pytest/__pycache__/ diff --git a/EKF/tests/pytest/CMakeLists.txt b/EKF/tests/pytest/CMakeLists.txt index f531ed977d..5868938042 100644 --- a/EKF/tests/pytest/CMakeLists.txt +++ b/EKF/tests/pytest/CMakeLists.txt @@ -36,20 +36,23 @@ if(EKF_PYTHON_TESTS) # pip3 install -r requirements.txt add_custom_target(ecl_EKF_pytest - COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --verbose + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --verbose --plots DEPENDS ecl_EKF _ecl_EKF + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} USES_TERMINAL ) add_custom_target(ecl_EKF_pytest-quick - COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --quick --verbose + COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --quick --verbose --plots DEPENDS ecl_EKF _ecl_EKF + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} USES_TERMINAL ) add_custom_target(ecl_EKF_pytest-benchmark COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --benchmark DEPENDS ecl_EKF _ecl_EKF + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} USES_TERMINAL ) @@ -57,6 +60,7 @@ if(EKF_PYTHON_TESTS) add_custom_target(ecl_EKF_pytest-plots COMMAND ${CMAKE_COMMAND} -E env PYTHONPATH=${CMAKE_BINARY_DIR}/EKF/swig/python ${CMAKE_CURRENT_SOURCE_DIR}/ekf_test.py --plots DEPENDS ecl_EKF _ecl_EKF + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} USES_TERMINAL ) diff --git a/EKF/tests/pytest/plot_utils.py b/EKF/tests/pytest/plot_utils.py index bfa89aa13f..39573618e0 100644 --- a/EKF/tests/pytest/plot_utils.py +++ b/EKF/tests/pytest/plot_utils.py @@ -44,18 +44,26 @@ import inspect from contextlib import contextmanager try: - from matplotlib import pyplot as plt - import seaborn as sns + # matplotlib don't use Xwindows backend (must be before pyplot import) + import matplotlib + matplotlib.use('Agg') + + import matplotlib.pyplot as plt + from matplotlib.backends.backend_pdf import PdfPages + + #from matplotlib import pyplot as plt + #from matplotlib.backends.backend_pdf import PdfPages + #import seaborn as sns except ImportError as err: print("Cannot import plotting libraries, " - "please install matplotlib and seaborn.") + "please install matplotlib.") raise err # Nice plot defaults -sns.set_style('darkgrid') -sns.set_palette('colorblind', - desat=0.6) +#sns.set_style('darkgrid') +#sns.set_palette('colorblind', desat=0.6) +pp = PdfPages("ecl_EKF_test.pdf") def quit_figure_on_key(key, fig=None): """Add handler to figure (defaults to current figure) that closes it @@ -82,10 +90,11 @@ def figure(name=None, params=None, figsize=None, subplots=None): # Get name of function calling the context from the stack name = inspect.stack()[2][3] fig, axes = plt.subplots(*subplots, figsize=figsize) - fig.canvas.set_window_title(name) - quit_figure_on_key('q', fig) + #fig.canvas.set_window_title(name) + #quit_figure_on_key('q', fig) yield fig, axes if params is not None: name += "\n" + repr(params) axes[0].set_title(name) - plt.show(True) + #plt.show(True) + pp.savefig() diff --git a/EKF/tests/pytest/requirements.txt b/EKF/tests/pytest/requirements.txt index 9a952d9960..ee4e40c3be 100644 --- a/EKF/tests/pytest/requirements.txt +++ b/EKF/tests/pytest/requirements.txt @@ -2,5 +2,4 @@ pytest>=3.2.1 hypothesis>=3.17.0 numpy>=1.12.1 matplotlib>=2.0.0 -seaborn>=0.7.1 pytest-benchmark>=3.1.1 diff --git a/Jenkinsfile b/Jenkinsfile index fef5519cfc..165d6c3c77 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -84,6 +84,7 @@ pipeline { sh 'make distclean' sh 'make test_EKF' sh 'ccache -s' + archiveArtifacts(artifacts: 'build/**/*.pdf') sh 'make distclean' } } diff --git a/Makefile b/Makefile index 4436e37000..97e0e54aba 100644 --- a/Makefile +++ b/Makefile @@ -99,6 +99,9 @@ test: test_build test_EKF: test_build @cmake --build $(SRC_DIR)/build/test_build --target ecl_EKF_pytest-quick + +test_EKF_plots: test_build + @cmake --build $(SRC_DIR)/build/test_build --target ecl_EKF_pytest-plots # Cleanup # --------------------------------------------------------------------