Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/different path shape #199

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion polytope/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Config(ConfigModel):
class PolytopeOptions(ABC):
@staticmethod
def get_polytope_options(options):

parser = argparse.ArgumentParser(allow_abbrev=False)
conflator = Conflator(app_name="polytope", model=Config, cli=False, argparser=parser, **options)
config_options = conflator.load()
Expand Down
69 changes: 69 additions & 0 deletions polytope/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,75 @@ def __repr__(self):
between the points {self._points}"


class Segment(Shape):
"""N-D polytope defined as the space between two shapes"""

def __init__(self, axes, shape1: Shape, shape2: Shape):
self._axes = axes
self._shape1 = shape1
self._shape2 = shape2

assert shape1.axes() == self.axes()
assert shape2.axes() == self.axes()

self.polytopes = []

for polytope1 in shape1.polytope():
# check that we do not have special "augmented" shapes like Point
assert polytope1.axes() == shape1.axes()
# Check that the shapes are not in a union
assert not polytope1.is_in_union
for polytope2 in shape2.polytope():
assert polytope2.axes() == shape2.axes()
assert not polytope2.is_in_union
assert polytope1.axes() == polytope2.axes()
points = polytope1.points
points.extend(polytope2.points)
poly = ConvexPolytope(polytope1.axes(), points)
poly.add_to_union()
self.polytopes.append(poly)

def axes(self):
return self._axes

def polytope(self):
return self.polytopes

def __repr__(self):
return f"Segment in {self._axes} obtained between a {self._shape1.__repr__()} \
and a {self._shape2.__repr__()} "


class ShapePath(Shape):
# TODO
"""N-D polytope defined by the space between multiple shapes"""

def __init__(self, axes, *shapes):
self._axes = axes
self._shapes = shapes

for shape in shapes:
assert shape.axes() == self.axes()

path_segments = []

for i in range(0, len(shapes) - 1):
new_segment = Segment(axes, shapes[i], shapes[i + 1])
path_segments.append(new_segment)

self.union = Union(self.axes(), *path_segments)

def axes(self):
return self._axes

def polytope(self):
return self.union.polytope()

def __repr__(self):
return f"Path in {self._axes} obtained by sweeping a {self._shape.__repr__()} \
between the points {self._points}"


class Union(Shape):
"""N-D union of two shapes with the same axes"""

Expand Down
43 changes: 42 additions & 1 deletion tests/test_slicing_xarray_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
Disk,
PathSegment,
Polygon,
Segment,
Select,
ShapePath,
Span,
Union,
)
Expand Down Expand Up @@ -64,7 +66,7 @@ def test_point(self):
result = self.API.retrieve(request)
assert len(result.leaves) == 1

def test_segment(self):
def test_span(self):
request = Request(Span("level", 10, 11), Select("date", ["2000-01-01"]), Select("step", [9]))
result = self.API.retrieve(request)
assert len(result.leaves) == 1
Expand All @@ -91,6 +93,45 @@ def test_mix_existing_nonexisting_data(self):
result = self.API.retrieve(request)
assert len(result.leaves) == 1

def test_segment_shape(self):
# TODO
box1 = Box(["level", "step"], [1, 0], [2, 3])
box2 = Box(["level", "step"], [1, 9], [3, 15])
request = Request(Select("date", ["2000-01-03"]), Segment(["level", "step"], box1, box2))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 15

box1 = Box(["level", "step"], [1, 0], [2, 3])
box2 = Box(["level", "step"], [1, 12], [5, 15])
request = Request(Select("date", ["2000-01-03"]), Segment(["level", "step"], box1, box2))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 21

box1 = Box(["level", "step"], [1, 0], [2, 3])
box2 = Box(["level", "step"], [1, 12], [10, 15])
request = Request(Select("date", ["2000-01-03"]), Segment(["level", "step"], box1, box2))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 40

def test_shape_path_shape(self):
# TODO
box1 = Box(["level", "step"], [1, 0], [2, 3])
box2 = Box(["level", "step"], [1, 9], [3, 15])
request = Request(Select("date", ["2000-01-03"]), ShapePath(["level", "step"], box1, box2))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 15

box2 = Box(["level", "step"], [1, 6], [5, 12])
box3 = Box(["level", "step"], [1, 12], [7, 15])
request = Request(Select("date", ["2000-01-03"]), ShapePath(["level", "step"], box1, box2, box3))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 30

def test_disk(self):
request = Request(Disk(["level", "step"], [6, 6], [3, 3]), Select("date", ["2000-01-01"]))
result = self.API.retrieve(request)
Expand Down
Loading