Advanced Datatypes with Python Node in LabVIEW

A recent LinkedIn post on using Python and LabVIEW got me thinking about passing more advanced datatypes between LabVIEW and Python. I have written about the Python node before, but I hadn’t really played with advanced datatypes before, so I thought I would play around and come up with a demo.

Here is the python script I wrote. It is pretty simple. All the code can be found at https://gitlab.com/sas-blog/python-adv-datatypes The code is in LV2020 and requires Python 3.6.8

A simple python script.

Multiple Return Values

One of the tricks with the Python node is returning more than one value. LabVIEW only let’s you have one return value. However that one value can be a tuple which translates to a LabVIEW cluster and that cluster can be arbitrarily complex. It can contain nested clusters and arrays (Python lists – as long as all elements are the same datatype).

Here is an example of returning a complex datatype that is a series of nested clusters and arrays.

Passing Maps

Python uses dictionaries very heavily. As of 2019 LabVIEW now has maps. However you can’t just pass a LabVIEW map into Python as dictionary, or return a Python dictionary to LabVIEW as a Map. At least not directly. The trick is to pass the keys and values as separate arrays or lists and then reconstruct the dictionary or map on either side. Now there is still the caveat that for Maps in LabVIEW all the keys must be of the same type and all the values must be of the same type. So that still holds.

This VI shows how to pull the keys and values out of a map to pass it to python and how to reconstruct a map from a set of keys and values. For the Python side, check out the python code above.

Array Special Case

You may have noticed that the Python code has a corresponding output for each input. In general these just pass out the same value that was passed in. The exception to this is arrays. Python passes lists (~ the equivalent of arrays) by-reference. So any changes you make to an array in Python are reflected in the output array. This works even inside clusters.

Test Code for array special case. Note function has no return value, we are just passing the wires through.
Corresponding front panel. Note the only things that got modified were the arrays.

Need help integrating Python into your LabVIEW or TestStand project? We can help. Let’s schedule a call to discuss.

4 Comments on “Advanced Datatypes with Python Node in LabVIEW

  1. Hi Sam,
    can you tell me the advantage of using Python code in LabVIEW? Does Python calculate faster? Or is it just for reusing code?
    For what do you use it?

    Cheers
    Gregor

    • Thanks for the question. To be transparent I haven’t really used Python much from LabVIEW, but there a few use cases I see, mostly dealing with existing Python Code. So if you have modules already written in Python, why rewrite them in LabVIEW? That could be something internal, it could also be something from Python’s large ecosystem. Seems like there already exists a pip package for just about anything you want to do. Lots of machine learning, vision, wrappers for various APIs, etc. It’s like VIPM only a lot more packages. Also you could use it to collaborate. If you had a team member who only knew Python, you could teach them LabVIEW or you could just call their Python code in LabVIEW.

      I guess that would fall under reusing code. As far as performance, I have no idea which is faster. I do know there are some functions that would be difficult to write in LabVIEW that are easy to write in Python. Certain types of string manipulation being one.

      Danielle Jobe did a great presentation at GDevCon about using Python and LabVIEW. It’s worth checking out. You can find it on the GDevCon YouTube Channel. https://youtu.be/5jEYAMWpvkY
      Hope that answers your question.
      Sam

Leave a Reply

Your email address will not be published. Required fields are marked *

*