base.py 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import ctypes as ct
from . import libcconfigspace

ccs_init = libcconfigspace.ccs_init
ccs_init.restype = ct.c_int

class Version(ct.Structure):
  _fields_ = [("revision", ct.c_ushort),
              ("patch",    ct.c_ushort),
              ("minor",    ct.c_ushort),
              ("major",    ct.c_ushort)]

Brice Videau's avatar
Brice Videau committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Base types
ccs_float  = ct.c_double
ccs_int    = ct.c_longlong
ccs_bool   = ct.c_int
ccs_result = ct.c_int
ccs_hash   = ct.c_uint
ccs_object = ct.c_void_p

# Objects
ccs_rng                 = ccs_object
ccs_distribution        = ccs_object
ccs_hyperparameter      = ccs_object
ccs_expression          = ccs_object
ccs_context             = ccs_object
ccs_configuration_space = ccs_object
ccs_configuration       = ccs_object
ccs_objective_space     = ccs_object
ccs_evaluation          = ccs_object
ccs_tuner               = ccs_object

ccs_false = 0
ccs_true = 1

# https://www.python-course.eu/python3_metaclasses.php
class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

class Inactive(metaclass=Singleton):
    pass

ccs_inactive = Inactive()

# derived and adapted from http://code.activestate.com/recipes/576415/
50 51 52
class CEnumerationType(type(ct.c_int)):
  def __new__(metacls, name, bases, dict):
    if not "_members_" in dict:
Brice Videau's avatar
Brice Videau committed
53 54 55
      raise ValueError("CEnumeration must define a _members_ attribute")
    last = -1
    if isinstance(dict["_members_"], list):
56
      _members_ = {}
Brice Videau's avatar
Brice Videau committed
57 58 59 60 61 62 63 64
      for item in dict["_members_"]:
        if isinstance(item, tuple):
          (i, v) = item
          _members_[i] = v
          last = v
        else:
          last += 1
          _members_[item] = last
65
      dict["_members_"] = _members_
Brice Videau's avatar
Brice Videau committed
66 67 68 69 70 71
    _reverse_members_ = {}
    for key,value in dict["_members_"].items():
      dict[key] = value
      _reverse_members_[value] = key
    dict["_reverse_members_"] = _reverse_members_
    cls = type(ct.c_int).__new__(metacls, name, bases, dict)
72 73 74 75 76 77 78 79 80 81
    for key,value in cls._members_.items():
      globals()[key] = value
    return cls

  def __contains__(self, value):
    return value in self._members_.values()

  def __repr__(self):
    return "<Enumeration %s>" % self.__name__

Brice Videau's avatar
Brice Videau committed
82
class CEnumeration(ct.c_int, metaclass=CEnumerationType):
83 84
  _members_ = {}
  def __init__(self, value):
Brice Videau's avatar
Brice Videau committed
85 86
    if value in self._reverse_members_:
        self.name = self._reverse_members_[value]
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    else:
      raise ValueError("No enumeration member with value %r" % value)
    ct.c_int.__init__(self, value)

  @classmethod
  def from_param(cls, param):
    if isinstance(param, CEnumeration):
      if param.__class__ != cls:
        raise ValueError("Cannot mix enumeration members")
      else:
        return param
    else:
      return cls(param)

  def __repr__(self):
    return "<member %s=%d of %r>" % (self.name, self.value, self.__class__)

  def __str__(self):
    return "%s.%s" % (self.__class__.__name__, self.name)

Brice Videau's avatar
Brice Videau committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
class CEnumerationType64(type(ct.c_longlong)):
  def __new__(metacls, name, bases, dict):
    if not "_members_" in dict:
      raise ValueError("CEnumeration must define a _members_ attribute")
    last = -1
    if isinstance(dict["_members_"], list):
      _members_ = {}
      for item in dict["_members_"]:
        if isinstance(item, tuple):
          (i, v) = item
          _members_[i] = v
          last = v
        else:
          last += 1
          _members_[item] = last
      dict["_members_"] = _members_
    _reverse_members_ = {}
    for key,value in dict["_members_"].items():
      dict[key] = value
      _reverse_members_[value] = key
    dict["_reverse_members_"] = _reverse_members_
    cls = type(ct.c_longlong).__new__(metacls, name, bases, dict)
    for key,value in cls._members_.items():
      globals()[key] = value
    return cls

  def __contains__(self, value):
    return value in self._members_.values()

  def __repr__(self):
    return "<Enumeration %s>" % self.__name__

