TypeError while using PythonActor in BCVTB

397 views
Skip to first unread message

Kopal Nihar

unread,
Jun 12, 2017, 8:16:16 AM6/12/17
to bcvtb
Hi, I was trying to learn how to use python actor in BCVTB. So I tried to take two constants ( x and y ), pass them as variables to python actor, add them using python script and plot a graph which should be a constant line. The python script that I have below gives me a TypeError. I can't seem to understand why '+' is not being recognised as an operand. I am also attaching the screenshots of individual actors and the system xml file. Can you please tell me where my problem lies ? My python script looks like:
class Main :
  def fire(self) :
    if not self.input.hasToken(0) :
      return
    t = self.input.get(0)
    x = self.cons2.get(0)
    y = int(t) + int(x)
    self.output.broadcast(t)

I am also attaching my xml file
model.xml

Christopher Brooks

unread,
Jun 12, 2017, 10:29:21 AM6/12/17
to bc...@googlegroups.com, Kopal Nihar

Hi Kopal,

Try

from ptolemy.data import IntToken


class Main :
  def fire(self) :
    if not self.input.hasToken(0) :
      return
    t = self.input.get(0)

    print(type(t))
    x = self.cons2.get(0)
    y = int(t.doubleValue()) + int(x.doubleValue())
    self.output.broadcast(IntToken(y))
I've attached an updated version.

What is happening here is that t and x are of type DoubleToken.

The print(type(t)) line prints

<type 'ptolemy.data.DoubleToken'>
So, you need to invoke the doubleValue() method of DoubleToken to get a double value.

Then, to broadcast, it is necessary to create an IntToken.

I'm not much of a Python programmer these days, but one thing that helped me was to right click on the Python actor and select Documentation->GetDocumentation and then click on Demo Usage

The demos that use the Python actor are at

You might also want to look at tests in $PTII/ptolemy/actor/lib/python/test/auto/

See also https://kepler-project.org/developers/reference/python-and-kepler

_Christopher

--
You received this message because you are subscribed to the Google Groups "bcvtb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

model.xml

Thierry Nouidui

unread,
Jun 12, 2017, 3:38:28 PM6/12/17
to bc...@googlegroups.com
I suggest using the SystemCommand actor to run your Python script since I remembered you wanting to use numpy. This won't be possible if you are using the PythonActor I think.

Thanks!

Thierry

--
You received this message because you are subscribed to the Google Groups "bcvtb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
--------------------------------------
Thierry Stephane Nouidui, PhD

Simulation Research Group
Lawrence Berkeley National Laboratory
1 Cyclotron Road, MS 90R3147
Berkeley, CA 94720
(510) 495-2337 voice
email: TSNo...@lbl.gov
http://simulationresearch.lbl.gov
----------------------------------------

Christopher Brooks

unread,
Jun 12, 2017, 3:47:20 PM6/12/17
to bc...@googlegroups.com, Thierry Nouidui

Yes, the Ptolemy Python actors use Jython, which is a Java implementation of Python.  Jython does not support C-based Python tools like Numpy.  Thus, the Ptolemy Python actor does not currently support Python Numpy

I believe Eclipse has a socket-based interface to a C-based Python that could be used from Java.  I believe http://dawnsci.org/ uses it.  It might be https://www.py4j.org/, or it could be something else.

So, if someone wanted to get a Python actor working with https://www.py4j.org/, the Numpy would be available.

To see how it is done for the R Language, see ptolemy/actor/lib/r/RExpression2.java

_Christopher

To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

Kopal Nihar

unread,
Jun 12, 2017, 3:50:43 PM6/12/17
to bc...@googlegroups.com, Thierry Nouidui
I thought I would use python actor to call my own system python and then use the libraries. Is that possible ? What should I use ? SystemCommand or Python Actor ? 

You received this message because you are subscribed to a topic in the Google Groups "bcvtb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bcvtb/N2RhjNb-Cbo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bcvtb+unsubscribe@googlegroups.com.

Thierry Nouidui

unread,
Jun 12, 2017, 3:55:25 PM6/12/17
to Kopal Nihar, bc...@googlegroups.com
I would think that using the SystemCommand actor should be more than sufficient.

Kopal Nihar

unread,
Jun 12, 2017, 3:58:25 PM6/12/17
to Thierry Nouidui, bc...@googlegroups.com
Okay, thanks a lot Thierry. I will try using the SystemCommand Actor and get back to you. Thank you so much

Kopal

Kopal Nihar

unread,
Jun 12, 2017, 4:02:56 PM6/12/17
to bc...@googlegroups.com, cxbr...@gmail.com
Thanks a lot Christopher. I will try using the SystemCommand actor to call my system python and get back to you. 

Kopal

On Jun 13, 2017 1:17 AM, "Christopher Brooks" <cxbr...@gmail.com> wrote:
You received this message because you are subscribed to a topic in the Google Groups "bcvtb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bcvtb/N2RhjNb-Cbo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bcvtb+unsubscribe@googlegroups.com.

