-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allowed resultdata to receive also lists
- Loading branch information
Showing
5 changed files
with
100 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import List, Union | ||
|
||
import numpy as np | ||
|
||
|
||
def check_array(array: Union[np.ndarray, List], allow_none=False) -> np.ndarray: | ||
"""Check if the input is a numpy array. If not, convert it to a numpy array. | ||
Parameters | ||
---------- | ||
array : Union[np.ndarray, List] | ||
The input array to check. | ||
allow_none : bool, optional | ||
Whether to allow the input to be None, by default False | ||
Returns | ||
------- | ||
np.ndarray | ||
The input array as a numpy array. | ||
""" | ||
|
||
if array is None: | ||
if allow_none: | ||
return None | ||
else: | ||
raise TypeError("None is not a valid array") | ||
if not isinstance(array, np.ndarray): | ||
if isinstance(array, list): | ||
return np.array(array) | ||
else: | ||
raise TypeError(f"{array} must be a numpy array") | ||
return array |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from remayn.utils.array import check_array | ||
|
||
|
||
def test_check_array(): | ||
array = [1, 2, 3] | ||
assert check_array(array).tolist() == array | ||
assert (check_array(np.array(array)) == np.array(array)).all() | ||
|
||
array = None | ||
assert check_array(array, allow_none=True) is None | ||
with pytest.raises(TypeError): | ||
check_array(array) | ||
|
||
array = "string" | ||
with pytest.raises(TypeError): | ||
check_array(array) |