Skip to content

Commit

Permalink
Merge pull request #24 from rodrigo-arenas/0.6.Xdev
Browse files Browse the repository at this point in the history
Parameterized tests
  • Loading branch information
rodrigo-arenas committed Jun 22, 2021
2 parents 0be029f + e4ac735 commit 2114508
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 226 deletions.
2 changes: 1 addition & 1 deletion sklearn_genetic/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.0"
__version__ = "0.6.0dev0"
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split

from .. import GASearchCV
from ..space import Integer, Continuous
from ..callbacks import (
from ... import GASearchCV
from ...space import Integer, Continuous
from .. import (
ThresholdStopping,
ConsecutiveStopping,
DeltaThreshold,
LogbookSaver,
)
from ..callbacks.validations import check_stats, check_callback
from ..callbacks.base import BaseCallback
from ..validations import check_stats, check_callback
from ..base import BaseCallback

data = load_digits()
label_names = data["target_names"]
Expand Down
8 changes: 6 additions & 2 deletions sklearn_genetic/space/base.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
class BaseDimension(object):
from abc import ABC, abstractmethod


class BaseDimension(ABC):
"""
Base class for the space definition of data types
"""

@abstractmethod
def sample(self):
"""
Sample a random value from the assigned distribution
"""

raise NotImplementedError(
"The sample method must be defined according each data type handler"
)
) # pragma: no cover
182 changes: 93 additions & 89 deletions sklearn_genetic/space/tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,94 @@
from ..base import BaseDimension


def test_sample_variables():
@pytest.mark.parametrize(
"data_object, parameters",
[
(Continuous, {"lower": 0.01, "upper": 0.5, "distribution": "log-uniform"}),
(Continuous, {"lower": 0.0, "upper": 0.5, "distribution": "uniform"}),
(Integer, {"lower": 5, "upper": 20, "distribution": "uniform"}),
],
)
def test_sample_variables(data_object, parameters):
my_categorical = Categorical(
choices=["car", "byc", "house"], priors=[0.2, 0.1, 0.7]
)
for _ in range(20):
assert my_categorical.sample() in ["car", "byc", "house"]

my_continuous = Continuous(lower=0.01, upper=0.5, distribution="log-uniform")
my_variable = data_object(**parameters)
for _ in range(100):
assert my_continuous.sample() <= 0.5
assert my_continuous.sample() >= 0
assert my_variable.sample() <= parameters["upper"]
assert my_variable.sample() >= parameters["lower"]

my_continuous = Continuous(lower=0.0, upper=0.5, distribution="uniform")
for _ in range(100):
assert my_continuous.sample() <= 0.5
assert my_continuous.sample() >= 0

my_integer = Integer(lower=5, upper=20, distribution="uniform")
for _ in range(100):
assert my_integer.sample() >= 5
assert my_integer.sample() <= 20


def test_wrong_boundaries():
with pytest.raises(Exception) as excinfo:
my_continuous = Continuous(lower=10, upper=0.5)

assert (
str(excinfo.value) == "The upper bound can not be smaller that the lower bound"
)

with pytest.raises(Exception) as excinfo:
my_integer = Integer(lower=10, upper=2)

assert (
str(excinfo.value) == "The upper bound can not be smaller that the lower bound"
)


def test_wrong_distributions():
with pytest.raises(Exception) as excinfo:
my_continuous = Continuous(lower=2, upper=10, distribution="normal")
assert (
str(excinfo.value)
== "distribution must be one of ['uniform', 'log-uniform'], got normal instead"
)

with pytest.raises(Exception) as excinfo:
my_categorical = Categorical([True, False], distribution="sample")
assert (
str(excinfo.value)
== "distribution must be one of ['choice'], got sample instead"
)

with pytest.raises(Exception) as excinfo:
my_integer = Integer(lower=2, upper=10, distribution="log-uniform")
assert (
str(excinfo.value)
== "distribution must be one of ['uniform'], got log-uniform instead"
)


def test_categorical_bad_parameters():

