Featured Post

Introduction to Python Tales

Hello my friends, my name is Nelson Carrasquel, and perhaps as you I have the philosophy of coding for life, since 2013 when I started programming with Python (I have started before in 2008 with Delphi and some Visual Basic, but that is my dark past) my value as a Developer really increased due to the things that I found that could be possible with Python, now Python (with some Javascript) are the core languages for almost every challenge that I face in the programming world. I am a Chemical Engineer graduated from the UCV (Central University of Venezuela) in 2015, was working as an engineer for almost two years for an Automation and Control Systems Company, programming with LabVIEW almost all the time, but also I was tutoring as a part time job, helping students from all around the world (Not all the world but all the America) in their Python and Javascript assignments and projects; I found out that I really enjoy teaching and tutoring programming languages, and from that on I was kin…

Scripting Python with LabVIEW

Hello my Pythoneers, in this set of entries I will explain the hard way where you can integrate Python Scripts in NI LabVIEW, but why the hard way? Because the easy way costs $749. Yes that’s right my friends, you can easily integrate Python with LabVIEW using a tool called Python Integration Toolkit for LabVIEW.

But you came here to find a way where you can use Python within LabVIEW with no cost at all, yes that’s right my friends, Python is and always will be, a free and open source tool for professional development of systems, so why to pay almost $800? There is no why; you just need to learn some practices to achieve what you have been looking for.

So let’s jump right into it. Here is the section titles in which these entries in divided by.
  • Problem definition.
  • Architecture in LabVIEW/Python Integration.
  • Turning Python into a Windows Executable.
  • Executing Windows Executable through LabVIEW.
  • Gathering all things up

Problem Definition


If you are using LabVIEW and Python it is really possible that you either are a scientist or an engineer, either you are a geek in math or communications, so for the explanations on this entries we are going to consider a typical problem found in our needs, this is regressions.
You would probably ask: “Ah? Regressions? I can do that with LabVIEW, Why Python?”, Well for the simplicity of this tutorial, we are going to choose regressions, and even simpler linear regressions, but what about if you face the challenge to implement Neural Networks, Natural Processing Language or even Machine Learning?, you rather pay thousands of dollars in unnecessary LabVIEW Toolkits, when you can use all of the Python Libraries for free? Awesome right? (In future entries we are going to cover also Neural Networks and Machine Learning)
In the many ways you can integrate Python and LabVIEW some problems are better by using an input-output scheme for a programming language into another; that is embedding a block of code that uses one language into another language.

So in this example we are going to embed a Python script for linear or non-linear regression as LabVIEW VI (Block) in a LabVIEW Program, as shown in the next scheme.

Architecture in LabVIEW/Python Integration




We are gathering the data sets using a LabVIEW VI as User Interface with some other parameters, feeding this information to the Python Executable (Block code), gathering its outputs with some UI Indicator in the same LabVIEW VI, and that’s it.
This architecture can be implemented in any problem that you might find Python useful in solving it; this scheme can be used by any situation where you need to use a Python Library for input/output or black box function.

Turning Python into a Windows Executable

I would like to point out here that isn’t a truly compilation as you would do with C++ or Basic. Instead you will use a tool to create a smaller version of the Python Interpreter that would only use those libraries required for the script to run; and will only execute the script that you will turn into an executable. This tool is called pyinstaller.
This tool will create a bunch of files and a .exe file that will be the one that you will “execute” in LabVIEW.

Before we turn something in Python into an executable we need first to understand how the communication of the python script is going to be with the outside world. If you turn a Python Script into executable, this program that will run with a black command windows screen, no Graphical User Interface, these kind of programs are executed using the command line windows, something like this.

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\My User>Script.exe  Param1 Param2

If you have use this kind of execution before, you can understand how to feed data into these programs, they are spaced separated values also called arguments, but how do we feed plenty of information to these program, let’s say data x and y to be regressed against a model?
One solution for this, since these arguments can only be strings values, is the implementation of a cross language format like JSON, which stands for JavaScript Object Notation, but this is just a caveat on how we are to proceed later, but first let’s take a look on inner part of the Python Script so how we can take those arguments and do something with them, like regressions.

