Skip to content

Script Parameters cannot handle multiple choices #156

Closed

Description

@dscho

The scripting service handles only a very limited set of parameter options. For example, this Beanshell parameter that cropped up in a coding session with @schmiedc fails:

// @String(label = 'Monty Hall', choices = { 'Door A', 'Door B', 'Door C' }) montyHall

The error javax.script.ScriptException: Invalid attribute: 'Door B' is hidden in the standard error stream.

The reason is that parameters that are even mildly complex are not handled correctly can be found here: It is assumed that the attributes (i.e. the "label = 'Monty Hall', choices = { 'Door A', 'Door B', 'Door C' }" of the example above) can be parsed simply by cutting it apart at every comma.

Please note that this parser would already fail to handle something as simple as @String(label = "Hello, world!") label.

Please note also that the comment // TODO: We probably want to use a real CSV parser. is quite misleading. The attributes of a parameter have nothing in common with CSV, but much more with, say, JSON.

Alas, there is already a full-fledged JSON parser in scijava-common, as part of the IndexReader we use to read the indexed annotations.

It would most likely require only a gentle amount of refactoring to turn this into a JSONReader that could be put into org.scijava.util. A cursory look makes this person believe that the only thing that would need to be factored out is the getLegacyReader() method.

To support the script parameters use case, the JSONReader would probably need to sprout a public char peek() method that would skip white space, continue parsing upon encountering a comma, finalize the parameters upon seeing a closing parenthesis, and erroring out otherwise.

In the meantime, we will have to resort to using code like the following:

// @ModuleService moduleService

import java.util.Arrays;
import org.scijava.module.DefaultMutableModule;

gui = new DefaultMutableModule();
info = gui.addInput("Monty Hall", String.class);
info.setChoices(Arrays.asList(new String[] { "Door A", "Door B", "Door C" }));

moduleService.run(gui, true, new Object[0]).get();
print(info.getValue(gui));

