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

Commit

Permalink
feat: update project (#6)
Browse files Browse the repository at this point in the history
* build: add matplotlib dependency

* build: add bumbag dependency

* refactor: import module instead of functions

* build: update bumbag dependency

* feat: use positional- and keyword-only parameters
  • Loading branch information
estripling committed Apr 29, 2023
1 parent 6df6c12 commit ee8f82c
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 43 deletions.
387 changes: 380 additions & 7 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.8"
numpy = "^1.24.2"
matplotlib = "^3.7.1"
bumbag = "^5.0.0"

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand Down
1 change: 1 addition & 0 deletions src/fbench/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .validation import *

del (
metadata,
function,
validation,
)
18 changes: 9 additions & 9 deletions src/fbench/function.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np

from fbench.validation import check_vector
from fbench import validation

__all__ = (
"ackley",
Expand All @@ -10,7 +10,7 @@
)


def ackley(x):
def ackley(x, /):
"""Ackley function.
.. math::
Expand Down Expand Up @@ -46,7 +46,7 @@ def ackley(x):
>>> round(ackley([1, 1]), 4)
3.6254
"""
x = check_vector(x, 1)
x = validation.check_vector(x, min_elements=1)
return float(
-20 * np.exp(-0.2 * np.sqrt((x**2).mean()))
- np.exp((np.cos(2 * np.pi * x)).sum() / len(x))
Expand All @@ -55,7 +55,7 @@ def ackley(x):
)


def rastrigin(x):
def rastrigin(x, /):
"""Rastrigin function.
.. math::
Expand Down Expand Up @@ -89,11 +89,11 @@ def rastrigin(x):
>>> round(rastrigin([4.5, 4.5]), 4)
80.5
"""
x = check_vector(x, 1)
x = validation.check_vector(x, min_elements=1)
return float(10 * len(x) + (x**2 - 10 * np.cos(2 * np.pi * x)).sum())


def rosenbrock(x):
def rosenbrock(x, /):
"""Rosenbrock function.
.. math::
Expand Down Expand Up @@ -132,11 +132,11 @@ def rosenbrock(x):
>>> round(rosenbrock([3, 3]), 4)
3604.0
"""
x = check_vector(x, 2)
x = validation.check_vector(x, min_elements=2)
return float((100 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2).sum())


def sphere(x):
def sphere(x, /):
"""Sphere function.
.. math::
Expand Down Expand Up @@ -166,5 +166,5 @@ def sphere(x):
>>> sphere([1, 1])
2.0
"""
x = check_vector(x, 1)
x = validation.check_vector(x, min_elements=1)
return float((x**2).sum())
16 changes: 8 additions & 8 deletions src/fbench/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

__all__ = ("check_vector",)

from fbench.exception import IncorrectNumberOfElements, NotAVectorError
from fbench import exception


def check_vector(x, min_number_of_elements):
def check_vector(x, /, *, min_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
min_elements : int
Specify the minimum number of elements ``x`` must have.
Returns
Expand All @@ -25,18 +25,18 @@ def check_vector(x, min_number_of_elements):
NotAVectorError
If ``x`` is not vector-like.
IncorrectNumberOfElements
If ``x`` does not satisfy the ``min_number_of_elements`` condition.
If ``x`` does not satisfy the ``min_elements`` condition.
"""
x = np.asarray(x)

if len(x.shape) != 1:
raise NotAVectorError(
raise exception.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} "
if not len(x) >= min_elements:
raise exception.IncorrectNumberOfElements(
f"number of elements must be at least {min_elements} "
f"- it has {x.shape[0]}"
)

Expand Down
26 changes: 13 additions & 13 deletions tests/test_function.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import pytest

import fbench as fb
import fbench


@pytest.mark.parametrize(
"arg, expected",
"x, expected",
[
([0], 0.0),
([0, 0], 0.0),
([1, 1], 3.6254),
([2, 2], 6.5936),
],
)
def test_ackley(arg, expected):
actual = fb.ackley(x=arg)
def test_ackley(x, expected):
actual = fbench.ackley(x)
assert round(actual, 4) == expected


@pytest.mark.parametrize(
"arg, expected",
"x, expected",
[
([0], 0.0),
([0, 0], 0.0),
Expand All @@ -31,13 +31,13 @@ def test_ackley(arg, expected):
([5.12, 5.12], 57.85),
],
)
def test_rastrigin(arg, expected):
actual = fb.rastrigin(x=arg)
def test_rastrigin(x, expected):
actual = fbench.rastrigin(x)
assert round(actual, 2) == expected


@pytest.mark.parametrize(
"arg, expected",
"x, expected",
[
([0, 0], 1.0),
([1, 1], 0.0),
Expand All @@ -51,20 +51,20 @@ def test_rastrigin(arg, expected):
([5.12, 5.12], 44514.35),
],
)
def test_rosenbrock(arg, expected):
actual = fb.rosenbrock(x=arg)
def test_rosenbrock(x, expected):
actual = fbench.rosenbrock(x)
assert round(actual, 2) == expected


@pytest.mark.parametrize(
"arg, expected",
"x, expected",
[
([0], 0),
([0, 0], 0),
([1, 1], 2),
([2, 2], 8),
],
)
def test_sphere(arg, expected):
actual = fb.sphere(x=arg)
def test_sphere(x, expected):
actual = fbench.sphere(x)
assert actual == expected
12 changes: 6 additions & 6 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import numpy.testing as npt
import pytest

from fbench import exception, validation
import fbench


def test_check_vector():
x = [1, 2, 3]
actual = validation.check_vector(x, 1)
actual = fbench.check_vector(x, min_elements=1)
npt.assert_array_equal(actual, np.array(x))

with pytest.raises(exception.NotAVectorError):
validation.check_vector([[1, 2]], 1)
with pytest.raises(fbench.exception.NotAVectorError):
fbench.check_vector([[1, 2]], min_elements=1)

with pytest.raises(exception.IncorrectNumberOfElements):
validation.check_vector([1, 2], 3)
with pytest.raises(fbench.exception.IncorrectNumberOfElements):
fbench.check_vector([1, 2], min_elements=3)

0 comments on commit ee8f82c

Please sign in to comment.