First let’s take a look at the next Python Code

imports

from scipy.optimize import curve_fit
import numpy as np

datasets

strain = np.array([
    1.5,
    2.0,
    3.2,
    6.5,
    11.5,
    16.0,
    25.0,
    50.0,
    100.0
    ]) # [1/s]

shear = np.array([
    12.5,
    16.0,
    25.2,
    40.0,
    62.0,
    80.5,
    120,
    240,
    475
    ]) # [dynes/cm2]


# Power Law model
def f1(x, *args):

    p0 = args[0]
    p1 = args[1]

    return p0 * (x ** p1)

# Curve fitting for Newtonian model

initial_guess = (1,1,) # Two parameters
popt1, pcov1 = curve_fit(f1, strain, shear, initial_guess)

# Curve fitting for Power Law model

print list(popt1) # [5.7324898259669332, 0.95819030526948312]

As you can see, we want to make a regression of the data in the shear variable against the data in the strain variable using the curve_fit function from the scipy.optimize library set. If you run this with your local Python 2, you will get at the end the next printing.


[5.7324898259669332, 0.95819030526948312]


A list of parameters values which are the best fit parameters for the model of the power law, which is the function used in this example. Now want to change this Python Script for being used as a Windows Executable.

We need to remove the hard coded datasets from the script, but let’s also implement the JSON and argument interpretation from the outside world.

First we need to import the sys module to catch the arguments using the parameter sys.argv which is a Python List that will hold the arguments provided to the Python Script execution, the first argument sys.argv[0], would be the place for the actual script location in the system directory. From that all the next arguments are the arguments provided, in the example shown above sys.argv[1] and sys.argv[2] would be Param1 and Param2 respectively.
The code would have the next shape.

# imports
import sys
from scipy.optimize import curve_fit
import numpy as np
data = sys.argv[1]
# datasets
strain = data.x # [1/s]
shear = data.y # [dynes/cm2]
# Power Law model
def f1(x, *args):
    p0 = args[0]
    p1 = args[1]
    return p0 * (x ** p1)
# Curve fitting for Newtonian model
initial_guess = (1,1,) # Two parameters
popt1, pcov1 = curve_fit(f1, strain, shear, initial_guess)
# Curve fitting for Power Law model
print list(popt1) # [5.7324898259669332, 0.95819030526948312]

As you can see now we have also placed the assignments for strain and shear variables as if they come from parameters taken from the data variable. But remember arguments are strings values, so before these assignments we need to convert the data which is in JSON format, to a Python Data Structure, let’s use Python Dictionary for this. And this conversion is done by using the json module from the Python Built-in libraries.

The code would have the next shape.

# imports
import sys
import json
from scipy.optimize import curve_fit
import numpy as np
data = sys.argv[1]
data = json.loads(data)

# datasets
strain = data['x'] # [1/s]
shear = data['y'] # [dynes/cm2]
# Power Law model
def f1(x, *args):
    p0 = args[0]
    p1 = args[1]
    return p0 * (x ** p1)
# Curve fitting for Newtonian model
initial_guess = (1,1,) # Two parameters
popt1, pcov1 = curve_fit(f1, strain, shear, initial_guess)
# Curve fitting for Power Law model

print list(popt1) # [5.7324898259669332, 0.95819030526948312]

The loads method from the json modules handles for you the conversion from a JSON to a Python Dictionary, so in this way you can assign the x and y data sets. But for this to have meaning the data provided using the argument needs to have the next JSON structure, otherwise the execution will crash.

{
  "x": [
    1.5,
    2.0,
    3.2,
    6.5,
    11.5,
    16.0,
    25.0,
    50.0,
    100.0
  ],
  "y": [
    12.5,
    16.0,
    25.2,
    40.0,
    62.0,
    80.5,
    120,
    240,
    475
  ]
}

