napari-imagej icon indicating copy to clipboard operation
napari-imagej copied to clipboard

java.util.Iterator has no python equivalent

Open gselzer opened this issue 2 years ago • 3 comments

This prevents the labeling.cca Op from running:

File ~/code/imagej/napari-imagej/src/napari_imagej/types/type_conversions.py:60, in python_type_of(module_item=<java object 'org.scijava.module.DefaultMutableModuleItem'>)
     58     if converted is not None:
     59         return converted
---> 60 raise ValueError(
     61     (
     62         f"Unsupported Java Type: {module_item.getType()}. "
     63         "Let us know about the failure at https://forum.image.sc, "
     64         "or file an issue at https://github.com/imagej/napari-imagej!"
     65     )
     66 )

ValueError: Unsupported Java Type: interface java.util.Iterator. Let us know about the failure at https://forum.image.sc, or file an issue at https://github.com/imagej/napari-imagej!

gselzer avatar Nov 02 '22 19:11 gselzer

The scyjava library does convert java.util.Iteratorcollections.abc.Iterator. But the other direction looks buggy right now:

>>> x = iter(a for a in range(5))
>>> type(x)
<class 'generator'>
>>> jx = scyjava.to_java(x)
>>> jx.getClass().getName()
'java.util.ArrayList'
>>> jx
<java object 'java.util.ArrayList'>
>>> jx.size()
5
>>> list(jx)
[0, 1, 2, 3, 4]
>>> isinstance(x, collections.abc.Iterator)
True
>>> isinstance(x, list)
False

I guess scyjava's behavior needs to be improved here?

ctrueden avatar Nov 17 '22 18:11 ctrueden

OTOH, it's weird that DefaultCCA takes an Iterator at all. Usually one would use Iterable instead. The problem with Iterator is that it is stateful, and can typically only be moved forward, not backward. What if someone passes an iterator that is already partially iterated, for example?

ctrueden avatar Nov 17 '22 19:11 ctrueden

I guess scyjava's behavior needs to be improved here?

Maybe? We also need a widget mapping, as napari-imagej doesn't know how to harvest an Iterator...

OTOH, it's weird that DefaultCCA takes an Iterator at all. Usually one would use Iterable instead. The problem with Iterator is that it is stateful, and can typically only be moved forward, not backward. What if someone passes an iterator that is already partially iterated, for example?

Yeah, I don't know why it does that. Nothing much we can do about it from this project, though...

gselzer avatar Nov 18 '22 16:11 gselzer