Python is the fastest growing programming language in terms of popularity especially in the engineering, scientific and computational domains. Python has 200,000+ open-source libraries that can be used across a wide variety of applications. Collimator is the first Python native modeling and simulation platform. It natively integrates with Python so engineers can use Python directly in their models and leverage open source libraries to streamline their development or verification and validation.
In this blog, we will review our favorite Python libraries and show you just how well they integrate with Collimator.
Numpy or Numerical Python is arguably the most popular Python library. Numpy is what solidifies Python as an excellent source for data science. It was built on older versions of Numeric and Numarray libraries and focuses on simplifying scientific and numerical computation on Python. Most data science and scientific packages are built on Numpy, including Pandas, SciPy, Matplotlib, Scikit-learn, TensorFlow, PyTorch and many others.
Because the core of NumPy’s package is built in C, Numpy enjoys the speed of well-optimized C code as well as the flexibility and ease of Python. As a result, Numpy performs just as well as any proprietary numerical computation tool. On top of this, Numpy, like R, MATLAB and Fortran, is built for array programming. These vector/array-oriented languages are useful in scientific and engineering settings because they treat the array as an object, in a similar fashion to scalers, matrices and vectors. Operations, like addition, multiplication and a number of others that run on scalers can also be run on matrices, vectors and higher-dimensional arrays.
Numpy revolves around the ndarray object, with this object you’re able to subtract, add, multiply and divide, as well as run logarithms, multiply by exponents and modular divide on vector-vector or matrix-vector contexts. You’re also able to run standard linear algebra algorithms like computing dot products, run decompositions, compute matrix eigenvalues, invert matrices, and solve algebraic equations
In Collimator, NumPy is able to do everything it can in a standalone environment. You’re able to use all of Numpy’s functions in the notebook contexts or in the Model Editor through a python block. For example, the code below converts image RGB values into a numpy array which is then converted into a tensor for neural network processing.
Since Collimator’s Python block allows matrixes as I/O variables you’re easily able to work complex data types into the Python block to run numerical computation with NumPy
Python Data Analysis Library or Panda’s is a data analysis library that lets you read and write data from different data structures into one Pandas data frame object. Like Numpy, parts of Pandas are written in Cython and C giving it the speed boost that pure Python would lack.
Pandas data frame object lets you slice, index, reshape, merge, or run time-series functions like data range generation, frequency conversion, window statistics, and even create domain-specific offsets on various forms of data.
By providing a standard data frame that is essentially backwards compatible with Numpy, Pandas unlocks the struggle of converting input data into a usable form in Python. JSON, CSV/Excel, HTML, XML, Latex, SPSS, even SQL tables read easily into a data frame object and can be pickled (saved) for later processing.
Pandas is a library that revolves around tables, making it very useful in the Collimator Notebook using the PyCollimator package. For example, you can export data generated from your notebook data as a data frame into a DataSource block:
And using the following Python code in a notebook:
You can place simulation data directly into the data source block.
Matplotlib is a library for creating animated, interactive or static visualizations of data in Python. Matplotlib is built on Numpy like Pandas and as a result it is easy to work across the three libraries. Creating a plot in Matplotlib from Pandas Data Frame requires very few lines of code and working from a Numpy array to a Data Frame object is just as simple.
Matplotlib lets you plot basic scatter, bar, stem, step, and stack plots. You can also plot arrays and fields into contour, barbs, stream plot and quivers. Statistics plots like boxplots, histograms, and pie plots as well as 3D plots like surface, wireframe and volumetric plots are also available with Matplotlib.
Graphs, plots, animations, and interactive images are Matplotlib’s standard output. As a result, implementation through the Model Editor is limited to the Notebook and Python script blocks. In the following example pulled from our inverted pendulum system tutorial:
The following code configures the variables of a pendulum model and plots the results using matplotlib.
Sklearn or Scikit-learn is a library built on top of Numpy, SciPy, and Matplotlib that provides various tools for machine learning in Python. Sklearn is one of the most useful and robust libraries for machine learning in Python. It provides many efficient tools for machine learning and statistical modeling.
Predictive data analysis. classification, regression, clustering, dimensionality reduction, model selection and preprocessing are all popular uses for Scikit-learning.
Sklearn provides extra tools to help analyze and improve the accuracy of machine learning models. Sklearn works uninhabited in Collimator. You’re able to run model analysis and improvement both in the Notebook and the Model Editor through script blocks. And since importing data is really easy with Collimator, you can easily work with already trained models within your project.
For example, the following code in a python script with X and Y corresponding to columns in a datafile will run a train test split on the data from the data source block for neural network training and testing.
The Python Control Systems Library or python-control is a package that focuses on implementing the basic analysis and design of feedback control systems in Python.
Python-controls lets you work with linear systems in state-space and frequency domains, nonlinear system modeling simulation and analysis, bode and Nyquist plots, control analysis like stability, reachability and observability and control design like eigenvalue placement, LQR, H2 and Hinf.
In Collimator, what the controls library offers is further customization, analysis, and design of feedback controllers. You can do all of this through the notebook, or through the model editor. A great example of how Collimator works with controls is our optimal cruise control tutorial. In this tutorial, the controls library is used throughout the model to assess the utility of the PID controller design.
Scientific Python or SciPy provides a vast array of functions for working with signals. SciPy Like Numpy and Pandas, works by wrapping up highly optimized functions in C and C++ into a Python shell.
SciPy offers a wide array of functions for signal processing, image manipulation, Fast Fourier Transforms (FFT’s), numerical integration, optimization and fit and interpolation.
The most popular use for SciPy is its Signal Processing module. Signal manipulation including filtering and spectral analysis are easily done using Collimator's Notebook.
The following model, adapted from our fir low pass filter tutorial, is called through a Collimator Notebook and the simulation result, a sine wave, is reshaped into a Numpy array for analysis in SciPy:
Open-Source Computer Vision Library or OpenCV is a computer vision and machine learning software library. It was built to enable computer vision/machine perceptions in applications. The library has over 2000 algorithms that help make it one of the largest resources for machine perception.
OpenCV has algorithms that can detect faces, identify objects, classify human actions, track camera movements, extract 3D models of objects, stitch images together and many more.
Working with OpenCV in Collimator is made easy by the inclusion of several blocks. The video source and image source blocks allow you to extract the RGB values from a video or image at any point in time and can be easily connected to a python script block for further processing.
Furthermore, the object detection block highlights Collimator’s goal to abstract the lower level visual processes from libraries like OpenCV into blocks.
The following piece of code taken from the polylanenet used on our lane keeping assist tutorial, we use OpenCV (cv2) to draw predicted lane edges over a video of a driving vehicle:
Generating this video output:
Like Sklearn, Google’s TensorFlow library is designed for training and deploying machine learning models. It offers multiple layers of abstraction over Sklearn thereby simplifying the building and training of models into much fewer lines of code.
Furthermore, TensorFlow allows you the flexibility of their distribution strategy API to train on different hardware configurations and their TFX module provides you with a full production ML pipeline. TensorFlow also provides platforms for mobile -TensorFlow lite and web environments - TensorFlow.js
TensorFlow models can easily be trained or deployed in Collimator. With the inclusion of the Scalar Broadcast block, you’re able to change scalar inputs into tensors for processing in your ML models:
PyTorch like TensorFlow is an open-source machine learning library that aims at providing a path for creating and deploying ai models in various environments.
PyTorch also features mobile kits, lets you run distributed training for asynchronous execution of functions and its TorchServe module makes deploying PyTorch models at scale easy.
PyTorch models can also be easily trained or deployed in Collimator. Feeding data to models is also easily done in Collimator.
The following lines of code within a Python script block. Unzip and load a Pytorch built neural network for inference.
The code is inserted inside the LaneDetector script block and fed RGB values of each frame of a video input. It is then able to infer the lane a vehicle is on.
Collimator’s Python package comes preinstalled in all Notebooks hosted by Collimator. It offers you an API to load models and run simulations from the notebook. Furthermore you can install PyCollimator on a standalone machine and call your simulations from that environment.
What makes this package really powerful is how easy it makes iterating over a simulation with different model parameters. For optimization problems, like PID controllers you can run hundreds of loops over a PID controller in order to determine the optimal values for your target response time and overshoot.
PyCollimator also effectively combines the Notebook and Model Editor environment. Through your notebook you’re able to see and call all models and blocks in your project and manipulate their parameters as you see fit. In the following example we attempt to try different parameters on a PID controller model in Collimator and plot their results using PyCollimator:
What has been your experience with these libraries in Collimator? Are there any others you’ve been using? Feel free to share with us on Twitter, Facebook or Linkedin or sign up to start using these libraries today.