Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions docs/tutorials/shape-creation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ to calculate its position and scale:
name = 'x'

def __init__(self, dataset: Dataset) -> None:
xmin, xmax = dataset.morph_bounds.x_bounds
ymin, ymax = dataset.morph_bounds.y_bounds
(xmin, xmax), (ymin, ymax) = dataset.morph_bounds

super().__init__([[xmin, ymin], [xmax, ymax]], [[xmin, ymax], [xmax, ymin]])

Expand Down
15 changes: 14 additions & 1 deletion src/data_morph/bounds/bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ def __init__(
)
"""Interval: The bounds for the y direction."""

self._bounds = (self.x_bounds, self.y_bounds)

def __iter__(self) -> Interval:
"""
Iterate over the bounds in the bounding box.

Returns
-------
Interval
The next set of bounds.
"""
return iter(self._bounds)

def __contains__(self, value: Iterable[Number]) -> bool:
"""
Add support for using the ``in`` operator to check whether
Expand Down Expand Up @@ -95,7 +108,7 @@ def __eq__(self, other: BoundingBox) -> bool:
"""
if not isinstance(other, BoundingBox):
raise TypeError('Equality is only defined between BoundingBox objects.')
return self.x_bounds == other.x_bounds and self.y_bounds == other.y_bounds
return self._bounds == other._bounds

def __repr__(self) -> str:
return f'<BoundingBox>\n x={self.x_bounds}\n y={self.y_bounds}'
Expand Down
11 changes: 6 additions & 5 deletions src/data_morph/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@
scale_base = 85

# data bounds
x_offset = self.data_bounds.x_bounds.range / scale_base
y_offset = self.data_bounds.y_bounds.range / scale_base
x_range, y_range = self.data_bounds.range
x_offset = x_range / scale_base
y_offset = y_range / scale_base

Check warning on line 225 in src/data_morph/data/dataset.py

View check run for this annotation

Codecov / codecov/patch

src/data_morph/data/dataset.py#L223-L225

