How to pass string between operators?

Haoyang
Haoyang New Altair Community Member
edited November 5 in Community Q&A
I am working on an extension where I will define a model in a string with some operator and then pass the model to a Python operator to parse the model. I noticed that I can only deliver IOObject through the output ports, how can I pass a string between ports? How can I pass a string from self-defined operators to a Python script operator as input?
Tagged:

Best Answers

  • CKönig
    CKönig New Altair Community Member
    Answer ✓
    Hi @Haoyang
    You can pass it as part of an ordinary example set, with one nominal attribute containing one example.
  • jwpfau
    jwpfau New Altair Community Member
    Answer ✓
    Hi,

    what you could also try:

    - Generate the complete python code and pass it as a FileObject (for the scr port) or call the Execute Python operator from your code
    - Use Macros 

    Greetings,
    Jonas

Answers

  • CKönig
    CKönig New Altair Community Member
    Answer ✓
    Hi @Haoyang
    You can pass it as part of an ordinary example set, with one nominal attribute containing one example.
  • Haoyang
    Haoyang New Altair Community Member
    Hi @CKönig 

    Could you give me some examples? I still have no idea.
    If I want to pass a string from input port to output port, what should I do?
    And if I want to process the string in a python script operator, how should I format the string in the former output port?

    Thank you!
  • CKönig
    CKönig New Altair Community Member
    edited November 2023
    Here is an example process how to take the strings from a dataset into python, work with them and return them to RM again.

    <?xml version="1.0" encoding="UTF-8"?><process version="10.2.000">
      <context>
        <input/>
        <output/>
        <macros/>
      </context>
      <operator activated="true" class="process" compatibility="10.2.000" expanded="true" name="Process">
        <parameter key="logverbosity" value="init"/>
        <parameter key="random_seed" value="2001"/>
        <parameter key="send_mail" value="never"/>
        <parameter key="notification_email" value=""/>
        <parameter key="process_duration_for_mail" value="30"/>
        <parameter key="encoding" value="SYSTEM"/>
        <process expanded="true">
          <operator activated="true" class="retrieve" compatibility="10.2.000" expanded="true" height="68" name="Retrieve Golf" width="90" x="45" y="34">
            <parameter key="repository_entry" value="//Samples/data/Golf"/>
          </operator>
          <operator activated="true" class="blending:select_attributes" compatibility="10.2.000" expanded="true" height="82" name="Select Attributes" width="90" x="179" y="34">
            <parameter key="type" value="include attributes"/>
            <parameter key="attribute_filter_type" value="one attribute"/>
            <parameter key="select_attribute" value="Outlook"/>
            <parameter key="select_subset" value=""/>
            <parameter key="also_apply_to_special_attributes_(id,_label..)" value="true"/>
            <description align="center" color="transparent" colored="false" width="126">Keep the nominal attribute &amp;quot;Outlook&amp;quot;, where every entry can be considered a &amp;quot;String&amp;quot;</description>
          </operator>
          <operator activated="true" class="python_scripting:execute_python" compatibility="10.0.001" expanded="true" height="103" name="Execute Python" width="90" x="313" y="34">
            <parameter key="script" value="import pandas&#10;&#10;# rm_main is a mandatory function, &#10;# the number of arguments has to be the number of input ports (can be none),&#10;#     or the number of input ports plus one if &quot;use macros&quot; parameter is set&#10;# if you want to use macros, use this instead and check &quot;use macros&quot; parameter:&#10;#def rm_main(data,macros):&#10;def rm_main(data):&#10;    &#10;    # add a new column with reversed strings of column &quot;Outlook&quot;&#10;    data[&quot;Outlook_reversed&quot;]=data[&quot;Outlook&quot;].str.split('').apply(lambda x: ''.join(x[::-1]))&#10;&#10;    return data&#10;"/>
            <parameter key="notebook_cell_tag_filter" value=""/>
            <parameter key="use_default_python" value="true"/>
            <parameter key="package_manager" value="conda (anaconda)"/>
            <parameter key="use_macros" value="false"/>
          </operator>
          <connect from_op="Retrieve Golf" from_port="output" to_op="Select Attributes" to_port="example set input"/>
          <connect from_op="Select Attributes" from_port="example set output" to_op="Execute Python" to_port="input 1"/>
          <connect from_op="Execute Python" from_port="output 1" to_port="result 1"/>
          <portSpacing port="source_input 1" spacing="0"/>
          <portSpacing port="sink_result 1" spacing="0"/>
          <portSpacing port="sink_result 2" spacing="0"/>
        </process>
      </operator>
    </process>

  • Haoyang
    Haoyang New Altair Community Member
    @"CKönig"

    Thank you for the example. 

    However I am working on an extension here, and I guess I can only pass a string like "This is a String" with an IOTable under an attribute "XX"?

    At this time I built a new IO type to pass the string between non-python operators, if python operator can only get data from a pandas data frame, maybe I should follow the IOTable way of doing this.
  • jwpfau
    jwpfau New Altair Community Member
    Answer ✓
    Hi,

    what you could also try:

    - Generate the complete python code and pass it as a FileObject (for the scr port) or call the Execute Python operator from your code
    - Use Macros 

    Greetings,
    Jonas