Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/data_morph/shapes/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ShapeFactory:
'dots': points.DotsGrid,
'down_parab': points.DownParabola,
'heart': points.Heart,
'infinity': points.Infinity,
'left_parab': points.LeftParabola,
'scatter': points.Scatter,
'right_parab': points.RightParabola,
Expand Down
49 changes: 49 additions & 0 deletions src/data_morph/shapes/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ def __init__(self, dataset: Dataset) -> None:
)


class Infinity(PointCollection):
"""
Class for the infinity shape.

.. plot::
:scale: 75
:caption:
This shape is generated using the panda dataset.

from data_morph.data.loader import DataLoader
from data_morph.shapes.points import Infinity

_ = Infinity(DataLoader.load_dataset('panda')).plot()

Parameters
----------
dataset : Dataset
The starting dataset to morph into other shapes.

Notes
-----
The formula for the infinity shape is directly taken from Lemniscate of
Bernoulli equation.
`Infinity Curve <https://mathworld.wolfram.com/Lemniscate.html>`_:

Weisstein, Eric W. "Lemniscate." From MathWorld--
A Wolfram Web Resource. https://mathworld.wolfram.com/Lemniscate.html
"""

def __init__(self, dataset: Dataset, scale: float = 0.75) -> None:
x_bounds = dataset.data_bounds.x_bounds
y_bounds = dataset.data_bounds.y_bounds

x_shift = sum(x_bounds) / 2
y_shift = sum(y_bounds) / 2

t = np.linspace(-3, 3, num=2000)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 2,000 points is a lot, the algorithm needs to calculate the distance to each point. Can we reduce this? For example, the heart only uses 80.


x = (np.sqrt(2) * np.cos(t)) / (1 + np.square(np.sin(t)))
y = (np.sqrt(2) * np.cos(t) * np.sin(t)) / (1 + np.square(np.sin(t)))

# scale by the half the widest width of the infinity
scale_factor = (x_bounds[1] - x_shift) * 0.75
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the y information here? Maybe pick one using the data? With the sheep for example, it seems like the figure eight is being drawn too tall, so we should probably take the minimum of the x and y scale factors and use that.

Try plotting a dataset and the shape on top of each other and see how that looks.


super().__init__(
*np.stack([x * scale_factor + x_shift, y * scale_factor + y_shift], axis=1)
)


class LeftParabola(PointCollection):
"""
Class for the left parabola shape.
Expand Down