Julien MARREC

unread,
Jun 13, 2017, 1:27:33 AM6/13/17
to bc...@googlegroups.com, Thierry Nouidui
If you need to include conditions for eg, then it might be easier to use the python actor (I've done that to trigger an external script every 24h of simulation for example, and leave the input untouched otherwise). 
Otherwise use the systemCommand

Envoyé de mon iPhone
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

Kopal Nihar

unread,
Jun 13, 2017, 2:02:03 AM6/13/17
to bc...@googlegroups.com
Hi Julien.I want my script to keep running throughout and send some commands to keep changing EMS schedules every timestep. I will require the use of python libraries too. So should SystemCommand actor be fine then ? 

Julien Marrec

unread,
Jun 13, 2017, 5:05:32 AM6/13/17
to bc...@googlegroups.com
Yes that's probably fine to use systemActor then.


--
Julien Marrec, EBCP, BPI MFBA
Owner at EffiBEM
T: +33 6 95 14 42 13

LinkedIn (en) | (fr) :

Kopal Nihar

unread,
Jun 13, 2017, 10:12:08 AM6/13/17
to bcvtb
Okay, thanks a lot Julien. I will try using the subproces.Popen from systemCommancd actor only.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
--------------------------------------
Thierry Stephane Nouidui, PhD

Simulation Research Group
Lawrence Berkeley National Laboratory
1 Cyclotron Road, MS 90R3147
Berkeley, CA 94720
(510) 495-2337 voice
email: TSNo...@lbl.gov
http://simulationresearch.lbl.gov
----------------------------------------

--
You received this message because you are subscribed to the Google Groups "bcvtb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "bcvtb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bcvtb/N2RhjNb-Cbo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bcvtb+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "bcvtb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "bcvtb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bcvtb/N2RhjNb-Cbo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bcvtb+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "bcvtb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+un...@googlegroups.com.

Kopal Nihar

unread,
Jun 14, 2017, 4:53:09 AM6/14/17
to bc...@googlegroups.com, Thierry Nouidui, Christopher Brooks
I tried running python code from SystemCommand Actor. I followed the documentation and even edited build, history.xml files. However, I can't seem to generate an executable for my python code. My python script is just trying to print the command line arguments. But I get a "file not found: 0.7" where 0.7 is one argument that I am passing. I am attaching my xml file. Please help me with this.

To unsubscribe from this group and all its topics, send an email to bcvtb+unsubscribe@googlegroups.com.
pytest.py
build.xml
history.xml
model.xml

Kopal Nihar

unread,
Jun 14, 2017, 10:50:30 AM6/14/17
to bc...@googlegroups.com
Also I need to get some output values from energyplus every time step. Could you please give me an idea how it can be done ? 

Thierry Nouidui

unread,
Jun 14, 2017, 2:32:58 PM6/14/17
to Kopal Nihar, bc...@googlegroups.com, Christopher Brooks
Your configuration is wrong.
  • ProgramName should be python or the path to the Python executable so the SystemCommand knows which programs to invoke.
  • ProgramArguments should be "pytest.py $x $y".
No need to edit build.xml and history.xml files.

Thanks!

Thierry

Thierry Nouidui

unread,
Jun 14, 2017, 2:33:55 PM6/14/17
to bc...@googlegroups.com
I suggest you look at the EnergyPlus examples. 

Thanks!

Thierry

To unsubscribe from this group and stop receiving emails from it, send an email to bcvtb+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Julien MARREC

unread,
Jun 14, 2017, 6:10:57 PM6/14/17
to bc...@googlegroups.com
Subprocess is a python thing, only useful if you use a PythonActor, not the systemCommand

Envoyé de mon iPhone

Kopal Nihar

unread,
Jun 15, 2017, 1:48:59 AM6/15/17
to bcvtb
Okay, thanks Thierry! 

Kopal Nihar

unread,
Jun 15, 2017, 1:51:31 AM6/15/17
to bcvtb
Okay, thanks Julien. I am trying some things right now. I will get back to you once I land in some problems.

Kopal Nihar

unread,
Jun 16, 2017, 6:55:31 AM6/16/17
to bcvtb
So I tried running the Eplus actuator example with my own python script instead of the controller. I tried to output yShade using sys.exit(yShade) in python. But it gives me a termination error. Is it necessary to write to a file and parse it when using system command actor ? Or else, what are the other alternatives so that I can directly send the value of yshade to energyplus ?

Banafsheh Pouyanfar

unread,
Aug 15, 2020, 9:08:43 PM8/15/20
to bcvtb
Hey all, 
I'm trying to do the same thing, running a simulation with a python script in SystemCommand and then send the output to energyplus and then the energyplus output again to the systemcommand actor. what I'm running into now is this error in ep.err : ** Severe  ** ExternalInterface: Received "1" double values, expected "2".

Anyone could help me fix this?? 

Bani
Reply all
Reply to author
Forward
0 new messages