예제 #1
0
파일: test_nodes.py 프로젝트: SIholin/dpEmu
def test_exception_raised_when_param_missing():
    data = np.random.rand(5)
    x_node = Array()
    x_node.addfilter(Missing("prob", "m_val"))
    try:
        x_node.generate_error(data, {"probb": .5, "m_val": np.nan})
    except Exception as e:
        assert "prob" in str(e)
예제 #2
0
def visualize(title, ind, data, flt, params):
    root_node = Array()
    root_node.addfilter(flt)
    result = root_node.generate_error(data, params)
    sub = fig.add_subplot(3, 4, ind + 1)
    sub.imshow(result)
    sub.set_title(title)
예제 #3
0
def main():
    data = cv2.imread("data/landscape.png")
    x_node = Array()

    # Some filters, e.g. Rotation, expect the data to have a specific data type, e.g. uint8.
    #
    # This example takes an image, sums the original image and 180 degrees rotated version,
    # and then takes the average of each pixel's value.
    #
    # cv2's rotation requires data to be uint8, but summing them needs datatype with larger
    # precision, and thus type conversions are required.

    const = Constant("c")
    rot1 = Rotation("deg1")
    mod1 = ModifyAsDataType("rotation_dtype", rot1)
    rot2 = Rotation("deg2")
    mod2 = ModifyAsDataType("rotation_dtype", rot2)
    add = Addition(mod1, mod2)
    avg = Division(add, const)
    x_node.addfilter(ModifyAsDataType("avg_dtype", avg))

    params = {}
    params['c'] = 2
    params['rotation_dtype'] = np.uint8
    params['avg_dtype'] = np.uint16
    params['deg1'] = 0
    params['deg2'] = 180

    result = x_node.generate_error(data, params)
    cv2.imshow("Rotated", result)
    cv2.waitKey(0)
예제 #4
0
파일: test_nodes.py 프로젝트: SIholin/dpEmu
def test_array_works_with_numpy_arrays():
    a = np.array([0.])
    x_node = Array()
    x_node.addfilter(Missing("prob", "m_val"))
    params = {"prob": 1., "m_val": np.nan}
    out = x_node.generate_error(a, params)
    assert np.isnan(out[0])
예제 #5
0
def test_modify_as_datatype():
    a = np.array([256 + 42])
    params = {}
    params['dtype'] = np.int8
    x_node = Array()
    x_node.addfilter(ModifyAsDataType('dtype', Identity()))
    out = x_node.generate_error(a, params)
    assert np.array_equal(out, np.array([42]))
예제 #6
0
def test_seed_determines_result_for_fastrain_filter_two():
    a = np.zeros((10, 10, 3), dtype=int)
    x_node = Array()
    x_node.addfilter(Rain("probability", "range"))
    params = {"probability": 0.03, "range": 1}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #7
0
def test_seed_determines_result_for_gap_filter():
    a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
    x_node = Array()
    x_node.addfilter(Gap("prob_break", "prob_recover", "missing_value"))
    params = {"prob_break": 0.1, "prob_recover": 0.1, "missing_value": 1337}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #8
0
def test_seed_determines_result_for_gaussian_noise_filter():
    a = np.array([0., 1., 2., 3., 4.])
    x_node = Array()
    x_node.addfilter(GaussianNoise("mean", "std"))
    params = {"mean": .5, "std": .5}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.allclose(out1, out2, equal_nan=True)
예제 #9
0
def main():
    img = Image.open("data/landscape.png")
    data = np.array(img)
    root_node = Array()
    root_node.addfilter(JPEG_Compression('quality'))
    result = root_node.generate_error(data, {'quality': 5})
    filtered_img = Image.fromarray(result.astype('uint8'), 'RGB')
    filtered_img.show()
예제 #10
0
def test_seed_determines_result_for_missing_filter():
    a = np.array([0., 1., 2., 3., 4.])
    x_node = Array()
    x_node.addfilter(Missing("prob", "m_val"))
    params = {"prob": .5, "m_val": np.nan}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.allclose(out1, out2, equal_nan=True)
예제 #11
0
파일: test_text.py 프로젝트: SIholin/dpEmu
def test_seed_determines_result_for_uppercase_filter():
    a = np.array(["hello world"])
    x_node = Array()
    x_node.addfilter(Uppercase("prob"))
    params = {"prob": .5}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.alltrue(out1 == out2)
예제 #12
0
def main():
    img = Image.open("data/yellow_circle.jpg")
    data = img_to_pixel_data(img)
    root_node = Array()
    root_node.addfilter(LensFlare())
    result = root_node.generate_error(data, {})
    filtered_img = Image.fromarray(result.astype('uint8'), 'RGB')
    filtered_img.show()
