From b27f33cc667a157a8f0a6a01f99f5215d1a14404 Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Tue, 19 Sep 2023 22:32:34 +0900 Subject: [PATCH 1/2] Replace deprecated mpl function in visualization test (#1274) ### Summary All PRs in Qiskit Experiments are currently failing in tests due to `TestPlotterAndMplDrawer.test_series_names_different_types` which includes deprecated functions in the recently updated matplotlib package. ### Details and comments No release note is included because this is update for unittest. --- test/visualization/test_plotter_mpldrawer.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/visualization/test_plotter_mpldrawer.py b/test/visualization/test_plotter_mpldrawer.py index b39f9fcd4d..fc5799535a 100644 --- a/test/visualization/test_plotter_mpldrawer.py +++ b/test/visualization/test_plotter_mpldrawer.py @@ -118,9 +118,10 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]]) """ # Create Matplotlib axes that use a PNG backend. The default backend, FigureCanvasSVG, does not - # have `tostring_rgb()` which is needed to compute the difference between two figures in this + # have `buffer_rgba()` which is needed to compute the difference between two figures in this # method. We need to set the axes as MplDrawer will use # `qiskit_experiments.framework.matplotlib.get_non_gui_ax` by default; which uses an SVG backend. + plt.close("all") plt.switch_backend("Agg") axes = {} for key in series_names.keys(): @@ -161,16 +162,12 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]]) for plot_type in legend_plot_types: plotter.enable_legend_for(series_name, plot_type) - # Generate figure and save to buffers for comparison. This requires a pixel backend, like AGG, so - # that `tostring_rgb()` is available. + # Generate figure and save to buffers for comparison. figure_data = {} for plotter_type, plotter in plotters.items(): figure = plotter.figure().figure figure.canvas.draw() - figure_data[plotter_type] = np.frombuffer( - figure.canvas.tostring_rgb(), - dtype=np.uint8, - ).reshape(figure.canvas.get_width_height() + (3,)) + figure_data[plotter_type] = np.asarray(figure.canvas.buffer_rgba(), dtype=np.uint8) # Compare root-mean-squared error between two images. for (fig1_type, fig1), (fig2_type, fig2) in combinations(figure_data.items(), 2): From c8362f3e3919220bc7ea1836e148894d1032dda3 Mon Sep 17 00:00:00 2001 From: Helena Zhang Date: Tue, 19 Sep 2023 20:53:13 -0400 Subject: [PATCH 2/2] Update CI tests (#1273) This PR fixes a deprecation error from Matplotlib since version 3.8: https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.8.0.html#auto-closing-of-figures-when-switching-backend It also removes the intermediate python versions from the daily cron jobs. --------- Co-authored-by: Will Shanks --- .github/workflows/cron-staging.yml | 2 +- test/visualization/test_plotter_mpldrawer.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cron-staging.yml b/.github/workflows/cron-staging.yml index 3176c910cb..27bfad72cd 100644 --- a/.github/workflows/cron-staging.yml +++ b/.github/workflows/cron-staging.yml @@ -10,7 +10,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: [3.8, 3.9, "3.10", "3.11"] + python-version: [3.8, "3.11"] os: ["ubuntu-latest", "macOS-latest", "windows-latest"] steps: - name: Print Concurrency Group diff --git a/test/visualization/test_plotter_mpldrawer.py b/test/visualization/test_plotter_mpldrawer.py index fc5799535a..9e795b3265 100644 --- a/test/visualization/test_plotter_mpldrawer.py +++ b/test/visualization/test_plotter_mpldrawer.py @@ -32,6 +32,11 @@ class TestPlotterAndMplDrawer(QiskitExperimentsTestCase): """Test generic plotter with Matplotlib drawer.""" + def tearDown(self): + """Clean up test case state""" + plt.close("all") + super().tearDown() + def test_end_to_end_short(self): """Test whether plotter with MplDrawer returns a figure.""" plotter = MockPlotter(MplDrawer()) @@ -121,7 +126,6 @@ def test_series_names_different_types(self, series_names: Dict[type, List[Any]]) # have `buffer_rgba()` which is needed to compute the difference between two figures in this # method. We need to set the axes as MplDrawer will use # `qiskit_experiments.framework.matplotlib.get_non_gui_ax` by default; which uses an SVG backend. - plt.close("all") plt.switch_backend("Agg") axes = {} for key in series_names.keys():