class CEnumeration64(ct.c_longlong, metaclass=CEnumerationType64):
  _members_ = {}
  def __init__(self, value):
    if value in self._reverse_members_:
        self._name = self._reverse_members_[value]
    else:
      raise ValueError("No enumeration member with value %r" % value)
    ct.c_longlong.__init__(self, value)

  def __repr__(self):
    return "<member %s=%d of %r>" % (self.name, self.value, self.__class__)

  def __str__(self):
    return "%s.%s" % (self.__class__.__name__, self.name)

  @property
  def name(self):
    if self.value in self._reverse_members_:
      return self._reverse_members_[self.value]
    else:
      raise ValueError("No enumeration member with value %r" % value)

161
  @classmethod
Brice Videau's avatar
Brice Videau committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  def from_param(cls, param):
    if isinstance(param, CEnumeration):
      if param.__class__ != cls:
        raise ValueError("Cannot mix enumeration members")
      else:
        return param
    else:
      return cls(param)

class ccs_object_type(CEnumeration):
  _members_ = [
    ('RNG', 0),
    'DISTRIBUTION',
    'HYPERPARAMETER',
    'EXPRESSION',
    'CONFIGURATION_SPACE',
    'CONFIGURATION',
    'OBJECTIVE_SPACE',
    'EVALUATION',
    'TUNER' ]

class ccs_error(CEnumeration):
  _members_ = [ 
    ('SUCCESS', 0),
    'INVALID_OBJECT',
    'INVALID_VALUE',
    'INVALID_TYPE',
    'INVALID_SCALE',
    'INVALID_DISTRIBUTION',
    'INVALID_EXPRESSION',
    'INVALID_HYPERPARAMETER',
    'INVALID_CONFIGURATION',
    'INVALID_NAME',
    'INVALID_CONDITION',
    'INVALID_TUNER',
    'INVALID_GRAPH',
    'TYPE_NOT_COMPARABLE',
    'INVALID_BOUNDS',
    'OUT_OF_BOUNDS',
    'SAMPLING_UNSUCCESSFUL',
    'INACTIVE_HYPERPARAMETER',
    'OUT_OF_MEMORY',
    'UNSUPPORTED_OPERATION' ]

class ccs_data_type(CEnumeration64):
  _members_ = [
    ('NONE', 0),
    'INTEGER',
    'FLOAT',
    'BOOLEAN',
    'STRING',
    'INACTIVE',
    'OBJECT' ]
 
class ccs_numeric_type(CEnumeration64):
  _members_ = [
    ('NUM_INTEGER', ccs_data_type.INTEGER),
    ('NUM_FLOAT', ccs_data_type.FLOAT) ]

class Numeric(ct.Union):
  _fields_ = [('f', ccs_float),
              ('i', ccs_int)]

  def get_value(self, t):
    if t == ccs_numeric_type.NUM_INTEGER:
      return self.f
    elif t == ccs_numeric_type.NUM_FLOAT:
      return self.v
    else:
      raise Error(ccs_error.INVALID_VALUE)

  def set_value(self, v):
    if isinstance(v, int):
      self.i = v
    elif isinstance(v, float):
      self.f = v
    else:
      raise Error(ccs_error.INVALID_VALUE)

class Value(ct.Union):
  _fields_ = [('f', ccs_float),
              ('i', ccs_int),
              ('s', ct.c_char_p),
              ('o', ccs_object)]

