MeasurementLink Support for Python
Project description
MeasurementLink™ Support for Python
- MeasurementLink™ Support for Python
Introduction
MeasurementLink Support for Python (ni-measurement-service
) is a python framework that enables measurement developers to quickly create python measurements and run them as a service (gRPC).
Dependencies
- Python >= 3.8 (3.9 recommended)
- grpcio >= 1.41.1, < 2.x
- protobuf >= 3.20, < 4.x
- pywin32 >= 303 (Only for Windows)
Examples
The examples
directory contains the following example projects:
sample_measurement
: Performs a loopback measurement with various data types.nidaqmx_analog_input
: Performs a finite analog input measurement with NI-DAQmx.nidcpower_source_dc_voltage
: Sources and measures a DC voltage with an NI SMU. Provides a Measurement UInidcpower_source_dc_voltage_with_labview_ui
: Sources and measures a DC voltage with an NI SMU. Provides a LabVIEW UInidmm_measurement
: Performs a measurement using an NI DMM.nifgen_standard_function
: Generates a standard function waveform using an NI waveform generator.niscope_acquire_waveform
: Acquires a waveform using an NI oscilloscope.niswitch_control_relays
: Controls relays using an NI relay driver (e.g. PXI-2567).
Setting up the Example Measurements
The example measurements shared are poetry-based projects. Follow the below steps to for setting up the example measurement:
-
Install
poetry
. Refer to https://python-poetry.org/docs/#installation for information on installing poetry. -
Open a command prompt, and change the working directory to the directory of the example measurement you want to work with.
cd <path_of_example_measurement> REM Example: cd "..\measurement-services-python\examples\dc_measurement"
-
Run
poetry install
. This command creates/updates the .venv and installs all the dependencies(includingni-measurement-service
package) needed for the Example into.venv
poetry install
- If you get a "command not found" error during
poetry install
, make sure that you added the Poetry path to the system path. Refer to https://python-poetry.org/docs/#installing-with-the-official-installer/Add-poetry-to-your-path
- If you get a "command not found" error during
Executing the Example Measurements
-
Start the discovery service if not already started.
-
Use
poetry run
to run the measurement service:poetry run python measurement.py
Developing Measurements: Quick Start
This section provides instructions to develop custom measurement services in Python using MeasurementLink Support for Python.
Installation
Make sure the system has the recommended python version is installed. Install MeasurementLink Support for Python using pip.
REM Activate the required virtual environment if any.
pip install ni-measurement-service
Check if you have installed the expected version of MeasurementLink Support for Python installed by running the below command:
pip show ni-measurement-service
Developing a minimal python measurement
- Install the
ni-measurement-generator
package.
REM Activate the required virtual environment if any.
pip install ni-measurement-generator
-
Run the
ni-measurement-generator
tool. Use command line arguments to specify thedisplay-name
and optionally theversion
,measurement-type
, andproduct-type
.- Running
ni-measurement-generator
without optional arguments:
ni-measurement-generator SampleMeasurement
'SampleMeasurement' is the display name of your measurement service. Without the optional arguments, the other arguments are generated for you based on the display name.
- Running
ni-measurement-generator
with optional arguments formeasurement-version
,ui-file
,service-class
, anddescription-url
:
ni-measurement-generator SampleMeasurement --measurement-version 0.1.0.0 --ui-file MeasurementUI.measui --service-class SampleMeasurement_Python --description-url https://www.example.com/SampleMeasurement.html
- Running
ni-measurement-generator
with optional argument fordirectory-out
ni-measurement-generator SampleMeasurement --directory-out <new_path_for_created_files>
If no output directory is specified, the files will be placed in a new folder under the current directory named after the display name without spaces.
- Running
-
To customize the created measurement, provide metadata of the measurement's configuration(input parameters) and outputs(output parameters) in
measurement.py
.-
Use the
configuration()
decorator to provide metadata about the configurations.The order of the configuration decorator must match with the order of the parameters defined in the function signature.@foo_measurement_service.register_measurement #Display Names can not contains backslash or front slash. @foo_measurement_service.configuration("DisplayNameForInput1", DataType.String, "DefaultValueForInput1") @foo_measurement_service.configuration("DisplayNameForInput2", DataType.String, "DefaultValueForInput2") def measure(input_1, input_2): ''' A simple Measurement method''' return ["foo", "bar"]
-
Use the
output()
decorator to provide metadata about the output.The order of the output decorators from top to bottom must match the order of the values of the list returned by the function.@foo_measurement_service.register_measurement @foo_measurement_service.configuration("DisplayNameForInput1", nims.DataType.String, "DefaultValueForInput1") @foo_measurement_service.configuration("DisplayNameForInput2", nims.DataType.String, "DefaultValueForInput2") @foo_measurement_service.output("DisplayNameForOutput1", nims.DataType.String) @foo_measurement_service.output("DisplayNameForOutput2", nims.DataType.String) def measure(input_1, input_2): return ["foo", "bar"]
-
-
Run/Debug the created measurement by following the steps discussed in the section "Steps to run/debug the measurement service".
Steps to run/debug the measurement service
-
Start the discovery service if not already started.
-
(Optional)Activate related virtual environments. Measurement developers can skip this step if they are not using any virtual environments or poetry-based projects.
.venv\scripts\activate
-
After successful activation, you can see the name of the environment,
(.venv)
is added to the command prompt. -
If you face an access issue when trying to activate, retry after allowing scripts to run as Administrator by executing the below command in Windows PowerShell:
Set-ExecutionPolicy RemoteSigned
-
-
To stop the running measurement service, press
Enter
in the terminal to properly close the service. -
(Optional)After the usage of measurement, deactivate the virtual environment. Measurement developers can skip this step if they are not using any virtual environments or poetry-based projects.
deactivate
Static Registration of Python Measurements
Refer to the Static Registration of measurements section for the detailed steps needed to statically register a measurement.
To Statically register the examples provided, the user can copy the example directory with the service config file with the startup batch file, to the search paths and follow the Setting up the Example Measurements section to set up the measurements.
Note: The startup batch file can be modified accordingly if the user wants to run with a custom python distribution or virtual environment
Create a batch file that runs a python measurement
The batch file used for static registration is responsible for starting the Python Scripts.
Typical Batch File:
"<path_to_python_exe>" "<path_to_measurement_file>"
Examples to start the fictitious file named foo_measurement.py
:
-
Using the Python system distribution
python foo_measurement.py
-
Using the virtual environment
REM Windows .\.venv\Scripts\python.exe foo_measurement.py REM Linux .venv/bin/python foo_measurement.py
Create Executable for Python Scripts
To create an executable from a measurement, measurement authors can use the pyinstaller tooling. During the executable creation, the user can also embed the User Interface file using the --add-data "<path_of_the_UI_File>;."
.
Typical Pyinstaller command to build executable.
pyinstaller --onefile --console --add-data "<path_of_the_UI_File>;." --paths .venv\Lib\site-packages\ <path_of_the_measurement_script>
API References
Click here to view the API reference documentation.
Appendix: Managing Measurement as Python Package(Project)
Measurement and its related files can be maintained as a python package. The basic components of any Python Measurement Package are:
-
Measurement Python Module(.py file)
- This file contains all the details related to the measurement and also contains the logic for the measurement execution.
- This file is run to start the measurement as a service.
-
UI File
- UI file for the Measurement. Types of supported UI files are:
- Measurement UI(.measui): created using the Measurement UI Editor application.
- LabVIEW UI(.vi)
- The path of this file is configured by
ui_file_path
inmeasurement_info
variable definition in Measurement Python Module(.py file).
- UI file for the Measurement. Types of supported UI files are:
Python communities have different ways of managing a python package and its dependencies. It is up to the measurement developer, on how they wanted to maintain the package and dependencies. Measurement developers can choose from a few common approaches discussed below based on their requirements.
Note: Once we have the template support for Python measurement, the approach to managing the python measurement package(project) will be streamlined and simplified.
Create and Manage Python Measurement Package using poetry
-
Setting up Poetry(One-time setup)
-
Make sure the system has the recommended python version installed.
-
Install the
poetry
using the installation steps given in https://python-poetry.org/docs/#installation.
-
-
Create a new python project and add
ni-measurement-service
as a dependency to the project.-
Open a command prompt, and change the working directory to the directory of your choice where you want to create the project.
cd <path_of_directory_of_your_choice>
-
Create a python package(project) using the poetry new command. Poetry will create boilerplate files and folders that are commonly needed for a python project.
poetry new <name_of_the_project>
-
Add the
ni-measurement-service
package as a dependency using the poetry add command.cd <name_of_the_project> poetry add ni-measurement-service
-
The virtual environment will be auto-created by poetry.
-
Create measurement modules as described in "Developing a minimal python measurement"
-
Any additional dependencies required by measurement can be added using add command.
poetry add <dependency_package_name>
-
-
For detailed info on managing projects using poetry refer to the official documentation.
Create and Manage Python Measurement Package using venv
-
Make sure the system has the recommended python version installed.
-
Open a command prompt, and change the working directory to the directory of your choice where you want to create a project.
cd <path_of_directory_of_your_choice>
-
Create a virtual environment.
REM This creates a virtual environment named .venv python -m venv .venv
-
Activate the virtual environment. After successful activation
.venv\scripts\activate REM Optionally upgrade the pip within the venv by executing the command python -m pip install -U pip
-
Install the
ni-measurement-service
package into the virtual environment.pip install ni-measurement-service
-
Create measurement modules as described in "Developing a minimal python measurement"
-
Any additional dependencies required by measurement can be added pip install.
pip install <dependency_package_name>
-
For detailed info on managing projects with a virtual environment refer to the official documentation.
Create and Manage Python Measurement Package by directly installing ni-measurement-service
as a system-level package
Measurement developers can also install ni-measurement-service
as a system package if necessary.
-
Install the
ni-measurement-service
package from the command promptpip install ni-measurement-service
-
Create measurement modules as described in "Developing a minimal python measurement"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ni_measurement_service-0.11.1.tar.gz
.
File metadata
- Download URL: ni_measurement_service-0.11.1.tar.gz
- Upload date:
- Size: 57.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f566b76171e52d201292d78a2b25535776592bbb7c34b5bcb1b7fb38c2ea3ba |
|
MD5 | e2d691277f858bfb51a600de63f4b66c |
|
BLAKE2b-256 | 679ba01e992918b5e0a2f8e4f2282db8a53d386a7cf213633a57b3bd5c8bfb7a |
File details
Details for the file ni_measurement_service-0.11.1-py3-none-any.whl
.
File metadata
- Download URL: ni_measurement_service-0.11.1-py3-none-any.whl
- Upload date:
- Size: 70.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.9.15 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77416758025b9d84840be25eefe39cb61c1c6ee5914ea9ce2f3202aa4bbb3938 |
|
MD5 | c653ed009cb24e858ac865a826fe45aa |
|
BLAKE2b-256 | cd8639656174759d39cb4a10967912cdf753af28b7c8c1e7e8ebcc659f165aaf |