def test_basic_assign(app_inst: ArrayApplication): from_arr: np.ndarray = np.arange(5) block_shape = 3, slice_params = list( get_slices(size=10, index_multiplier=2, basic_step=True)) pbar = tqdm.tqdm(total=len(slice_params)) for slice_sel in slice_params: pbar.set_description(str(slice_sel)) pbar.update(1) from_ba = app_inst.array(from_arr, block_shape=block_shape) from_bav = ArrayView.from_block_array(from_ba) to_arr: np.ndarray = np.zeros(5) to_ba = app_inst.array(to_arr, block_shape=block_shape) to_bav = ArrayView.from_block_array(to_ba) to_bav[slice_sel] = from_bav[slice_sel] to_arr[slice_sel] = from_arr[slice_sel] from_res = (from_arr, from_bav.create().get()) assert np.allclose(*from_res), str(from_res) to_res = (to_arr, to_bav.create().get()) assert np.allclose(*to_res), str(to_res)
def __getitem__(self, item): if not isinstance(item, tuple): ss = (item,) else: ss = item # We need to fetch any block arrays. tmp = [] for entry in ss: if isinstance(entry, BlockArray): tmp.append(entry.get()) else: tmp.append(entry) ss = tmp is_handled_advanced = True if len(ss) > 1: # Check if all entries are full slices except the last entry. for entry in ss[:-1]: is_handled_advanced = is_handled_advanced and (isinstance(entry, slice) and entry.start is None and entry.stop is None) if is_handled_advanced and selection.is_advanced_selection((ss[-1],)): # Treat this as a shuffle. return self._advanced_single_array_subscript(sel=(ss[-1],), axis=len(ss)-1) av: ArrayView = ArrayView.from_block_array(self) # TODO (hme): We don't have to create, but do so for now until we need to optimize. return av[item].create(BlockArray)
def test_assignment(left_item, left_mode, right_item, right_mode): lshape, lblock_shape, laccessor = left_item laccessor = tuple(left_mode[i](*laccessor[i]) for i in range(num_axes)) rshape, rblock_shape, raccessor = right_item raccessor = tuple(right_mode[i](*raccessor[i]) for i in range(num_axes)) if not is_broadcastable(lshape, laccessor, rshape, raccessor): return False npA = np.zeros(np.product(lshape)).reshape(*lshape) npB = np.random.random_sample(np.product(rshape)).reshape(*rshape) baA = app_inst.array(npA, block_shape=lblock_shape) baB = app_inst.array(npB, block_shape=rblock_shape) bavA = ArrayView.from_block_array(baA) bavB = ArrayView.from_block_array(baB) assert np.allclose(npA[laccessor], bavA[laccessor].create().get()) assert np.allclose(npB[raccessor], bavB[raccessor].create().get()) npA[laccessor] = npB[raccessor] bavA[laccessor] = bavB[raccessor] assert np.allclose(npA, bavA.create().get()) assert np.allclose(npB, bavB.create().get()) return True
def test_basic_select(app_inst: ArrayApplication): arr: np.ndarray = np.arange(5) block_shape = 3, slice_params = list(get_slices(size=10, index_multiplier=2, basic_step=True)) pbar = tqdm.tqdm(total=len(slice_params)) for slice_sel in slice_params: pbar.set_description(str(slice_sel)) pbar.update(1) ba = app_inst.array(arr, block_shape=block_shape) bav = ArrayView.from_block_array(ba) res = (arr[slice_sel], bav[slice_sel].create().get()) assert np.allclose(*res), str(res)
def __setitem__(self, key, value): av: ArrayView = ArrayView.from_block_array(self) av[key] = value
def __getitem__(self, item): av: ArrayView = ArrayView.from_block_array(self) # TODO (hme): We don't have to create, but do so for now until we need to optimize. return av[item].create(BlockArray)