Skip to content

Commit

Permalink
FIX: Adding issubset function for Dependent Coordinates -- it was mis…
Browse files Browse the repository at this point in the history
…sing and breaks the Sentinel datalib (and possibly others)
  • Loading branch information
mpu-creare committed Jul 8, 2020
1 parent 7e9268f commit 8dc06f4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion podpac/core/coordinates/coordinates1d.py
Expand Up @@ -412,4 +412,4 @@ def issubset(self, other):
elif other_coordinates[0].dtype < my_coordinates[0].dtype:
other_coordinates = other_coordinates.astype(my_coordinates.dtype)

return set(my_coordinates).issubset(other_coordinates)
return set(my_coordinates).issubset(other_coordinates.ravel())
68 changes: 68 additions & 0 deletions podpac/core/coordinates/dependent_coordinates.py
Expand Up @@ -8,6 +8,7 @@
import lazy_import
from six import string_types
import numbers
import logging

from podpac.core.settings import settings
from podpac.core.utils import ArrayTrait, TupleTrait
Expand All @@ -19,6 +20,8 @@
from podpac.core.coordinates.stacked_coordinates import StackedCoordinates
from podpac.core.coordinates.cfunctions import clinspace

_logger = logging.getLogger(__file__)


class DependentCoordinates(BaseCoordinates):
"""
Expand Down Expand Up @@ -492,6 +495,39 @@ def transpose(self, *dims, **kwargs):
properties["dims"] = dims
return DependentCoordinates(coordinates, **properties)

def issubset(self, other):
""" Report whether other coordinates contains these coordinates.
Arguments
---------
other : Coordinates, StackedCoordinates
Other coordinates to check
Returns
-------
issubset : bool
True if these coordinates are a subset of the other coordinates.
"""

# Check at least that the individual dims are there
if not all([self[dim].issubset(other[dim]) for dim in self.dims]):
return False

# NOTE: From this point forward, it gets expensive. We're essentially doing NN interpolation here. Would be nice
# to reuse these results...

# Check that the pairs line up as well.
# make pair sets
mine = []
theirs = []
for dim in self.dims:
mine.append(self[dim].coordinates.ravel())
theirs.append(other[dim].coordinates.ravel())
mine = set([tuple(a) for a in np.stack(mine, axis=1)])
theirs = set([tuple(a) for a in np.stack(theirs, axis=1)])
# Compare
return mine.issubset(theirs)

# ------------------------------------------------------------------------------------------------------------------
# Debug
# ------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -586,3 +622,35 @@ def intersect(self, other, outer=False, return_indices=False):
def select(self, bounds, outer=False, return_indices=False):
""" restricted """
raise RuntimeError("ArrayCoordinatesNd select is unavailable.")

def issubset(self, other):
""" Report whether other coordinates contains these coordinates.
Arguments
---------
other : Coordinates, StackedCoordinates
Other coordinates to check
Returns
-------
issubset : bool
True if these coordinates are a subset of the other coordinates.
"""
# short-cuts that don't require checking coordinates
if self.size == 0:
return True

if other.size == 0:
return False

if self.dtype != other.dtype:
return False

if self.bounds[0] < other.bounds[0] or self.bounds[1] > other.bounds[1]:
return False

# check actual coordinates using built-in set method issubset
# for datetimes, convert to the higher resolution
my_coordinates = self.coordinates.ravel()
other_coordinates = other.coordinates.ravel()
return set(my_coordinates).issubset(other_coordinates)
21 changes: 21 additions & 0 deletions podpac/core/coordinates/test/test_dependent_coordinates.py
Expand Up @@ -106,6 +106,27 @@ def test_eq_coordinates(self):
assert c1 != c3
assert c1 != c4

def test_is_subset(self):
c = DependentCoordinates([LAT, LON], dims=["lat", "lon"])
c2 = DependentCoordinates([LAT[:2, :2], LON[:2, :2]], dims=["lat", "lon"])
c2p = DependentCoordinates([LAT[1:3, :2], LON[:2, :2]], dims=["lat", "lon"])

# Dependent to Dependent
assert c2.issubset(c)
assert not c.issubset(c2)
assert not c2p.issubset(c) # pairs don't match

# Dependent to stacked
cs = podpac.Coordinates([[LAT.ravel(), LON.ravel()]], [["lat", "lon"]])
assert c2.issubset(cs)

# Stacked to Dependent
css = podpac.Coordinates([[LAT[0], LON[0]]], [["lat", "lon"]])
assert css.issubset(c)

csp = podpac.Coordinates([[np.roll(LAT.ravel(), 1), LON.ravel()]], [["lat", "lon"]])
assert not c2.issubset(csp)


class TestDependentCoordinatesSerialization(object):
def test_definition(self):
Expand Down

0 comments on commit 8dc06f4

Please sign in to comment.