@pytest.mark.parametrize(
"data_object, parameters, message",
[
(
Continuous,
{"lower": 10, "upper": 0.5},
"The upper bound can not be smaller that the lower bound",
),
(
Integer,
{"lower": 10, "upper": 2},
"The upper bound can not be smaller that the lower bound",
),
],
)
def test_wrong_boundaries(data_object, parameters, message):
with pytest.raises(Exception) as excinfo:
my_categorical = Categorical(priors=[0.1, 0.9])
assert str(excinfo.value) == "choices must be a non empty list"
data_object(**parameters)
assert str(excinfo.value) == message

with pytest.raises(Exception) as excinfo:
my_categorical = Categorical(choices=[True, False], priors=[0.1, 0.8])
assert (
str(excinfo.value)
== "The sum of the probabilities in the priors must be one, got 0.9 instead"
)

@pytest.mark.parametrize(
"data_object, parameters, message",
[
(
Continuous,
{"lower": 10, "upper": 50, "distribution": "normal"},
"distribution must be one of ['uniform', 'log-uniform'], got normal instead",
),
(
Categorical,
{"choices": [True, False], "distribution": "sample"},
"distribution must be one of ['choice'], got sample instead",
),
(
Integer,
{"lower": 2, "upper": 10, "distribution": "log-uniform"},
"distribution must be one of ['uniform'], got log-uniform instead",
),
],
)
def test_wrong_distributions(data_object, parameters, message):
with pytest.raises(Exception) as excinfo:
data_object(**parameters)
assert str(excinfo.value) == message


@pytest.mark.parametrize(
"data_object, parameters, message",
[
(Categorical, {"priors": [0.1, 0.9]}, "choices must be a non empty list"),
(
Categorical,
{"choices": [True, False], "priors": [0.1, 0.8]},
"The sum of the probabilities in the priors must be one, got 0.9 instead",
),
(
Categorical,
{"choices": [True], "priors": [0.1, 0.9]},
"priors and choices must have same size",
),
],
)
def test_categorical_bad_parameters(data_object, parameters, message):
with pytest.raises(Exception) as excinfo:
my_categorical = Categorical([True], priors=[0.1, 0.9])
assert str(excinfo.value) == "priors and choices must have same size"
data_object(**parameters)
assert str(excinfo.value) == message


def test_check_space_fail():
Expand All @@ -106,30 +116,27 @@ def test_check_space_fail():
)


def test_bad_data_types():

with pytest.raises(Exception) as excinfo:
Categorical((True, False))
assert str(excinfo.value) == "choices must be a non empty list"

@pytest.mark.parametrize(
"data_object, parameters, message",
[
(Categorical, (True, False), "choices must be a non empty list"),
(Integer, (5.4, 10), "lower bound must be an integer"),
(Integer, (5, 10.4), "upper bound must be an integer"),
(Continuous, ([1], 10), "lower bound must be an integer or float"),
(Continuous, (5, [10.4]), "upper bound must be an integer or float"),
],
)
def test_bad_data_types(data_object, parameters, message):
with pytest.raises(Exception) as excinfo:
Integer(5.4, 10)
assert str(excinfo.value) == "lower bound must be an integer"

with pytest.raises(Exception) as excinfo:
Integer(5, 10.4)
assert str(excinfo.value) == "upper bound must be an integer"

with pytest.raises(Exception) as excinfo:
Continuous([1], 10)
assert str(excinfo.value) == "lower bound must be an integer or float"

with pytest.raises(Exception) as excinfo:
Continuous(5, [10.4])
assert str(excinfo.value) == "upper bound must be an integer or float"
data_object(*parameters)
assert str(excinfo.value) == message


def test_wrong_dimension():
possible_messages = [
"Can't instantiate abstract class FakeDimension with abstract methods sample",
"Can't instantiate abstract class FakeDimension with abstract method sample",
]
with pytest.raises(Exception) as excinfo:

class FakeDimension(BaseDimension):
Expand All @@ -138,7 +145,4 @@ def __init__(self):

FakeDimension().sample()

assert (
str(excinfo.value)
== "The sample method must be defined according each data type handler"
)
assert any([str(excinfo.value) == i for i in possible_messages])

0 comments on commit 2114508

Please sign in to comment.