Pass parameters from a Command line to BeanShell Script

Dear all,

I want to run the WekaSegmentation in headless mode without GUI on a Linux machine (connected viaa ssh). The following code works just fine:

In the command line:

$ /homeb/.../ImageJ-linux64 --headless --bsh /homeb.../MacroClassifyBean.bsh

My Skript called MacroClassifyBean.bsh goes as follows:

import trainableSegmentation.*;
import trainableSegmentation.WekaSegmentation;
import ij.io.FileSaver;
import ij.IJ;
import ij.ImagePlus;

fileinx="/work/.../11611_2461_1_1-1.tif";
 input = IJ.openImage( fileinx ); 
 // create Weka Segmentation object 
 segmentator = new WekaSegmentation(); 
 // load classifier from file 
 segmentator.loadClassifier( "/work/.../classifiertest.model" ); 

 // apply classifier to current training image and get label result 
 // (set parameter to true to get probabilities) 
 result = segmentator.applyClassifier( input ); 
 // get result (float image) 
 //result = segmentator.getClassifiedImage(); 
 new FileSaver( result ).saveAsTiff( "/work/.../ImagesOut/11611_2461_1_1-1.tif" );

However, now I want to integrate the filename in the command line and pass it to the script:

$ /homeb/.../ImageJ-linux64 --headless --bsh /homeb.../MacroClassifyBean.bsh /work/.../11611_2461_1_1-1.tif

How do I have to change the script to do so?

Thank you!
Lars

1 Like

You can create a script parameter for the file name:

#@ String (label="Input file name") fileinx

and then call it from the command line as indicated in this thread:

2 Likes

Dear Jan,

Thank you for your prompt answer! Unfortunately, I always get the following error message back:

Evaluation Error: Sourced file: /homeb/.../MacroClassifyBean.bsh Token Parsing Error: Lexical error at line 7, column 2.  Encountered: "@" (64), after : "#": <at unknown location>

I tried several versions and implemented the code at different locations in the script, the error message was more or less the same.

One version was:

import trainableSegmentation.*;
import trainableSegmentation.WekaSegmentation;
import ij.io.FileSaver;
import ij.IJ;
import ij.ImagePlus;

#@String (label="Input file name") fileinx;

// fileinx="/work/.../ImagesIn/11611_2461_1_1-1.tif";
 input = IJ.openImage( fileinx ); 
 // create Weka Segmentation object 
 segmentator = new WekaSegmentation(); 
 // load classifier from file 
 segmentator.loadClassifier( "/work/.../classifiertest.model" ); 

 // apply classifier to current training image and get label result 
 // (set parameter to true to get probabilities) 
 result = segmentator.applyClassifier( input ); 
 // get result (float image) 
 //result = segmentator.getClassifiedImage(); 
 new FileSaver( result ).saveAsTiff( "/work/.../ImagesOut/11611_2461_1_1-1.tif" );

For the command line I used:

/homeb/.../Fiji.app/ImageJ-linux64 --headless --bsh /homeb/.../MacroClassifyBean.bsh "fileinx='"/work/.../ImagesIn/11611_2461_1_1-1.tif"'"

Are you sure it works with beanshell, too? The examples are all in python or groovy.

Thanks a lot!
Lars

Yes, the script parameter parsing is language-agnostic and happens before the actual script is run by the respective scripting engine.

This error suggests that the script parameter parsing actually didn’t happen (so the Beanshell engine tried to execute the parameter line, and throws a syntax error).

  • Is your Fiji up to date? The parameter syntax recently changed, and the new #@ syntax requires at least scijava-common-2.64.0.jar to be present in your ./jars directory. (The legacy syntax // @String should work as well, but I recommend updating Fiji/ImageJ anyways.)
  • Does your script (including the script parameter) run fine when run from within the script editor?
2 Likes

Hi Jan,

thanks again for your prompt reply!
The Fiji is uptodate. On my Desktop, the script with #@String works fine. However, I still get the error on the linux machine. I tried running it with legacy syntax and tried running similar stuff in other languages, but constantly similar error messages. How do I teach my linux machine to parse the parameters correctly?

best,
Lars

Dear @ladida,

Could you please try to execute your script with the following line:

/homeb/.../Fiji.app/ImageJ-linux64 --headless --run /homeb/.../MacroClassifyBean.bsh "fileinx='/work/.../ImagesIn/11611_2461_1_1-1.tif'"

Please mind that I have replaced --bsh with --run. It seems that the parsing of the parameters (as mentioned by @imagejan) is not triggered when executing with --bsh (which is a legacy option I assume).

Best,
Stefan

PS: I have removed --ij2 from the command since it is not necessary.

4 Likes

Thank you so much! It worked. I still hat a minor mistake in my beanshell script, so for references here the correct code:

Command line:

/homeb/.../Fiji.app/ImageJ-linux64 --headless --ij2 --run /homeb/.../MacroClassifyBean.bsh "fileinx='/work/.../ImagesIn/11611_2461_1_1-1.tif'"

Beanshell Script MacroClassifyBean.bsh

#@String fileinx
import trainableSegmentation.*;
import trainableSegmentation.WekaSegmentation;
import ij.*;
import ij.io.FileSaver;
import ij.IJ;
import ij.ImagePlus;


// fileinx="/work/.../ImagesIn/11611_2461_1_1-1.tif";
 input = IJ.openImage( fileinx ); 
 // create Weka Segmentation object 
 segmentator = new WekaSegmentation(); 
 // load classifier from file 
 segmentator.loadClassifier( "/work/.../classifiertest.model" ); 

 // apply classifier to current training image and get label result 
 // (set parameter to true to get probabilities) 
 result = segmentator.applyClassifier( input ); 
 // get result (float image) 
 //result = segmentator.getClassifiedImage(); 
 new FileSaver( result ).saveAsTiff( "/work/.../ImagesOut/11611_2461_1_1-1.tif" );

Best wishes,
Lars

1 Like