Skip to content

Commit

Permalink
remove duplicates in Select shape
Browse files Browse the repository at this point in the history
  • Loading branch information
mathleur committed Nov 17, 2024
1 parent 290869b commit 4cc5555
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math
from copy import deepcopy

from ....utility.combinatorics import unique
from ....utility.list_tools import unique
from ..datacube_transformations import DatacubeAxisTransformation


Expand Down
3 changes: 2 additions & 1 deletion polytope_feature/engine/hullslicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from ..datacube.datacube_axis import UnsliceableDatacubeAxis
from ..datacube.tensor_index_tree import TensorIndexTree
from ..shapes import ConvexPolytope
from ..utility.combinatorics import argmax, argmin, group, tensor_product, unique
from ..utility.combinatorics import group, tensor_product
from ..utility.exceptions import UnsliceableShapeError
from ..utility.geometry import lerp
from ..utility.list_tools import argmax, argmin, unique
from .engine import Engine


Expand Down
6 changes: 4 additions & 2 deletions polytope_feature/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import tripy

from .utility.list_tools import unique

"""
Shapes used for the constructive geometry API of Polytope
"""
Expand Down Expand Up @@ -60,11 +62,11 @@ def polytope(self):

# This is the only shape which can slice on axes without a discretizer or interpolator
class Select(Shape):
"""Matches several discrete value"""
"""Matches several discrete values"""

def __init__(self, axis, values, method=None):
self.axis = axis
self.values = values
self.values = unique(values)
self.method = method

def axes(self):
Expand Down
16 changes: 0 additions & 16 deletions polytope_feature/utility/combinatorics.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,3 @@ def validate_axes(actual_axes, test_axes):
raise AxisNotFoundError(ax)

return True


def unique(points):
points.sort()
points = [k for k, _ in itertools.groupby(points)]
return points


def argmin(points):
amin = min(range(len(points)), key=points.__getitem__)
return amin


def argmax(points):
amax = max(range(len(points)), key=points.__getitem__)
return amax
19 changes: 19 additions & 0 deletions polytope_feature/utility/list_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import itertools


def bisect_left_cmp(arr, val, cmp):
left = -1
r = len(arr)
Expand All @@ -20,3 +23,19 @@ def bisect_right_cmp(arr, val, cmp):
else:
r = e
return r


def unique(points):
points.sort()
points = [k for k, _ in itertools.groupby(points)]
return points


def argmin(points):
amin = min(range(len(points)), key=points.__getitem__)
return amin


def argmax(points):
amax = max(range(len(points)), key=points.__getitem__)
return amax
8 changes: 8 additions & 0 deletions tests/test_slicing_xarray_3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,11 @@ def test_intersection_point_disk_polygon(self):
result = self.API.retrieve(request)
paths = [r.flatten().values() for r in result.leaves]
assert ((pd.Timestamp("2000-01-01 00:00:00"),), (3,), (1,)) in paths

def test_duplicate_values_select(self):
request = Request(Select("step", [3, 3]), Select("level", [1]), Select("date", ["2000-01-01"]))
result = self.API.retrieve(request)
result.pprint()
assert len(result.leaves) == 1
path = result.leaves[0].flatten()["step"]
assert len(path) == 1

0 comments on commit 4cc5555

Please sign in to comment.