Best way to collect script @OUTPUT parameters

Hi Guys,

I am exploring the reuse of script with parameter annotation and I was wondering what was the best way to catch the output. Here is a dummy script (dummy.py)

# @String name1
# @String name2
# @OUTPUT String greeting1
# @OUTPUT String greeting2
 
greeting1 = "Hello, " + name1 + "!"
greeting2 = "Hello, " + name2 + "!"

After copying the script to my Fiji plugins directory I can call it from another script with

IJ.run("dummy","name1=aa name2=bb")

I was wondering if there was an equally simple way to call the script and collect output programatically. following the IJ Tutorial execute-commands I managed to collect the output with the following strategy:

  • based on script name discover the script path
  • use a scriptService to run the script and collect the outputs
    Is that the way to go ?

Also I had the following issues during my trial :

  • from Jython I did not manage to use the interface scriptservice.run(scriptPath, Process, InputMap) (it worked fine in Java). For some reason the content of my InputMap was not recognised independant whether I use a Jython dictionnary or instanciate a Hashmap (I could provide some code to illustrate that). I started playing with module but things were gettting a bit too complicated for me. Any clues?
  • How can I inform the script that output don’t need to pop up in the UI? Setting Process parameter to False will disable postProcessor Plugins but then I also loose preprocessors (and harvesting of missing parameters).

Thanks for your help and comments.

1 Like

As a follow up to my previous post,
Here is my attempt to run script in a generic way.
I run it on window, in the script editor of an up to date Fiji. solution 2 and 3 through an error in the console

#@ScriptService scriptservice

from java.io import File
from java.util import HashMap

import os

def runScript(scriptname, Process, **params) :

    # find script path based on script name
    # todo: check only one script is match
    scriptFile = None
    scriptList = scriptservice.getScripts()
    for scriptInfo in scriptList :
        basename = os.path.basename(scriptInfo.getPath() )
        basename = os.path.splitext(basename)[0]
        if basename == scriptName :
            myscriptInfo = scriptInfo
            scriptFile = File(scriptInfo.getPath())

    # if a corresponding script file was found run with the provided parameter
    if not scriptFile == None :
        # 1 - call the script with a pairs of key value as argument
        # (works fine but not generic)
        keys = params.keys()
        future = scriptservice.run(scriptFile, Process, keys[0], params[keys[0]], keys[1], params[keys[1]]) 

        # 2 - call the script with a dictionnary as argument (through an error: 
        # "[ERROR] Ignoring extraneous argument: {'name2': 'bb', 'name1': 'aa'}")
        #future = scriptservice.run(scriptFile, Process, params)

        # 3 - call the script with a java.util HashMap as argument (through an error: 
        # "[ERROR] Ignoring extraneous argument: {name1=aa, name2=bb}")
        #inputMap = HashMap()
        #for key, val in params.items() :
        #    inputMap.put( key, val )
        #print inputMap
        #future = scriptservice.run(scriptFile, Process, inputMap)

        # get the ouputs
        module = future.get()
        return module.getOutputs()


# main script #######################################################

scriptName = "multi_ouptput"
Process = True
outputs = runScript(scriptName, Process, name1="aa",name2="bb" )
print outputs
1 Like