Add distributions
@@ -67,9 +67,10 @@ class Tensor(fields.Field):
if isinstance(value, six.string_types):
return [value, 0, 0]
if isinstance(value, list) and len(value) == 3:
- if (isinstance(value[0], str) and
+ condition = (isinstance(value[0], str) and
isinstance(value[1], int) and
- isinstance(value[1], int)):
+ isinstance(value[1], int))
+ if condition:
return value
raise ValidationError("This field expects a str or a list of [str, int, int].")
@@ -149,6 +150,71 @@ class LogSpace(Range):
KEYS = REQUIRED_KEYS + OPTIONAL_KEYS
+def quniform(low, high, q, size=None):
+ value = np.random.uniform(low=low, high=high, size=size)
+ return np.round(value // q) * q
+
+
+def loguniform(low, high, size=None):
+ value = np.random.uniform(low=low, high=high, size=size)
+ return np.exp(value)
+
+
+def qloguniform(low, high, q, size=None):
+ value = loguniform(low=low, high=high, size=size)
+ return np.round(value // q) * q
+
+
+def qnormal(loc, scale, q, size=None):
+ draw = np.random.normal(loc=loc, scale=scale, size=size)
+ return np.round(draw // q) * q
+
+
+def lognormal(loc, scale, size=None):
+ draw = np.random.normal(loc=loc, scale=scale, size=size)
+ return np.exp(draw)
+
+
+class Uniform(Range):
+ REQUIRED_KEYS = ['low', 'high', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class QUniform(Range):
+ REQUIRED_KEYS = ['low', 'high', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class LogUniform(Range):
+ REQUIRED_KEYS = ['low', 'high', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class QLogUniform(Range):
+ REQUIRED_KEYS = ['low', 'high', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class Normal(Range):
+ REQUIRED_KEYS = ['loc', 'scale', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class QNormal(Range):
+ REQUIRED_KEYS = ['loc', 'scale', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class LogNormal(Range):
+ REQUIRED_KEYS = ['loc', 'scale', 'size']
+ KEYS = REQUIRED_KEYS
+
+
+class QLogNormal(Range):
+ REQUIRED_KEYS = ['start', 'stop', 'num']
+ KEYS = REQUIRED_KEYS
+
+
class StrOrFct(fields.Str):
def serialize(self, attr, obj, accessor=None):
value = getattr(obj, attr)