Activity

  1. dscho commented on Mar 30, 2015

    @dscho
    ContributorAuthor

    This would be an appropriate diff to start from, in this humble person's opinion:

    diff --git a/src/test/java/org/scijava/script/ScriptInfoTest.java b/src/test/java/org/scijava/script/ScriptInfoTest.java
    index 7c81966..ef7b503 100644
    --- a/src/test/java/org/scijava/script/ScriptInfoTest.java
    +++ b/src/test/java/org/scijava/script/ScriptInfoTest.java
    @@ -32,6 +32,7 @@
     package org.scijava.script;
    
     import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
     import static org.junit.Assert.assertTrue;
     import static org.junit.Assert.fail;
    
    @@ -52,6 +53,7 @@ import org.junit.AfterClass;
     import org.junit.BeforeClass;
     import org.junit.Test;
     import org.scijava.Context;
    +import org.scijava.plugin.Parameter;
     import org.scijava.plugin.Plugin;
     import org.scijava.test.TestUtils;
     import org.scijava.util.DigestUtils;
    @@ -85,13 +87,15 @@ public class ScriptInfoTest {
        public void testParameterParsing() throws Exception {
            final String script =
                "" + "% @LogService log\n"
    -               + "% @OUTPUT Integer output"
    +               + "% @String(label = 'Monty Hall', choices = { 'Door A', 'Door B', 'Door C' }) montyHall\n"
    +               + "% @OUTPUT Integer output\n"
                    + "% kraken@blah.net\n";
            ScriptModule scriptModule = scriptService.run("hello.bsizes", script, true).get();
    
            Object output = scriptModule.getOutput("result");
    
    -       if (output == null || !(output instanceof Integer)) fail();
    +       assertNotNull(output);
    +       assertTrue(output instanceof Integer);
            assertEquals(3, ((Integer)output).intValue());
        }
    

    [EDIT: Since Firefox started to crash frequently here when editing GitHub issues on MacOSX here, I had to paste the diff within a time window of 3 seconds, and edit the comment on another machine on which I did not have that diff. Sorry.]

  2. seanvw commented on Apr 20, 2016

    @seanvw

    hi @hinerm et al

    I tried (and need) to get a drop down list. Saw Curtis Rueden's tutorial from the Image J conference (very nice), then this issue #156. My parameter specification (from Jython code):

    ​# @File(label = "Path", description = "Choose the path to the /Images/ directory e.g. ./EXPT_004/SEAN_384_10x_EPI_470_60ms_19.02.2016_144300/Images/", style = "directory") images_dir
    # @Integer(label = "Limit", description = "Limit the number of directories to process", value="2") dir_lim_in 
    # @String(label = "Monty Hall", choices = { "Door A", "Door B", "Door C" } montyHall
    

    It gives... :(

    [ERROR] Invalid parameter syntax for script: /Users/seanwalsh/DOCS/IMAGEJ/siam_z_stitch_and_merge.py
    javax.script.ScriptException: Invalid parameter: String(label = "Monty Hall", choices = { "Door A", "Door B", "Door C" } montyHall
        at org.scijava.script.ScriptInfo.parseParam(ScriptInfo.java:338)
        at org.scijava.script.ScriptInfo.parseParameters(ScriptInfo.java:245)
        at org.scijava.module.AbstractModuleInfo.initParameters(AbstractModuleInfo.java:295)
        at org.scijava.module.AbstractModuleInfo.inputList(AbstractModuleInfo.java:257)
        at org.scijava.module.AbstractModuleInfo.inputs(AbstractModuleInfo.java:103)
        at org.scijava.module.DefaultModuleService.getSingleInput(DefaultModuleService.java:255)
        at net.imagej.display.process.SingleInputPreprocessor.getSingleInput(SingleInputPreprocessor.java:82)
        at net.imagej.display.process.SingleInputPreprocessor.process(SingleInputPreprocessor.java:68)
        at org.scijava.module.ModuleRunner.preProcess(ModuleRunner.java:104)
        at org.scijava.module.ModuleRunner.run(ModuleRunner.java:156)
        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(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    

    I updated to v1.5.0i and then to the daily build...still no joy

    What am i doing wrong please?

  3. hinerm commented on Apr 20, 2016

    @hinerm
    Member

    Hi @seanvw!

    It looks like your parameter is missing a closing parenthesis. I just tried using:

    # @String(label = "Monty Hall", choices = { "Door A", "Door B", "Door C" }) montyHall
    

    instead and it worked for me.

    Thanks for testing this feature out. Let us know if you run into any more problems.

  4. seanvw commented on Apr 20, 2016

    @seanvw

    Thanks for that tip :)
    Yeah, sorry, bit of a typo in trying all combinations of braces and quotes. Now back to this error :(

    # @String(label = "Monty Hall", choices = { "Door A", "Door B", "Door C" }) montyHall

    javax.script.ScriptException: Invalid attribute: "Door B"
    at org.scijava.script.ScriptInfo.parseAttrs(ScriptInfo.java:382)
    at org.scijava.script.ScriptInfo.parseParam(ScriptInfo.java:345)
    at org.scijava.script.ScriptInfo.parseParameters(ScriptInfo.java:245)
    at org.scijava.module.AbstractModuleInfo.initParameters(AbstractModuleInfo.java:295)
    at org.scijava.module.AbstractModuleInfo.inputList(AbstractModuleInfo.java:257)
    at org.scijava.module.AbstractModuleInfo.inputs(AbstractModuleInfo.java:103)
    at org.scijava.module.DefaultModuleService.getSingleInput(DefaultModuleService.java:255)
    at net.imagej.display.process.SingleInputPreprocessor.getSingleInput(SingleInputPreprocessor.java:82)
    at net.imagej.display.process.SingleInputPreprocessor.process(SingleInputPreprocessor.java:68)
    at org.scijava.module.ModuleRunner.preProcess(ModuleRunner.java:104)
    at org.scijava.module.ModuleRunner.run(ModuleRunner.java:156)
    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(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

  5. ctrueden commented on Apr 20, 2016

    @ctrueden
    Member

    @seanvw Are you using Java-8, with the Java-8 update site enabled? At the moment, we are in the middle of a transition from Java 6 to Java 8, and only the Java-8 update site is receiving updated versions of components. So this bug-fix was only uploaded to the Java-8 version of ImageJ2.

    The easiest way to get the Java-8 version of ImageJ2 is to simply download Fiji from http://fiji.sc/#download

    Alternately, you can:

    1. rename the java folder from your Fiji.app folder to java6
    2. make sure you have Java 8 installed
    3. launch Fiji again and make sure the status bar indicates "Java 1.8.0"
    4. run Help ▶ Update..., click "Manage update sites" and turn on the Java-8 site
    5. update and restart
  6. seanvw commented on Apr 20, 2016

    @seanvw

    @hinerm, @ctrueden

    Turning on the Java-8 site as you suggested fixed the issue for me. Thanks so much for the super rapid help.

    Now then, do i win a car or the goat? :)

  7. ctrueden commented on Apr 20, 2016

    @ctrueden
    Member

    Now then, do i win a car or the goat? :)

    Programming is like pinball. The reward for doing it well is the opportunity to do it again. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions