Commit f90f8190 authored by Brice Videau's avatar Brice Videau

Added some python tests.

parent 2ce430ab
......@@ -239,9 +239,9 @@ class ccs_numeric(ct.Union):
def get_value(self, t):
if t == ccs_numeric_type.NUM_INTEGER:
return self.f
return self.i
elif t == ccs_numeric_type.NUM_FLOAT:
return self.v
return self.f
else:
raise Error(ccs_error(ccs_error.INVALID_VALUE))
......
import ctypes as ct
from . import libcconfigspace
from .base import Object, Error, ccs_error, ccs_int, ccs_float, ccs_bool, ccs_result, ccs_rng, ccs_distribution, ccs_numeric_type, ccs_numeric, CEnumeration, NUM_FLOAT, NUM_INTEGER, _ccs_get_function
from .base import Object, Error, ccs_error, ccs_int, ccs_float, ccs_bool, ccs_result, ccs_rng, ccs_distribution, ccs_numeric_type, ccs_numeric, CEnumeration, NUM_FLOAT, NUM_INTEGER, _ccs_get_function, ccs_false, ccs_true
from .interval import ccs_interval
class ccs_distribution_type(CEnumeration):
......@@ -81,6 +81,8 @@ class Distribution(Object):
self._scale_type = v.value
return self._scale_type
scale = scale_type
@property
def quantization(self):
if hasattr(self, "_quantization"):
......@@ -219,6 +221,14 @@ class NormalDistribution(Distribution):
else:
super().__init__(handle = handle, retain = retain)
@classmethod
def int(cls, mu, sigma, scale = ccs_scale_type.LINEAR, quantization = 0):
return cls(data_type = NUM_INTEGER, mu = mu, sigma = sigma, scale = scale, quantization = quantization)
@classmethod
def float(cls, mu, sigma, scale = ccs_scale_type.LINEAR, quantization = 0.0):
return cls(data_type = NUM_FLOAT, mu = mu, sigma = sigma, scale = scale, quantization = quantization)
@property
def mu(self):
if hasattr(self, "_mu"):
......
......@@ -9,11 +9,30 @@ class ccs_interval(ct.Structure):
('_lower_included', ccs_bool),
('_upper_included', ccs_bool)]
def __init__(self, t = ccs_numeric_type.NUM_FLOAT, lower = 0.0, upper = 1.0, lower_included = True, upper_included = False):
if t == ccs_numeric_type.NUM_INTEGER:
self._lower.i = lower
self._upper.i = upper
elif t == ccs_numeric_type.NUM_FLOAT:
self._lower.f = lower
self._upper.f = upper
else:
raise Error(ccs_error(ccs_error.INVALID_VALUE))
self._type.value = t
if lower_included:
self._lower_included = ccs_true
else:
self._lower_included = ccs_false
if upper_included:
self._upper_included = ccs_true
else:
self._upper_included = ccs_false
@property
def type(self):
return self._type.value
@property
@type.setter
def type(self, v):
self._type.value = v
......@@ -79,6 +98,7 @@ class ccs_interval(ct.Structure):
else:
self._upper_included = ccs_false
@property
def empty(self):
v = ccs_bool(0)
res = ccs_interval_empty(ct.byref(self), ct.byref(v))
......
......@@ -23,10 +23,12 @@ class Rng(Object):
def from_handle(cls, handle):
return cls(handle, retain = True)
def set_seed(self, value):
res = ccs_rng_set_seed(self.handle, value)
Error.check(res)
return self
def __setattr__(self, name, value):
if name == 'seed':
res = ccs_rng_set_seed(self.handle, value)
Error.check(res)
return None
super().__setattr__(name, value)
def get(self):
v = ct.c_ulong(0)
......@@ -40,12 +42,14 @@ class Rng(Object):
Error.check(res)
return v.value
@property
def min(self):
v = ct.c_ulong(0)
res = ccs_rng_min(self.handle, ct.byref(v))
Error.check(res)
return v.value
@property
def max(self):
v = ct.c_ulong(0)
res = ccs_rng_max(self.handle, ct.byref(v))
......
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs
from math import sin
class TestBase(unittest.TestCase):
def test_version(self):
ver = ccs.ccs_get_version()
self.assertIsInstance(ver, ccs.ccs_version)
def test_datum_value(self):
d = ccs.ccs_datum()
d.value = None
self.assertEqual( ccs.NONE, d.type )
self.assertIsNone( d.value )
d.value = ccs.ccs_inactive
self.assertEqual( ccs.INACTIVE, d.type )
self.assertEqual( ccs.ccs_inactive, d.value )
d.value = False
self.assertEqual( ccs.BOOLEAN, d.type )
self.assertFalse( d.value )
d.value = True
self.assertEqual( ccs.BOOLEAN, d.type )
self.assertTrue( d.value )
d.value = 1.5
self.assertEqual( ccs.FLOAT, d.type )
self.assertEqual( 1.5, d.value )
d.value = 2
self.assertEqual( ccs.INTEGER, d.type )
self.assertEqual( 2, d.value )
d.value = "foo"
self.assertEqual( ccs.STRING, d.type )
self.assertEqual( "foo", d.value )
d.value = ccs.Rng()
self.assertEqual( ccs.OBJECT, d.type )
self.assertIsInstance( d.value, ccs.Rng )
self.assertEqual( ccs.RNG, d.value.object_type )
def test_numeric(self):
n = ccs.ccs_numeric()
n.set_value(2)
self.assertEqual( 2, n.get_value(ccs.NUM_INTEGER))
n.set_value(1.5)
self.assertEqual( 1.5, n.get_value(ccs.NUM_FLOAT))
if __name__ == '__main__':
unittest.main()
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs
class TestDistribution(unittest.TestCase):
def test_from_handle_Roulette(self):
d = ccs.RouletteDistribution(areas = [ 1.0, 2.0, 1.0, 0.5 ])
d2 = ccs.Object.from_handle(d.handle)
self.assertEqual( d.__class__, d2.__class__)
self.assertEqual( d.handle.value, d2.handle.value)
def test_create_roulette(self):
areas = [ 1.0, 2.0, 1.0, 0.5 ]
s = sum(areas)
d = ccs.RouletteDistribution(areas = areas)
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.ROULETTE, d.type )
self.assertEqual( ccs.NUM_INTEGER, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
a = d.areas
self.assertTrue( sum(a) > 0.999 )
self.assertTrue( sum(a) < 1.001 )
for i in range(len(a)):
self.assertTrue( a[i] < areas[i] * 1.001 / s )
self.assertTrue( a[i] > areas[i] * 0.999 / s )
i = d.bounds
self.assertEqual( ccs.NUM_INTEGER, i.type)
self.assertEqual( 0, i.lower )
self.assertEqual( 4, i.upper )
self.assertTrue( i.lower_included )
self.assertFalse( i.upper_included )
def test_from_handle_normal(self):
d = ccs.NormalDistribution()
d2 = ccs.Object.from_handle(d.handle)
self.assertEqual( d.__class__, d2.__class__)
self.assertEqual( d.handle.value, d2.handle.value)
def test_create_normal(self):
d = ccs.NormalDistribution()
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.NORMAL, d.type )
self.assertEqual( ccs.NUM_FLOAT, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
self.assertEqual( 0.0, d.mu )
self.assertEqual( 1.0, d.sigma )
i = d.bounds
self.assertEqual( ccs.NUM_FLOAT, i.type )
self.assertEqual( float('-inf'), i.lower )
self.assertEqual( float('inf'), i.upper )
self.assertFalse( i.lower_included )
self.assertFalse( i.upper_included )
def test_create_normal_float(self):
d = ccs.NormalDistribution.float(mu = 2.0, sigma = 6.0)
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.NORMAL, d.type )
self.assertEqual( ccs.NUM_FLOAT, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
self.assertEqual( 2.0, d.mu )
self.assertEqual( 6.0, d.sigma )
def test_create_normal_int(self):
d = ccs.NormalDistribution.int(mu = 2.0, sigma = 6.0)
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.NORMAL, d.type )
self.assertEqual( ccs.NUM_INTEGER, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
self.assertEqual( 2.0, d.mu )
self.assertEqual( 6.0, d.sigma )
def test_sample_normal(self):
rng = ccs.Rng()
d = ccs.NormalDistribution()
i = d.bounds
v = d.sample(rng)
self.assertTrue( i.include(v) )
a = d.samples(rng, 100)
self.assertEqual( 100, len(a) )
for v in a:
self.assertTrue( i.include(v) )
def test_from_handle_uniform(self):
d = ccs.UniformDistribution()
d2 = ccs.Object.from_handle(d.handle)
self.assertEqual( d.__class__, d2.__class__)
self.assertEqual( d.handle.value, d2.handle.value)
def test_create_uniform(self):
d = ccs.UniformDistribution()
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.UNIFORM, d.type )
self.assertEqual( ccs.NUM_FLOAT, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
i = d.bounds
self.assertEqual( ccs.NUM_FLOAT, i.type )
self.assertEqual( 0.0, i.lower)
self.assertEqual( 1.0, i.upper)
self.assertEqual( 0.0, d.lower)
self.assertEqual( 1.0, d.upper)
self.assertTrue( i.lower_included )
self.assertFalse( i.upper_included )
def test_create_uniform_float(self):
d = ccs.UniformDistribution.float(lower = -1.0, upper = 1.0)
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.UNIFORM, d.type )
self.assertEqual( ccs.NUM_FLOAT, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
i = d.bounds
self.assertEqual( ccs.NUM_FLOAT, i.type )
self.assertEqual( -1.0, i.lower)
self.assertEqual( 1.0, i.upper)
self.assertEqual( -1.0, d.lower)
self.assertEqual( 1.0, d.upper)
self.assertTrue( i.lower_included )
self.assertFalse( i.upper_included )
def test_create_uniform_int(self):
d = ccs.UniformDistribution.int(lower = 0, upper = 100)
self.assertEqual( ccs.DISTRIBUTION, d.object_type )
self.assertEqual( ccs.UNIFORM, d.type )
self.assertEqual( ccs.NUM_INTEGER, d.data_type )
self.assertEqual( ccs.LINEAR, d.scale )
self.assertEqual( 1, d.dimension )
i = d.bounds
self.assertEqual( ccs.NUM_INTEGER, i.type )
self.assertEqual( 0, i.lower)
self.assertEqual( 100, i.upper)
self.assertEqual( 0, d.lower)
self.assertEqual( 100, d.upper)
self.assertTrue( i.lower_included )
self.assertFalse( i.upper_included )
def test_oversampling_uniform_float(self):
d = ccs.UniformDistribution.float(lower = -1.0, upper = 1.0)
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -0.2, upper = 0.2)
self.assertTrue( d.oversampling(i) )
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -0.2, upper = 2.0)
self.assertTrue( d.oversampling(i) )
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -2.0, upper = 2.0)
self.assertFalse( d.oversampling(i) )
def test_oversampling_uniform_int(self):
d = ccs.UniformDistribution.int(lower = 0, upper = 100)
i = ccs.ccs_interval(t = ccs.NUM_INTEGER, lower = 5, upper = 50)
self.assertTrue( d.oversampling(i) )
i = ccs.ccs_interval(t = ccs.NUM_INTEGER, lower = 5, upper = 150)
self.assertTrue( d.oversampling(i) )
i = ccs.ccs_interval(t = ccs.NUM_INTEGER, lower = -5, upper = 150)
self.assertFalse( d.oversampling(i) )
def test_sample_uniform(self):
rng = ccs.Rng()
d = ccs.UniformDistribution()
i = d.bounds
v = d.sample(rng)
self.assertTrue( i.include(v) )
a = d.samples(rng, 100)
self.assertEqual( 100, len(a) )
for v in a:
self.assertTrue( i.include(v) )
if __name__ == '__main__':
unittest.main()
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs
class TestHyperparameter(unittest.TestCase):
def test_from_handle_discrete(self):
values = [0, 1.5, 2, 7.2]
h = ccs.DiscreteHyperparameter(values = values)
h2 = ccs.Object.from_handle(h.handle)
self.assertEqual( h.__class__, h2.__class__ )
self.assertEqual( h.handle, h2.handle )
def test_discrete(self):
values = [0.2, 1.5, 2, 7.2]
h = ccs.DiscreteHyperparameter(values = values)
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.DISCRETE, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( 0.2, h.default_value )
self.assertFalse( h.check_value("foo") )
v = h.sample()
self.assertTrue( v in values )
vals = h.samples(100)
for v in vals:
self.assertTrue( v in values )
def test_ordinal_compare(self):
values = ["foo", 2, 3.0]
h = ccs.OrdinalHyperparameter(values = values)
self.assertEqual( 0, h.compare("foo", "foo") )
self.assertEqual( -1, h.compare("foo", 2) )
self.assertEqual( -1, h.compare("foo", 3.0) )
self.assertEqual( 1, h.compare(2, "foo") )
self.assertEqual( 0, h.compare(2, 2) )
self.assertEqual( -1, h.compare(2, 3.0) )
self.assertEqual( 1, h.compare(3.0, "foo") )
self.assertEqual( 1, h.compare(3.0, 2) )
self.assertEqual( 0, h.compare(3.0, 3.0) )
self.assertRaises( ccs.Error, h.compare, 4.0, "foo" )
def test_from_handle_ordinal(self):
values = ["foo", 2, 3.0]
h = ccs.OrdinalHyperparameter(values = values)
h2 = ccs.Object.from_handle(h.handle)
self.assertEqual( h.__class__, h2.__class__ )
self.assertEqual( h.handle, h2.handle )
def test_ordinal(self):
values = ["foo", 2, 3.0]
h = ccs.OrdinalHyperparameter(values = values)
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.ORDINAL, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( "foo", h.default_value )
self.assertEqual( ccs.UNIFORM, h.default_distribution.type )
self.assertEqual( values, h.values )
for v in values:
self.assertTrue( h.check_value(v) )
self.assertFalse( h.check_value("bar") )
v = h.sample()
self.assertTrue( v in values )
vals = h.samples(100)
for v in vals:
self.assertTrue( v in values )
def test_from_handle_categorical(self):
values = ["foo", 2, 3.0]
h = ccs.CategoricalHyperparameter(values = values)
h2 = ccs.Object.from_handle(h.handle)
self.assertEqual( h.__class__, h2.__class__ )
self.assertEqual( h.handle, h2.handle )
def test_categorical(self):
values = ["foo", 2, 3.0]
h = ccs.CategoricalHyperparameter(values = values)
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.CATEGORICAL, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( "foo", h.default_value )
self.assertEqual( ccs.UNIFORM, h.default_distribution.type )
self.assertEqual( values, h.values )
for v in values:
self.assertTrue( h.check_value(v) )
self.assertFalse( h.check_value("bar") )
v = h.sample()
self.assertTrue( v in values )
vals = h.samples(100)
for v in vals:
self.assertTrue( v in values )
def test_from_handle_numerical(self):
h = ccs.NumericalHyperparameter()
h2 = ccs.Object.from_handle(h.handle)
self.assertEqual( h.__class__, h2.__class__ )
self.assertEqual( h.handle, h2.handle )
def test_numerical(self):
h = ccs.NumericalHyperparameter()
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.NUMERICAL, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( 0.0, h.default_value )
self.assertEqual( ccs.UNIFORM, h.default_distribution.type )
self.assertEqual( ccs.NUM_FLOAT, h.data_type )
self.assertEqual( 0.0, h.lower )
self.assertEqual( 1.0, h.upper )
self.assertEqual( 0.0, h.quantization )
self.assertTrue( h.check_value(0.5) )
self.assertFalse( h.check_value(1.5) )
v = h.sample()
self.assertIsInstance( v, float )
self.assertTrue( v >= 0.0 and v < 1.0 )
vals = h.samples(100)
for v in vals:
self.assertIsInstance( v, float )
self.assertTrue( v >= 0.0 and v < 1.0 )
def test_numerical_float(self):
h = ccs.NumericalHyperparameter.float(lower = 0.0, upper = 1.0)
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.NUMERICAL, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( 0.0, h.default_value )
self.assertEqual( ccs.UNIFORM, h.default_distribution.type )
self.assertEqual( ccs.NUM_FLOAT, h.data_type )
self.assertEqual( 0.0, h.lower )
self.assertEqual( 1.0, h.upper )
self.assertEqual( 0.0, h.quantization )
self.assertTrue( h.check_value(0.5) )
self.assertFalse( h.check_value(1.5) )
v = h.sample()
self.assertIsInstance( v, float )
self.assertTrue( v >= 0.0 and v < 1.0 )
vals = h.samples(100)
for v in vals:
self.assertIsInstance( v, float )
self.assertTrue( v >= 0.0 and v < 1.0 )
def test_numerical_int(self):
h = ccs.NumericalHyperparameter.int(lower = 0, upper = 100)
self.assertEqual( ccs.HYPERPARAMETER, h.object_type )
self.assertEqual( ccs.NUMERICAL, h.type )
self.assertTrue( h.name[:5] == "param" )
self.assertIsNone( h.user_data.value )
self.assertEqual( 0, h.default_value )
self.assertEqual( ccs.UNIFORM, h.default_distribution.type )
self.assertEqual( ccs.NUM_INTEGER, h.data_type )
self.assertEqual( 0, h.lower )
self.assertEqual( 100, h.upper )
self.assertEqual( 0, h.quantization )
self.assertTrue( h.check_value(50) )
self.assertFalse( h.check_value(150) )
v = h.sample()
self.assertIsInstance( v, int )
self.assertTrue( v >= 0 and v < 100 )
vals = h.samples(100)
for v in vals:
self.assertIsInstance( v, int )
self.assertTrue( v >= 0 and v < 100 )
if __name__ == '__main__':
unittest.main()
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs
from math import sin
class TestInterval(unittest.TestCase):
def test_create(self):
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
self.assertEqual( ccs.NUM_FLOAT, i.type )
self.assertEqual( -1.0, i.lower )
self.assertEqual( 1.0, i.upper )
self.assertTrue( i.lower_included )
self.assertFalse( i.upper_included )
def test_empty(self):
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
self.assertFalse( i.empty )
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = 1.0, upper = -1.0)
self.assertTrue( i.empty )
def test_equal(self):
i1 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
i2 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
self.assertEqual(i1, i2)
i2 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0, upper_included = True)
self.assertNotEqual(i1, i2)
def test_intersect(self):
i1 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
i2 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = 0.0, upper = 2.0)
i3 = i1.intersect(i2)
i4 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = 0.0, upper = 1.0)
self.assertEqual( i4, i3 )
i1 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 0.0)
i2 = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = 0.0, upper = 2.0)
i3 = i1.intersect(i2)
self.assertTrue( i3.empty )
def test_include(self):
i = ccs.ccs_interval(t = ccs.NUM_FLOAT, lower = -1.0, upper = 1.0)
self.assertTrue( i.include(0.0) )
self.assertFalse( i.include(2.0) )
i = ccs.ccs_interval(t = ccs.NUM_INTEGER, lower = -5, upper = 5)
self.assertTrue( i.include(0) )
self.assertFalse( i.include(6) )
if __name__ == '__main__':
unittest.main()
import unittest
import sys
sys.path.insert(1, '.')
sys.path.insert(1, '..')
import cconfigspace as ccs
from math import sin
class TestRng(unittest.TestCase):
def test_create(self):
rng = ccs.Rng()
self.assertEqual( ccs.RNG, rng.object_type )
self.assertEqual( 1, rng.refcount )
rng = None
def test_get(self):
rng = ccs.Rng()
v = rng.get()
self.assertIsInstance( v, int)
self.assertTrue( v >= rng.min )
self.assertTrue( v <= rng.max )
def test_uniform(self):
rng = ccs.Rng()
v = rng.uniform()
self.assertIsInstance( v, float)
self.assertTrue( v >= 0.0 )
self.assertTrue( v < 1.0 )
def test_set_seed(self):
rng = ccs.Rng()
rng.seed = 10
v1 = rng.get()
rng.seed = 10
v2 = rng.get()
self.assertEqual(v1, v2)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment