Skip to content

Commit

Permalink
Remove warnings of too many open figures during testing: plt.close("a…
Browse files Browse the repository at this point in the history
…ll")
  • Loading branch information
tillbiskup committed Dec 12, 2024
1 parent af6d9cf commit 6654393
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 19 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0.dev54
0.12.0.dev55
28 changes: 28 additions & 0 deletions tests/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

import aspecd.annotation as annotation
Expand Down Expand Up @@ -161,6 +162,9 @@ class TestPlotAnnotation(unittest.TestCase):
def setUp(self):
self.annotation = annotation.PlotAnnotation()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -238,6 +242,9 @@ def setUp(self):
self.annotation = annotation.VerticalLine()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -330,6 +337,9 @@ def setUp(self):
self.annotation = annotation.HorizontalLine()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -416,6 +426,9 @@ def setUp(self):
self.annotation = annotation.Text()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -531,6 +544,9 @@ def setUp(self):
self.annotation = annotation.VerticalSpan()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -629,6 +645,9 @@ def setUp(self):
self.annotation = annotation.HorizontalSpan()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -727,6 +746,9 @@ def setUp(self):
self.annotation = annotation.TextWithLine()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -906,6 +928,9 @@ def setUp(self):
self.annotation = annotation.Marker()
self.plotter = aspecd.plotting.Plotter()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -1083,6 +1108,9 @@ def setUp(self):
self.dataset = aspecd.dataset.CalculatedDataset()
self.dataset.data.data = np.ones(101)

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down
51 changes: 34 additions & 17 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
def tearDown(self):
if os.path.isfile(self.filename):
os.remove(self.filename)
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_plot_sets_figure_property(self):
self.assertTrue(
isinstance(self.plotter.figure, matplotlib.figure.Figure)
)
plt.close(self.plotter.figure)
plt.close("all")

def test_plot_sets_fig_property(self):
self.plotter.plot()
Expand All @@ -119,12 +119,12 @@ def test_plot_sets_fig_property(self):
def test_plot_sets_axes_property(self):
self.plotter.plot()
self.assertTrue(isinstance(self.plotter.axes, matplotlib.axes.Axes))
plt.close(self.plotter.figure)
plt.close("all")

def test_plot_sets_ax_property(self):
self.plotter.plot()
self.assertTrue(isinstance(self.plotter.ax, matplotlib.axes.Axes))
plt.close(self.plotter.figure)
plt.close("all")

def test_plot_sets_no_new_figure_property_if_existing(self):
fig, ax = plt.subplots()
Expand Down Expand Up @@ -347,7 +347,7 @@ def setUp(self):
self.plotter = plotting.SinglePlotter()

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -502,7 +502,7 @@ def test_instantiate_class(self):
pass

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_has_type_property(self):
self.assertTrue(hasattr(self.plotter, "type"))
Expand Down Expand Up @@ -711,7 +711,7 @@ def setUp(self):
self.plotter = plotting.SinglePlotter2D()

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -1139,7 +1139,7 @@ def setUp(self):
def tearDown(self):
if os.path.exists(self.filename):
os.remove(self.filename)
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -1470,7 +1470,7 @@ def setUp(self):
def tearDown(self):
if os.path.exists(self.filename):
os.remove(self.filename)
plt.close(self.plotter.figure)
plt.close("all")

def create_dataset(self, devices=("device1", "device2")):
self.dataset = aspecd.dataset.CalculatedDataset()
Expand Down Expand Up @@ -1722,7 +1722,7 @@ def setUp(self):
self.dataset2d.data.axes[2].unit = "V"

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -1861,7 +1861,7 @@ def setUp(self):
self.plotter = plotting.MultiPlotter()

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -2071,7 +2071,7 @@ def setUp(self):
self.dataset.data.axes[1].unit = "V"

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -2312,7 +2312,7 @@ def setUp(self):
self.plotter.datasets.append(dataset_)

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -2529,7 +2529,7 @@ def setUp(self):
self.dataset.data.data = np.sin(np.linspace(0, 2 * np.pi, 101))

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -2994,7 +2994,7 @@ def setUp(self):
self.plotter = plotting.SingleCompositePlotter()

def tearDown(self):
plt.close(self.plotter.figure)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -3085,8 +3085,7 @@ def setUp(self):
def tearDown(self):
if os.path.isfile(self.filename):
os.remove(self.filename)
if self.saver.plotter and self.saver.plotter.fig:
plt.close(self.saver.plotter.fig)
plt.close("all")

def test_instantiate_class(self):
pass
Expand Down Expand Up @@ -3376,6 +3375,9 @@ class TestPlotProperties(unittest.TestCase):
def setUp(self):
self.plot_properties = plotting.PlotProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -3429,6 +3431,9 @@ class TestFigureProperties(unittest.TestCase):
def setUp(self):
self.figure_properties = plotting.FigureProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -3486,6 +3491,9 @@ class TestAxesProperties(unittest.TestCase):
def setUp(self):
self.axis_properties = plotting.AxesProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -3818,6 +3826,9 @@ class TestLegendProperties(unittest.TestCase):
def setUp(self):
self.legend_properties = plotting.LegendProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -3954,6 +3965,9 @@ class TestGridProperties(unittest.TestCase):
def setUp(self):
self.grid_properties = plotting.GridProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down Expand Up @@ -4047,6 +4061,9 @@ class TestSinglePlotProperties(unittest.TestCase):
def setUp(self):
self.plot_properties = plotting.SinglePlotProperties()

def tearDown(self):
plt.close("all")

def test_instantiate_class(self):
pass

Expand Down
2 changes: 1 addition & 1 deletion tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4936,7 +4936,7 @@ def test_serve_issues_warning_if_told_to_not_write_history(self):
self.assertIn(
"No history has been written. This is considered bad "
"practice in terms of reproducible research",
cm.output[1],
cm.output[2],
)

def test_written_history_can_be_used_as_recipe(self):
Expand Down

0 comments on commit 6654393

Please sign in to comment.