From 8c904d7a41f10e3722d1870af5d5d8cb6a56d3fc Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 13 May 2023 21:15:02 -0400 Subject: [PATCH 1/2] Add diamond shape. --- src/data_morph/shapes/factory.py | 1 + src/data_morph/shapes/polygons.py | 36 +++++++++++++++++++++++++++++++ tests/shapes/test_polygons.py | 12 +++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/data_morph/shapes/factory.py b/src/data_morph/shapes/factory.py index ed081af0..caa0cb6b 100644 --- a/src/data_morph/shapes/factory.py +++ b/src/data_morph/shapes/factory.py @@ -48,6 +48,7 @@ class ShapeFactory: 'scatter': points.Scatter, 'right_parab': points.RightParabola, 'up_parab': points.UpParabola, + 'diamond': polygons.Diamond, 'rectangle': polygons.Rectangle, 'star': polygons.Star, } diff --git a/src/data_morph/shapes/polygons.py b/src/data_morph/shapes/polygons.py index 86a8f711..f9583145 100644 --- a/src/data_morph/shapes/polygons.py +++ b/src/data_morph/shapes/polygons.py @@ -4,6 +4,42 @@ from .bases.line_collection import LineCollection +class Diamond(LineCollection): + """ + Class for the diamond shape. + + .. plot:: + :scale: 75 + :caption: + This shape is generated using the panda dataset. + + import matplotlib.pyplot as plt + from data_morph.data.loader import DataLoader + from data_morph.shapes.polygons import Diamond + + _ = Diamond(DataLoader.load_dataset('panda')).plot() + + Parameters + ---------- + dataset : Dataset + The starting dataset to morph into other shapes. + """ + + def __init__(self, dataset: Dataset) -> None: + xmin, xmax = dataset.df.x.quantile([0.05, 0.95]) + ymin, ymax = dataset.df.y.quantile([0.05, 0.95]) + + xmid = (xmax + xmin) / 2 + ymid = (ymax + ymin) / 2 + + super().__init__( + [[xmin, ymid], [xmid, ymax]], + [[xmid, ymax], [xmax, ymid]], + [[xmax, ymid], [xmid, ymin]], + [[xmid, ymin], [xmin, ymid]], + ) + + class Rectangle(LineCollection): """ Class for the rectangle shape. diff --git a/tests/shapes/test_polygons.py b/tests/shapes/test_polygons.py index 17602fcc..1c8603cb 100644 --- a/tests/shapes/test_polygons.py +++ b/tests/shapes/test_polygons.py @@ -48,6 +48,18 @@ def test_lines_form_polygon(self, shape): assert np.unique(endpoints, axis=0).shape[0] == self.expected_line_count +class TestDiamond(PolygonsModuleTestBase): + """Test the Diamond class.""" + + shape_name = 'diamond' + distance_test_cases = [[(20, 50), 0.0], [(30, 60), 2.773501]] + expected_line_count = 4 + + def test_slopes(self, slopes): + """Test that the slopes are as expected.""" + np.testing.assert_array_equal(np.sort(slopes).flatten(), [-1.5, -1.5, 1.5, 1.5]) + + class TestRectangle(PolygonsModuleTestBase): """Test the Rectangle class.""" From f99740e3ab063d5ce5286e3188d548602517bf36 Mon Sep 17 00:00:00 2001 From: Stefanie Molin <24376333+stefmolin@users.noreply.github.com> Date: Sat, 13 May 2023 21:15:30 -0400 Subject: [PATCH 2/2] Increase point size for shapes in docs. --- src/data_morph/shapes/bases/point_collection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data_morph/shapes/bases/point_collection.py b/src/data_morph/shapes/bases/point_collection.py index 5afe3f7f..14850d29 100644 --- a/src/data_morph/shapes/bases/point_collection.py +++ b/src/data_morph/shapes/bases/point_collection.py @@ -70,5 +70,5 @@ def plot(self, ax: Axes = None) -> Axes: fig, ax = plt.subplots(layout='constrained') fig.get_layout_engine().set(w_pad=0.2, h_pad=0.2) _ = ax.axis('equal') - _ = ax.scatter(*self.points.T, s=2, color='k', alpha=self._alpha) + _ = ax.scatter(*self.points.T, s=5, color='k', alpha=self._alpha) return ax