C++/Qt中调用Python模块
Date | Author | Version | Note |
---|---|---|---|
2021.03.15 | Dog Tao | V1.0 | 整理后发表。 |
2023.11.10 | Dog Tao | V1.1 | 1. 调用Py_SetPythonHome 函数指定python库与解释器的路径。 2. 增加应用程序打包部署的相关说明。 |
文章目录
开发环境搭建
作为一种胶水语言,Python 能够很容易地调用 C 、 C++ 等语言,也能够通过其他语言调用 Python 的模块。Python 提供了 C++ 库,使得开发者能很方便地从 C++ 程序中调用 Python 模块。
值得注意的是,Windows平台下的Python提供的静态库接口只支持MSVC编译器。
参考的资料:
开发Python模块
Python 模块的源码示例:
# -*- coding: utf-8 -*-
' a module for alwhales data fit project '
__author__ = 'Dog Tao'
import sys
# 根据不同的开发环境设置对应的外部模块的路径
sys.path.append('E:\\Working\\PythonDev\\DataFitting\\venv\Lib\\site-packages')
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
from scipy import interpolate
from scipy.optimize import curve_fit
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决负数坐标显示问题
def polynomial_fitting(x_series, y_series, deg=3):
"""
多项式拟合方法
:param x_series: x series of data point
:param y_series: y series of data point
:param deg: order of polynomial
:return: list type of polynomial fit parameters
"""
return np.polyfit(x_series, y_series, deg)
def interpolate_linear(x_series, y_series, x_fit_series):
"""
线性插值拟合
:param x_series: x series of data point
:param y_series: y series of data point
:param x_fit_series: new series of x to fit b_spline
:return: list type of linear fit callable object
"""
f_linear = interpolate.interp1d(x_series, y_series)
return f_linear(x_fit_series)
def interpolate_b_spline(x_series, y_series, x_fit_series):
"""
B样条曲线插值
:param x_series: x series of data point
:param y_series: y series of data point
:param x_fit_series: new series of x to fit b_spline
:return: y series of fit data
"""
tck = interpolate.splrep(x_series, y_series)
return interpolate.splev(x_fit_series, tck, 0)
def func_4PL(x, a, b, c, d):
"""
四参数模式为Y=(a-d)/[1+(x/c)^b]+d, 目前最常用与免疫检测领域,用于描述吸光度随抗原浓度变化的规律
:param x: value
:param a: 曲线上渐近线估值
:param b: 曲线下渐近线估值
:param c: 曲线的斜率
:param d: 最大结合一半时对应的剂量
:return: y value
"""
return (a - d) / (1 + (x / c) ** b) + d
def func_5PL(x, a, b, c, d, e):
"""
四参数模式为Y=(a-d)/[1+(x/c)^b]+d, 目前最常用与免疫检测领域,用于描述吸光度随抗原浓度变化的规律
:pa