Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
use input 'label' if specified #7
Browse files Browse the repository at this point in the history
  • Loading branch information
lforesta committed Aug 19, 2020
1 parent 1045ed8 commit 6e36722
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/openeo_processes/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from openeo_processes.errors import ArrayElementNotAvailable
from openeo_processes.errors import ArrayElementParameterMissing
from openeo_processes.errors import ArrayElementParameterConflict
from openeo_processes.errors import GenericError

########################################################################################################################
# Array Contains Process
Expand Down Expand Up @@ -109,7 +110,7 @@ def exec_num():
pass

@staticmethod
def exec_np(data, index=None, label=None, dimension=0, return_nodata=False):
def exec_np(data, index=None, label=None, dimension=0, return_nodata=False, labels=None):
"""
Returns the element with the specified index or label from the array. Either the parameter `index` or `label`
must be specified, otherwise the `ArrayElementParameterMissing` exception is thrown. If both parameters are set
Expand All @@ -128,6 +129,8 @@ def exec_np(data, index=None, label=None, dimension=0, return_nodata=False):
return_nodata : bool, optional
By default this process throws an `ArrayElementNotAvailable` exception if the index or label is invalid.
If you want to return np.nan instead, set this flag to `True`.
labels : np.array, optional
The available labels.
Returns
-------
Expand All @@ -144,8 +147,10 @@ def exec_np(data, index=None, label=None, dimension=0, return_nodata=False):
Only `index` or `labels` allowed to be set.
"""
ArrayElement._check_input(index, label)

ArrayElement._check_input(index, label, labels)
if label:
# Convert label to index, using labels
index = labels.tolist().index(label)
if index >= data.shape[dimension]:
if not return_nodata:
raise ArrayElementNotAvailable()
Expand All @@ -166,7 +171,7 @@ def exec_da():
pass

@staticmethod
def _check_input(index, label):
def _check_input(index, label, labels):
"""
Checks if `index` and `label` are given correctly.
Expand All @@ -179,6 +184,8 @@ def _check_input(index, label):
The zero-based index of the element to retrieve (default is 0).
label : int or str, optional
The label of the element to retrieve.
labels : np.array, optional
The available labels.
Raises
------
Expand All @@ -193,6 +200,10 @@ def _check_input(index, label):

if index is None and label is None:
raise ArrayElementParameterMissing()

if label and labels is None:
msg = "Parameter 'labels' is needed when specifying input parameter 'label'."
raise GenericError(msg)


########################################################################################################################
Expand Down
6 changes: 6 additions & 0 deletions src/openeo_processes/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ def __str__(self):
return self.message


class GenericError(Exception):
def __init__(self, msg):
self.message = msg

def __str__(self):
return self.message
1 change: 1 addition & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_array_contains(self):

def test_array_element(self):
""" Tests `array_element` function. """
assert oeop.array_element([9, 8], label="B", labels=np.array(["A", "B"])) == 8
assert oeop.array_element([9, 8, 7, 6, 5], index=2) == 7
assert oeop.array_element(["A", "B", "C"], index=0) == "A"
assert np.isnan(oeop.array_element([], index=0, return_nodata=True))
Expand Down

0 comments on commit 6e36722

Please sign in to comment.