예제 #13
0
def test_clip():
    a = np.arange(5)
    params = {}
    params['min'] = 2
    params['max'] = 3
    x_node = Array()
    x_node.addfilter(Clip('min', 'max'))
    out = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out, np.array([2, 2, 2, 3, 3]))
예제 #14
0
def main():
    img = Image.open("data/landscape.png")
    data = np.array(img)
    root_node = Array()
    # root_node.addfilter(filters.Blur_Gaussian('std'))
    # result = root_node.generate_error(data, {'std': 10.0})
    root_node.addfilter(Blur('repeats', 'radius'))
    result = root_node.generate_error(data, {'repeats': 1, 'radius': 20})
    filtered_img = Image.fromarray(result.astype('uint8'), 'RGB')
    filtered_img.show()
예제 #15
0
def test_seed_determines_result_for_strange_behaviour_filter():
    def f(data, random_state):
        return data * random_state.randint(2, 4)

    a = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
    x_node = Array()
    x_node.addfilter(StrangeBehaviour("f"))
    params = {"f": f}
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #16
0
def main():
    d = {"resolution": 10}
    data = plt.imread("data/landscape.png")
    x_node = Array()
    r = Resolution("resolution")
    x_node.addfilter(r)
    start = time.time()
    result = x_node.generate_error(data, d)
    end = time.time()
    print(f"Time vectorized: {end-start}")

    plt.imshow(result)
    plt.show()
예제 #17
0
def test_seed_determines_result_for_snow_filter():
    a = np.zeros((10, 10, 3), dtype=int)
    x_node = Array()
    x_node.addfilter(
        Snow("snowflake_probability", "snowflake_alpha", "snowstorm_alpha"))
    params = {
        "snowflake_probability": 0.04,
        "snowflake_alpha": 0.4,
        "snowstorm_alpha": 1
    }
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #18
0
파일: test_text.py 프로젝트: SIholin/dpEmu
def test_seed_determines_result_for_ocr_error_filter():
    a = np.array(["hello world"])
    x_node = Array()
    x_node.addfilter(OCRError("probs", "p"))
    params = {
        "probs": {
            "e": (["E", "i"], [.5, .5]),
            "g": (["q", "9"], [.2, .8])
        },
        "p": 1
    }
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #19
0
파일: test_text.py 프로젝트: SIholin/dpEmu
def test_seed_determines_result_for_missing_area_filter_with_gaussian_radius_generator(
):
    a = np.array(["hello world\n" * 10])
    x_node = Array()
    x_node.addfilter(
        MissingArea("probability", "radius_generator", "missing_value"))
    params = {
        "probability": 0.05,
        "radius_generator": radius_generators.GaussianRadiusGenerator(1, 1),
        "missing_value": "#"
    }
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
def main():
    d = {"tar": 1, "rat": 0.5, "range": 1}
    img_path = "data/landscape.png"

    data = plt.imread(img_path)
    x_node = Array()
    b = Brightness("tar", "rat", "range")
    x_node.addfilter(b)
    start = time.time()
    result = x_node.generate_error(data, d)
    end = time.time()
    print(f"Time vectorized: {end-start}")

    plt.imshow(result)
    plt.show()
def main():
    img_path = "data/landscape.png"
    d = {"tar": 1, "rat": 0.55, "range": 255}

    img2 = Image.open(img_path)
    data = np.array(img2)
    x_node = Array()
    b2 = Brightness("tar", "rat", "range")
    x_node.addfilter(b2)
    start = time.time()
    result = x_node.generate_error(data, d)
    end = time.time()
    print(f"Time vectorized: {end-start}")

    plt.imshow(result)
    plt.show()
예제 #22
0
def main():
    img_path = "data/landscape.png"
    d = {"tar": 0, "rat": 0.8, "range": 255}

    img = Image.open(img_path)
    data = np.array(img)
    x_node = Array()
    s = Saturation("tar", "rat", "range")
    x_node.addfilter(s)
    start = time.time()
    result = x_node.generate_error(data, d)
    end = time.time()
    print(f"Time vectorized: {end-start}")

    plt.imshow(result)
    plt.show()
예제 #23
0
def main():
    img_path = "data/landscape.png"
    img = Image.open(img_path)
    data = np.array(img)
    # data = plt.imread(img_path)

    root_node = Array()
    # root_node.addfilter(Snow("p", "flake_alpha", "storm_alpha"))
    root_node.addfilter(Rain("p", "r"))
    before = time.time()
    result = root_node.generate_error(data, {'p': .01, 'r': 255})
    end = time.time()

    print(f"{end - before} faster time")

    plt.imshow(result)
    plt.show()
