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])
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)
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)
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)
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)
def get_err_root_node(): err_img_node = Array(reshape=(28, 28)) err_root_node = Series(err_img_node) # err_img_node.addfilter(GaussianNoise("mean", "std")) # err_img_node.addfilter(Clip("min_val", "max_val")) # err_img_node.addfilter(Missing("probability", "missing_value")) err_img_node.addfilter(Rotation("min_angle", "max_angle")) return err_root_node
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)
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)
def test_series_and_array_work_with_regular_arrays(): a = [0.] x_node = Array() x_node.addfilter(Missing("prob", "m_val")) series_node = Series(x_node) params = {"prob": 1., "m_val": np.nan} out = series_node.generate_error(a, params) assert np.isnan(out[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()
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()
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)
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]))
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]))
def test_visualizing_array_node_with_filter(): x_node = Array() x_node.addfilter(Missing("p", "missing_value")) tree = x_node.get_parametrized_tree({'p': 0.5, 'missing_value': np.nan}) path = plotting_utils.visualize_error_generator(tree, False) file = open(path, 'r') data = file.read() assert re.compile(r'2.*Missing.*probability: 0').search(data) assert re.compile(r'1 -> 2').search(data)
def test_visualizing_tuple_series_and_two_array_nodes(): x_node = Array() y_node = Array() series_node = TupleSeries([x_node, y_node]) path = plotting_utils.visualize_error_generator(series_node, False) file = open(path, 'r') data = file.read() assert re.compile(r'1.*TupleSeries').search(data) assert re.compile(r'1 -> 2').search(data) assert re.compile(r'1 -> 3').search(data)
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()
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)
def test_tuple_series_works_with_numpy_arrays(): a = np.array([[1, 2, 3]]) b = np.array([[1, 1, 1]]) data = (a, b) x_node = Array() x_node.addfilter(SensorDrift("a")) y_node = Array() root_node = TupleSeries([x_node, y_node]) res = root_node.generate_error(data, {'a': 1}) assert np.array_equal(res[0], np.array([[2, 4, 6]])) and np.array_equal( res[1], np.array([[1, 1, 1]]))
def get_err_root_node(): err_node = Array() err_root_node = Series(err_node) # err_node.addfilter(GaussianNoise("mean", "std")) # err_node.addfilter(Blur_Gaussian("std")) # err_node.addfilter(Snow("snowflake_probability", "snowflake_alpha", "snowstorm_alpha")) # err_node.addfilter(FastRain("probability", "range")) # err_node.addfilter(StainArea("probability", "radius_generator", "transparency_percentage")) # err_node.addfilter(JPEG_Compression("quality")) err_node.addfilter(Resolution("k")) # err_node.addfilter(Brightness("tar", "rat", "range")) # err_node.addfilter(Identity()) return err_root_node
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()
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)
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)
def test_visualizing_array_node_with_complex_filter(): x_node = Array() const = Constant("c") addition = Addition(const, const) x_node.addfilter(addition) path = plotting_utils.visualize_error_generator( x_node.get_parametrized_tree({'c': 5}), False) file = open(path, 'r') data = file.read() assert re.compile(r'2.*Addition').search(data) assert re.compile(r'3.*Constant.*value: 5').search(data) assert re.compile(r'4.*Constant.*value: 5').search(data) assert re.compile(r'2 -> 3.*filter_a').search(data) assert re.compile(r'2 -> 4.*filter_b').search(data)
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()
def test_seed_determines_result_for_time_dependent_gaussian_noise(): a = np.arange(25).reshape((5, 5)).astype(np.float64) params = {} params['mean'] = 2. params['std'] = 3. params['mean_inc'] = 1. params['std_inc'] = 4. x_node = Array() x_node.addfilter( GaussianNoiseTimeDependent('mean', 'std', 'mean_inc', 'std_inc')) series_node = Series(x_node, dim_name="time") out1 = series_node.generate_error(a, params, np.random.RandomState(seed=42)) out2 = series_node.generate_error(a, params, np.random.RandomState(seed=42)) assert np.allclose(out1, out2)
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()
def main(): """An example that rotates MNIST digits and displays one. Usage: python run_rotate_MNIST_example <angle> where <angle> is the angle of rotation (e.g. 90 to rotate by pi / 2) """ x, _, _, _ = load_mnist() xs = x[:20] # small subset of x angle = float(sys.argv[1]) print(f"x subset shape: {xs.shape}") img_node = Array(reshape=(28, 28)) root_node = Series(img_node) img_node.addfilter(Rotation("angle")) result = root_node.generate_error(xs, {'angle': angle}) plt.matshow(result[0].reshape((28, 28))) plt.show()