This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
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.
- Loading branch information
1 parent
046bece
commit fefa861
Showing
6 changed files
with
79 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
__version__ = version("fbench") | ||
|
||
from .core import * | ||
from .validation import * | ||
|
||
del core | ||
del core, validation |
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,10 @@ | ||
class IncorrectNumberOfElements(ValueError): | ||
"""Raise if number of elements is incorrect.""" | ||
|
||
pass | ||
|
||
|
||
class NotAVectorError(TypeError): | ||
"""Raise if object is not vector-like.""" | ||
|
||
pass |
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,43 @@ | ||
import numpy as np | ||
|
||
__all__ = ("check_vector",) | ||
|
||
from fbench.exception import IncorrectNumberOfElements, NotAVectorError | ||
|
||
|
||
def check_vector(x, min_number_of_elements): | ||
"""Validate an n-dimensional vector. | ||
Parameters | ||
---------- | ||
x : array_like | ||
Input data with :math:`n` elements that can be converted to an array. | ||
min_number_of_elements : int | ||
Specify the minimum number of elements ``x`` must have. | ||
Returns | ||
------- | ||
np.ndarray | ||
An n-dimensional vector. | ||
Raises | ||
------ | ||
NotAVectorError | ||
If ``x`` is not vector-like. | ||
IncorrectNumberOfElements | ||
If ``x`` does not satisfy the ``min_number_of_elements`` condition. | ||
""" | ||
x = np.asarray(x) | ||
|
||
if len(x.shape) != 1: | ||
raise NotAVectorError( | ||
f"input must be vector-like object - it has shape={x.shape}" | ||
) | ||
|
||
if not len(x) >= min_number_of_elements: | ||
raise IncorrectNumberOfElements( | ||
f"number of elements must be at least {min_number_of_elements} " | ||
f"- it has {x.shape[0]}" | ||
) | ||
|
||
return x |
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,17 @@ | ||
import numpy as np | ||
import numpy.testing as npt | ||
import pytest | ||
|
||
from fbench import exception, validation | ||
|
||
|
||
def test_check_vector(): | ||
x = [1, 2, 3] | ||
actual = validation.check_vector(x, 1) | ||
npt.assert_array_equal(actual, np.array(x)) | ||
|
||
with pytest.raises(exception.NotAVectorError): | ||
validation.check_vector([[1, 2]], 1) | ||
|
||
with pytest.raises(exception.IncorrectNumberOfElements): | ||
validation.check_vector([1, 2], 3) |