예제 #24
0
def test_seed_determines_result_for_stain_filter():
    def f(data, random_state):
        return data * random_state.randint(2, 4)

    a = np.random.RandomState(seed=42).randint(0, 255, size=300).reshape(
        (10, 10, 3))
    x_node = Array()
    x_node.addfilter(
        StainArea("probability", "radius_generator",
                  "transparency_percentage"))
    params = {
        "probability": .005,
        "radius_generator": radius_generators.GaussianRadiusGenerator(10, 5),
        "transparency_percentage": 0.5
    }
    out1 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    out2 = x_node.generate_error(a, params, np.random.RandomState(seed=42))
    assert np.array_equal(out1, out2)
예제 #25
0
    "laboris nisi ut aliquid ex ea commodi consequat. \n" +
    "Quis aute iure reprehenderit in voluptate\n" +
    "velit esse cillum dolore eu fugiat nulla pariatur.\n" +
    "Excepteur sint obcaecat cupiditat non proident,\n" +
    "sunt in culpa qui officia\n" + "deserunt mollit anim id est laborum.",
    "Hello\n" +  # the next string starts here
    "Hello\n" + "Hello\n" + "Hello\n" + "Hello"
])

root_node = Array()
root_node.addfilter(MissingArea("p", "radius_gen", "value"))

params = {}
params['value'] = " "

params['p'] = .03
params['radius_gen'] = radius_generators.GaussianRadiusGenerator(1, 1)

out = root_node.generate_error(data, params)

for elem in out:
    print(elem, end="\n\n")

params['p'] = .04
params['radius_gen'] = radius_generators.ProbabilityArrayRadiusGenerator(
    [0, 0.6, 0.4])

out = root_node.generate_error(data, params)
for elem in out:
    print(elem, end="\n\n")
예제 #26
0
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np

from dpemu.nodes import Array
from dpemu.filters.text import Uppercase

data = np.array(["hello world", "all your Bayes' theorems are belong to us"])

root_node = Array()
root_node.addfilter(Uppercase("prob"))
out = root_node.generate_error(data, {'prob': .45})
print(out)
print("output shape:", out.shape, ", output dtype:", out.dtype)
예제 #27
0
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""This example raises an exception (on purpose).
The parameter identifier "mean" is misspelled in the
params dictionary. This should result in an exception
with a helpful error message.
"""

import numpy as np
from dpemu.filters.common import GaussianNoise
from dpemu.nodes import Array

xs = np.random.rand(100, 200)
array_node = Array()
array_node.addfilter(GaussianNoise("mean", "std"))
params = {"meany": 0.0, "std": 20.0}
print("""
---
This example should terminate with a helpful error message.
---
""")
errorified = array_node.generate_error(xs, params)
예제 #28
0
import numpy as np

from dpemu.nodes import Array
from dpemu.filters.time_series import Gap

y = np.arange(100.0, 200.0)
print("Original y:\n", y)

data = y
root_node = Array()
print(f"input shape: {data.shape}")

"""
Every increase in time results in drift increasing by 0.1
root_node.addfilter(SensorDrift(magnitude=0.1))
"""

"""
def strange(a):
    if a <= 170 and a >= 150:
        return 1729

    return a
"""
# y_node.addfilter(StrangeBehaviour(strange))
root_node.addfilter(Gap("prob_break", "prob_recover", "missing_value"))

output = root_node.generate_error(data, {'prob_break': .3, 'prob_recover': .3, 'missing_value': np.nan})
print("output:\n", output)
print(f"output dtype: {output.dtype}")
예제 #29
0
from dpemu.filters import Constant, Identity, Subtraction

# generate image with bitwise operations
data = []
for y in range(0, 512):
    data.append([])
    for x in range(0, 512):
        data[y].append((x ^ y, x & y, 0))
data = np.array(data, dtype=np.uint8)

# show original image
img_original = Image.fromarray(data, "RGB")
img_original.show()

# generate error
root_node = Array()
# add filter which subtracts each pixel value from 255
root_node.addfilter(Subtraction("const", "identity"))
out = root_node.generate_error(data, {
    'c': 255,
    'const': Constant("c"),
    'identity': Identity()
})

# show modified image
img_modified = Image.fromarray(out, "RGB")
img_modified.show()

print(out)
print("output shape:", out.shape, ", output dtype:", out.dtype)
예제 #30
0
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import numpy as np
from dpemu.nodes import Array
from dpemu.filters.text import OCRError
from dpemu import pg_utils

data = np.array([
    "shambler", "shub-niggurath", "ogre", "difficulty: nightmare",
    "quad damage", "health 100, health 99, health 0"
])

root_node = Array()
params = pg_utils.load_ocr_error_params("data/example_ocr_error_config.json")
normalized_params = pg_utils.normalize_ocr_error_params(params)
root_node.addfilter(OCRError("params", "p"))
out = root_node.generate_error(data, {'params': normalized_params, 'p': .5})

for c in ["a", "q", "1", ":"]:
    print(c, params[c], normalized_params[c])

print(out)
print("output shape:", out.shape, ", output dtype:", out.dtype)