Problems using weaver to run java code inside jython

jython
weaver
fiji
Tags: #<Tag:0x00007fb87d2cb608> #<Tag:0x00007fb87d2cb450> #<Tag:0x00007fb87d2cb2e8>

#1

I’ve been trying to use the java inline functionality using Weaver as described in this tutorial. However, when I try to run any of the examples, I get this exception: “java.lang.ClassNotFoundException: com.sun.tools.javac.Main”. I know my installation of Fiji can access the JDK, since I can compile and run standard java plugins in the script editor.

I’m running a standard, up-to-date installation of Fiji on a mac (os 10.12.3) with 64-bit java version 1.8.0_121. Fiji was installed from the “no-JRE” version so that I could use my previously installed JDK. I don’t know if that has any impact on this issue.

Any help from someone who has successfully used the weaver would be appreciated.

Thanks,

Jon


The code weaver to inline java snippets into scripts needs maintenance
#2

For me this fails because javac.jar is missing. I tried to run the first Weaver example from the wiki. I can compile Java plugins using my local JDK v1.8.0_112 from the editor.

Is it possible to rewrite the Java code in Jython? For me Weaver is a bit strange, because you compile Jython code to Java code and again compile inline Java code using weaver. This reminds me a bit of the film Inception.


#3

Thanks for verifying this issue. At least I know it’s not just my own installation that has this problem. I don’t know why Fiji can’t find javac for Weaver.

As to your other point: Ideally I would do all the processing in pure Jython, but for processor intensive operations on large images, compiled java code is a lot faster. To quote the tutorial (which is verified by my own experience), “Jython is great at doing high-level operations on images. But sometimes one wants to edit pixels specifically. Such low-level loops in jython are far from the performance offered by java.”

Cheers,

Jon


#4

Have you considered using Groovy or Javascript for your scripts? Both are closer to Java and might be much faster than Jython.

See also this discussion:


#5

Consider using JavaScript, which is a compiled language on Java 8. The following JavaScript runs 30 times faster than the Jython version.

  sphere();
  
  function sphere() {
     var size = 4096;
     var ip = new FloatProcessor(size,size);
     var t0 = System.currentTimeMillis();
     var x, y, dx, dy, d;
     for (y=0; y<size; y++) {
        for (x=0; x<size; x++) {
           dx=x-size/2;
           dy=y-size/2;
           d = Math.sqrt(dx*dx+dy*dy);
           ip.setf(x,y,-d);
        }
     }
     var time = (System.currentTimeMillis()-t0)/1000+" seconds";
     var img = new ImagePlus(time,ip);
     IJ.run(img,"Red/Green","");
     img.show();
  }

#6

Thanks to everyone for the tips on using JavaScript. I work in a Python-centric lab, so I’m constrained a bit in my choice of programming language. I’m still curious as to why Weaver doesn’t work. Probably a question for the ages… :slight_smile:


#7

I have extended the JavaScript to make it runnable and implemented the Jython version. For me it is 0.3s to 0.4s for JavaScript and 8s to 9s for Jython. It gets even slower if I create the pixels first and call FloatProcessor(size,size,pixels) subsequently.

I’m a bit surprised by this huge difference.

JavaScript

sphere();

function sphere() {
	var size = 4096;
	var ip = new Packages.ij.process.FloatProcessor(size,size);
	var t0 = java.lang.System.currentTimeMillis();
	var x, y, dx, dy, d;
	for (y=0; y<size; y++) {
		for (x=0; x<size; x++) {
		   dx=x-size/2;
		   dy=y-size/2;
		   d = Math.sqrt(dx*dx+dy*dy);
		   ip.setf(x,y,-d);
		}
	}
	var time = (java.lang.System.currentTimeMillis()-t0)/1000+" seconds";
	var img = new Packages.ij.ImagePlus(time,ip);
	Packages.ij.IJ.run(img,"Red/Green","");
	img.show();
}

Jython

import math
from java.lang.System import currentTimeMillis
from ij import IJ
from ij import ImagePlus
from ij.process import FloatProcessor

def sphere():
    size = 4096
    ip = FloatProcessor(size,size)
    t0 = currentTimeMillis()
    for y in range(size):
        for x in range(size):        
           dx = x-size/2
           dy = y-size/2
           d = math.sqrt(dx*dx+dy*dy)
           ip.setf(x,y,-d)
    time = '%.3f seconds' % ((currentTimeMillis() - t0) / 1000,)
    img = ImagePlus(time,ip)
    IJ.run(img,'Red/Green', '')
    img.show()

sphere()

#8

Using com.sun.tools.javac.Main is deprecated in favor of javax.tools.JavaCompiler. We made the switch with the Java 8 version of ImageJ2 + Fiji, but the Weaver was not updated accordingly.

That tutorial is quite old, and no one is taking care to maintain the Weaver component (sorry).

Actually, the Weaver (which is here on GitHub) never made the transition to stable releases in 2015, and is currently not officially part of Fiji at all. I simply never deleted the old weaver JAR via the updater. So in light of this, the fact that it no longer works is not at all surprising.

If there is really demand for it, I could take a look at resurrecting it. I vaguely recall there being some technical issue which made it non-trivial to keep working. But perhaps it simply slipped through the cracks.


#9

JavaScript is 20-30 times faster than Jython because it is a compiled language. The Nashorn engine compiles JavaScript code into Java bytecode, which is converted into machine code by Java’s JIT compiler.


#10

Thanks for explaining the source of the error – makes sense.

If you and or the other developers eventually have interest or free time for resurrecting the Weaver, I would certainly use it. In the meantime, I’ll fall back to using plain Java or JavaScript.

Cheers,

Jon


#11

@JonNathan I’m curious how your adventures with JavaScript have been going. Is it fast enough, as @Wayne suggested? If so, I will not worry about the weaver anymore. Otherwise, if you still miss it in Jython, then feel free to file an issue here asking for its return.


#12

Thanks for asking. For expediency, I’ve had to fall back to Java, but my colleagues would prefer that the code I contribute be python or jython, since that is the lingua franca of our lab, and they’d like to be able to review it easily. So to answer your question, yes, I would love to have access to the weaver if/when it becomes available. I’ll file an issue as per your suggestion.

Again, thanks to you and the ImageJ community for considering end user requests!

Cheers,

Jon