summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xexample.py2
-rw-r--r--ricky/config.py1
-rw-r--r--ricky/param/__init__.py17
-rw-r--r--ricky/param/bool.py6
-rw-r--r--ricky/param/color.py5
-rw-r--r--ricky/param/constrainednumber.py4
-rw-r--r--ricky/param/enum.py6
-rw-r--r--ricky/params.py36
-rw-r--r--ricky/pbgradient/probabilities.py102
-rw-r--r--share/probabilities/PbBreaker.json (renamed from ricky/pbbreaker/probability.json)0
-rw-r--r--share/probabilities/PbGradient.json (renamed from ricky/pbgradient/probabilities.json)0
-rw-r--r--share/probabilities/PbGrid.json (renamed from ricky/pbgrid/probabilities.json)21
-rw-r--r--unused/probabilities.py21
-rw-r--r--unused/probability.py9
-rw-r--r--unused/weight.py57
15 files changed, 105 insertions, 182 deletions
diff --git a/example.py b/example.py
index d34c3f6..5c087b0 100755
--- a/example.py
+++ b/example.py
@@ -5,7 +5,7 @@ from ricky.pbgrid import PbGrid
from ricky.pbpattern import PbPattern
-api = PbPattern()
+api = PbBreaker()
params = api.params_init()
print params
params.randomize()
diff --git a/ricky/config.py b/ricky/config.py
index 819def3..9d0ddfa 100644
--- a/ricky/config.py
+++ b/ricky/config.py
@@ -10,3 +10,4 @@ IMGRID_URL = "http://localhost:8999/im/api/imgrid"
IMGRADIENT_URL = "http://localhost:8999/im/api/imgradient"
IMBREAK_URL = "http://localhost:8999/im/api/imbreak"
OFFLINE = False
+PROBABILITIES_DIR = "share/probabilities"
diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py
index cbacf6f..4ec8204 100644
--- a/ricky/param/__init__.py
+++ b/ricky/param/__init__.py
@@ -1,4 +1,5 @@
import pprint
+import random
class Param(object):
@@ -53,7 +54,21 @@ class Param(object):
default = property(default_get, default_set)
- def randomize(self):
+ def _choose_from_probabilities(self, probabilities):
+ """
+ if using weights, considers all weights as a percentage of
+ 100
+ """
+ choice = random.randint(0, 100)
+ position = 0
+ for elem in probabilities:
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return True
+ return True
+
+ def randomize(self, probabilities=None):
pass
def from_normalized(self, value):
diff --git a/ricky/param/bool.py b/ricky/param/bool.py
index 2b25bbd..4f75dc6 100644
--- a/ricky/param/bool.py
+++ b/ricky/param/bool.py
@@ -32,8 +32,10 @@ class Bool(Param):
raise ValueError("Bad Value for Bool %s" % value)
return value
- def randomize(self):
- self.value_set(random.choice([True, False]))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice([True, False])
def from_normalized(self, value):
value_as_int = int(round(value, 0))
diff --git a/ricky/param/color.py b/ricky/param/color.py
index 5da05ed..7abc23a 100644
--- a/ricky/param/color.py
+++ b/ricky/param/color.py
@@ -35,10 +35,11 @@ class Color(Param):
value = int(self.as_hex(), 16)
return decimal.Decimal(value)/decimal.Decimal(maximum)
- def randomize(self):
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
self.value = "rgb(%s,%s,%s)" % (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)
)
- self._is_rgb = True
diff --git a/ricky/param/constrainednumber.py b/ricky/param/constrainednumber.py
index 3563d22..3af3bf9 100644
--- a/ricky/param/constrainednumber.py
+++ b/ricky/param/constrainednumber.py
@@ -73,7 +73,9 @@ class ConstrainedNumber(Param):
value = int(value)
self.value = value
- def randomize(self, tries=0):
+ def randomize(self, tries=0, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
tries_max = 30
try:
self._generate_random()
diff --git a/ricky/param/enum.py b/ricky/param/enum.py
index d4a839a..137c58e 100644
--- a/ricky/param/enum.py
+++ b/ricky/param/enum.py
@@ -30,8 +30,10 @@ class Enum(Param):
value = property(value_get, value_set)
- def randomize(self):
- self.value_set(random.choice(self._options))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice(self._options)
def as_dict(self):
my_dict = super(Enum, self).as_dict()
diff --git a/ricky/params.py b/ricky/params.py
index 4f91776..476e0d2 100644
--- a/ricky/params.py
+++ b/ricky/params.py
@@ -1,5 +1,9 @@
"""base class for all params"""
import pprint
+import simplejson as json
+import sys
+import os
+from ricky.config import PROBABILITIES_DIR
class Params(object):
@@ -18,6 +22,28 @@ class Params(object):
"""string representation"""
return pprint.pformat(self.as_dict())
+ def _load_probabilities_json(self, probabilities_file=None):
+ if probabilities_file:
+ filepath = probabilities_file
+ else:
+ filepath = os.path.join(
+ PROBABILITIES_DIR,
+ "%s.json" % (self.api.__class__.__name__)
+ )
+ try:
+ f = open(filepath, 'r')
+ data = f.read()
+ f.close()
+ return json.loads(data)
+ except json.scanner.JSONDecodeError as e:
+ sys.stderr.write("Invalid Json - Problem decoding %s\n" % filepath)
+ sys.stderr.write("%s\n" % e)
+ sys.exit(1)
+ except IOError:
+ sys.stderr.write(
+ "Could not find probabilities file %s\n" % filepath)
+ sys.exit(1)
+
def randomize(
self,
probabilities=None,
@@ -27,13 +53,13 @@ class Params(object):
if using a probabilities.json file, weight is taken
into account"""
if probabilities:
- probabilities = self._load_probabilities(probabilities)
- else if probabilities_local:
- probabilities = self._load_probabilities(probabilities_local)
+ probabilities_dict = self._load_probabilities_json(probabilities)
+ elif probabilities_local:
+ probabilities_dict = self._load_probabilities_json()
else:
- probabilities = {}
+ probabilities_dict = {}
for param in self._params:
- param.randomize(probability=probabilities.get(param.name))
+ param.randomize(probabilities=probabilities_dict.get(param.name))
@property
def api(self):
diff --git a/ricky/pbgradient/probabilities.py b/ricky/pbgradient/probabilities.py
deleted file mode 100644
index d77739f..0000000
--- a/ricky/pbgradient/probabilities.py
+++ /dev/null
@@ -1,102 +0,0 @@
-from ricky.param.probabilities import Probabilities
-
-width_probabilities = Probabilities.from_dict(
- {"value": 40, "weight": 10},
-)
-height_probabilities = Probabilities.from_dict(
- {"value": 400, "weight": 100},
-)
-color1_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "black", "weight": 1},
-# {"value": "white", "weight": 2},
-)
-color2_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "black", "weight": 2},
-# {"value": "white", "weight": 1},
-)
-stripes_probabilities = Probabilities.from_dict(
- {"value": "true", "weight": 3},
- {"value": "false", "weight": 1},
-)
-stripenumber_probabilities = Probabilities.from_dict(
- {"value": 3, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 100, "weight": 10},
- {"value": 40, "weight": 10},
-# {"value": 1, "weight": 50},
-# {"value": 2, "weight": 50},
-# {"value": 2, "weight": 50},
-
-)
-stripeintensity_probabilities = Probabilities.from_dict(
- {"value": 1000, "weight": 10},
- {"value": 4, "weight": 10},
-)
-# contrast_probabilities = \
-brightness_probabilities = \
- saturation_probabilities = \
- hue_probabilities = \
- Probabilities.from_dict(
- {"value": "", "weight": 0},
-# {"value": "", "weight": 300},
-)
-halftone_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 60},
- {"value": "checkeredfade", "weight": 10},
- {"value": "etchedtransition", "weight": 10},
- {"value": "bendaydots", "weight": 10},
- {"value": "smallerdots1", "weight": 10},
- {"value": "smallerdots2", "weight": 10},
- {"value": "flatstripes", "weight": 10},
-)
-bevel_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 4},
- {"value": "flatout", "weight": 1},
- {"value": "flatinner", "weight": 0},
- {"value": "evenlyframed", "weight": 1},
-# {"value": "biginner", "weight": 1},
- {"value": "bigouter", "weight": 1},
- {"value": "dramaticflatout", "weight": 1},
-# {"value": "dramaticflatinner", "weight": 1},
-)
-
-blurriness_probabilities = \
- percentbeveled_probabilities = Probabilities.from_dict(
- {"value": 30, "weight": 10},
- {"value": 10, "weight": 10},
- {"value": 5, "weight": 10},
- {"value": 20, "weight": 10},
- {"value": 25, "weight": 10},
- {"value": 7, "weight": 10},
- {"value": "", "weight": 1},
-)
-rotate_probabilities = \
- tilt_probabilities = Probabilities.from_dict(
- {"value": 0, "weight": 200},
- {"value": 90, "weight": 2},
- {"value": 180, "weight": 2},
- {"value": 270, "weight": 2},
-)
-flop_probabilities = flip_probabilities = Probabilities.from_dict(
- {"value": "", "weight": 1},
- {"value": "true", "weight": 1},
-)
-
-filetype_probabilities = Probabilities.from_dict(
- {"value": "png", "weight": 10},
- {"value": "jpg", "weight": 2},
- {"value": "gif", "weight": 2},
-)
-gradienttype_probabilities = Probabilities.from_dict(
- {"value": "canvas", "weight": 1},
- {"value": "gradient", "weight": 5},
- {"value": "radial", "weight": 1},
- {"value": "colorspace", "weight": 1},
- {"value": "plasmawash", "weight": 2},
- {"value": "gradientwash", "weight": 1},
- {"value": "mirrored", "weight": 0},
- {"value": "noise", "weight": 1},
-)
diff --git a/ricky/pbbreaker/probability.json b/share/probabilities/PbBreaker.json
index 9c28e6d..9c28e6d 100644
--- a/ricky/pbbreaker/probability.json
+++ b/share/probabilities/PbBreaker.json
diff --git a/ricky/pbgradient/probabilities.json b/share/probabilities/PbGradient.json
index 0c6c0df..0c6c0df 100644
--- a/ricky/pbgradient/probabilities.json
+++ b/share/probabilities/PbGradient.json
diff --git a/ricky/pbgrid/probabilities.json b/share/probabilities/PbGrid.json
index f9defe8..09a3121 100644
--- a/ricky/pbgrid/probabilities.json
+++ b/share/probabilities/PbGrid.json
@@ -26,28 +26,31 @@
{ "value" : "silver", "weight" : 1 },
{ "value" : null, "weight" : 10 }
],
-
"linecolor" : [
{ "value" : "black", "weight" : 1 },
{ "value" : "white", "weight" : 1 },
{ "value" : "silver", "weight" : 1 }
],
"swing" : [
- {"value": ", "weight": 2},
+ {"value": "", "weight": 2},
{"value": 30, "weight": 1},
{"value": -30, "weight": 1}
],
"tilt" : [
- {"value": ", "weight": 2},
+ {"value": "", "weight": 2},
{"value": 30, "weight": 1},
{"value": -30, "weight": 1}
],
"roll" : [
- {"value": ", "weight": 2},
+ {"value": "", "weight": 2},
{"value": 30, "weight": 1},
{"value": -30, "weight": 1}
],
- "width" : "height" : [
+ "width" : [
+ { "value" : 400, "weight" : 1 },
+ { "value" : 600, "weight" : 1 }
+ ],
+ "height" : [
{ "value" : 400, "weight" : 1 },
{ "value" : 600, "weight" : 1 }
],
@@ -64,15 +67,15 @@
{"value":15, "weight": 1}
],
"vlines" : [
- {"value":", "weight": 2},
+ {"value": "", "weight": 2},
{"value":"true", "weight": 1}
],
"hlines" : [
- {"value":", "weight": 2},
+ {"value": "", "weight": 2},
{"value":"true", "weight": 1}
],
"shadow" : [
- {"value":", "weight": 1},
+ {"value": "", "weight": 1},
{"value":"true", "weight": 1}
],
"zoom" : [
@@ -81,7 +84,7 @@
{"value": -1.2, "weight": 1}
],
"trim" : [
- {"value":", "weight": 1},
+ {"value": "", "weight": 1},
{"value":"true", "weight": 1}
]
}
diff --git a/unused/probabilities.py b/unused/probabilities.py
new file mode 100644
index 0000000..d109b18
--- /dev/null
+++ b/unused/probabilities.py
@@ -0,0 +1,21 @@
+from ricky.param.probability import Probability
+import sys
+class Probabilities:
+ def __init__(self, *args):
+ self._values = args
+ def __iter__(self):
+ return iter(self._values)
+ def __len__(self):
+ return len(self._values)
+ def __str__(self):
+ return str(self._values)
+ def __getitem__(self, i):
+ return self._values[i]
+ def search(self, s):
+ for i in self:
+ if str(s) in i.value:
+ return i
+ @classmethod
+ def from_dict(cls, *args):
+ probabilities = map(lambda x: Probability(**x), args);
+ return cls(*probabilities);
diff --git a/unused/probability.py b/unused/probability.py
new file mode 100644
index 0000000..0d27b70
--- /dev/null
+++ b/unused/probability.py
@@ -0,0 +1,9 @@
+class Probability(dict):
+ def __init__(self, **kwargs):
+ super(Probability, self).__init__(**kwargs)
+ self.value = kwargs["value"]
+ self.weight = kwargs["weight"]
+ def __getattr__(self, attr):
+ return self.get(attr)
+ __setattr__= dict.__setitem__
+ __delattr__= dict.__delitem__
diff --git a/unused/weight.py b/unused/weight.py
deleted file mode 100644
index 4bd4af4..0000000
--- a/unused/weight.py
+++ /dev/null
@@ -1,57 +0,0 @@
- weights_total = sum(
- map(lambda x: x["weight"], self.probabilities())
- ) + (255 * 255 * 255)
- choice = random.randint(0, weights_total)
- position = 0
- for elem in self.probabilities():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- return
-
- weights_total = sum(
- map(lambda x: x["weight"], self.probabilities())
- )# + self.range_max - self.range_min
- if weights_total < 100:
- weights_total = 100;
- choice = random.randint(0, weights_total)
- import sys
- sys.stderr.write("choosing %s: random_int: %s, probabilities: %s\n" % (
- self.name,
- choice,
- self.probabilities()))
- position = 0
- for elem in self.probabilities():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- return
-
-
- weights_total = sum(map(lambda x: x["weight"], self.probabilities()))
- choice = random.randint(0, weights_total)
- position = 0
- for elem in self.probabilities():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- break
- def _choose_heaviest(self):
- heaviest_idx = 0
- heaviest_weight = 0
- idx = 0
- if (len(self.options())):
- for elem in self.options():
- if elem["weight"] > heaviest_weight:
- heaviest_weight = elem["weight"]
- heaviest_idx = idx
- idx += 1
- return self.options()[heaviest_idx]["value"]
- else:
- self.randomize()
-
- def heaviest(self):
- self.value = self._choose_heaviest()
-
- """default value is the probability with the heaviest weight"""
- self.default(self._choose_heaviest())