def test_compressed_segmentation_roundtrip_uint64(block_size): encoder = CompressedSegmentationEncoder("uint64", 1, block_size) test_chunk = (np.arange(64 * 64 * 63, dtype="<Q").reshape(1, 64, 64, 63) + 560328569340672) buf = encoder.encode(test_chunk) decoded_chunk = encoder.decode(buf, (63, 64, 64)) assert np.array_equal(decoded_chunk, test_chunk)
def test_compressed_segmentation_roundtrip_uint32(block_size): encoder = CompressedSegmentationEncoder("uint32", 1, block_size) test_chunk = (np.arange(11 * 50 * 64, dtype="<I").reshape(1, 64, 50, 11) + 75621928) buf = encoder.encode(test_chunk) decoded_chunk = encoder.decode(buf, (11, 50, 64)) assert np.array_equal(decoded_chunk, test_chunk)
def test_compressed_segmentation_8bit_roundtrip(): encoder = CompressedSegmentationEncoder("uint32", 1, [8, 8, 8]) test_chunk = ( np.arange(14 * 5 * 8, dtype="<I").reshape(1, 8, 5, 14) % 256 + 452435) buf = encoder.encode(test_chunk) decoded_chunk = encoder.decode(buf, (14, 5, 8)) assert np.array_equal(decoded_chunk, test_chunk)
def test_cseg_decoder_invalid_data_in_non_0bit_encoding(): encoder = CompressedSegmentationEncoder("uint32", 1, [8, 8, 8]) test_chunk = np.reshape(np.arange(11, dtype="uint32"), (1, 1, 1, 11)) buf = encoder.encode(test_chunk) # truncated encoded values for non-0bit encoding with pytest.raises(InvalidFormatError): encoder.decode(buf[:-1], (1, 1, 11)) # truncated encoded values for non-0bit encoding with pytest.raises(InvalidFormatError): encoder.decode(buf[:-1], (1, 1, 11)) # Manipulate the LUT offset so that LUT indices are past the end lut_offset_position = 4 lut_offset_and_bits = struct.unpack_from("<I", buf, lut_offset_position)[0] lut_offset_and_bits = ((lut_offset_and_bits & 0xFF000000) | (len(buf) // 4 - 2)) struct.pack_into("<I", buf, lut_offset_position, lut_offset_and_bits) with pytest.raises(InvalidFormatError): encoder.decode(buf, (1, 1, 11))
def test_cseg_decoder_invalid_data(): encoder = CompressedSegmentationEncoder("uint32", 2, [8, 8, 8]) buf = encoder.encode(np.ones((2, 1, 1, 11), dtype="uint32")) # file too short even for all-0bit encoding with pytest.raises(InvalidFormatError): encoder.decode(buf[:39], (1, 1, 11)) # channel offset too large for the file size with pytest.raises(InvalidFormatError): encoder.decode(buf[:40], (1, 1, 11)) # truncated lookup table for 0-bit encoding with pytest.raises(InvalidFormatError): encoder.decode(buf[:-1], (1, 1, 11)) buf[11] = 255 # invalid number of encoding bits with pytest.raises(InvalidFormatError): encoder.decode(buf, (1, 1, 11))
def test_compressed_segmentation_1bit_roundtrip(): encoder = CompressedSegmentationEncoder("uint32", 1, [8, 8, 8]) test_chunk = np.arange(8 * 8 * 8, dtype="<I").reshape(1, 8, 8, 8) % 2 + 37 buf = encoder.encode(test_chunk) decoded_chunk = encoder.decode(buf, (8, 8, 8)) assert np.array_equal(decoded_chunk, test_chunk)
def test_compressed_segmentation_0bit_roundtrip(): encoder = CompressedSegmentationEncoder("uint32", 1, [8, 8, 8]) test_chunk = np.zeros((1, 2, 3, 1), dtype="<I") + 37 buf = encoder.encode(test_chunk) decoded_chunk = encoder.decode(buf, (1, 3, 2)) assert np.array_equal(decoded_chunk, test_chunk)
def test_compressed_segmentation_invalid_data_type(): with pytest.raises(IncompatibleEncoderError): CompressedSegmentationEncoder("uint8", 1, [8, 8, 8])