Skip to content

Commit

Permalink
Validate input numpy (#105)
Browse files Browse the repository at this point in the history
* Add numpy class of variable to conditional in spatial extent validation

* numpy scalars now available for the case where the spatial extent consists of a polygon and it is given by a single list

* I added some modification in the case where spatial_extent consists on a list of tuples in order to accept np.scalars as valid inptus

* I modified line 117 in order that now _spat_extent is a list of floats, just as all the other cases. Without this line, sometimes we will have cases of numpy arrays and it is easier if all the _spatial_extent are list of floats

* I added test_numpyfloatlist_bbox and test_numpfloatarray_bbox
  • Loading branch information
facusapienza21 authored Aug 31, 2020
1 parent 99460e6 commit cb9fa86
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ order by last name) and are considered "The icepyx Developers":
* `Zheng Liu <https://github.com/liuzheng-arctic>`_ - University of Washington
* `Joachim Meyer <https://github.com/jomey>`_ - University of Utah
* `Fernando Perez <https://github.com/fperez>`_ - University of California, Berkeley
* `Facundo Sapienza <https://github.com/facusapienza21>`_ - University of California, Berkeley
* `Jessica Scheick <https://github.com/jessicas11>`_ - Unaffiliated (ORCID: `0000-0002-3421-4459 <https://www.orcid.org/0000-0002-3421-4459>`_)
* `David Shean <https://github.com/dshean>`_ - University of Washington
* `Ben Smith <https://github.com/smithb>`_ - University of Washington
Expand Down
20 changes: 14 additions & 6 deletions icepyx/core/validate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ def spatial(spatial_extent):
"""
Validate the input spatial extent and return the needed parameters to the query object.
"""
if isinstance(spatial_extent, list):

scalar_types = (np.int, np.float, np.int64)

if isinstance(spatial_extent, (list, np.ndarray)):

# bounding box
if len(spatial_extent) == 4 and all(
type(i) in [int, float] for i in spatial_extent
isinstance(i, scalar_types) for i in spatial_extent
):
assert -90 <= spatial_extent[1] <= 90, "Invalid latitude value"
assert -90 <= spatial_extent[3] <= 90, "Invalid latitude value"
Expand All @@ -110,11 +114,15 @@ def spatial(spatial_extent):
assert (
spatial_extent[1] <= spatial_extent[3]
), "Invalid bounding box latitudes"
_spat_extent = spatial_extent
_spat_extent = [float(x) for x in spatial_extent]
extent_type = "bounding_box"

# user-entered polygon as list of lon, lat coordinate pairs
elif all(type(i) in [list, tuple] for i in spatial_extent):
elif all(type(i) in [list, tuple, np.ndarray] for i in spatial_extent) and all(
all( isinstance(i[j], scalar_types) for j in range(len(i)) ) for i in spatial_extent
):
if any( len(i) != 2 for i in spatial_extent):
raise ValueError("Each element in spatial_extent should be a list or tuple of length 2")
assert (
len(spatial_extent) >= 4
), "Your spatial extent polygon has too few vertices"
Expand All @@ -140,7 +148,7 @@ def spatial(spatial_extent):
# warnings.warn("this type of input is not yet well handled and you may not be able to find data")

# user-entered polygon as a single list of lon and lat coordinates
elif all(type(i) in [int, float] for i in spatial_extent):
elif all( isinstance(i, scalar_types) for i in spatial_extent):
assert (
len(spatial_extent) >= 8
), "Your spatial extent polygon has too few vertices"
Expand All @@ -162,7 +170,7 @@ def spatial(spatial_extent):
# _spat_extent = polygon

else:
raise ValueError("Your spatial extent does not meet minimum input criteria")
raise ValueError("Your spatial extent does not meet minimum input criteria or the input format is not correct")

# DevGoal: write a test for this?
# make sure there is nothing set to _geom_filepath since its existence determines later steps
Expand Down
14 changes: 14 additions & 0 deletions icepyx/tests/test_validate_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,21 @@ def test_floatlist_bbox():
expected = ["bounding_box", [-64.2, 66.2, -55.5, 72.5], None]
for i in range(len(expected)):
assert obs[i] == expected[i]


def test_numpyfloatarray_bbox():
obs = val.spatial(np.array([-64.2, 66.2, -55.5, 72.5]))
expected = ["bounding_box", [-64.2, 66.2, -55.5, 72.5], None]
for i in range(len(expected)):
assert obs[i] == expected[i]


def test_numpyfloatlist_bbox():
obs = val.spatial(list(np.array([-64.2, 66.2, -55.5, 72.5])))
expected = ["bounding_box", [-64.2, 66.2, -55.5, 72.5], None]
for i in range(len(expected)):
assert obs[i] == expected[i]


def test_list_latlon_pairs():
out = val.spatial([[-55, 68], [-55, 71], [-48, 71], [-48, 68], [-55, 68]])
Expand Down

0 comments on commit cb9fa86

Please sign in to comment.