From 14d253bab72d166085ce6f84450d124ff78c825f Mon Sep 17 00:00:00 2001 From: mathleur Date: Thu, 22 Aug 2024 15:31:54 +0200 Subject: [PATCH 1/5] add segment and shapepath shapes --- polytope/shapes.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/polytope/shapes.py b/polytope/shapes.py index c3a45a6a7..cacc58d63 100644 --- a/polytope/shapes.py +++ b/polytope/shapes.py @@ -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() + # Check that the shapes are not in a union + assert not shape1.is_in_union + assert not shape2.is_in_union + + self.polytopes = [] + + for polytope1 in shape1.polytope(): + # check that we do not have special "augmented" shapes like Point + assert polytope1.axes() == shape1.axes() + for polytope2 in shape2.polytope(): + assert polytope2.axes() == shape2.axes() + 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""" From 2a35011a39ed4c1e6be84b1bec204bc3a6818814 Mon Sep 17 00:00:00 2001 From: mathleur Date: Thu, 22 Aug 2024 16:40:37 +0200 Subject: [PATCH 2/5] add tests --- tests/test_slicing_xarray_3D.py | 53 ++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tests/test_slicing_xarray_3D.py b/tests/test_slicing_xarray_3D.py index cbffeccae..a1aa0ded2 100644 --- a/tests/test_slicing_xarray_3D.py +++ b/tests/test_slicing_xarray_3D.py @@ -16,6 +16,8 @@ PathSegment, Polygon, Select, + Segment, + ShapePath, Span, Union, ) @@ -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 @@ -91,6 +93,55 @@ 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) From 92ebee0deddeff17a2ab24cb091d11a4d202e16e Mon Sep 17 00:00:00 2001 From: mathleur Date: Thu, 22 Aug 2024 16:44:29 +0200 Subject: [PATCH 3/5] isort --- tests/test_slicing_xarray_3D.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_slicing_xarray_3D.py b/tests/test_slicing_xarray_3D.py index a1aa0ded2..ec0198061 100644 --- a/tests/test_slicing_xarray_3D.py +++ b/tests/test_slicing_xarray_3D.py @@ -15,8 +15,8 @@ Disk, PathSegment, Polygon, - Select, Segment, + Select, ShapePath, Span, Union, From 9166fda878ce915d7c119f261a790b3f56aa124b Mon Sep 17 00:00:00 2001 From: mathleur Date: Thu, 22 Aug 2024 16:46:06 +0200 Subject: [PATCH 4/5] add shapes and black --- polytope/shapes.py | 6 +++--- tests/test_slicing_xarray_3D.py | 20 +++++--------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/polytope/shapes.py b/polytope/shapes.py index cacc58d63..af1b4f2cf 100644 --- a/polytope/shapes.py +++ b/polytope/shapes.py @@ -351,17 +351,17 @@ def __init__(self, axes, shape1: Shape, shape2: Shape): assert shape1.axes() == self.axes() assert shape2.axes() == self.axes() - # Check that the shapes are not in a union - assert not shape1.is_in_union - assert not shape2.is_in_union 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) diff --git a/tests/test_slicing_xarray_3D.py b/tests/test_slicing_xarray_3D.py index ec0198061..3757732e9 100644 --- a/tests/test_slicing_xarray_3D.py +++ b/tests/test_slicing_xarray_3D.py @@ -97,27 +97,21 @@ 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) - ) + 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) - ) + 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) - ) + request = Request(Select("date", ["2000-01-03"]), Segment(["level", "step"], box1, box2)) result = self.API.retrieve(request) result.pprint() assert len(result.leaves) == 40 @@ -126,18 +120,14 @@ 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) - ) + 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) - ) + 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 From dc2d2ffbaa031d62ef5e0bb9867d9e069691a7ac Mon Sep 17 00:00:00 2001 From: mathleur Date: Thu, 22 Aug 2024 16:47:13 +0200 Subject: [PATCH 5/5] black --- polytope/options.py | 1 - polytope/shapes.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/polytope/options.py b/polytope/options.py index 78c7a20da..6baf8bc73 100644 --- a/polytope/options.py +++ b/polytope/options.py @@ -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() diff --git a/polytope/shapes.py b/polytope/shapes.py index af1b4f2cf..095df57d3 100644 --- a/polytope/shapes.py +++ b/polytope/shapes.py @@ -394,7 +394,7 @@ def __init__(self, axes, *shapes): path_segments = [] for i in range(0, len(shapes) - 1): - new_segment = Segment(axes, shapes[i], shapes[i+1]) + new_segment = Segment(axes, shapes[i], shapes[i + 1]) path_segments.append(new_segment) self.union = Union(self.axes(), *path_segments)