Skip to content

Commit

Permalink
Add conversion for BitType and BoolType imgs
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer authored and elevans committed Jan 29, 2024
1 parent a173d5a commit c43c0fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/imagej/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import scyjava as sj
from jpype import JException

from imagej._java import jc

Expand All @@ -14,6 +15,8 @@
# fmt: off
_imglib2_types = {
"net.imglib2.type.logic.NativeBoolType": "bool_",
"net.imglib2.type.logic.BitType": "bool_",
"net.imglib2.type.logic.BoolType": "bool_",
"net.imglib2.type.numeric.integer.ByteType": "int8",
"net.imglib2.type.numeric.integer.ByteLongAccessType": "int8",
"net.imglib2.type.numeric.integer.ShortType": "int16",
Expand Down Expand Up @@ -136,22 +139,25 @@ def copy_rai_into_ndarray(
if not is_arraylike(narr):
raise TypeError("narr is not arraylike")

# Check imglib2 version for fast copy availability.
imglib2_version = sj.get_version(jc.RandomAccessibleInterval)
if sj.is_version_at_least(imglib2_version, "5.9.0"):
# ImgLib2 is new enough to use net.imglib2.util.ImgUtil.copy.
ImgUtil = sj.jimport("net.imglib2.util.ImgUtil")
ImgUtil.copy(rai, sj.to_java(narr))
return narr

# Check imagej-common version for fast copy availability.
imagej_common_version = sj.get_version(jc.Dataset)
if sj.is_version_at_least(imagej_common_version, "0.30.0"):
# ImageJ Common is new enough to use (deprecated)
# net.imagej.util.Images.copy.
Images = sj.jimport("net.imagej.util.Images")
Images.copy(rai, sj.to_java(narr))
return narr
try:
# Check imglib2 version for fast copy availability.
imglib2_version = sj.get_version(jc.RandomAccessibleInterval)
if sj.is_version_at_least(imglib2_version, "5.9.0"):
# ImgLib2 is new enough to use net.imglib2.util.ImgUtil.copy.
ImgUtil = sj.jimport("net.imglib2.util.ImgUtil")
ImgUtil.copy(rai, sj.to_java(narr))
return narr

# Check imagej-common version for fast copy availability.
imagej_common_version = sj.get_version(jc.Dataset)
if sj.is_version_at_least(imagej_common_version, "0.30.0"):
# ImageJ Common is new enough to use (deprecated)
# net.imagej.util.Images.copy.
Images = sj.jimport("net.imagej.util.Images")
Images.copy(rai, sj.to_java(narr))
return narr
except JException:
pass

# Fall back to copying with ImageJ Ops's copy.rai op. In theory, Ops
# should always be faster. But in practice, the copy.rai operation is
Expand Down
17 changes: 17 additions & 0 deletions tests/test_image_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
import scyjava as sj
import xarray as xr
from jpype import JArray, JLong

import imagej.convert as convert
import imagej.dims as dims
Expand Down Expand Up @@ -376,6 +377,22 @@ def test_dataset_converts_to_xarray(ij_fixture):
assert_inverted_xarr_equal_to_xarr(dataset, ij_fixture, xarr)


def test_bittype_img_to_ndarray(ij_fixture):
ArrayImgs = sj.jimport("net.imglib2.img.array.ArrayImgs")
dims = JArray(JLong)(3)
dims[:] = [10, 10, 10]
j_img = ArrayImgs.bits(dims)
p_img = ij_fixture.py.from_java(j_img)
assert p_img.dtype == np.bool_


def test_boolean_ndarray_to_img(ij_fixture):
narr = np.ones((10, 10), dtype=np.bool_)
j_img = ij_fixture.py.to_java(narr)
BooleanType = sj.jimport("net.imglib2.type.BooleanType")
assert isinstance(j_img.firstElement(), BooleanType)


def test_image_metadata_conversion(ij_fixture):
# Create a ImageMetadata
DefaultImageMetadata = sj.jimport("io.scif.DefaultImageMetadata")
Expand Down

0 comments on commit c43c0fc

Please sign in to comment.