<commit_before>__author__ = '<NAME>'

from apsis.models.parameter_definition import *
from nose.tools import assert_equal, assert_raises, assert_items_equal, \
    assert_true, assert_false, assert_almost_equal, assert_less_equal, \
    assert_greater_equal

class TestParameterDefinitions(object):

    def test_nominal_param_def(self):
        with assert_raises(ValueError):
            _ = NominalParamDef(False)
        with assert_raises(ValueError):
            _ = NominalParamDef([])
        test_values = ["A", "B", "C"]
        pd = NominalParamDef(test_values)
        assert_items_equal(pd.values, test_values)
        x = random.choice(test_values)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_true(pd.is_in_parameter_domain("A"))
        assert_false(pd.is_in_parameter_domain(1))

    def test_ordinal_param_def(self):
        with assert_raises(ValueError):
            _ = OrdinalParamDef(False)
        with assert_raises(ValueError):
            _ = OrdinalParamDef([])



        test_values = ["A", "B", "C"]
        pd = OrdinalParamDef(test_values)

        with assert_raises(ValueError):
            pd.compare_values("A", "D")
        with assert_raises(ValueError):
            pd.compare_values("D", "A")
        with assert_raises(ValueError):
            pd.distance("A", "D")
        with assert_raises(ValueError):
            pd.distance("D", "A")
        assert_items_equal(pd.values, test_values)
        x = random.choice(test_values)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_true(pd.is_in_parameter_domain("A"))
        assert_false(pd.is_in_parameter_domain(1))
        assert_equal(pd.distance("A", "B"), 1./3)
        assert_equal(pd.distance("A", "C"), 2./3)
        assert_equal(pd.compare_values("A", "B"), -1)
        assert_equal(pd.compare_values("A", "A"), 0)

    def test_min_max_def(self):
        with assert_raises(ValueError):
            _ = MinMaxNumericParamDef("Bla", 1)
        with assert_raises(ValueError):
            _ = MinMaxNumericParamDef([], 2)
        test = MinMaxNumericParamDef(-1, 10)

        x = random.uniform(-1, 10)
        assert_equal(x, test.warp_out(test.warp_in(x)))

        assert_true(test.is_in_parameter_domain(0.5))
        assert_false(test.is_in_parameter_domain(11))
        assert_equal(test.distance(0, 1), 1./11)
        assert_equal(test.distance(-1, 10), 1)


    def test_numeric_def(self):
        f_in = lambda x: float(x)/10
        f_out = lambda x: float(x)*10
        pd = NumericParamDef(f_in, f_out)

        assert_true(pd.is_in_parameter_domain(0.5))
        assert_false(pd.is_in_parameter_domain(11))
        assert_equal(pd.distance(0, 1), 0.1)
        assert_equal(pd.distance(0, 10), 1)
        with assert_raises(ValueError):
            pd.distance("A", 1)
        with assert_raises(ValueError):
            pd.distance(0, "B")

        x = random.uniform(0, 10)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_equal(pd.compare_values(0, 10), -1)
        assert_equal(pd.compare_values(1, 0), 1)<commit_msg>Added test for non-inclusive minmax parameter definition.<commit_after>__author__ = '<NAME>'

from apsis.models.parameter_definition import *
from nose.tools import assert_equal, assert_raises, assert_items_equal, \
    assert_true, assert_false, assert_almost_equal, assert_less_equal, \
    assert_greater_equal

class TestParameterDefinitions(object):

    def test_nominal_param_def(self):
        with assert_raises(ValueError):
            _ = NominalParamDef(False)
        with assert_raises(ValueError):
            _ = NominalParamDef([])
        test_values = ["A", "B", "C"]
        pd = NominalParamDef(test_values)
        assert_items_equal(pd.values, test_values)
        x = random.choice(test_values)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_true(pd.is_in_parameter_domain("A"))
        assert_false(pd.is_in_parameter_domain(1))

    def test_ordinal_param_def(self):
        with assert_raises(ValueError):
            _ = OrdinalParamDef(False)
        with assert_raises(ValueError):
            _ = OrdinalParamDef([])



        test_values = ["A", "B", "C"]
        pd = OrdinalParamDef(test_values)

        with assert_raises(ValueError):
            pd.compare_values("A", "D")
        with assert_raises(ValueError):
            pd.compare_values("D", "A")
        with assert_raises(ValueError):
            pd.distance("A", "D")
        with assert_raises(ValueError):
            pd.distance("D", "A")
        assert_items_equal(pd.values, test_values)
        x = random.choice(test_values)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_true(pd.is_in_parameter_domain("A"))
        assert_false(pd.is_in_parameter_domain(1))
        assert_equal(pd.distance("A", "B"), 1./3)
        assert_equal(pd.distance("A", "C"), 2./3)
        assert_equal(pd.compare_values("A", "B"), -1)
        assert_equal(pd.compare_values("A", "A"), 0)

    def test_min_max_def(self):
        with assert_raises(ValueError):
            _ = MinMaxNumericParamDef("Bla", 1)
        with assert_raises(ValueError):
            _ = MinMaxNumericParamDef([], 2)
        test = MinMaxNumericParamDef(-1, 10)

        x = random.uniform(-1, 10)
        assert_almost_equal(x, test.warp_out(test.warp_in(x)))

        assert_true(test.is_in_parameter_domain(0.5))
        assert_false(test.is_in_parameter_domain(11))
        assert_almost_equal(test.distance(0, 1), 1./11)
        assert_almost_equal(test.distance(-1, 10), 1)

        # test non-inclusive
        test = MinMaxNumericParamDef(-1, 10, False, False)

        x = random.uniform(0, 9)
        assert_almost_equal(x, test.warp_out(test.warp_in(x)))

        assert_true(test.is_in_parameter_domain(0.5))
        assert_false(test.is_in_parameter_domain(-1))
        assert_false(test.is_in_parameter_domain(10))
        assert_false(test.is_in_parameter_domain(11))



    def test_numeric_def(self):
        f_in = lambda x: float(x)/10
        f_out = lambda x: float(x)*10
        pd = NumericParamDef(f_in, f_out)

        assert_true(pd.is_in_parameter_domain(0.5))
        assert_false(pd.is_in_parameter_domain(11))
        assert_equal(pd.distance(0, 1), 0.1)
        assert_equal(pd.distance(0, 10), 1)
        with assert_raises(ValueError):
            pd.distance("A", 1)
        with assert_raises(ValueError):
            pd.distance(0, "B")

        x = random.uniform(0, 10)
        assert_equal(x, pd.warp_out(pd.warp_in(x)))

        assert_equal(pd.compare_values(0, 10), -1)
        assert_equal(pd.compare_values(1, 0), 1)
{'fraction_non_alphanumeric': 0.1260327474838516, 'fraction_numerical': 0.01892744479495268, 'mean_word_length': 2.850780798149219, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4666955', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2226, 'n_words': 333}