If you need a Python-based ecosystem of open-source software for mathematics, science, and engineering, then Python’s SciPy (pronounced “Sigh Pie”) library is exactly what you need. You can easily run this library and give it a nice GUI using Python4Delphi (P4D). Python4Delphi is a free tool that allows you to work with Python scripts and objects, even create new Python modules and types in the Windows GUI.
SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers, and other tasks common in science and engineering. SciPy is a collection of mathematical algorithms and convenience functions built on the NumPy extension of Python. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data. With SciPy, an interactive Python session becomes a data-processing and system-prototyping environment rivaling systems, such as MATLAB, IDL, Octave, R-Lab, and SciLab.
The SciPy library is currently distributed under the BSD license, and its development is sponsored and supported by an open community of developers. It is also supported by NumFOCUS, a community foundation for supporting reproducible and accessible science.
The additional benefit of basing SciPy on Python is that this also makes a powerful programming language available for use in developing sophisticated programs and specialized applications. Scientific applications using SciPy benefit from the development of additional modules in numerous niches of the software landscape by developers across the world. Everything from parallel programming to web and database subroutines and classes has been made available to the Python programmer. All of this power is available in addition to the mathematical libraries in SciPy.
Table of Contents
This post will guide you on how to run the SciPy library using Python for Delphi to display it in the Delphi Windows GUI app.First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
This post will introduce you to how to perform curve fitting with SciPy, and we will run it in Python GUI.
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a “smooth” function is constructed that approximately fits the data.
Here is the complete code for curve fitting example using SciPy inside Python4Delphi GUI:
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt
# Our test function
def f(t, omega, phi):
return np.cos(omega * t + phi)
# Our x and y data
x = np.linspace(0, 3, 50)
y = f(x, 1.5, 1) + .1*np.random.normal(size=50)
# Fit the model: the parameters omega and phi can be found in the
# ‘params’ vector
params, params_cov = optimize.curve_fit(f, x, y)
# plot the data and the fitted curve
t = np.linspace(0, 3, 1000)
plt.plot(x, y, 'bx')
plt.plot(t, f(t, *params), 'r-')
Congratulations, now you have learned how to run the SciPy library using Python for Delphi to display it in the Delphi Windows GUI app! Now you can try lots more advanced examples from these documentations using the SciPy library and Python4Delphi.
Check out the SciPy library for Python and use it in your projects: https://pypi.org/project/scipy/ and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi
I had quite some trouble getting this to work. Here are the additional steps that I had to take. Perhaps you can include those in the blog:
(I am using an Anaconda environment)
If you are using Anaconda, before starting Delphi, make sure to activate your Anaconda environment (with numpy, scipy and matplotlib installed in it):
Start an Anaconda prompt, run conda activate , then start Delphi from within that environment: “C:\Program Files (x86)\Embarcadero\Studio\21.0\bin\bds.exe” -pDelphi
In order to avoid error messages from Qt when plotting:
Add a folder names platforms under the directory in which your compiled executable is stored, and copy qwindows.dll to it.
(it can be found under \envs\\Library\plugins\platforms\
Thanks for the tip Reinier!
there’s a typo:
Add a folder names platforms
Add a folder named platforms
(or perhaps better: Add a folder with the name “platforms”)
Please correct that..
Hi Reinier – I looked though this post and I can’t see that typo. Are you perhaps referring to a different post? If that is the case can you reply with the link to the post with the typo and I’ll amend it straight away.