def test_error_when_unsupported_dim(self): arr1 = RowMajorArray(np.random.rand(4, 4, 4)) arr2 = RowMajorArray(np.random.rand(4, 4, 4)) with pytest.raises(AssertionError) as err: matmul_rec(arr1, arr2, 10) msg = "Arrays should be 2D but got dimensions 3 and 3" assert err.value.args[0] == msg
def test_error_when_not_same_shape(self): arr1 = RowMajorArray(np.random.rand(4, 4)) arr2 = RowMajorArray(np.random.rand(8, 8)) with pytest.raises(AssertionError) as err: matmul_rec(arr1, arr2, 10) msg = "Arrays should be same shape but got (4, 4) and (8, 8)" assert err.value.args[0] == msg
def test_same_as_np_basic(self): arr1 = np.random.rand(16, 16) arr2 = np.random.rand(16, 16) res_np = arr1 @ arr2 # Will use simple matmul straight away when rec depth is 0 res = matmul_rec(RowMajorArray(arr1), RowMajorArray(arr2), 0) np.testing.assert_almost_equal(res_np, res.to_numpy())
def test_internal_index_correct_3d(self): row_arr = RowMajorArray(shape=(8, 8, 8)) assert row_arr.internal_index(0, 0, 0) == 0 assert row_arr.internal_index(0, 0, 1) == 1 assert row_arr.internal_index(1, 1, 1) == 8**2 + 8 + 1 assert row_arr.internal_index(7, 3, 3) == 7 * 8**2 + 3 * 8 + 3 assert row_arr.internal_index(7, 4, 4) == 7 * 8**2 + 4 * 8 + 4 assert row_arr.internal_index(7, 7, 7) == 8**3 - 1
def test_3d(self): arr = rand_complex_array(4, 4, 4) res = fftn(RowMajorArray(arr)) res_scipy = fftn_scipy(arr) np.testing.assert_almost_equal(res_scipy, res.to_numpy()) arr = rand_complex_array(16, 16, 16) res = fftn(RowMajorArray(arr)) res_scipy = fftn_scipy(arr) np.testing.assert_almost_equal(res_scipy, res.to_numpy())
def test_error_when_unsupported_dim(self): arr = RowMajorArray(np.random.rand(4, 4, 4, 4)) with pytest.raises(AssertionError) as err: convolution(arr, np.ones((3, 3, 3, 3))) msg = "Convolution for pictures of dim 4 not implemented" assert err.value.args[0] == msg arr = RowMajorArray(np.random.rand(4, 4, 4, 4, 4)) with pytest.raises(AssertionError) as err: convolution(arr, np.ones((3, 3, 3, 3, 3))) msg = "Convolution for pictures of dim 5 not implemented" assert err.value.args[0] == msg
def test_invalid_index_3d(self): row_arr = RowMajorArray(shape=(16, 16, 16)) assert not row_arr.valid_index(5) assert not row_arr.valid_index(5, 5) assert not row_arr.valid_index(5, 5, 5, 5) assert not row_arr.valid_index(-1, -1, -1) assert not row_arr.valid_index(16, 16, 16) assert not row_arr.valid_index(0, 0, 0, pad=1) assert not row_arr.valid_index(15, 15, 15, pad=1)
def test_internal_index_invalid_arg_3d(self): row_arr = RowMajorArray(shape=(8, 8, 8)) with pytest.raises(TypeError) as _: row_arr.internal_index(1) with pytest.raises(TypeError) as _: row_arr.internal_index(1, 1) with pytest.raises(TypeError) as _: row_arr.internal_index(1, 1, 1, 1)
def test_iter_keys_3d(self): row_arr = RowMajorArray(shape=(2, 2, 2)) assert list(row_arr.iter_keys()) == [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)] row_arr = RowMajorArray(shape=(8, 8, 8)) assert len(list(row_arr.iter_keys())) == 8**3
def test_fill(self): row_arr = RowMajorArray([[False, False], [False, False]]) for key in row_arr.iter_keys(): assert not row_arr[key] row_arr.fill(True, dtype=bool) for key in row_arr.iter_keys(): assert row_arr[key]
def test_valid_index_3d(self): row_arr = RowMajorArray(shape=(16, 16, 16)) assert row_arr.valid_index(0, 0, 0) assert row_arr.valid_index(8, 7, 4) assert row_arr.valid_index(15, 15, 15) assert row_arr.valid_index(1, 1, 1, pad=1) assert row_arr.valid_index(14, 14, 14, pad=1)
def test_iter_keys_2d(self): row_arr = RowMajorArray(shape=(4, 4)) assert list(row_arr.iter_keys()) == [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)] row_arr = RowMajorArray(shape=(32, 32)) assert len(list(row_arr.iter_keys())) == 32**2
def test_to_numpy(self): vals = np.random.rand(8, 8) row_arr = RowMajorArray(vals) np.testing.assert_almost_equal(vals, row_arr.to_numpy()) vals = np.random.rand(8, 8, 8) row_arr = RowMajorArray(vals) np.testing.assert_almost_equal(vals, row_arr.to_numpy())
def test_same_internal_acces_pattern_yields_same_stats(self): vals = np.random.rand(16, 16, 16) morton = MortonOrder(vals, cache=make_cache()) block_arr = BlockArray(vals, cache=make_cache()) row_arr = RowMajorArray(vals, cache=make_cache()) stats = [] for data in [morton, block_arr, row_arr]: for is_warm_up in [True, False]: res = data.empty_of_same_shape() for key in data.iter_keys(): res[key] = 2 * data[key] data.cache.force_write_back() if not is_warm_up: stats.append(list(data.cache.stats())) data.cache.reset_stats() assert stats[0] == stats[1] and stats[1] == stats[2]
def test_same_as_np_depth(self): # Just set depth high so we know we will get to max depth r = 100 arr1 = np.random.rand(4, 4) arr2 = np.random.rand(4, 4) res_np = arr1 @ arr2 res = matmul_rec(RowMajorArray(arr1), RowMajorArray(arr2), r) np.testing.assert_almost_equal(res_np, res.to_numpy()) arr1 = np.random.rand(16, 16) arr2 = np.random.rand(16, 16) res_np = arr1 @ arr2 res = matmul_rec(RowMajorArray(arr1), RowMajorArray(arr2), r) np.testing.assert_almost_equal(res_np, res.to_numpy()) arr1 = np.random.rand(32, 32) arr2 = np.random.rand(32, 32) res_np = arr1 @ arr2 res = matmul_rec(RowMajorArray(arr1), RowMajorArray(arr2), r) np.testing.assert_almost_equal(res_np, res.to_numpy())
# Set to true if you want to run the tests again. # Otherwise just loads results from JSON GENERATE_NEW_RESULTS = True # Set to true if you want to save figures to disk. Change path as needed SAVE_FIGURES_TO_DISK = True FIG_SAVE_PATH = "../../thesis/figures/props/" # Global constants for test. Change these if you want to run another test MAX_PROP_LEVEL = 32 SHAPE = (128, 128, 128) MID = SHAPE[0] // 2 START_POINTS = [(0, 0, 0), (MID, MID, MID)] DATA_ARRS = [ MortonOrder(shape=SHAPE), RowMajorArray(shape=SHAPE), BlockArray(shape=SHAPE) ] # Global constants used for type-safety when accessing properties in # dictionary. Do not change INF = float("inf") XS, YS1, YS2 = "xs", "ys1", "ys2" META, MAX_PROP_LEVEL_STR, SHAPE_STR = "meta", "max_prop_level", "shape" sns.set(font_scale=1.9) matplotlib.rcParams['figure.figsize'] = (1.1*18.0, 1.1*4.8) def indices_for_prop_level_2d(prop_level: int, start_point: tuple) -> set: """
def test_empty_of_same_shape(self): vals = np.random.rand(8, 8) row_arr1 = RowMajorArray(vals) row_arr2 = row_arr1.empty_of_same_shape() # All 0's in same shape assert row_arr1.shape == row_arr2.shape np.testing.assert_almost_equal(np.zeros((8, 8)), row_arr2.to_numpy())
def test_bit_rev_copy(self): arr = [0, 1, 2, 3, 4, 5, 6, 7] expected = np.array([0, 4, 2, 6, 1, 5, 3, 7]) res = bit_rev_copy(RowMajorArray(arr), 0) np.testing.assert_almost_equal(expected, res.to_numpy())
def test_setting_to_3d_pic(self): vals = np.random.rand(4, 4, 4) row_arr = RowMajorArray(picture=vals) assert row_arr.dim == 3 assert row_arr.shape == vals.shape np.testing.assert_almost_equal(vals, row_arr.to_numpy())
def test_same_as_scipy_3d(self): a = np.ones((8, 8, 8)) sci = ndimage.convolve(a, self.kernel_3d, mode='constant', cval=3.0) own = convolution(RowMajorArray(a), self.kernel_3d, cval=3.0) np.testing.assert_almost_equal(sci, own.to_numpy())