[kepler-users] Set parameters using python/jython?

Christopher Brooks cxh at eecs.berkeley.edu
Mon Oct 21 15:03:14 PDT 2013


Hi Jonathan,

Ptolemy has a Python actor that uses Jython

http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIfaq.htm#Scripting says:

> *5.9 Is there a scripting interface to Ptolemy II ?*
>     The PythonScript actor includes and interface to Jython
>     <http://www.jython.org/#in_browser>, a Java implementation of
>     Python. The PythonScript actor allows the user to define an actor
>     on the fly by defining the |fire()| and other methods in Python.
>     Using an interpreted language in this context instead of defining
>     actors in Java means that we need not recompile and restart
>     Ptolemy II to make a change to an actor. The tradeoff is that a
>     PythonScript actor will run more slowly than a similar actor
>     implemented in Java.
>     For details, see
>     |http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIlatest/ptII/doc/codeDoc/ptolemy/actor/lib/python/PythonScript.html|
>
>
>     The Ptolemy II test suite uses Jacl, a Java implementation of Tcl
>     as a scripting language. With Jacl, we can build and run models
>     using an interpreted scripting environment without compilation and
>     avoiding the usual edit/compile/run loop.
>     For details, see the Testing link in
>     |http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIlatest/ptII/doc/coding/|
>     <http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIlatest/ptII/doc/coding/index.htm>
>
>

I'm not that up on how Jython accesses Java objects, but in theory one 
could write an actor that gets the container object, then finds a 
parameter and then calls setExpression o the parameter.  This could 
happen at runtime.  However, this is difficult to get right. The 
SetVariable actor does something similar and it is not an easy actor to 
write.  A Jython-based actor that set parameter values in the container 
would need to be very similar to SetVariable.  See 
http://ptolemy.eecs.berkeley.edu/ptolemyII/ptII8.0/ptII/doc/codeDoc/ptolemy/actor/lib/SetVariable.html

If you are trying to do a parameter sweep, then 
http://ptolemy.eecs.berkeley.edu/ptolemyII/ptIIfaq.htm#parameterSweeps says:

> *4.11 How do a do a parameter sweep?*
>     A Parameter Sweep is when a model is run a number of times with
>     different parameters to perhaps find an optimal solution. For
>     details, see
>     http://chess.eecs.berkeley.edu/ptexternal/wiki/Main/ParameterSweeps <http://chess.eecs.berkeley.edu/ptexternal/wiki/Main/ParameterSweeps#in_browser>.

Another idea is to have Jython set the parameters and then run the 
model.  We do similar things using Tcl in the test case.  See 
ptolemy/moml/test/MoMLParser.tcl

This test from ptolemy/actor/lib/test/Test.xml loads a .xml file and runs it

> test Test-3.0 {Test case where training and we get no data} {
>     # Thomas Mandl pointed out this bug and supplied a fix
>     set workspace [java::new ptolemy.kernel.util.Workspace "Test3_0"]
>     set parser [java::new ptolemy.moml.MoMLParser $workspace]
>     $parser reset
>     $parser purgeAllModelRecords
>     $parser setMoMLFilters [java::null]
>     $parser addMoMLFilters \
>             [java::call ptolemy.moml.filter.BackwardCompatibility 
> allFilters]
>
>     $parser addMoMLFilter [java::new \
>             ptolemy.moml.filter.RemoveGraphicalClasses]
>     #set model3_0 \
>     #    [java::cast ptolemy.actor.TypedCompositeActor [$parser 
> parseFile BooleanSwitch\
> _RegressionTest.xml]]
>     set model3_0 \
>          [java::cast ptolemy.actor.TypedCompositeActor [$parser 
> parseFile BooleanSwitch\
> _RegressionTest_WrongTrainingValues.xml]]
>     set manager [java::new ptolemy.actor.Manager $workspace 
> "test3_0Manager"]
>     $model3_0 setManager $manager
>     $manager execute
>
>     # Get the corrrectValues parameter, which should be {}
>     set test2 [$model3_0 getEntity Test2]
>     set correctValues [java::cast ptolemy.data.expr.Parameter [$test2 
> getAttribute corr\
> ectValues]]
>
>     list [$correctValues getExpression]
>
> } {{{}}}

Later in that file we get the Test2 entity
>    set test2 [$model3_0 getEntity Test2]
and the get the "correctValues" parameter
>     set correctValues [java::cast ptolemy.data.expr.Parameter [$test2 
> getAttribute correctValues]]
To get the value of the correctValues parameter, call "$correctValues 
getExpression".

To set the value
$correctValues setExpression "{1,2}"

It is probably possible to do something similar in Jython, where you 
instantiate the model by calling the parser  set the variables and run 
it.  You could use the Recorder actor to get the outputs of the model, 
or write the data using ExpressionWriter or LineWriter.

_Christopher

On 10/21/13 10:51 AM, Jonathan Boright wrote:
> Hi All,
>
> I'm wondering if there is a way to set workflow parameters using 
> python code? I see the ParameterSet actor, but I'd like to set 
> parameters dynamically using python instead of using a static text 
> file...
>
> Thanks for your help.
>
> Jon
>
>
> ---------------------------------
> Jonathan Boright
> Research Scientist
> ISciences, LLC
> 61 Main Street, Suite 200
> Burlington, VT 05401
>
>
>
> _______________________________________________
> Kepler-users mailing list
> Kepler-users at kepler-project.org
> http://lists.nceas.ucsb.edu/kepler/mailman/listinfo/kepler-users


-- 
Christopher Brooks, PMP                       University of California
Academic Program Manager & Software Engineer  US Mail: 337 Cory Hall
CHESS/iCyPhy/Ptolemy/TerraSwarm               Berkeley, CA 94720-1774
cxh at eecs.berkeley.edu, 707.332.0670           (Office: 545Q Cory)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nceas.ucsb.edu/kepler/pipermail/kepler-users/attachments/20131021/abc4975c/attachment.html>


More information about the Kepler-users mailing list