Skip to content

Commit 379487b

Browse files
authored
Merge pull request #101 from lguerard/add_byte_conversion
Add function to convert size from bytes to a more readable format
2 parents 7a0be28 + 6e7b2fe commit 379487b

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

src/imcflibs/imagej/misc.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010

1111
from ij import IJ # pylint: disable-msg=import-error
1212
from ij.plugin import Duplicator, ImageCalculator, StackWriter
13+
from org.scijava.widget import TextWidget, WidgetStyle
1314

1415
from .. import pathtools
1516
from ..log import LOG as log
1617
from . import bioformats as bf
1718
from . import prefs
1819

19-
from org.scijava.widget import WidgetStyle
20-
from org.scijava.widget import TextWidget
21-
2220

2321
def show_status(msg):
2422
"""Update the ImageJ status bar and issue a log message.
@@ -533,7 +531,9 @@ def write_ordereddict_to_csv(out_file, content):
533531
dict_writer.writerows(content)
534532

535533

536-
def save_image_in_format(imp, format, out_dir, series, pad_number, split_channels, suffix=""):
534+
def save_image_in_format(
535+
imp, format, out_dir, series, pad_number, split_channels, suffix=""
536+
):
537537
"""Save an ImagePlus object in the specified format.
538538
539539
This function provides flexible options for saving ImageJ images in various
@@ -737,3 +737,32 @@ def run_imarisconvert(file_path, pixel_calibration=None, output_folder=""):
737737
else:
738738
timed_log("Error converting [%s]: %d" % (file_path, result))
739739

740+
741+
def bytes_to_human_readable(size):
742+
"""Convert a byte count to a human-readable string using binary units.
743+
744+
Parameters
745+
----------
746+
size : int
747+
Byte size (number of bytes).
748+
749+
Returns
750+
-------
751+
str
752+
Human-friendly size string, e.g. `"512.0 bytes"`, `"2.0 KB"`,
753+
`"1.0 MB"`.
754+
755+
Notes
756+
-----
757+
- Uses powers of 1024 (KB = 1024 bytes).
758+
- Always returns a string with one decimal place and the unit.
759+
"""
760+
761+
for unit in ["bytes", "KB", "MB", "GB", "TB"]:
762+
if size < 1024.0:
763+
return "%3.1f %s" % (size, unit)
764+
size /= 1024.0
765+
766+
# If the value is larger than the largest unit, fall back to TB with
767+
# the current value (already divided accordingly).
768+
return "%3.1f %s" % (size, "TB")

tests/test_misc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Tests for `imcflibs.imagej.misc` utility functions."""
2+
3+
from imcflibs.imagej.misc import bytes_to_human_readable
4+
5+
6+
def test_bytes_to_human_readable_simple():
7+
"""Ensure common sizes are formatted into human-readable strings."""
8+
assert bytes_to_human_readable(500) == "500.0 bytes"
9+
assert bytes_to_human_readable(2048) == "2.0 KB"
10+
assert bytes_to_human_readable(1024 * 1024) == "1.0 MB"
11+
assert bytes_to_human_readable(5 * 1024**3) == "5.0 GB"
12+
13+
14+
def test_bytes_to_human_readable_large():
15+
"""Verify formatting for large sizes such as terabytes."""
16+
# 1.5 TB in bytes should format as 1.5 TB
17+
size = int(1.5 * (1024**4))
18+
assert bytes_to_human_readable(size) == "1.5 TB"

0 commit comments

Comments
 (0)