Skip to content

Commit

Permalink
Add formatter for copy_rai_into_ndarray exceptions
Browse files Browse the repository at this point in the history
This formatter converts the exceptions from copy_rai_into_ndarray into a
more human readable format.

**Before:**

Exception: (ClassCastException("class net.imglib2.type.logic.BitType cannot
be cast to class net.imglib2.type.logic.NativeBoolType (net.imglib2.type.logic.BitType
and net.imglib2.type.logic.NativeBoolType are in unnamed module of loader 'app')"),
ClassCastException("class net.imglib2.type.logic.BitType cannot be cast to class
net.imglib2.type.logic.NativeBoolType (net.imglib2.type.logic.BitType and
net.imglib2.type.logic.NativeBoolType are in unnamed module of loader 'app')"),
RuntimeException("java.util.concurrent.ExecutionException: java.lang.ClassCastException:
class net.imglib2.type.logic.BitType cannot be cast to class net.imglib2.type.logic.NativeBoolType
(net.imglib2.type.logic.BitType and net.imglib2.type.logic.NativeBoolType are in unnamed module
of loader 'app')"))

**After:**

Exception:
Error: Unsupported type cast via net.imglib2.util.ImgUtil.copy
    Source type: net.imglib2.type.logic.BitType
    Target type: net.imglib2.type.logic.NativeBoolType
Error: Unsupported type cast via net.imglib2.util.Images.copy
    Source type: net.imglib2.type.logic.BitType
    Target type: net.imglib2.type.logic.NativeBoolType
Error: Unsupported type cast via net.imagej.ops.copy.CopyNamespace.rai
    Source type: net.imglib2.type.logic.BitType
    Target type: net.imglib2.type.logic.NativeBoolType
  • Loading branch information
elevans committed Jan 14, 2025
1 parent 3f25216 commit a01f655
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/imagej/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ def copy_rai_into_ndarray(
return narr
except JException as exc:
# Try another method
failure_exceptions.append(exc)
failure_exceptions.append(
_format_copy_exception(exc.toString(), "net.imglib2.util.ImgUtil.copy")
)

# Check imagej-common version for fast copy availability.
imagej_common_version = sj.get_version(jc.Dataset)
Expand All @@ -167,7 +169,9 @@ def copy_rai_into_ndarray(
return narr
except JException as exc:
# Try another method
failure_exceptions.append(exc)
failure_exceptions.append(
_format_copy_exception(exc.toString(), "net.imglib2.util.Images.copy")
)

# 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 All @@ -178,10 +182,15 @@ def copy_rai_into_ndarray(
return
except JException as exc:
# Try another method
failure_exceptions.append(exc)
failure_exceptions.append(
_format_copy_exception(
exc.toString(), "net.imagej.ops.copy.CopyNamespace.rai"
)
)

# Failed
raise Exception("Could not copy rai into ndarray", *failure_exceptions)
failure_msg = "\n".join(failure_exceptions)
raise Exception("\n" + failure_msg)


def dtype(image_or_type) -> np.dtype:
Expand Down Expand Up @@ -238,3 +247,25 @@ def dtype(image_or_type) -> np.dtype:
raise TypeError(f"Unsupported original ImageJ type: {imagej_type}")

raise TypeError("Unsupported Java type: " + str(sj.jclass(image_or_type).getName()))


def _format_copy_exception(exc: str, fun_name: str) -> str:
"""Format copy exceptions strings.
:param exc: Exception as a String.
:param fun_name: Name of the function producing the exception.
:return: The formatted exception.
"""
# format cast exception
m = exc.split(" ")
from_class = ""
to_class = ""
if m[0] == "java.lang.ClassCastException:" and "net.imglib2.util" in fun_name:
from_class = m[2]
to_class = m[8]
elif m[0] == "java.lang.RuntimeException:" and "net.imagej.ops" in fun_name:
from_class = m[4]
to_class = m[10]
msg = f"Error: Unsupported type cast via {fun_name}\n Source type: {from_class}\n Target type: {to_class}"

return msg

0 comments on commit a01f655

Please sign in to comment.