def test_apply_parallel_rgb_channel_axis(depth, chunks): cat = img_as_float(data.chelsea()) func = color.rgb2ycbcr cat_ycbcr_expected = func(cat) cat_ycbcr = apply_parallel(func, cat, chunks=chunks, depth=depth, dtype=cat.dtype, channel_axis=-1) assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr) # channels axis along first dimension instead cat = np.moveaxis(cat, -1, 0) cat_ycbcr = apply_parallel(func, cat, chunks=chunks, depth=depth, dtype=cat.dtype, channel_axis=0) # move channels of output back to the last dimension cat_ycbcr = np.moveaxis(cat_ycbcr, 0, -1) assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr)
def test_apply_parallel_lazy(): # data a = np.arange(144).reshape(12, 12).astype(float) d = da.from_array(a, chunks=(6, 6)) # apply the filter expected1 = threshold_local(a, 3) result1 = apply_parallel(threshold_local, a, chunks=(6, 6), depth=5, extra_arguments=(3, ), extra_keywords={'mode': 'reflect'}, compute=False) # apply the filter on a Dask Array result2 = apply_parallel(threshold_local, d, depth=5, extra_arguments=(3, ), extra_keywords={'mode': 'reflect'}) assert isinstance(result1, da.Array) assert_array_almost_equal(result1.compute(), expected1) assert isinstance(result2, da.Array) assert_array_almost_equal(result2.compute(), expected1)
def test_apply_parallel(): # data a = np.arange(144).reshape(12, 12).astype(float) # apply the filter expected1 = threshold_local(a, 3) result1 = apply_parallel(threshold_local, a, chunks=(6, 6), depth=5, extra_arguments=(3, ), extra_keywords={'mode': 'reflect'}) assert_array_almost_equal(result1, expected1) def wrapped_gauss(arr): return gaussian(arr, 1, mode='reflect') expected2 = gaussian(a, 1, mode='reflect') result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5) assert_array_almost_equal(result2, expected2) expected3 = gaussian(a, 1, mode='reflect') result3 = apply_parallel(wrapped_gauss, da.from_array(a, chunks=(6, 6)), depth=5, compute=True) assert isinstance(result3, np.ndarray) assert_array_almost_equal(result3, expected3)
def test_apply_parallel_lazy(): import dask.array as da # data a = np.arange(144).reshape(12, 12).astype(float) d = da.from_array(a, chunks=(6, 6)) # apply the filter expected1 = threshold_local(a, 3) result1 = apply_parallel(threshold_local, a, chunks=(6, 6), depth=5, extra_arguments=(3,), extra_keywords={'mode': 'reflect'}, compute=False) # apply the filter on a Dask Array result2 = apply_parallel(threshold_local, d, depth=5, extra_arguments=(3,), extra_keywords={'mode': 'reflect'}, compute=False) assert isinstance(result1, da.Array) assert_array_almost_equal(result1.compute(), expected1) assert isinstance(result2, da.Array) assert_array_almost_equal(result2.compute(), expected1)
def test_apply_parallel(): import dask.array as da # data a = np.arange(144).reshape(12, 12).astype(float) # apply the filter expected1 = threshold_local(a, 3) result1 = apply_parallel(threshold_local, a, chunks=(6, 6), depth=5, extra_arguments=(3,), extra_keywords={'mode': 'reflect'}) assert_array_almost_equal(result1, expected1) def wrapped_gauss(arr): return gaussian(arr, 1, mode='reflect') expected2 = gaussian(a, 1, mode='reflect') result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5) assert_array_almost_equal(result2, expected2) expected3 = gaussian(a, 1, mode='reflect') result3 = apply_parallel( wrapped_gauss, da.from_array(a, chunks=(6, 6)), depth=5, compute=True ) assert isinstance(result3, np.ndarray) assert_array_almost_equal(result3, expected3)
def test_apply_parallel_rgb_channel_axis(depth, chunks, channel_axis): """Test channel_axis combinations. For depth and chunks, test in three ways: 1.) scalar (to be applied over all axes) 2.) tuple of length ``image.ndim - 1`` corresponding to spatial axes 3.) tuple of length ``image.ndim`` corresponding to all axes """ cat = img_as_float(data.chelsea()) func = color.rgb2ycbcr cat_ycbcr_expected = func(cat, channel_axis=-1) # move channel axis to another position cat = np.moveaxis(cat, -1, channel_axis) if chunks == 'ndim': # explicitly specify the chunksize for the channel axis chunks = [128, 128] chunks.insert(channel_axis % cat.ndim, cat.shape[channel_axis]) if depth == 'ndim': # explicitly specify the depth for the channel axis depth = [8, 8] depth.insert(channel_axis % cat.ndim, 0) cat_ycbcr = apply_parallel(func, cat, chunks=chunks, depth=depth, dtype=cat.dtype, channel_axis=channel_axis, extra_keywords=dict(channel_axis=channel_axis)) # move channels of output back to the last dimension cat_ycbcr = np.moveaxis(cat_ycbcr, channel_axis, -1) assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr)
def test_apply_parallel_wrap(): def wrapped(arr): return gaussian(arr, 1, mode='wrap') a = np.arange(144).reshape(12, 12).astype(float) expected = gaussian(a, 1, mode='wrap') result = apply_parallel(wrapped, a, chunks=(6, 6), depth=5, mode='wrap') assert_array_almost_equal(result, expected)
def test_apply_parallel_nearest(): def wrapped(arr): return gaussian_filter(arr, 1, mode='nearest') a = np.arange(144).reshape(12, 12).astype(float) expected = gaussian_filter(a, 1, mode='nearest') result = apply_parallel(wrapped, a, chunks=(6, 6), depth={0: 5, 1: 5}, mode='nearest') assert_array_almost_equal(result, expected)
def test_no_chunks(): a = np.ones(1 * 4 * 8 * 9).reshape(1, 4, 8, 9) def add_42(arr): return arr + 42 expected = add_42(a) result = apply_parallel(add_42, a) assert_array_almost_equal(result, expected)
def test_apply_parallel(): # data a = np.arange(144).reshape(12, 12).astype(float) # apply the filter expected1 = threshold_adaptive(a, 3) result1 = apply_parallel(threshold_adaptive, a, chunks=(6, 6), depth=5, extra_arguments=(3,), extra_keywords={'mode': 'reflect'}) assert_array_almost_equal(result1, expected1) def wrapped_gauss(arr): return gaussian(arr, 1, mode='reflect') expected2 = gaussian(a, 1, mode='reflect') result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5) assert_array_almost_equal(result2, expected2)
def test_apply_parallel(): # data a = np.arange(144).reshape(12, 12).astype(float) # apply the filter expected1 = threshold_adaptive(a, 3) result1 = apply_parallel(threshold_adaptive, a, chunks=(6, 6), depth=5, extra_arguments=(3, ), extra_keywords={'mode': 'reflect'}) assert_array_almost_equal(result1, expected1) def wrapped_gauss(arr): return gaussian(arr, 1, mode='reflect') expected2 = gaussian(a, 1, mode='reflect') result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5) assert_array_almost_equal(result2, expected2)
def test_apply_parallel_rgb(depth, chunks, dtype): cat = data.chelsea().astype(dtype) / 255. func = color.rgb2ycbcr cat_ycbcr_expected = func(cat) cat_ycbcr = apply_parallel(func, cat, chunks=chunks, depth=depth, dtype=dtype, multichannel=True) assert_equal(cat_ycbcr.dtype, cat.dtype) assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr)
def test_apply_parallel_nearest(): def wrapped(arr): return gaussian_filter(arr, 1, mode='nearest') a = np.arange(144).reshape(12, 12).astype(float) expected = gaussian_filter(a, 1, mode='nearest') result = apply_parallel(wrapped, a, chunks=(6, 6), depth={ 0: 5, 1: 5 }, mode='nearest') assert_array_almost_equal(result, expected)