Created
January 9, 2026 10:47
-
-
Save almarklein/37a94bd10b05cd5ecaa506f73a146689 to your computer and use it in GitHub Desktop.
label_compression.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Quick benchmark to investigate compression of labels: separate arrays vs bitpacked. | |
| """ | |
| import imageio.v3 as iio | |
| import numcodecs | |
| filename_base = os.path.abspath(os.path.join(__file__, "..", "tmp", "astronaut")) | |
| im = iio.imread("imageio:astronaut.png") | |
| data = im[:, :, 2].copy() | |
| masks = [] | |
| masks.append(im[:, :, 0] > 230) | |
| masks.append(im[:, :, 0] < 20) | |
| masks.append(im[:, :, 1] > 230) | |
| masks.append(im[:, :, 1] < 20) | |
| masks.append(im[:, :, 2] > 230) | |
| masks.append(im[:, :, 2] < 20) | |
| mask_dtype = np.uint16 | |
| combined_mask = np.zeros_like(mask1, dtype=mask_dtype) | |
| for i, mask in enumerate(masks): | |
| combined_mask |= mask.astype(mask_dtype) * 2**i | |
| bb = numcodecs.blosc.compress(data, b"zstd", 9) | |
| print(f"data {100 * len(bb) / arr.size:0.1f} %") | |
| bb = numcodecs.blosc.compress(combined_mask, b"zstd", 9) | |
| print(f"combined_mask {100 * len(bb) / combined_mask.size:0.1f} %") | |
| total_mask_size_compressed = 0 | |
| for mask in masks: | |
| bb = numcodecs.blosc.compress(arr, b"zstd", 9) | |
| total_mask_size_compressed += len(bb) | |
| print(f"loose masks {100 * total_mask_size_compressed / combined_mask.size:0.1f} %") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment