def test_set_slice_range(): """ Sets a slice across a range of one of the dimensions. """ stack = synthetic_stack() zrange = slice(1, 3) Y, X = stack.tile_shape expected = np.ones((stack.shape[Indices.HYB], stack.shape[Indices.CH], zrange.stop - zrange.start, Y, X)) * 10 index = {Indices.Z: zrange} stack.set_slice(index, expected) assert np.array_equal(stack.get_slice(index)[0], expected)
def test_set_slice_middle_index(): """ Sets a slice across one of the indices in the middle. For instance, if the dimensions are (P, Q0,..., Qn-1, R), slice across one of the Q axes. """ stack = synthetic_stack() ch = 1 Y, X = stack.tile_shape expected = np.ones( (stack.shape[Indices.HYB], stack.shape[Indices.Z], Y, X)) * 2 index = {Indices.CH: ch} stack.set_slice(index, expected) assert np.array_equal(stack.get_slice(index)[0], expected)
def test_set_slice_simple_index(): """ Sets a slice across one of the indices at the end. For instance, if the dimensions are (P, Q0,..., Qn-1, R), sets a slice across either P or R. """ stack = synthetic_stack() hyb = 1 Y, X = stack.tile_shape expected = np.ones( (stack.shape[Indices.CH], stack.shape[Indices.Z], Y, X)) * 2 index = {Indices.HYB: hyb} stack.set_slice(index, expected) assert np.array_equal(stack.get_slice(index)[0], expected)
def test_conflict(): """ Tiles that have extras that conflict with indices should produce an error. """ def tile_extras_provider(hyb: int, ch: int, z: int) -> Any: return { Indices.HYB: hyb, Indices.CH: ch, Indices.Z: z, } stack = synthetic_stack( tile_extras_provider=tile_extras_provider, ) with pytest.raises(ValueError): stack.tile_metadata
def test_get_slice_range(): """ Retrieve a slice across a range of one of the dimensions. """ stack = synthetic_stack() zrange = slice(1, 3) imageslice, axes = stack.get_slice({Indices.Z: zrange}) Y, X = stack.tile_shape assert axes == [Indices.HYB, Indices.CH, Indices.Z] for hyb in range(stack.shape[Indices.HYB]): for ch in range(stack.shape[Indices.CH]): for z in range(zrange.stop - zrange.start): data = np.empty((Y, X)) data.fill((hyb * stack.shape[Indices.CH] + ch) * stack.shape[Indices.Z] + (z + zrange.start)) assert data.all() == imageslice[hyb, ch, z].all()
def test_metadata(): """ Normal situation where all the tiles have uniform keys for both indices and extras. """ def tile_extras_provider(hyb: int, ch: int, z: int) -> Any: return { 'random_key': { Indices.HYB: hyb, Indices.CH: ch, Indices.Z: z, } } stack = synthetic_stack( tile_extras_provider=tile_extras_provider, ) table = stack.tile_metadata assert len(table) == DEFAULT_NUM_HYB * DEFAULT_NUM_CH * DEFAULT_NUM_Z
def test_get_slice_simple_index(): """ Retrieve a slice across one of the indices at the end. For instance, if the dimensions are (P, Q0,..., Qn-1, R), slice across either P or R. """ stack = synthetic_stack() hyb = 1 imageslice, axes = stack.get_slice({Indices.HYB: hyb}) assert axes == [Indices.CH, Indices.Z] Y, X = stack.tile_shape for ch in range(stack.shape[Indices.CH]): for z in range(stack.shape[Indices.Z]): data = np.empty((Y, X)) data.fill((hyb * stack.shape[Indices.CH] + ch) * stack.shape[Indices.Z] + z) assert data.all() == imageslice[ch, z].all()
def test_get_slice_middle_index(): """ Retrieve a slice across one of the indices in the middle. For instance, if the dimensions are (P, Q0,..., Qn-1, R), slice across one of the Q axes. """ stack = synthetic_stack() ch = 1 imageslice, axes = stack.get_slice({Indices.CH: ch}) assert axes == [Indices.HYB, Indices.Z] Y, X = stack.tile_shape for hyb in range(stack.shape[Indices.HYB]): for z in range(stack.shape[Indices.Z]): data = np.empty((Y, X)) data.fill((hyb * stack.shape[Indices.CH] + ch) * stack.shape[Indices.Z] + z) assert data.all() == imageslice[hyb, z].all()
def test_missing_extras(): """ If the extras are not present on some of the tiles, it should still work. """ def tile_extras_provider(hyb: int, ch: int, z: int) -> Any: if hyb == 0: return { 'random_key': { Indices.HYB: hyb, Indices.CH: ch, Indices.Z: z, } } else: return None stack = synthetic_stack( tile_extras_provider=tile_extras_provider, ) table = stack.tile_metadata assert len(table) == DEFAULT_NUM_HYB * DEFAULT_NUM_CH * DEFAULT_NUM_Z
def test_apply_3d(): """test that apply correctly applies a simple function across 3d volumes of a Stack""" stack = synthetic_stack() assert np.all(stack.numpy_array == 1) stack.apply(multiply, value=4, is_volume=True) assert (stack.numpy_array == 4).all()
def test_apply(): """test that apply correctly applies a simple function across 2d tiles of a Stack""" stack = synthetic_stack() assert (stack.numpy_array == 1).all() stack.apply(multiply, value=2) assert (stack.numpy_array == 2).all()