Added lines #L223 - L225 were not covered by tests
data_rectangle = [
self.data_bounds.x_bounds[0] - x_offset,
self.data_bounds.y_bounds[0] - y_offset,
Expand All @@ -230,16 +231,16 @@
ax.add_patch(
plt.Rectangle(
data_rectangle,
width=self.data_bounds.x_bounds.range + x_offset * 2,
height=self.data_bounds.y_bounds.range + y_offset * 2,
width=x_range + x_offset * 2,
height=y_range + y_offset * 2,
ec='blue',
linewidth=2,
fill=False,
)
)
ax.text(
(self.data.x.max() + self.data.x.min()) / 2,
self.data.y.max() + self.data_bounds.y_bounds.range / scale_base,
self.data.y.max() + y_offset,
'DATA BOUNDS',
color='blue',
va='bottom',
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/high_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class HighLines(LineCollection):
name = 'high_lines'

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

offset = y_bounds.range / 5
lower = y_bounds[0] + offset
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/horizontal_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class HorizontalLines(LineCollection):
name = 'h_lines'

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

super().__init__(
*[
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/slant_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class SlantDownLines(LineCollection):
name = 'slant_down'

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.morph_bounds.x_bounds
y_bounds = dataset.morph_bounds.y_bounds
x_bounds, y_bounds = dataset.morph_bounds

xmin, xmax = x_bounds
xmid = xmin + x_bounds.range / 2
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/slant_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class SlantUpLines(LineCollection):
name = 'slant_up'

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.morph_bounds.x_bounds
y_bounds = dataset.morph_bounds.y_bounds
x_bounds, y_bounds = dataset.morph_bounds

xmin, xmax = x_bounds
xmid = xmin + x_bounds.range / 2
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/vertical_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class VerticalLines(LineCollection):
name = 'v_lines'

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

super().__init__(
*[
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/wide_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class WideLines(LineCollection):
name = 'wide_lines'

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

offset = x_bounds.range / 5
lower = x_bounds[0] + offset
Expand Down
3 changes: 1 addition & 2 deletions src/data_morph/shapes/lines/x_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class XLines(LineCollection):
name = 'x'

def __init__(self, dataset: Dataset) -> None:
xmin, xmax = dataset.morph_bounds.x_bounds
ymin, ymax = dataset.morph_bounds.y_bounds
(xmin, xmax), (ymin, ymax) = dataset.morph_bounds

super().__init__([[xmin, ymin], [xmax, ymax]], [[xmin, ymax], [xmax, ymin]])
4 changes: 2 additions & 2 deletions src/data_morph/shapes/points/heart.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Heart(PointCollection):
"""

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.data_bounds.x_bounds
_, xmax = dataset.data_bounds.x_bounds
x_shift, y_shift = dataset.data_bounds.center

t = np.linspace(-3, 3, num=80)
Expand All @@ -44,7 +44,7 @@ def __init__(self, dataset: Dataset) -> None:
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)

# scale by the half the widest width of the heart
scale_factor = (x_bounds[1] - x_shift) / 16
scale_factor = (xmax - x_shift) / 16

super().__init__(
*np.stack([x * scale_factor + x_shift, y * scale_factor + y_shift], axis=1)
Expand Down
36 changes: 16 additions & 20 deletions src/data_morph/shapes/points/parabola.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ class DownParabola(PointCollection):
name = 'down_parab'

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.data_bounds.x_bounds
x_bounds, (ymin, ymax) = dataset.data_bounds
xmin, xmax = x_bounds
xmid = xmax - x_bounds.range / 2

x_offset = x_bounds.range / 10
xmin += x_offset
xmax -= x_offset

ymin, ymax = dataset.data_bounds.y_bounds

poly = np.polynomial.Polynomial.fit([xmin, xmid, xmax], [ymin, ymax, ymin], 2)
poly = np.polynomial.Polynomial.fit(
[xmin, x_bounds.center, xmax], [ymin, ymax, ymin], 2
)

super().__init__(*np.stack(poly.linspace(), axis=1))

Expand Down Expand Up @@ -67,17 +66,16 @@ class LeftParabola(PointCollection):
name = 'left_parab'

def __init__(self, dataset: Dataset) -> None:
y_bounds = dataset.data_bounds.y_bounds
(xmin, xmax), y_bounds = dataset.data_bounds
ymin, ymax = y_bounds
ymid = ymax - y_bounds.range / 2

y_offset = y_bounds.range / 10
ymin += y_offset
ymax -= y_offset

xmin, xmax = dataset.data_bounds.x_bounds

poly = np.polynomial.Polynomial.fit([ymin, ymid, ymax], [xmin, xmax, xmin], 2)
poly = np.polynomial.Polynomial.fit(
[ymin, y_bounds.center, ymax], [xmin, xmax, xmin], 2
)

super().__init__(*np.stack(poly.linspace()[::-1], axis=1))

Expand Down Expand Up @@ -105,17 +103,16 @@ class RightParabola(PointCollection):
name = 'right_parab'

def __init__(self, dataset: Dataset) -> None:
y_bounds = dataset.data_bounds.y_bounds
(xmin, xmax), y_bounds = dataset.data_bounds
ymin, ymax = y_bounds
ymid = ymax - y_bounds.range / 2

y_offset = y_bounds.range / 10
ymin += y_offset
ymax -= y_offset

xmin, xmax = dataset.data_bounds.x_bounds

poly = np.polynomial.Polynomial.fit([ymin, ymid, ymax], [xmax, xmin, xmax], 2)
poly = np.polynomial.Polynomial.fit(
[ymin, y_bounds.center, ymax], [xmax, xmin, xmax], 2
)

super().__init__(*np.stack(poly.linspace()[::-1], axis=1))

Expand Down Expand Up @@ -143,16 +140,15 @@ class UpParabola(PointCollection):
name = 'up_parab'

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.data_bounds.x_bounds
x_bounds, (ymin, ymax) = dataset.data_bounds
xmin, xmax = x_bounds
xmid = xmax - x_bounds.range / 2

x_offset = x_bounds.range / 10
xmin += x_offset
xmax -= x_offset

ymin, ymax = dataset.data_bounds.y_bounds

poly = np.polynomial.Polynomial.fit([xmin, xmid, xmax], [ymax, ymin, ymax], 2)
poly = np.polynomial.Polynomial.fit(
[xmin, x_bounds.center, xmax], [ymax, ymin, ymax], 2
)

super().__init__(*np.stack(poly.linspace(), axis=1))
4 changes: 2 additions & 2 deletions src/data_morph/shapes/points/spade.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class Spade(PointCollection):
"""

def __init__(self, dataset: Dataset) -> None:
x_bounds = dataset.data_bounds.x_bounds
_, xmax = dataset.data_bounds.x_bounds
x_shift, y_shift = dataset.data_bounds.center

# upside-down heart
heart_points = self._get_inverted_heart(dataset, y_shift)

# base of the spade
base_x, base_y = self._get_base(x_bounds[1], x_shift, y_shift)
base_x, base_y = self._get_base(xmax, x_shift, y_shift)

# combine all points
x = np.concatenate((heart_points[:, 0], base_x), axis=0)
Expand Down