Skip to content

Commit

Permalink
Merge pull request #107 from niaid/add_is_16bit
Browse files Browse the repository at this point in the history
Add is_16bit method
  • Loading branch information
blowekamp committed May 31, 2024
2 parents 4e5dc3b + eea1d9e commit 3f3ba49
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
15 changes: 14 additions & 1 deletion pytools/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ def is_int16(file_path: PathType) -> bool:
return _make_image_file_reader_with_info(file_path).GetPixelID() == sitk.sitkInt16


__all__ = ["is_int16"]
def is_16bit(file_path: PathType) -> bool:
"""
Read an image file header to inspect meta-data.
Supported file formats include TIFF, and others supported by SimpleITK and the Insight toolkit.
:param file_path: The path to an image file.
:returns: True if the pixel type is a 16-bit integer (signed or unsigned), False otherwise.
"""
return _make_image_file_reader_with_info(file_path).GetPixelID() in [sitk.sitkInt16, sitk.sitkUInt16]


__all__ = ["is_int16", "is_16bit"]
17 changes: 16 additions & 1 deletion test/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
#

from pytools.meta import is_int16
from pytools.meta import is_int16, is_16bit
import pytest
import SimpleITK as sitk

Expand Down Expand Up @@ -43,3 +43,18 @@ def test_is_int16_mrc(image_mrc, expected_result):
)
def test_is_int16_tif(image_tiff, expected_result):
assert is_int16(image_tiff) == expected_result


@pytest.mark.parametrize(
"image_tiff,expected_result",
[
(sitk.sitkUInt8, False),
(sitk.sitkInt8, False),
(sitk.sitkInt16, True),
(sitk.sitkUInt16, True),
(sitk.sitkFloat32, False),
],
indirect=["image_tiff"],
)
def test_is_16bit_tif(image_tiff, expected_result):
assert is_16bit(image_tiff) == expected_result

0 comments on commit 3f3ba49

Please sign in to comment.