class Datum(ct.Structure):
  _fields_ = [('_value', Value),
              ('type', ccs_data_type)]

  def __init__(self, v = None):
    super().__init__()
    self._string = None
    self._object = None
    self.value = v

  @property
  def value(self):
    if self.type.value == ccs_data_type.NONE:
      return None
    elif self.type.value == ccs_data_type.INTEGER:
      return self._value.i
    elif self.type.value == ccs_data_type.FLOAT:
      return self._value.f
    elif self.type.value == ccs_data_type.BOOLEAN:
      return False if self._value.i == ccs_false else True
    elif self.type.value == ccs_data_type.STRING:
      return self._value.s.decode()
    elif self.type.value == ccs_data_type.INACTIVE:
      return ccs_inactive
    elif self.type.value == ccs_data_type.OBJECT:
      return Object.from_handle(ct.c_void_p(self._value.o))
    else:
      raise Error(ccs_error.INVALID_VALUE)

  @value.setter
  def value(self, v):
    self._string = None
    self._object = None
    if v is None:
      self.type.value = ccs_data_type.NONE
      self._value.i = 0
    elif isinstance(v, bool):
      self.type.value = ccs_data_type.BOOLEAN
      self._value.i = 1 if v else 0
    elif isinstance(v, int):
      self.type.value = ccs_data_type.INTEGER
      self._value.i = v
    elif isinstance(v, float):
      self.type.value = ccs_data_type.FLOAT
      self._value.f = v
    elif isinstance(v, str):
      self.type.value = ccs_data_type.STRING
      self._string = str.encode(v)
      self._value.s = ct.c_char_p(self._string)
    elif v is ccs_inactive:
      self.type.value = ccs_data_type.INACTIVE
      self._value.i = 0
    elif isinstance(v, Object):
      self.type.value = ccs_data_type.OBJECT
      self_object = v
      self._value.o = v.handle
    else:
      raise Error(ccs_error.INVALID_VALUE)

class Error(Exception):
307 308 309 310 311 312
  def __init__(self, message):
    self.message = message

  @classmethod
  def check(cls, err):
    if err < 0:
Brice Videau's avatar
Brice Videau committed
313
      raise cls(ccs_error(-err))
314 315 316 317 318

ccs_get_version = libcconfigspace.ccs_get_version
ccs_get_version.restype = Version

ccs_retain_object = libcconfigspace.ccs_retain_object
Brice Videau's avatar
Brice Videau committed
319 320
ccs_retain_object.restype = ccs_result
ccs_retain_object.argtypes = [ccs_object]
321 322

ccs_release_object = libcconfigspace.ccs_release_object
Brice Videau's avatar
Brice Videau committed
323 324
ccs_release_object.restype = ccs_result
ccs_release_object.argtypes = [ccs_object]
325 326

ccs_object_get_type = libcconfigspace.ccs_object_get_type
Brice Videau's avatar
Brice Videau committed
327 328
ccs_object_get_type.restype = ccs_result
ccs_object_get_type.argtypes = [ccs_object, ct.POINTER(ccs_object_type)]
329 330

ccs_object_get_refcount = libcconfigspace.ccs_object_get_refcount
Brice Videau's avatar
Brice Videau committed
331 332
ccs_object_get_refcount.restype = ccs_result
ccs_object_get_refcount.argtypes = [ccs_object, ct.POINTER(ct.c_int)]
333 334 335 336

class Object:
  def __init__(self, handle, retain = False, auto_release = True):
    if handle is None:
Brice Videau's avatar
Brice Videau committed
337
      raise Error(ccs_error.INVALID_OBJECT)
338 339 340 341
    self.handle = handle
    self.auto_release = auto_release
    if retain:
      res = ccs_retain_object(handle)
Brice Videau's avatar
Brice Videau committed
342
      Error.check(res)
343 344 345

  def __del__(self):
    res = ccs_release_object(self.handle)
Brice Videau's avatar
Brice Videau committed
346
    Error.check(res)
347 348

  def object_type(self):
Brice Videau's avatar
Brice Videau committed
349
    t = ccs_object_type(0)
350
    res = ccs_object_get_type(self.handle, ct.byref(t))
Brice Videau's avatar
Brice Videau committed
351
    Error.check(res)
352 353 354 355 356
    return t

  def refcount(self):
    c = ct.c_int(0)
    res = ccs_object_get_refcount(self.handle, ct.byref(c))
Brice Videau's avatar
Brice Videau committed
357
    Error.check(res)
358
    return c.value
Brice Videau's avatar
Brice Videau committed
359 360 361 362 363 364 365 366 367 368 369

  @classmethod
  def from_handle(cls, h):
    t = ccs_object_type(0)
    res = ccs_object_get_type(h, ct.byref(t))
    Error.check(res)
    if t.value == ccs_object_type.RNG:
      from .rng import Rng
      return Rng.from_handle(h)
    else:
      raise Error(ccs_error.INVALID_OBJECT)