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

FIX: Adding issubset function for Dependent Coordinates #414

Merged
merged 5 commits into from Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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):
mpu-creare marked this conversation as resolved.
Show resolved Hide resolved
""" 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