Skip to content

ops.op("math.subtract") does not work #134

Closed

Description

@elevans

While testing SciJavaOps in the Fiji script editor (using Jython) I discovered that I was unable to use the "math.subtract" op to subtract two images from each other.

Here is a minimal example you can run with an open 2D image in Fiji:

#@ Img img_in

# get op environoment
from org.scijava.ops.api import OpEnvironment
ops = OpEnvironment.getEnvironment()

# create gauss blur of image
gauss = ops.op("filter.gauss").arity2().input(img_in, 3.0).apply()

# subtract gauss blur from input image
res = ops.op("math.subtract").arity2().input(img_in, gauss).apply()

This produces the following output:

...
... 24 more
	Suppressed: org.scijava.ops.api.features.OpMatchingException: No matching 'math.subtract/java.util.function.BiFunction<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>' op

Request:
-	Name: "math.subtract", Types: java.util.function.BiFunction<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>
Input Types: 
		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
Output Type: 
		* java.lang.Object

Candidates:
-	 No candidates found!
		at org.scijava.ops.api.features.MatchingResult.singleMatch(MatchingResult.java:67)
		at org.scijava.ops.engine.matcher.impl.RuntimeSafeMatchingRoutine.findMatch(RuntimeSafeMatchingRoutine.java:60)
		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
		... 36 more
	Suppressed: org.scijava.ops.api.features.OpMatchingException: Unable to find an Op adaptation for org.scijava.ops.api.features.MatchingConditions@14eb64f2
		at org.scijava.ops.engine.matcher.adapt.AdaptationMatchingRoutine.findMatch(AdaptationMatchingRoutine.java:157)
		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
		... 36 more
	Suppressed: org.scijava.ops.api.features.OpMatchingException: No matching 'math.subtract/java.util.function.BiFunction<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>' op

Request:
-	Name: "math.subtract", Types: java.util.function.BiFunction<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>
Input Types: 
		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
Output Type: 
		* java.lang.Object

Candidates:
-	 No candidates found!
		at org.scijava.ops.api.features.MatchingResult.singleMatch(MatchingResult.java:67)
		at org.scijava.ops.engine.matcher.impl.RuntimeSafeMatchingRoutine.findMatch(RuntimeSafeMatchingRoutine.java:60)
		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
		... 36 more

