From 61c16c8423db6acdae0a41d4ebdd0bc990508fc4 Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Fri, 1 Nov 2024 13:56:57 +0000 Subject: [PATCH 1/2] use log10 for zero padding tiffs --- brainglobe_utils/IO/image/save.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainglobe_utils/IO/image/save.py b/brainglobe_utils/IO/image/save.py index c18f9b5..0b03575 100644 --- a/brainglobe_utils/IO/image/save.py +++ b/brainglobe_utils/IO/image/save.py @@ -89,7 +89,7 @@ def to_tiffs(img_volume, path_prefix, path_suffix="", extension=".tif"): path_prefix = str(path_prefix.resolve()) z_size = img_volume.shape[0] - pad_width = int(round(z_size / 10)) + 1 + pad_width = int(np.floor(np.log10(z_size)) + 1) for i in range(z_size): img = img_volume[i, :, :] dest_path = ( From 7101ed1538af8975599bad15f7d81c3759d294d8 Mon Sep 17 00:00:00 2001 From: alessandrofelder Date: Fri, 1 Nov 2024 14:16:32 +0000 Subject: [PATCH 2/2] test zero padding --- tests/tests/test_IO/test_image_io.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/tests/test_IO/test_image_io.py b/tests/tests/test_IO/test_image_io.py index c888722..dc873ec 100644 --- a/tests/tests/test_IO/test_image_io.py +++ b/tests/tests/test_IO/test_image_io.py @@ -7,7 +7,7 @@ import pytest import tifffile -from brainglobe_utils.IO.image import load, save, utils +from brainglobe_utils.IO.image import load, save, to_tiffs, utils @pytest.fixture() @@ -435,3 +435,16 @@ def test_read_with_dask_raises(tmp_path): with pytest.raises(ValueError) as e: load.read_with_dask(tmp_path) assert e.match("not contain any .tif or .tiff files") + + +@pytest.mark.parametrize( + "z_size, expected_length", + [(10000, 5), (9999, 4), (1, 1)], +) +def test_to_tiffs_padwidth(tmp_path, z_size, expected_length): + volume = np.ones((z_size, 1, 1)) + prefix = "test" + to_tiffs(volume, tmp_path / prefix) + image_path = list(tmp_path.glob("*.tif"))[0] + assert str(image_path.stem).split("_")[0] == prefix + assert len(str(image_path.stem).split("_")[1]) == expected_length