TG-PlatformPlus/UserScripts/data_analysis.py

81 lines
4.0 KiB
Python
Raw Permalink Normal View History

2026-03-02 14:29:58 +08:00
from PIL import ImageGrab
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from mpl_toolkits.mplot3d import Axes3D
def processData(data):
try:
result = {}
def func_T(x, a0, a1, a2, a3):
return a0+a1*x+a2*x**2+a3*x**3
def func_P(x, a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23):
return (a00+a01*x[1]+a02*x[1]**2+a03*x[1]**3)+(a10+a11*x[1]+a12*x[1]**2+a13*x[1]**3)*x[0]+(a20+a21*x[1]+a22*x[1]**2+a23*x[1]**3)*x[0]**2
def func_P1(x, a0, a1, a2):
return a0+a1*x+a2*x**2
data = data['Data']
temperature_C = np.array(data['temperature_C'], dtype=np.float64)
temperature_AD = np.array(data['temperature_AD'], dtype=np.float64)
pressure_psi = np.array(data['pressure_psi'], dtype=np.float64)
pressure_AD = np.array(data['pressure_AD'], dtype=np.float64)
# p0 = [1, 0.1, 0.01, 0.01] # 初始参数值
# params, pcov = curve_fit(func_T, temperature_AD, temperature_C, p0)
# modelTParamsName = ['a0', 'a1', 'a2', 'a3']
# modelTParamsValue = list(params)
# result['modelParams_Temperature'] = dict(zip(modelTParamsName, modelTParamsValue))
# result['modelExpress_Temperature'] = 'Temperature = a0+a1*x+a2*x**2+a3*x**3'
# T_pred = func_T(temperature_AD, *params)
# result['modelPreValue_Temperature'] = list(T_pred)
# result['modelError_Temperature'] = list(temperature_C - T_pred)
# result['modelMaxError_Temperature'] = max(result['modelError_Temperature'])
# result['modelMinError_Temperature'] = min(result['modelError_Temperature'])
#
# p0 = [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] # 初始参数值
# params, pcov = curve_fit(func_P, (pressure_AD, temperature_C), pressure_psi, p0)
# modelPParamsName = ['a00', 'a01', 'a02', 'a03', 'a10', 'a11', 'a12', 'a13', 'a20', 'a21', 'a22', 'a23']
# modelPParamsValue = list(params)
# result['modelParams_Pressure'] = dict(zip(modelPParamsName, modelPParamsValue))
# result['modelExpress_Pressure'] = 'Pressure = (a00+a01*T+a02*T**2+a03*T**3)+(a10+a11*T+a12*T**2)*x+(a20+a21*T+a22*T**2)*x**2'
# P_pred = func_P((pressure_AD, temperature_C), *params)
# result['modelPreValue_Pressure'] = list(P_pred)
# result['modelError_Pressure'] = list(pressure_psi - P_pred)
# result['modelMaxError_Pressure'] = max(result['modelError_Pressure'])
# result['modelMinError_Pressure'] = min(result['modelError_Pressure'])
# fig = plt.figure(figsize=(8, 16))
# x = np.linspace(0, 20000, 201)
# y = np.linspace(0, 180, 37)
# x, y = np.meshgrid(x, y)
# z = func_P((x, y), *params)
#
# # 绘制3D曲面
# ax = fig.add_subplot(1, 2, 1, projection='3d')
# ax.plot_surface(y, x, z, cmap='viridis', edgecolor='none')
# ax.set_title('3D Surface')
#
# # 绘制误差图
# ax2 = fig.add_subplot(1, 2, 2, projection='3d')
# ax2.scatter(temperature_C, pressure_AD, pressure_psi - P_pred, cmap='viridis')
# ax2.set_title('Error Map')
#
# plt.savefig(f"{proInfo['path']}\\data\\T175_MCA\\my_figure.png", bbox_inches='tight', dpi=300)
# plt.close()
p0 = [1, 1, 1] # 初始参数值
params, pcov = curve_fit(func_P1, pressure_AD, pressure_psi, p0)
modelPParamsName = ['a0', 'a1', 'a2']
modelPParamsValue = list(params)
result['modelParams_Pressure'] = dict(zip(modelPParamsName, modelPParamsValue))
result['modelExpress_Pressure'] = 'Pressure = a0+a1*x+a2*x**2'
P_pred = func_P1(pressure_AD, *params)
result['modelPreValue_Pressure'] = list(P_pred)
result['modelError_Pressure'] = list(pressure_psi - P_pred)
result['modelMaxError_Pressure'] = max(result['modelError_Pressure'])
result['modelMinError_Pressure'] = min(result['modelError_Pressure'])
return result
except Exception as e:
log_e(f"Error in processData(): {str(e)}")