Python Data Processing with Pandas, NumPy, and SciPy: Interpolate Stress-Strain Curves and Extract Key Values
"import pandas as pd\nimport numpy as np\nfrom scipy.interpolate import interp1d\nM0 = pd.read_csv("E:\shujushuchu\shuchu1.csv") #导入四个参数\nM1 = pd.read_csv("E:\shujushuchu\shuru1.csv") #导入abaqus生成的应力应变曲线的点\nB0=M0.values \nB1=M1.values #将导入到参数转换为矩阵\nprint(np.size(B1,0)/2) #0表示行数,1表示列数,并赋值给a1\nprint(B1[1,1]) #因为py语言里下标从0开始,检查数据的位置是否正确\nprint(B1.shape) #根据这个值来修改B3、B4的矩阵大小,将后者赋值给z\na1=1\nz=160\nB3 = np.zeros((a1, z)) #储存曲线点的矩阵\nB4 = np.zeros((a1, z)) #储存预测参数的矩阵\n #这两个矩阵根据前一步的计算结果修改这两个矩阵的大小,a1做为行元素的大小,而列元素大小要大于B1的shape\na2 = 0 #用于循环填充B3\n\nfor i in range(1, a1+1): # a1值加1放在这儿,总循环,以行为单位\n a3 = np.sum(~np.isnan(B1[i, :])) #表格到进来后存在nan值,此处去除nan值,并且获得该行的长度,之后用到a3值错位行的长度,才能够实现插值(存在0会报错)\n B5 = np.zeros((2, a3)) #B5矩阵用于两两储存对应的应力应变点,便于之后的循环\n for i1 in range(1, a3 + 1):\n B5[0, i1 - 1] = B1[2 * i - 2, i1 - 1]\n B5[1, i1 - 1] = B1[2 * i - 1, i1 - 1] #将应力应变数据储存为两行数据\n\n B2 = np.zeros((2, a3)) #创建一个空矩阵(该矩阵将用于储存应力应变曲线塑性阶段的点)\n C = B5[1, 2] / B5[0, 2] #计算弹性模量\n a5 = np.size(B5, 1) #计算B5矩阵有多少行\n a4 = 2 #用于之后循环跳出\n B7 = np.zeros((2, 3)) #创建空的B7矩阵便于储存条件屈服点(从弹性阶段进入塑性阶段)附近的三个点(用于寻找条件屈服点)\n for ii in range(2, a5): \n\n if B5[1, ii - 1] <= C * (B5[0, ii - 1] - 0.002): #寻找生成的数据中从弹性阶段进入塑性阶段瞬间的后一个点\n a4 = a4 + 1\n if a4 == 3:\n a6 = ii\n B7[0, 2] = B5[0, ii - 1]\n B7[1, 2] = B5[1, ii - 1]\n B2[0, a4 - 1] = B5[0, ii - 1]\n B2[1, a4 - 1] = B5[1, ii - 1] #这两行循环储存条件屈服点后的实验数据于B2矩阵中\n\n\n B7[0, 0] = B5[0, a6 - 3]\n B7[0, 1] = B5[0, a6 - 2]\n B7[1, 0] = B5[1, a6 - 3]\n B7[1, 1] = B5[1, a6 - 2] #该部分在B7中储存了条件屈服那一瞬间的后一个点和前两个点(用于后面的插值计算)\n B2[0, 1] = B5[0, a6 - 2]\n B2[1, 1] = B5[1, a6 - 2] #该部分在B2中储存了条件屈服那一瞬间的后一个点\n a11 = (B7[0, 2] - B7[0, 1]) / 500 #将储存的三个点长度细分单位长度,用于插值计算获得中间更多的点\n f1 = interp1d(B7[0, :], B7[1, :], kind='linear') #定义插值计算的数组、范围以及插值类型\n for i2 in np.arange(B7[0, 1], B7[0, 2], a11): #利用for循环将数据细分并一一进行插值计算\n\n a12 = f1(i2)\n a13 = a12\n if a12 <= C * (i2 - 0.002): #寻找条件屈服点的准确大概值\n B2[0, 1] = i2 - a11 #将条件屈服点应变储存进B2\n B2[1, 1] = a13 - C * a11 #将条件屈服点应力储存进B2\n break\n\n B6 = np.zeros((2, 10000)) #创建空白矩阵,用于插值计算条件屈服点后的点\n a7 = (np.max(B2[0, :]) - B2[0, 1]) / 500 #将塑性阶段细分单位长度用于插值计算\n a10 = 0 #用于循环递增储存插值后的细分的塑性阶段的点\n B2 = B2[:, np.nonzero(np.sum(B2, axis=0))[0]] #去除矩阵末尾没储存数据的0值,保证插值计算的正常进行\n f2 = interp1d(B2[0, :], B2[1, :], kind='cubic') #定义插值计算的数组、范围以及插值类型\n for i3 in np.arange(B2[0, 0], np.max(B2[0, :]) + a7, a7): #利用for循环将数据细分并一一进行插值计算\n a20 = i3 + a7\n a22 = np.max(B2[0, :])\n if a20 > a22:\n a20 = a22\n a9 = f2(a20)\n a23 = i3\n if a23 > a22:\n a23 = a22\n a8 = f2(a23) #为了寻找塑性失稳点,差值范围之前设置只到了原始数据的最大值处,插值时可能会超过这个范围而不能正常进行,所以将超过的部分等最大点处的数据\n derivative = (a9 - a8) / a7 #计算曲线的斜率\n if derivative - a9 >= 0: #在斜率减缓到0之前,将所有的插值点都循环储存到B6矩阵里\n a10 += 1\n B6[0, a10 - 1] = i3\n B6[1, a10 - 1] = a8\n\n\n \n B6 = B6[:, np.nonzero(np.sum(B6, axis=0))[0]] #去除矩阵末尾没储存数据的0值,保证插值计算的正常进行\n df = pd.DataFrame(B6)\n writer = pd.ExcelWriter('output3.xlsx', engine='openpyxl')\n df.to_excel(writer, sheet_name='Sheet1', index=False)\n writer.book.save('output3.xlsx')\n\n \n if np.max(B6[0, :]) > 0.47: #将未能插值计算到应力极限点的数据去除掉(不能进行接下来的循环的话将不会储存进最终的矩阵里)\n continue\n else:\n a2 += 1\n for iiii in range(1, 5):\n B4[a2 - 1, iiii - 1] = B0[i - 1, iiii - 1] #将参数储存进B4矩阵中\n\n B3[a2 - 1, 9] = np.min(B6[0, :])\n B3[a2 - 1, 10] = np.max(B6[0, :]) #将塑性阶段应变的最大值与最小值储存到B3矩阵中的第9列与第10列\n B6[0, :] = (B6[0, :] - B6[0, 0]) / (max(B6[0, :]) - B6[0, 0]) #将B6中的数据做归一化处理便于之后的插值计算(用于插值时取对应比例处的值)\n \n \n f = interp1d(B6[0, :], B6[1, :], kind='cubic') #定义插值计算的数组、范围以及插值类型\n xx = np.array([0, 0.03, 0.09, 0.18, 0.3, 0.45, 0.63, 0.84, 1.0]) \n yy = f(xx) #取得xx比例处的值\n\n for iii in range(1, 10):\n B3[a2 - 1, iii - 1] = yy[iii - 1] #将按比例选取的9个值储存进B3矩阵当中\n \n#完成数据的处理\n\nB3 = B3[:, np.nonzero(np.sum(B3, axis=0))[0]]\nB4 = B4[:, np.nonzero(np.sum(B4, axis=0))[0]] #去除矩阵中的多余0的值便于储存数据\ndf = pd.DataFrame(B3)\nwriter = pd.ExcelWriter('output1.xlsx', engine='openpyxl')\ndf.to_excel(writer, sheet_name='Sheet1', index=False)\nwriter.book.save('output1.xlsx')\ndf = pd.DataFrame(B4)\nwriter = pd.ExcelWriter('output2.xlsx', engine='openpyxl')\ndf.to_excel(writer, sheet_name='Sheet1', index=False)\nwriter.book.save('output2.xlsx') #将数据生成为表格\n上述代码为数据处理代码,一般情况下能正确的运行处理出正确的结果,但是处理很多数据的时候,会偶尔出现某组数据处理错误的情况,这是什么原因呀,是因为这段程序里面有缺陷吗,具体出现的处理错误的情况是插值寻找寻找塑性失稳点数值出现错误或者是f = interp1d(B6[0, :], B6[1, :], kind='cubic') #定义插值计算的数组、范围以及插值类型\n xx = np.array([0, 0.03, 0.09, 0.18, 0.3, 0.45, 0.63, 0.84, 1.0]) 这段程序出现负数的情况,正确的情况应该是从前往后依次递增的数据内容:There could be several reasons why you are experiencing errors or negative values in your data processing code. Here are a few possibilities:
-
Data quality: Check the quality and accuracy of your input data. Ensure that there are no missing or incorrect values that could be causing the errors.
-
Interpolation method: The choice of interpolation method (linear, cubic, etc.) can affect the accuracy of the results. Try using different interpolation methods to see if it improves the accuracy of the calculations.
-
Data range: Ensure that the range of your input data is appropriate for the chosen interpolation method. Some interpolation methods may not work well for certain data ranges, leading to inaccurate or negative results.
-
Algorithm logic: Review your algorithm logic to ensure that it is correctly implemented and handles all possible edge cases. Check for any potential errors or inconsistencies in your code.
-
Debugging: When encountering errors, use print statements or debugging tools to track the values of variables and identify any issues. This can help you pinpoint the exact location of the error and understand the cause.
By addressing these points, you should be able to identify and resolve any issues with your data processing code.
原文地址: https://www.cveoy.top/t/topic/pf15 著作权归作者所有。请勿转载和采集!