No items found.
December 2, 2022

Top 10 Python Libraries to use with Collimator

Our Top 10 Picks of Python Libraries to use with Collimator

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.

#Convert to PyTorch tenser
import torch
import numpy as np

image = np.array([R,G,B], dtype=np.float32)
tensor = torch.from_numpy(image)
tensor = tensor.unsqueeze(0)

Converting an image to a Tensor

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:

Calculating the difference between a sine wave and the data in a datasource block

And using the following Python code in a notebook:

import collimator
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create some data in python
times = np.arange(0,20,0.1)
values = np.sin(times)
df = pd.DataFrame(values, index=times)

# Load the model
model = collimator.load_model('data source')

# Assign the data to the block
ds = model.find_block('DataSource')

# Run simulation and show results

sim = collimator.run_simulation(model)
res = sim.results.to_pandas()

You can place simulation data directly into the data source block.

Since the data source block = the sine wave, the adder result is 0


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.

IP_sys_closed =*K, B, C, D, states=['pos','pos_d','ang', 'ang_d'], 
                        inputs=['r'], outputs=['pos','ang'])
T, yout = ctrl.step_response(IP_sys_closed,T=np.arange(0, 5, 0.01))

plt.rcParams["figure.figsize"] = (12,8)
fig, (ax1, ax2) = plt.subplots(2)
ax1.plot(T, yout[0,:].T)
ax2.plot(T, yout[1,:].T)

Inverted pendulum position and angle over time


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.

from sklearn.model_selection import train_test_split
train_test_ratio = 0.9
X = data[“col_1”]
y = data[“col_2”]
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size = 1-train_test_ratio, random_state = 0)


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.

Cruise Control Model
plt.rcParams["figure.figsize"] = (12,8)
plt.ylabel("Control Action")
cruise_closed_u =,cruise_tf)
T, u = ctrl.step_response(10*cruise_closed_u,T=np.arange(0, 8, 0.01))

Cruise Controller Feedback


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:

Noise Filtration Model
# retrieving block parameters and calculating the sample rate and number of samples
signal_model = C.load_model('Signal Processing')
sample_rate = 1/signal_model.configuration.discrete_step
nsamples = signal_model.configuration.stop_time*sample_rate

sound = C.run_simulation(signal_model).results
time = sound['time'].to_numpy()
sinewave = sound['signal'].to_numpy().reshape(len(time)) # ensure this is reshaped into 1D array

# visualizing the frequency response of our noisy signal
w,h = sp.signal.freqz(sinewave)
pb.plot(w*sample_rate, abs(h))

Frequency Response off Model


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:

# draw left and right lanes
for current_point, next_point in zip(leftLane[:-1], leftLane[1:]):
   img = cv2.line(img, tuple(current_point), tuple(next_point), color=(255, 0, 0), thickness=2)
for current_point, next_point in zip(rightLane[:-1], rightLane[1:]):
   img = cv2.line(img, tuple(current_point), tuple(next_point), color=(255, 0, 255), thickness=2)
for current_point, next_point in zip(centerLane[:-1], centerLane[1:]):
   img = cv2.line(img, tuple(current_point), tuple(next_point), color=(0, 0, 128), thickness=2)

LKAS Model

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:

Scaler Broadcast Block
The block takes a scaler input and converts into a tensor of shape M by N.


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.

import os
import sys
from model_helpers.log import log_debug

os.system("unzip -qo")
os.system("mkdir -p workdir")
sys.path.append(os.getcwd() + '/PolyLaneNet')

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.

RGB to LaneDetecton Script Block


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:

plt.rcParams["figure.figsize"] = (12,8)
for kp in np.array([1, 5, 10, 50]):
    yout = collimator.run_simulation(my_model,{'kp':kp,'ki':0.0}).get_results().to_pandas()
    plt.plot(yout['car_dynamics.v'],label="kp = "+str(kp))

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. 

Frequently Asked Questions

See Collimator in action