def test_multiple_tiles_of_different_kind(): with pytest.raises(TypeError): ImageStack.synthetic_stack( NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH, tile_data_provider=create_tile_data_provider( np.uint32, np.float32), )
def labeled_synthetic_dataset(): stp = synthesize.SyntheticSpotTileProvider() image = ImageStack.synthetic_stack(tile_data_provider=stp.tile) max_proj = image.max_proj(Indices.HYB, Indices.CH, Indices.Z) view = max_proj.reshape((1, 1, 1) + max_proj.shape) dots = ImageStack.from_numpy_array(view) def labeled_synthetic_dataset_factory(): return deepcopy(image), deepcopy(dots), deepcopy(stp.codebook) return labeled_synthetic_dataset_factory
def test_multiple_tiles_of_same_dtype(): stack = ImageStack.synthetic_stack( NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH, tile_data_provider=create_tile_data_provider(np.uint32, np.uint32), ) expected = np.ones((NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH), dtype=np.uint32) assert np.array_equal(stack.numpy_array, expected)
def test_set_slice_range(): """ Sets a slice across a range of one of the dimensions. """ stack = ImageStack.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 = ImageStack.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 = ImageStack.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_float_type_promotion(): with warnings.catch_warnings(record=True) as w: stack = ImageStack.synthetic_stack( NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH, tile_data_provider=create_tile_data_provider( np.float64, np.float32), ) assert len(w) == 1 assert issubclass(w[0].category, DataFormatWarning) expected = np.ones((NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH), dtype=np.float64) assert np.array_equal(stack.numpy_array, 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 = ImageStack.synthetic_stack( num_hyb=NUM_HYB, num_ch=NUM_CH, num_z=NUM_Z, tile_extras_provider=tile_extras_provider, ) with pytest.raises(ValueError): stack.tile_metadata
def test_int_type_promotion(): with warnings.catch_warnings(record=True) as w: stack = ImageStack.synthetic_stack( NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH, tile_data_provider=create_tile_data_provider(np.int32, np.int8), ) assert len(w) == 1 assert issubclass(w[0].category, DataFormatWarning) expected = np.ones((NUM_HYB, NUM_CH, NUM_Z, HEIGHT, WIDTH), dtype=np.int32) corner = np.empty((HEIGHT, WIDTH), dtype=np.int32) corner.fill(16777216) expected[0, 0, 0] = corner assert np.array_equal(stack.numpy_array, expected)
def test_get_slice_range(): """ Retrieve a slice across a range of one of the dimensions. """ stack = ImageStack.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 = ImageStack.synthetic_stack( num_hyb=NUM_HYB, num_ch=NUM_CH, num_z=NUM_Z, tile_extras_provider=tile_extras_provider, ) table = stack.tile_metadata assert len(table) == NUM_HYB * NUM_CH * NUM_Z
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 = ImageStack.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_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 = ImageStack.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_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 = ImageStack.synthetic_stack( num_hyb=NUM_HYB, num_ch=NUM_CH, num_z=NUM_Z, tile_extras_provider=tile_extras_provider, ) table = stack.tile_metadata assert len(table) == NUM_HYB * NUM_CH * NUM_Z
def test_apply_3d(): """test that apply correctly applies a simple function across 3d volumes of a Stack""" stack = ImageStack.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 = ImageStack.synthetic_stack() assert (stack.numpy_array == 1).all() stack.apply(multiply, value=2) assert (stack.numpy_array == 2).all()