From f8f878a4d6605dcd4689d13f3d05687359d6f7a1 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Mon, 13 Jan 2025 13:02:11 +0100 Subject: [PATCH 01/13] temp save --- plot_serializer/matplotlib/serializer.py | 11 +++++++---- requirements.txt | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/plot_serializer/matplotlib/serializer.py b/plot_serializer/matplotlib/serializer.py index 5af88b5..dbd2ec0 100644 --- a/plot_serializer/matplotlib/serializer.py +++ b/plot_serializer/matplotlib/serializer.py @@ -22,7 +22,7 @@ from matplotlib.lines import Line2D from matplotlib.patches import Polygon from mpl_toolkits.mplot3d.art3d import Path3DCollection, Poly3DCollection from mpl_toolkits.mplot3d.axes3d import Axes3D as MplAxes3D -from numpy import ndarray +from numpy import isin, ndarray from plot_serializer.model import ( Axis, @@ -224,13 +224,16 @@ class _AxesProxy(Proxy[MplAxes]): try: bars: List[Bar2D] = [] - - if isinstance(x, float): + if isinstance(x, np.generic): + x = x.item() + if isinstance(x, (float, int, str)): x = [x] else: x = np.asarray(x) - if isinstance(height, float): + if isinstance(height, np.generic): + height = height.item() + if isinstance(height, (float, int, str)): height = [height] else: height = np.asarray(height) diff --git a/requirements.txt b/requirements.txt index b5494a8..6bbe74d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,6 +22,7 @@ types-docutils # Unit testing pytest +hypothesis #Debug debugpy \ No newline at end of file -- GitLab From 3b14f670318c09f28304cfa31cf399deb3c79b93 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Mon, 20 Jan 2025 12:33:52 +0100 Subject: [PATCH 02/13] added all automatic tests --- plot_serializer/matplotlib/serializer.py | 10 +++++----- plot_serializer/model.py | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/plot_serializer/matplotlib/serializer.py b/plot_serializer/matplotlib/serializer.py index dbd2ec0..027e2c3 100644 --- a/plot_serializer/matplotlib/serializer.py +++ b/plot_serializer/matplotlib/serializer.py @@ -748,8 +748,8 @@ class _AxesProxy3D(Proxy[MplAxes3D]): def plot( self, - x_values: list[float], - y_values: list[float], + x_values, + y_values, *args: Any, **kwargs: Any, ) -> Path3DCollection: @@ -809,9 +809,9 @@ class _AxesProxy3D(Proxy[MplAxes3D]): def plot_surface( self, - x: list[list[float]], - y: list[list[float]], - z: list[list[float]], + x, + y, + z, *args: Any, **kwargs: Any, ) -> Poly3DCollection: diff --git a/plot_serializer/model.py b/plot_serializer/model.py index fd6575d..bda7649 100644 --- a/plot_serializer/model.py +++ b/plot_serializer/model.py @@ -82,9 +82,9 @@ class Point2D(BaseModel): class Point3D(BaseModel): metadata: Metadata = {} - x: Any # used to be: float - y: Any # used to be: float - z: Any # used to be: float + x: Any + y: Any + z: Any color: Optional[Color] = None size: Any = None @@ -184,7 +184,7 @@ class LineTrace2D(BaseModel): color: Optional[Color] = None linewidth: Optional[float] = None linestyle: Optional[str] = None - marker: Optional[str] = None # used to be MarkerStyle, however Markerstyle is a collection of markers + marker: Optional[str] = None label: Optional[str] = None datapoints: List[Point2D] -- GitLab From 665602c19680481ad89dbfa1924cedd477618f0b Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Fri, 24 Jan 2025 21:46:41 +0100 Subject: [PATCH 03/13] fixed all automatic tests so that they run, updated gitignore, still need to update CONTRIBUTING --- .gitignore | 1 + plot_serializer/matplotlib/serializer.py | 40 ++++++---- tests_automatic/__init__.py | 0 tests_automatic/test_bar_automatic.py | 47 +++++++++++ tests_automatic/test_box_automatic.py | 88 +++++++++++++++++++++ tests_automatic/test_errorbar_automatic.py | 64 +++++++++++++++ tests_automatic/test_hist_automatic.py | 52 ++++++++++++ tests_automatic/test_line3d_automatic.py | 53 +++++++++++++ tests_automatic/test_line_automatic.py | 46 +++++++++++ tests_automatic/test_pie_automatic.py | 45 +++++++++++ tests_automatic/test_scatter3d_automatic.py | 57 +++++++++++++ tests_automatic/test_scatter_automatic.py | 50 ++++++++++++ tests_automatic/test_surface3d_automatic.py | 58 ++++++++++++++ 13 files changed, 586 insertions(+), 15 deletions(-) create mode 100644 tests_automatic/__init__.py create mode 100644 tests_automatic/test_bar_automatic.py create mode 100644 tests_automatic/test_box_automatic.py create mode 100644 tests_automatic/test_errorbar_automatic.py create mode 100644 tests_automatic/test_hist_automatic.py create mode 100644 tests_automatic/test_line3d_automatic.py create mode 100644 tests_automatic/test_line_automatic.py create mode 100644 tests_automatic/test_pie_automatic.py create mode 100644 tests_automatic/test_scatter3d_automatic.py create mode 100644 tests_automatic/test_scatter_automatic.py create mode 100644 tests_automatic/test_surface3d_automatic.py diff --git a/.gitignore b/.gitignore index 4a77670..3324633 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ __pycache__ !/.gitlab !/.vscode !/tests +!/tests_automatic !/plot_serializer !/doc diff --git a/plot_serializer/matplotlib/serializer.py b/plot_serializer/matplotlib/serializer.py index 027e2c3..c775090 100644 --- a/plot_serializer/matplotlib/serializer.py +++ b/plot_serializer/matplotlib/serializer.py @@ -181,7 +181,7 @@ class _AxesProxy(Proxy[MplAxes]): color_list = c color_list = _convert_matplotlib_color(self, color_list, len(x), cmap="viridis", norm="linear")[0] - x = np.asarray(x, np.float32) + x = np.asarray(x) if not explode_list: explode_list = itertools.repeat(None) if not label_list: @@ -301,7 +301,6 @@ class _AxesProxy(Proxy[MplAxes]): xdata = mpl_line.get_xdata() ydata = mpl_line.get_ydata() - print(type(xdata)) points: List[Point2D] = [] @@ -365,6 +364,11 @@ class _AxesProxy(Proxy[MplAxes]): cmap = kwargs.get("cmap") or "viridis" norm = kwargs.get("norm") or "linear" + if isinstance(x, np.generic): + x = x.item() + if isinstance(x, (float, int, str)): + x = [x] + (color_list, cmap_used) = _convert_matplotlib_color(self, color_list, len(x), cmap, norm) if sizes_list is not None: @@ -511,21 +515,22 @@ class _AxesProxy(Proxy[MplAxes]): if yerr is not None and not isinstance(yerr, np.ndarray): yerr = _upcast_err(yerr) np.broadcast_to(xerr, (2, len(y))) + if xerr is None: xerr = itertools.repeat(None) + else: + if xerr.ndim == 0 or xerr.ndim == 1: + xerr = np.broadcast_to(xerr, (2, len(x))) + xerr = xerr.T if yerr is None: yerr = itertools.repeat(None) - - print(xerr) - print(xerr.ndim) - - if xerr.ndim == 0 or xerr.ndim == 1: - xerr = np.broadcast_to(xerr, (2, len(x))) - if yerr.ndim == 0 or yerr.ndim == 1: - yerr = np.broadcast_to(yerr, (2, len(y))) + else: + if yerr.ndim == 0 or yerr.ndim == 1: + yerr = np.broadcast_to(yerr, (2, len(y))) + yerr = yerr.T errorpoints: List[ErrorPoint2D] = [] - for xi, yi, x_error, y_error in zip(x, y, xerr.T, yerr.T): + for xi, yi, x_error, y_error in zip(x, y, xerr, yerr): errorpoints.append( ErrorPoint2D( x=xi, @@ -575,9 +580,9 @@ class _AxesProxy(Proxy[MplAxes]): raise try: - bins = kwargs.get("bins") or 10 - density = kwargs.get("density") or False - cumulative = kwargs.get("cumulative") or False + bins = kwargs.get("bins", 10) + density = kwargs.get("density", False) + cumulative = kwargs.get("cumulative", False) label_list = kwargs.get("label") color_list = kwargs.get("color") c = kwargs.get("c") @@ -592,7 +597,6 @@ class _AxesProxy(Proxy[MplAxes]): if np.isscalar(x): x = [x] x = _reshape_2D(x, "x") - print(x) color_list = _convert_matplotlib_color(self, color_list, len(x), "viridis", "linear")[0] @@ -702,6 +706,12 @@ class _AxesProxy3D(Proxy[MplAxes3D]): color_list = color cmap = kwargs.get("cmap") or "viridis" norm = kwargs.get("norm") or "linear" + + if isinstance(xs, np.generic): + xs = xs.item() + if isinstance(xs, (float, int, str)): + xs = [xs] + (color_list, cmap_used) = _convert_matplotlib_color(self, color_list, len(xs), cmap, norm) trace: List[ScatterTrace3D] = [] diff --git a/tests_automatic/__init__.py b/tests_automatic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests_automatic/test_bar_automatic.py b/tests_automatic/test_bar_automatic.py new file mode 100644 index 0000000..018b898 --- /dev/null +++ b/tests_automatic/test_bar_automatic.py @@ -0,0 +1,47 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.integers(), + st.floats(), + st.text(), + st.lists(st.text()), + st.lists(st.integers()), + st.lists(st.floats()), + arrays(np.int64, st.integers(min_value=0, max_value=10)), + arrays(np.float64, st.integers(min_value=0, max_value=10)), +) +heights_strategy = st.one_of( + st.integers(), + st.floats(), + st.lists(st.integers()), + st.lists(st.floats()), + arrays(np.int64, st.integers(min_value=0, max_value=10)), + arrays(np.float64, st.integers(min_value=0, max_value=10)), +) + + +@given(x=x_strategy, heights=heights_strategy) +def test_bar_properties( + x: Any, + heights: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.bar(x, heights) + except Exception as _e: + plt.close() + else: + serializer_ax.bar(x, heights) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_box_automatic.py b/tests_automatic/test_box_automatic.py new file mode 100644 index 0000000..e967664 --- /dev/null +++ b/tests_automatic/test_box_automatic.py @@ -0,0 +1,88 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.lists(st.lists(st.floats(), min_size=1), min_size=1), + st.lists(arrays(np.float64, st.integers(min_value=1, max_value=10)), min_size=1), + arrays(np.float64, st.integers(min_value=1, max_value=10)), + arrays(np.int64, st.integers(min_value=1, max_value=10)), +) +notch_strategy = st.one_of(st.none(), st.booleans()) +whis_strategy = st.one_of( + st.none(), + st.floats(), + st.tuples(st.floats(), st.floats()), +) +bootstrap_strategy = st.one_of(st.none(), st.integers(min_value=0)) +usermedians_strategy = st.one_of( + st.none(), + st.lists(st.one_of(st.floats(), st.none()), min_size=1), + arrays(np.float64, st.integers(min_value=1, max_value=10)), +) +conf_intervals_strategy = st.one_of( + st.none(), + st.lists( + st.tuples( + st.one_of(st.floats(), st.none()), + st.one_of(st.floats(), st.none()), + ), + min_size=1, + ), + arrays(np.float64, st.integers(min_value=1, max_value=10)), +) +tick_label_strategy = st.one_of(st.none(), st.lists(st.text(), min_size=1)) + + +@given( + x=x_strategy, + notch=notch_strategy, + whis=whis_strategy, + bootstrap=bootstrap_strategy, + usermedians=usermedians_strategy, + conf_intervals=conf_intervals_strategy, + tick_label=tick_label_strategy, +) +def test_box_properties( + x: Any, + notch: Any, + whis: Any, + bootstrap: Any, + usermedians: Any, + conf_intervals: Any, + tick_label: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.boxplot( + x=x, + notch=notch, + whis=whis, + bootstrap=bootstrap, + usermedians=usermedians, + conf_intervals=conf_intervals, + tick_label=tick_label, + ) + except Exception as _e: + plt.close() + else: + serializer_ax.boxplot( + x=x, + notch=notch, + whis=whis, + bootstrap=bootstrap, + usermedians=usermedians, + conf_intervals=conf_intervals, + labels=tick_label, + ) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_errorbar_automatic.py b/tests_automatic/test_errorbar_automatic.py new file mode 100644 index 0000000..26c2d2c --- /dev/null +++ b/tests_automatic/test_errorbar_automatic.py @@ -0,0 +1,64 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +y_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +xerr_strategy = st.one_of( + st.none(), + st.floats(min_value=0, allow_nan=False, allow_infinity=False), + st.lists(st.floats(min_value=0, allow_nan=False, allow_infinity=False), min_size=1), + arrays(np.float64, shape=st.integers(min_value=1, max_value=10)), + arrays(np.float64, shape=st.tuples(st.just(2), st.integers(min_value=1, max_value=10))), +) + +yerr_strategy = st.one_of( + st.none(), + st.floats(min_value=0, allow_nan=False, allow_infinity=False), + st.lists(st.floats(min_value=0, allow_nan=False, allow_infinity=False), min_size=1), + arrays(np.float64, shape=st.integers(min_value=1, max_value=10)), + arrays(np.float64, shape=st.tuples(st.just(2), st.integers(min_value=1, max_value=10))), +) + +marker_strategy = st.one_of(st.none(), st.text()) + +label_strategy = st.one_of(st.none(), st.text()) + + +@given(x=x_strategy, y=y_strategy, xerr=xerr_strategy, yerr=yerr_strategy, marker=marker_strategy, label=label_strategy) +def test_bar_properties( + x: Any, + y: Any, + xerr: Any, + yerr: Any, + marker: Any, + label: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.errorbar(x, y, xerr=xerr, yerr=yerr, marker=marker, label=label) + except Exception as _e: + plt.close() + else: + serializer_ax.errorbar(x, y, xerr=xerr, yerr=yerr, marker=marker, label=label) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_hist_automatic.py b/tests_automatic/test_hist_automatic.py new file mode 100644 index 0000000..d72e85a --- /dev/null +++ b/tests_automatic/test_hist_automatic.py @@ -0,0 +1,52 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), + st.lists(st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), min_size=1), + st.lists(arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), min_size=1), + arrays( + dtype=np.float64, + shape=st.tuples(st.integers(min_value=1, max_value=10), st.integers(min_value=1, max_value=10)), + ), +) +bins_strategy = st.one_of( + st.integers(min_value=1, max_value=50), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=50)), +) +density_strategy = st.booleans() +cumulative_strategy = st.one_of(st.booleans(), st.just(-1)) +label_strategy = st.one_of(st.none(), st.text()) + + +@given( + x=x_strategy, bins=bins_strategy, label=label_strategy, densitiy=density_strategy, culumative=cumulative_strategy +) +def test_hist_properties( + x: Any, + bins: Any, + label: Any, + densitiy: Any, + culumative: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.hist(x, bins=bins, label=label, density=densitiy, cumulative=culumative) + except Exception as _e: + plt.close() + else: + serializer_ax.hist(x, bins=bins, label=label, density=densitiy, cumulative=culumative) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_line3d_automatic.py b/tests_automatic/test_line3d_automatic.py new file mode 100644 index 0000000..e6499ba --- /dev/null +++ b/tests_automatic/test_line3d_automatic.py @@ -0,0 +1,53 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +y_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +z_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +label_strategy = st.one_of(st.none(), st.text()) + +linewidth_strategy = st.one_of(st.floats(allow_nan=False, allow_infinity=False), st.integers()) + + +@given(x=x_strategy, y=y_strategy, z=z_strategy, label=label_strategy, linewidth=linewidth_strategy) +def test_line_properties( + x: Any, + y: Any, + z: Any, + label: Any, + linewidth: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.plot(x, y, z, label=label, linewidth=linewidth) + except Exception as _e: + plt.close() + else: + serializer_ax.plot(x, y, z, label=label, linewidth=linewidth) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_line_automatic.py b/tests_automatic/test_line_automatic.py new file mode 100644 index 0000000..e01b9a6 --- /dev/null +++ b/tests_automatic/test_line_automatic.py @@ -0,0 +1,46 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +y_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +label_strategy = st.one_of(st.none(), st.text()) + +linewidth_strategy = st.one_of(st.floats(allow_nan=False, allow_infinity=False), st.integers()) + + +@given(x=x_strategy, y=y_strategy, label=label_strategy, linewidth=linewidth_strategy) +def test_line_properties( + x: Any, + y: Any, + label: Any, + linewidth: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.plot(x, y, label=label, linewidth=linewidth) + except Exception as _e: + plt.close() + else: + serializer_ax.plot(x, y, label=label, linewidth=linewidth) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_pie_automatic.py b/tests_automatic/test_pie_automatic.py new file mode 100644 index 0000000..3adc1f6 --- /dev/null +++ b/tests_automatic/test_pie_automatic.py @@ -0,0 +1,45 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +explode_strategy = st.one_of( + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), + st.none(), +) + +radius_strategy = st.floats(allow_nan=False, allow_infinity=False) + +labels_strategy = st.one_of(st.lists(st.text(), min_size=1), st.none()) + + +@given(x=x_strategy, explode=explode_strategy, labels=labels_strategy, radius=radius_strategy) +def test_pie_properties( + x: Any, + explode: Any, + labels: Any, + radius: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.pie(x, explode=explode, labels=labels, radius=radius) + except Exception as _e: + plt.close() + else: + serializer_ax.pie(x, explode=explode, labels=labels, radius=radius) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_scatter3d_automatic.py b/tests_automatic/test_scatter3d_automatic.py new file mode 100644 index 0000000..6875614 --- /dev/null +++ b/tests_automatic/test_scatter3d_automatic.py @@ -0,0 +1,57 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +y_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +z_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +s_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +label_strategy = st.one_of(st.none(), st.text()) + + +@given(x=x_strategy, y=y_strategy, z=z_strategy, s=s_strategy, label=label_strategy) +def test_scatter3d_properties( + x: Any, + y: Any, + z: Any, + s: Any, + label: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) + _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) + try: + ax.scatter(x, y, z, s=s, label=label) + except Exception as _e: + plt.close() + else: + serializer_ax.scatter(x, y, z, s=s, label=label) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_scatter_automatic.py b/tests_automatic/test_scatter_automatic.py new file mode 100644 index 0000000..da65180 --- /dev/null +++ b/tests_automatic/test_scatter_automatic.py @@ -0,0 +1,50 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +x_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +y_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +s_strategy = st.one_of( + st.floats(allow_nan=False, allow_infinity=False), + st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=1), + arrays(dtype=np.float64, shape=st.integers(min_value=1, max_value=10)), +) + +label_strategy = st.one_of(st.none(), st.text()) + + +@given(x=x_strategy, y=y_strategy, s=s_strategy, label=label_strategy) +def test_scatter_properties( + x: Any, + y: Any, + s: Any, + label: Any, +) -> None: + plt.close() + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots() + _fig, ax = plt.subplots() + try: + ax.scatter(x, y, s=s, label=label) + except Exception as _e: + plt.close() + else: + serializer_ax.scatter(x, y, s=s, label=label) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py new file mode 100644 index 0000000..97b4c87 --- /dev/null +++ b/tests_automatic/test_surface3d_automatic.py @@ -0,0 +1,58 @@ +from typing import Any + +import numpy as np +from hypothesis import given +from hypothesis import strategies as st +from hypothesis.extra.numpy import arrays +from matplotlib import pyplot as plt + +from plot_serializer.matplotlib.serializer import MatplotlibSerializer + +# surface strategies are hard to do, as they all need the same dimensions + +x_strategy = st.one_of( + arrays( + dtype=np.float64, + shape=(st.integers(min_value=1, max_value=10), st.integers(min_value=1, max_value=10)), + ) +) + + +@st.composite +def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value=100): + rows = draw(st.integers(min_value=min_dim, max_value=max_dim)) + cols = draw(st.integers(min_value=min_dim, max_value=max_dim)) + + matrix_strategy = st.one_of( + st.lists( + st.lists(st.integers(min_value=min_value, max_value=max_value), min_size=cols, max_size=cols), + min_size=rows, + max_size=rows, + ), + arrays(dtype=np.int64, shape=(rows, cols)), + ) + + matrix1 = draw(matrix_strategy) + matrix2 = draw(matrix_strategy) + matrix3 = draw(matrix_strategy) + + return matrix1, matrix2, matrix3 + + +@given(matrix_triplet_strategy()) +def test_surface_properties( + matrix_triplet: Any, +) -> None: + plt.close() + x, y, z = matrix_triplet + serializer = MatplotlibSerializer() + _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) + _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) + try: + ax.surface(x, y, z) + except Exception as _e: + plt.close() + else: + serializer_ax.surface(x, y, z) + assert serializer.to_json() != "{}", "Serialized JSON is empty check input" + plt.close() -- GitLab From 07ac7ddd2de36709264257d6a5236dc41a4d4b83 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 26 Jan 2025 21:30:47 +0100 Subject: [PATCH 04/13] fixed unit test error, unit tests might need a big overhaul when changing all kwargs.get --- plot_serializer/matplotlib/serializer.py | 2 +- tests/test_hist_plot.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/plot_serializer/matplotlib/serializer.py b/plot_serializer/matplotlib/serializer.py index c775090..957c4fe 100644 --- a/plot_serializer/matplotlib/serializer.py +++ b/plot_serializer/matplotlib/serializer.py @@ -22,7 +22,7 @@ from matplotlib.lines import Line2D from matplotlib.patches import Polygon from mpl_toolkits.mplot3d.art3d import Path3DCollection, Poly3DCollection from mpl_toolkits.mplot3d.axes3d import Axes3D as MplAxes3D -from numpy import isin, ndarray +from numpy import ndarray from plot_serializer.model import ( Axis, diff --git a/tests/test_hist_plot.py b/tests/test_hist_plot.py index d7f3c51..4beebfc 100644 --- a/tests/test_hist_plot.py +++ b/tests/test_hist_plot.py @@ -109,6 +109,9 @@ def test_hist_plot( update_tests = request.config.getoption("--update-tests") _, ax = serializer.subplots() + if bins is None and cumulative is None and density is None: + # giving arguments the none value throws errors, might need a test overhaul overall + ax.hist(x) ax.hist( x, bins=bins, @@ -126,7 +129,7 @@ def test_hist_plot( ax.set_ylabel(ylabel) if metadata: - ax.hist(x, bins=bins, color=color, label=label, cumulative=cumulative, density=density) + ax.hist(x) serializer.add_custom_metadata_figure(metadata) serializer.add_custom_metadata_plot(metadata, plot_selector=0) serializer.add_custom_metadata_axis(metadata, axis="y", plot_selector=0) -- GitLab From 8d7e49b48c1196b8f8a58c39399f2468d6cdca37 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 26 Jan 2025 21:38:53 +0100 Subject: [PATCH 05/13] fixed small errors --- tests_automatic/test_box_automatic.py | 10 +++++----- tests_automatic/test_surface3d_automatic.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tests_automatic/test_box_automatic.py b/tests_automatic/test_box_automatic.py index e967664..69329b0 100644 --- a/tests_automatic/test_box_automatic.py +++ b/tests_automatic/test_box_automatic.py @@ -37,7 +37,7 @@ conf_intervals_strategy = st.one_of( ), arrays(np.float64, st.integers(min_value=1, max_value=10)), ) -tick_label_strategy = st.one_of(st.none(), st.lists(st.text(), min_size=1)) +tick_labels_strategy = st.one_of(st.none(), st.lists(st.text(), min_size=1)) @given( @@ -47,7 +47,7 @@ tick_label_strategy = st.one_of(st.none(), st.lists(st.text(), min_size=1)) bootstrap=bootstrap_strategy, usermedians=usermedians_strategy, conf_intervals=conf_intervals_strategy, - tick_label=tick_label_strategy, + tick_labels=tick_labels_strategy, ) def test_box_properties( x: Any, @@ -56,7 +56,7 @@ def test_box_properties( bootstrap: Any, usermedians: Any, conf_intervals: Any, - tick_label: Any, + tick_labels: Any, ) -> None: plt.close() serializer = MatplotlibSerializer() @@ -70,7 +70,7 @@ def test_box_properties( bootstrap=bootstrap, usermedians=usermedians, conf_intervals=conf_intervals, - tick_label=tick_label, + tick_labels=tick_labels, ) except Exception as _e: plt.close() @@ -82,7 +82,7 @@ def test_box_properties( bootstrap=bootstrap, usermedians=usermedians, conf_intervals=conf_intervals, - labels=tick_label, + tick_labels=tick_labels, ) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" plt.close() diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index 97b4c87..0392b1a 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -19,7 +19,7 @@ x_strategy = st.one_of( @st.composite -def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value=100): +def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value=100): # type: ignore rows = draw(st.integers(min_value=min_dim, max_value=max_dim)) cols = draw(st.integers(min_value=min_dim, max_value=max_dim)) @@ -49,10 +49,10 @@ def test_surface_properties( _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) try: - ax.surface(x, y, z) + ax.plot_surface(x, y, z) except Exception as _e: plt.close() else: - serializer_ax.surface(x, y, z) + serializer_ax.plot_surface(x, y, z) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" plt.close() -- GitLab From 8aa3cb42598cee10fe2307ade77d9112698ec9a2 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 15:56:46 +0100 Subject: [PATCH 06/13] trying fix for mypy error --- tests_automatic/test_surface3d_automatic.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index 0392b1a..3af2f03 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -8,15 +8,6 @@ from matplotlib import pyplot as plt from plot_serializer.matplotlib.serializer import MatplotlibSerializer -# surface strategies are hard to do, as they all need the same dimensions - -x_strategy = st.one_of( - arrays( - dtype=np.float64, - shape=(st.integers(min_value=1, max_value=10), st.integers(min_value=1, max_value=10)), - ) -) - @st.composite def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value=100): # type: ignore @@ -29,7 +20,7 @@ def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value= min_size=rows, max_size=rows, ), - arrays(dtype=np.int64, shape=(rows, cols)), + arrays(dtype=np.int64, shape=(rows, cols)), # type: ignore ) matrix1 = draw(matrix_strategy) -- GitLab From 3cc8c3ea9bda8db1f078d5a9182182843dd3e7c1 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 16:13:24 +0100 Subject: [PATCH 07/13] another try to bypass mypy --- tests_automatic/test_surface3d_automatic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index 3af2f03..cca17e8 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -20,7 +20,7 @@ def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value= min_size=rows, max_size=rows, ), - arrays(dtype=np.int64, shape=(rows, cols)), # type: ignore + arrays(dtype=np.int64, shape=(rows, cols)), ) matrix1 = draw(matrix_strategy) @@ -38,7 +38,7 @@ def test_surface_properties( x, y, z = matrix_triplet serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) - _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) + _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) # type: ignore try: ax.plot_surface(x, y, z) except Exception as _e: -- GitLab From ca2cf96f31d91a3b31b7927d3abe474d6a4c6a55 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 16:17:15 +0100 Subject: [PATCH 08/13] again --- tests_automatic/test_surface3d_automatic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index cca17e8..2e47cce 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -38,12 +38,12 @@ def test_surface_properties( x, y, z = matrix_triplet serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) - _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) # type: ignore + _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) try: ax.plot_surface(x, y, z) except Exception as _e: plt.close() else: - serializer_ax.plot_surface(x, y, z) + serializer_ax.plot_surface(x, y, z) # type: ignore assert serializer.to_json() != "{}", "Serialized JSON is empty check input" plt.close() -- GitLab From 05490f62216f0564ae8f35ea7e6c6aa08c6c0e0c Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 16:20:04 +0100 Subject: [PATCH 09/13] again on right plot_surface call --- tests_automatic/test_surface3d_automatic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index 2e47cce..6aa29d4 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -40,10 +40,10 @@ def test_surface_properties( _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) try: - ax.plot_surface(x, y, z) + ax.plot_surface(x, y, z) # type: ignore except Exception as _e: plt.close() else: - serializer_ax.plot_surface(x, y, z) # type: ignore + serializer_ax.plot_surface(x, y, z) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" plt.close() -- GitLab From 0a4849065207b3e022be78fee4458ec4f9fc4680 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 16:22:59 +0100 Subject: [PATCH 10/13] fixed other error for mypy having wrong static type from subplots() --- tests_automatic/test_scatter3d_automatic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests_automatic/test_scatter3d_automatic.py b/tests_automatic/test_scatter3d_automatic.py index 6875614..8bfcd52 100644 --- a/tests_automatic/test_scatter3d_automatic.py +++ b/tests_automatic/test_scatter3d_automatic.py @@ -48,7 +48,7 @@ def test_scatter3d_properties( _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) try: - ax.scatter(x, y, z, s=s, label=label) + ax.scatter(x, y, z, s=s, label=label) # type: ignore except Exception as _e: plt.close() else: -- GitLab From 268b8b60f4449bb00e889f78cb2a892647255de2 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 17:43:09 +0100 Subject: [PATCH 11/13] added doc for automatic testing, found another bug inside boxplot via mask, fix next --- CONTRIBUTING.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8ccba99..88b59c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -181,3 +181,10 @@ pytest tests --update-tests=confirm The first type of files created being all the tests accomodating the new model under the tests_updated folder. They have to be manually checked for correctness and then moved into the initial [plots](./tests/plots) folder. The second being the new model under [specification](./doc/static/specification) where it will have to be renamed according to the current version of PlotSerializer to be pushed. The third being pictures of every plottype deserialized into a figure to broadly confirm the deserializer is still functioning. Once again the contents must me renamed and replace the old files in [folder](./tests/deserializer_matrix) upon confirming correctness. + +Besides the manual tests comparing generated JSON files to the expected result there are automatic tests checking if errors are thrown for automatically generated input arguments and testing if the returned JSON is empty. So far these tests mostly only support the main parameters of the functions and only some keyword parameters. +It is recommended to run the test for the specific plot under scrutiny, not the entire testfolder via pytest as the . An example for boxplot: + +```cmd +pytest tests_automatic test_box_automatic.py +``` \ No newline at end of file -- GitLab From 12c96175724247c09be51d20d51514e53f0235b0 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 22:00:46 +0100 Subject: [PATCH 12/13] added doc, fixed timing bug in tests, fixed boxplot bug --- plot_serializer/matplotlib/serializer.py | 2 +- tests_automatic/test_bar_automatic.py | 7 ++++--- tests_automatic/test_box_automatic.py | 11 +++++++---- tests_automatic/test_errorbar_automatic.py | 7 ++++--- tests_automatic/test_hist_automatic.py | 7 ++++--- tests_automatic/test_line3d_automatic.py | 7 ++++--- tests_automatic/test_line_automatic.py | 7 ++++--- tests_automatic/test_pie_automatic.py | 8 +++++--- tests_automatic/test_scatter3d_automatic.py | 7 ++++--- tests_automatic/test_scatter_automatic.py | 7 ++++--- tests_automatic/test_surface3d_automatic.py | 7 ++++--- 11 files changed, 45 insertions(+), 32 deletions(-) diff --git a/plot_serializer/matplotlib/serializer.py b/plot_serializer/matplotlib/serializer.py index 957c4fe..6cd93f3 100644 --- a/plot_serializer/matplotlib/serializer.py +++ b/plot_serializer/matplotlib/serializer.py @@ -444,7 +444,7 @@ class _AxesProxy(Proxy[MplAxes]): conf_intervals = itertools.repeat(None) for dataset, label, umedian, cintervals in zip(x, labels, usermedians, conf_intervals): - x = np.ma.asarray(x) + x = np.ma.asarray(x, dtype="object") x = x.data[~x.mask].ravel() boxes.append( Box( diff --git a/tests_automatic/test_bar_automatic.py b/tests_automatic/test_bar_automatic.py index 018b898..58c669b 100644 --- a/tests_automatic/test_bar_automatic.py +++ b/tests_automatic/test_bar_automatic.py @@ -33,15 +33,16 @@ def test_bar_properties( x: Any, heights: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.bar(x, heights) except Exception as _e: - plt.close() + pass else: serializer_ax.bar(x, heights) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_box_automatic.py b/tests_automatic/test_box_automatic.py index 69329b0..643e31d 100644 --- a/tests_automatic/test_box_automatic.py +++ b/tests_automatic/test_box_automatic.py @@ -1,10 +1,11 @@ from typing import Any import numpy as np -from hypothesis import given +from hypothesis import given, settings from hypothesis import strategies as st from hypothesis.extra.numpy import arrays from matplotlib import pyplot as plt +from matplotlib.pylab import f from plot_serializer.matplotlib.serializer import MatplotlibSerializer @@ -49,6 +50,7 @@ tick_labels_strategy = st.one_of(st.none(), st.lists(st.text(), min_size=1)) conf_intervals=conf_intervals_strategy, tick_labels=tick_labels_strategy, ) +@settings(deadline=500) def test_box_properties( x: Any, notch: Any, @@ -58,7 +60,6 @@ def test_box_properties( conf_intervals: Any, tick_labels: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() @@ -73,7 +74,7 @@ def test_box_properties( tick_labels=tick_labels, ) except Exception as _e: - plt.close() + pass else: serializer_ax.boxplot( x=x, @@ -85,4 +86,6 @@ def test_box_properties( tick_labels=tick_labels, ) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_errorbar_automatic.py b/tests_automatic/test_errorbar_automatic.py index 26c2d2c..c2b688f 100644 --- a/tests_automatic/test_errorbar_automatic.py +++ b/tests_automatic/test_errorbar_automatic.py @@ -50,15 +50,16 @@ def test_bar_properties( marker: Any, label: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.errorbar(x, y, xerr=xerr, yerr=yerr, marker=marker, label=label) except Exception as _e: - plt.close() + pass else: serializer_ax.errorbar(x, y, xerr=xerr, yerr=yerr, marker=marker, label=label) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_hist_automatic.py b/tests_automatic/test_hist_automatic.py index d72e85a..35dba2b 100644 --- a/tests_automatic/test_hist_automatic.py +++ b/tests_automatic/test_hist_automatic.py @@ -38,15 +38,16 @@ def test_hist_properties( densitiy: Any, culumative: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.hist(x, bins=bins, label=label, density=densitiy, cumulative=culumative) except Exception as _e: - plt.close() + pass else: serializer_ax.hist(x, bins=bins, label=label, density=densitiy, cumulative=culumative) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_line3d_automatic.py b/tests_automatic/test_line3d_automatic.py index e6499ba..4490649 100644 --- a/tests_automatic/test_line3d_automatic.py +++ b/tests_automatic/test_line3d_automatic.py @@ -39,15 +39,16 @@ def test_line_properties( label: Any, linewidth: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.plot(x, y, z, label=label, linewidth=linewidth) except Exception as _e: - plt.close() + pass else: serializer_ax.plot(x, y, z, label=label, linewidth=linewidth) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_line_automatic.py b/tests_automatic/test_line_automatic.py index e01b9a6..2684401 100644 --- a/tests_automatic/test_line_automatic.py +++ b/tests_automatic/test_line_automatic.py @@ -32,15 +32,16 @@ def test_line_properties( label: Any, linewidth: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.plot(x, y, label=label, linewidth=linewidth) except Exception as _e: - plt.close() + pass else: serializer_ax.plot(x, y, label=label, linewidth=linewidth) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_pie_automatic.py b/tests_automatic/test_pie_automatic.py index 3adc1f6..4026d9d 100644 --- a/tests_automatic/test_pie_automatic.py +++ b/tests_automatic/test_pie_automatic.py @@ -1,3 +1,4 @@ +import time from typing import Any import numpy as np @@ -31,15 +32,16 @@ def test_pie_properties( labels: Any, radius: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.pie(x, explode=explode, labels=labels, radius=radius) except Exception as _e: - plt.close() + pass else: serializer_ax.pie(x, explode=explode, labels=labels, radius=radius) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_scatter3d_automatic.py b/tests_automatic/test_scatter3d_automatic.py index 8bfcd52..5eb4d42 100644 --- a/tests_automatic/test_scatter3d_automatic.py +++ b/tests_automatic/test_scatter3d_automatic.py @@ -43,15 +43,16 @@ def test_scatter3d_properties( s: Any, label: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) _fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) try: ax.scatter(x, y, z, s=s, label=label) # type: ignore except Exception as _e: - plt.close() + pass else: serializer_ax.scatter(x, y, z, s=s, label=label) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_scatter_automatic.py b/tests_automatic/test_scatter_automatic.py index da65180..8f26315 100644 --- a/tests_automatic/test_scatter_automatic.py +++ b/tests_automatic/test_scatter_automatic.py @@ -36,15 +36,16 @@ def test_scatter_properties( s: Any, label: Any, ) -> None: - plt.close() serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots() _fig, ax = plt.subplots() try: ax.scatter(x, y, s=s, label=label) except Exception as _e: - plt.close() + pass else: serializer_ax.scatter(x, y, s=s, label=label) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) diff --git a/tests_automatic/test_surface3d_automatic.py b/tests_automatic/test_surface3d_automatic.py index 6aa29d4..fd8573a 100644 --- a/tests_automatic/test_surface3d_automatic.py +++ b/tests_automatic/test_surface3d_automatic.py @@ -34,7 +34,6 @@ def matrix_triplet_strategy(draw, min_dim=1, max_dim=10, min_value=0, max_value= def test_surface_properties( matrix_triplet: Any, ) -> None: - plt.close() x, y, z = matrix_triplet serializer = MatplotlibSerializer() _, serializer_ax = serializer.subplots(subplot_kw={"projection": "3d"}) @@ -42,8 +41,10 @@ def test_surface_properties( try: ax.plot_surface(x, y, z) # type: ignore except Exception as _e: - plt.close() + pass else: serializer_ax.plot_surface(x, y, z) assert serializer.to_json() != "{}", "Serialized JSON is empty check input" - plt.close() + finally: + plt.close(_) + plt.close(_fig) -- GitLab From d85a471b039b138f6e22d9a441e22026dc213084 Mon Sep 17 00:00:00 2001 From: Julius <juliusflorstedt@gmail.com> Date: Sun, 2 Feb 2025 22:24:51 +0100 Subject: [PATCH 13/13] removed imports --- tests_automatic/test_box_automatic.py | 1 - tests_automatic/test_pie_automatic.py | 1 - 2 files changed, 2 deletions(-) diff --git a/tests_automatic/test_box_automatic.py b/tests_automatic/test_box_automatic.py index 643e31d..91fcb9b 100644 --- a/tests_automatic/test_box_automatic.py +++ b/tests_automatic/test_box_automatic.py @@ -5,7 +5,6 @@ from hypothesis import given, settings from hypothesis import strategies as st from hypothesis.extra.numpy import arrays from matplotlib import pyplot as plt -from matplotlib.pylab import f from plot_serializer.matplotlib.serializer import MatplotlibSerializer diff --git a/tests_automatic/test_pie_automatic.py b/tests_automatic/test_pie_automatic.py index 4026d9d..44d14bb 100644 --- a/tests_automatic/test_pie_automatic.py +++ b/tests_automatic/test_pie_automatic.py @@ -1,4 +1,3 @@ -import time from typing import Any import numpy as np -- GitLab