You can now have a better idea on how you can make the integration; at this point it is kind of obvious that you will require preprocessing your data in LabVIEW to a JSON format, before using the Windows Executable, but we will cover that later.

Now, what about the output? How do we pass the results? Well it is kind the same way; we need first to convert from Python Data Structure to a JSON format, using the dumps method of the json module, and we just print this JSON, later in LabVIEW we can catch this printing and process it, so our final code up to now will look like this.

# imports
import sys
import json
from scipy.optimize import curve_fit
import numpy as np
data = sys.argv[1]
data = json.loads(data)
# datasets
strain = data['x'] # [1/s]
shear = data['x'] # [dynes/cm2]
# Power Law model
def f1(x, *args):
    p0 = args[0]
    p1 = args[1]

    return p0 * (x ** p1)
# Curve fitting for Newtonian model

initial_guess = (1,1,) # Two parameters
popt1, pcov1 = curve_fit(f1, strain, shear, initial_guess)
# Curve fitting for Power Law model
print list(json.dumps(popt1))

This is great but what is the next step you would ask; well now we convert this Script into a Windows Executable.

First we need to install pyinstaller, you can use pip by executing this command line.

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\My User>pip install pyinstaller

This will download and install the pyinstaller module. Now how do we use it?, as simple as executing a command, but first you need to change your directory the location of the script that you want to make; and execute this command.

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\My User\Script Directory>pyinstaller script.py

This will take a while to make the executable, after that you will find two new directories within your script directory, build and dist folders, inside the dist folder you will find a directory with the same name as the script file name, inside this directory an bunch of files with also the executable, called script.exe, will have the same name as the Python Script filename, but with the extension .exe.
All the files in this directory are required by the .exe file to work properly, so in order to use the executable in the LabVIEW you will need to include all of these files in your LabVIEW Project, create a virtual subfolder first (recommended) and copy all the files in you LabVIEW Project directory, this just to have a better organization.

Well from now on, the rest of the work needs to be done in LabVIEW so let’s create a project for this, with a main VI and including all the Python generated files.



Now let’s create a simple user interface where we can input the data to be regressed with some indicators to show the json representation of the input and output of the Python execution, and finally an array indicator to show the resulting parameters.



Now let's open the Block Diagram and create a simple Event Pattern to execute the Python code when the Regress button is Clicked.



Executing Windows Executable through LabVIEW
Inside the Event Structure and the Mouse Down event for the Regress button we are going to place a SubVI that will make the call of the Regression.exe file, passing the inputs and gathering the outputs, this SubVI will have the following in the Block Diagram.



There are some interesting things to mention in the diagram, the first one is the replacement of all the doble quotes (") with a backslash before a double quote again (\"), this is because parameters in  a command line execution can be separated with double quotes, and since property names in JSON use double quotes, for instance

{"x":[1.5,2,3.2000000000000002,6.5,11.5,16,25,50,100],"y":[12.5,16,25.199999999999999,40,62,80.5,120,240,475]}

In this example, x and y, are property names, and they are surrounded by double quotes, this could be a conflict with the proper execution of the Python Script because the  Windows Command will split this entry into several parameters, we want only one parameter to express all the inputs as a JSON string. So to escape this behavior, we make this replacement in every double quote.

Another interesting thing is that we can execute this program, Regresion.exe as hidden, and last the standard output of the Windows Command execution is a multiline string, but the output of the Python Script is just one line, so the line that matters in this standard output is the first line, so we use a SubVI utility so we can convert a multiline string into an array of string, so in this way we can take the first index of the array. In the front panel we would have something like this.



Gathering all things up
Finally the Main VI Block Diagram would have the following



Since the output of the Python Program/Script is also in JSON format, we make the conversion into an array of doubles. And we will have the following in the Front Panel.



And when we make click in the Make Regression button, the regression parameters are obtained, and the result are displayed in the JSON Out String Indicator and also in the Parameters Double Array Indicator.


So I hope you have enjoyed this entry tutorial as much as I enjoyed writing it, come by the comments section and leave your comment, and don’t forget to share also. Your opinion is very important to me, thanks.