Error in Trainable Weka Segmentation macro

Dear all,

I get the following error when trying to run trainable WEka segemtation commands from a macro. I wonder if it can be solved by adding a wait?

(Fiji Is Just) ImageJ 2.0.0-rc-54/1.51h; Java 1.8.0_101 [64-bit]; Windows 10 10.0; 18815MB of 97973MB (19%)

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at ij.macro.Functions.call(Functions.java:4110)
at ij.macro.Functions.getStringFunction(Functions.java:272)
at ij.macro.Interpreter.getStringTerm(Interpreter.java:1293)
at ij.macro.Interpreter.getString(Interpreter.java:1272)
at ij.macro.Interpreter.doStatement(Interpreter.java:281)
at ij.macro.Interpreter.doBlock(Interpreter.java:608)
at ij.macro.Interpreter.doStatement(Interpreter.java:272)
at ij.macro.Interpreter.doIf(Interpreter.java:966)
at ij.macro.Interpreter.doStatement(Interpreter.java:248)
at ij.macro.Interpreter.doBlock(Interpreter.java:608)
at ij.macro.Interpreter.doStatement(Interpreter.java:272)
at ij.macro.Interpreter.doFor(Interpreter.java:530)
at ij.macro.Interpreter.doStatement(Interpreter.java:254)
at ij.macro.Interpreter.doBlock(Interpreter.java:608)
at ij.macro.Interpreter.runFirstMacro(Interpreter.java:707)
at ij.macro.Interpreter.doStatement(Interpreter.java:263)
at ij.macro.Interpreter.doStatements(Interpreter.java:218)
at ij.macro.Interpreter.run(Interpreter.java:115)
at ij.macro.Interpreter.run(Interpreter.java:85)
at ij.macro.Interpreter.run(Interpreter.java:96)
at ij.plugin.Macro_Runner.runMacro(Macro_Runner.java:155)
at ij.IJ.runMacro(IJ.java:134)
at ij.IJ.runMacro(IJ.java:123)
at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1039)
at net.imagej.legacy.IJ1Helper$3.call(IJ1Helper.java:1035)
at net.imagej.legacy.IJ1Helper.runMacroFriendly(IJ1Helper.java:986)
at net.imagej.legacy.IJ1Helper.runMacro(IJ1Helper.java:1035)
at net.imagej.legacy.plugin.IJ1MacroEngine.eval(IJ1MacroEngine.java:97)
at net.imagej.legacy.plugin.IJ1MacroEngine.eval(IJ1MacroEngine.java:144)
at org.scijava.script.ScriptModule.run(ScriptModule.java:177)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)
at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:191)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at trainableSegmentation.Weka_Segmentation$CustomWindow.setButtonsEnabled(Weka_Segmentation.java:1107)
at trainableSegmentation.Weka_Segmentation.loadClassifier(Weka_Segmentation.java:2761)
… 42 more

The macro is as follows

    macro "Batch Analyze Wound"{
	//run("Close All");
	dir1 = getDirectory("Choose Source Directory 1 ");
//setBatchMode(true);
	list=getFileList(dir1);
	segmentation=dir1+"segmentation"+File.separator;
	File.makeDirectory(segmentation);
	run("Set Measurements...", "area redirect=None decimal=1");
		for (l=0; l<list.length; l++){
			if(endsWith(list[l], ".tif")){
				//clearROIs();
			open(dir1+list[l]);
			name=getTitle();
			resultsName=replace(list[l],".tif", "");
			rename("Temp");
			run("Split Channels");  
			close("C1-Temp");
			selectWindow("C2-Temp");	
			saveAs("Tiff", segmentation+name);
			run("Trainable Weka Segmentation 3D");
			selectWindow("Trainable Weka Segmentation v3.2.2");
			call("trainableSegmentation.Weka_Segmentation.loadClassifier", "C:\\Users\\Admin\\Desktop\\AutomatedTestFiles\\WoundHealing\\Analyze\\classifier.model");
			call("trainableSegmentation.Weka_Segmentation.getResult");
			selectWindow("Classified image");
			run("8-bit");
			setAutoThreshold("Default dark");
			setOption("BlackBackground", true);
			run("Make Binary", "method=Default background=Dark black");
			//run("Open", "stack");
			//run("Close-", "stack");
				for (s=1; s<=nSlices; s+=1) {
					setSlice(s);
					run("Create Selection");
					getStatistics(area);
					setResult(resultsName, s-1, area);
				}
			run("Close All");
			run("Collect Garbage");
		}
	updateResults();
	selectWindow("Results");
	saveAs("results", ""+segmentation+"segmentationResults.txt");
}
		}	
	function clearROIs(){
		if(isOpen("ROI Manager")){
			if (roiManager("count")>0) {
			roiManager("Reset");
						}
				}
			}

Two comments about these lines:

  1. After calling the plugin for the first time, you might need to wait some seconds so all Weka properties are loaded. So you should add a wait in between:
run("Trainable Weka Segmentation 3D");
// wait 3 seconds
wait(3000);
selectWindow("Trainable Weka Segmentation v3.2.2");
  1. The new version of Trainable Weka Segmentation is 3.2.3, so you might need to update the last line as well if you update Fiji.
1 Like

Thanks mate,
With regards to

  1. After calling the plugin for the first time, you might need to wait some seconds so all Weka properties are loaded. So you should add a wait in between:

The loop seems to be able to run through once without issue and then bugs out the second time round. There also seems to be an issue with memory usage, perhaps a memory leak. After processing an image. with nothing open there will often be 30GB of memory in use.

I will test the pauses

I see, most probably Java is not calling the garbage collector on each iteration of the loop. I managed to solve that problem on a script. Have a look at how to process a whole folder of images using a trained classifier.

If you have problems translating your macro to a script, I can help.

ignacio