Skip to content

Commit 16b5571

Browse files
edyoshikunziw-liu
andauthored
Add decompressed OME-Zarr dataset size to iohub info (#248)
* adding datastore size to info * adding uncompressed string * adding changes for readability * typo * Only show decompressed size due to zarr-python bug * add test for size formatting * add test for CLI size info --------- Co-authored-by: Ziwen Liu <67518483+ziw-liu@users.noreply.github.com>
1 parent 0b301a4 commit 16b5571

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

iohub/reader.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,23 @@ def print_info(path: StrOrBytesPath, verbose=False):
262262
print("Zarr hierarchy:")
263263
reader.print_tree()
264264
positions = list(reader.positions())
265+
total_bytes_uncompressed = sum(
266+
p["0"].nbytes for _, p in positions
267+
)
265268
msgs.append(f"Positions:\t\t {len(positions)}")
266269
msgs.append(f"Chunk size:\t\t {positions[0][1][0].chunks}")
270+
msgs.append(
271+
f"No. bytes decompressed:\t\t {total_bytes_uncompressed} "
272+
f"[{sizeof_fmt(total_bytes_uncompressed)}]"
273+
)
267274
else:
275+
total_bytes_uncompressed = reader["0"].nbytes
268276
msgs.append(f"(Z, Y, X) scale (um):\t {tuple(reader.scale[2:])}")
269277
msgs.append(f"Chunk size:\t\t {reader['0'].chunks}")
278+
msgs.append(
279+
f"No. bytes decompressed:\t\t {total_bytes_uncompressed} "
280+
f"[{sizeof_fmt(total_bytes_uncompressed)}]"
281+
)
270282
if verbose:
271283
msgs.extend(
272284
[
@@ -280,3 +292,18 @@ def print_info(path: StrOrBytesPath, verbose=False):
280292
reader.print_tree()
281293
print("\n".join(msgs))
282294
reader.close()
295+
296+
297+
def sizeof_fmt(num: int) -> str:
298+
"""
299+
Human readable file size
300+
Adapted form:
301+
https://web.archive.org/web/20111010015624/
302+
http://blogmag.net/blog/read/38/Print_human_readable_file_size
303+
"""
304+
if num < 1024:
305+
return f"{num} B"
306+
for x in ["KiB", "MiB", "GiB", "TiB"]:
307+
num /= 1024
308+
if num < 1024:
309+
return f"{num:.1f} {x}"

tests/cli/test_cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ def test_cli_info_ome_zarr(verbose):
9090
assert result.exit_code == 0
9191
assert re.search(r"Wells:\s+1", result.output)
9292
assert ("Chunk size" in result.output) == bool(verbose)
93+
assert ("No. bytes decompressed" in result.output) == bool(verbose)
9394
# Test on single position
9495
result_pos = runner.invoke(cli, ["info", str(hcs_ref / "B" / "03" / "0")])
9596
assert "Channel names" in result_pos.output
9697
assert "scale (um)" in result_pos.output
9798
assert "Chunk size" in result_pos.output
99+
assert "84.4 MiB" in result_pos.output
98100

99101

100102
@pytest.mark.parametrize("grid_layout", ["-g", None])

tests/test_reader.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from iohub._deprecated.singlepagetiff import MicromanagerSequenceReader
44
from iohub.mmstack import MMStack
55
from iohub.ndtiff import NDTiffDataset
6-
from iohub.reader import read_images
6+
from iohub.reader import read_images, sizeof_fmt
77
from tests.conftest import (
88
mm2gamma_ome_tiffs,
99
mm2gamma_singlepage_tiffs,
@@ -36,3 +36,11 @@ def test_detect_ndtiff(data_path):
3636
def test_detect_single_page_tiff(data_path):
3737
reader = read_images(data_path)
3838
assert isinstance(reader, MicromanagerSequenceReader)
39+
40+
41+
@pytest.mark.parametrize(
42+
"num_bytes,expected",
43+
[(3, "3 B"), (2.234 * 2**20, "2.2 MiB"), (3.456 * 2**40, "3.5 TiB")],
44+
)
45+
def test_sizeof_fmt(num_bytes, expected):
46+
assert sizeof_fmt(num_bytes) == expected

0 commit comments

Comments
 (0)