Activity

  1. added this to the 1.0.0 milestone on Sep 26, 2023
  2. hinerm commented on Sep 26, 2023

    @hinerm
    Member

    Our theory was that the problem was that the output type of java.lang.Object prevented lifters from matching to raise pixel-wise subtracts to work on these images.

    I tested this with the following in Java:

    		try (Context ctx = new Context()) {
    			ImgOpener opener = new ImgOpener(ctx);
    			var img =
    					opener.openImgs("https://imagej.net/images/lymp.tif").get(0).getImg();
    			var gauss = ops.op("filter.gauss").arity2().input(img, 3.0).apply();
    
    			// subtract gauss blur from input image
    			var oType = new Nil<ArrayImg<UnsignedByteType, ByteArray>>(){};
    			var res = ops.op("math.subtract").arity2().input(img, gauss).outType(oType).apply();
    			System.out.println(res);
    		}
    

    But it produces the same exception:

    Request:
    -	Name: "math.subtract", Types: java.util.function.BiFunction<net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>, net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>, net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>>
    Input Types: 
    		* net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>
    		* net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>
    Output Type: 
    		* net.imglib2.img.array.ArrayImg<net.imglib2.type.numeric.integer.UnsignedByteType, net.imglib2.img.basictypeaccess.array.ByteArray>
    

    I will try to investigate further to determine the root cause of the failure here.

  3. elevans commented on Sep 28, 2023

    @elevans
    MemberAuthor

    I don't know if this helps but ops.op("convert.float32") also errors out the same way when using it from Jython:

    #@ Img img
    
    # get op environment
    from org.scijava.ops.api import OpEnvironment
    ops = OpEnvironment.getEnvironment()
    
    # convert to 32-bit img
    f_img = ops.op("convert.float32").arity1().input(img).apply()

    Produces this stack trace:

    ... 24 more
    	Suppressed: org.scijava.ops.api.features.OpMatchingException: No matching 'convert.float32/java.util.function.Function<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>' op
    
    Request:
    -	Name: "convert.float32", Types: java.util.function.Function<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>
    Input Types: 
    		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
    Output Type: 
    		* java.lang.Object
    
    Candidates:
    -	 No candidates found!
    		at org.scijava.ops.api.features.MatchingResult.singleMatch(MatchingResult.java:67)
    		at org.scijava.ops.engine.matcher.impl.RuntimeSafeMatchingRoutine.findMatch(RuntimeSafeMatchingRoutine.java:60)
    		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
    		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
    		... 37 more
    	Suppressed: org.scijava.ops.api.features.OpMatchingException: Unable to find an Op adaptation for org.scijava.ops.api.features.MatchingConditions@9cb269ef
    		at org.scijava.ops.engine.matcher.adapt.AdaptationMatchingRoutine.findMatch(AdaptationMatchingRoutine.java:157)
    		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
    		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
    		... 37 more
    	Suppressed: org.scijava.ops.api.features.OpMatchingException: No matching 'convert.float32/java.util.function.Function<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>' op
    
    Request:
    -	Name: "convert.float32", Types: java.util.function.Function<net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>, java.lang.Object>
    Input Types: 
    		* net.imagej.ImgPlus<net.imglib2.type.numeric.integer.UnsignedShortType>
    Output Type: 
    		* java.lang.Object
    
    Candidates:
    -	 No candidates found!
    		at org.scijava.ops.api.features.MatchingResult.singleMatch(MatchingResult.java:67)
    		at org.scijava.ops.engine.matcher.impl.RuntimeSafeMatchingRoutine.findMatch(RuntimeSafeMatchingRoutine.java:60)
    		at org.scijava.ops.api.features.MatchingRoutine.match(MatchingRoutine.java:43)
    		at org.scijava.ops.engine.matcher.impl.DefaultOpMatcher.match(DefaultOpMatcher.java:66)
    		... 37 more

    I also tried creating an output image for the op to see if that helped...it did not:

    #@ Img img
    
    # get op environment
    from org.scijava.ops.api import OpEnvironment
    ops = OpEnvironment.getEnvironment()
    
    # convert to 32-bit img
    f_img = ops.op("create.img").arity1().input(img).apply()
    f_img = ops.op("convert.float32").arity2().input(f_img, img).apply()
  4. hinerm commented on Sep 28, 2023

    @hinerm
    Member

    I don't know if this helps but ops.op("convert.float32") also errors out the same way when using it from Jython:

    @elevans Currently I wouldn't expect Functions operating on images to work. Most algorithms are implemented as Computers on single types - they can be lifted to work on Images or adapted to Functions, but currently not both (required to match a Function Op that operates on Images). Let me know if that doesn't make sense.

  5. hinerm commented on Sep 29, 2023

    @hinerm
    Member

    I have outlined the problems and started exploring solutions on this branch.

    What I see as needing to happen:

    • Auto-gen lift ops for combinations of I and Img<I>, for computers and functions (e.g. so math.add(Img, 5) works)
    • Auto-gen complex lift ops for combinations of I and Img<I>, for compters and functions (e.g. so math.add can be called as a Computer or a Function)
  6. self-assigned this
    on Oct 2, 2023
  7. hinerm commented on Nov 13, 2023

    @hinerm
    Member

    OK I thought I was done working on https://github.com/scijava/incubator/tree/imagej/imagej-ops2/convert-common-image-types

    But matches like this with no outType specified don't work:

    final Img<IntType> imgA = TestImgGeneration.intArray(true, 10, 10);
    var imgC = (Img<IntType>)ops.binary("math.sub").input(imgA, new IntType(25)).apply();
    

    I suspect they have to work because trying to make a Nil<Img<IntType>> is probably a nightmare in python? Does that intuition seem correct @elevans?

    @gselzer any thoughts off the top of your head on why matching without an outType doesn't work, but with an outType does?

  8. gselzer commented on Nov 13, 2023

    @gselzer
    Member

    @hinerm you're trying to look for a BiFunction<Img<IntType>, IntType, Object>. I'm guessing none exist, so it tries to adapt something. Adaptation to Functions with an Object output is super tricky.

    You're probably trying use this adapt Op, right? The second Op dependency is then maybe this guy, and that guy will need some Function<RAII, RAIO> creator - I'm guessing that can't be found because that O type is vacuous, since it's all coming from that Object output type.

  9. hinerm commented on Nov 13, 2023

    @hinerm
    Member

    @gselzer How devastating would it be for a match request with unspecified output to behave like matching an Any instead of Object? I feel like that is closer to the semantic meaning of leaving off output specification right?

  10. gselzer commented on Nov 13, 2023

    @gselzer
    Member

    @gselzer How devastating would it be for a match request with unspecified output to behave like matching an Any instead of Object? I feel like that is closer to the semantic meaning of leaving off output specification right?

    You're right, I agree Any is better, although there may be some fun casting involved. I think it's worth a try...

  11. added 2 commits that reference this issue on May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions