import numpy as np
import pandas as pd
import os
import shutil
import math
# 文件路径
path = os.path.abspath(r'./m1/m1')?
# 文件数量
tol = len([lists for lists in os.listdir(path) if os.path.isfile(os.path.join(path, lists))])
# 面积
area = 0
Perimeter =0
# 初始化矩阵
Area = np.zeros(tol)
Area_S = np.zeros(tol)
Perimeter = np.zeros(tol)
Center = np.zeros(tol)
Xmax = np.zeros(tol)
Xmin = np.zeros(tol)
Ymax = np.zeros(tol)
Ymin = np.zeros(tol)
dY = np.zeros(tol)
Beta = np.zeros(tol)
Time = np.zeros(tol)
# 遍历所有文件
for t in np.arange(0, tol):
? ? # 读取文件
? ? data = pd.read_csv(path + '/m1_yanxing_%d.txt' % int(t), names=['T', 'x', 'y', 'z','c','w'],
? ? ? ? ? ? ? ? ? ? skiprows=1, sep=',', dtype='float64')
? ? # 读取x,y坐标
? ? Coordinate = pd.DataFrame(data,columns=['x','y'])
? ? # 判断是空非空
? ? if Coordinate.empty:
? ? ? print(t,Area[t])
? ? ? break
? ? else:
? ? ? # x轴升序排列,-1至+1
? ? ? Coordinate = Coordinate.sort_values(by=['x'], axis=0, ascending=True)
? ? ? # 重置行索引
? ? ? Coordinate = Coordinate.reset_index(drop=True)
? ? ? # 按行遍历
? ? ? for i in range(len(Coordinate)-1):
? ? ? ? area = area + (Coordinate.iloc[i,1] + Coordinate.iloc[i+1,1]) * (Coordinate.iloc[i+1,0] - Coordinate.iloc[i,0])
? ? ? ? Perimeter = Perimeter + np.sqrt(pow((Coordinate.iloc[i+1,0] - Coordinate.iloc[i,0]) , 2) + pow((Coordinate.iloc[i+1,1] + Coordinate.iloc[i,1]),2))
? ? ? Area[t] = area
? ? ? Area_S[t] = Area[t] / Area[0] - 1
? ? ? Perimeter[t] =Perimeter
? ? ? Xmax[t] = max(Coordinate['x'])
? ? ? Xmin[t] = min(Coordinate['x'])
? ? ? Ymax[t] = max(Coordinate['y'])
? ? ? Ymin[t] = min(Coordinate['y'])
? ? ? Center[t] = np.mean(Coordinate['x'])
? ? ? Beta[t] = (Xmax[t] - Xmin[t]) / (Ymax[t] * 2)
? ? ? dY [t] = Ymax[t] - Ymin[t]
? ? ? Time[t] = data.iloc[0,0]
? ? ? # 显示进度
? ? ? print(t,Area[t])
? ? ? area = 0
# 保存数据
np.savetxt(path + '/../Time/1_time.txt', Time, fmt='%.10f')
np.savetxt(path + '/../Time/2_area_s.txt', Area_S, fmt='%.5f')
np.savetxt(path + '/../Time/3_center.txt', Center, fmt='%.5f')
np.savetxt(path + '/../Time/4_beta.txt', Beta, fmt='%.5f')
np.savetxt(path + '/../Time/5_area.txt', Area, fmt='%.5f')
np.savetxt(path + '/../Time/6_xmax.txt', Xmax, fmt='%.5f')
np.savetxt(path + '/../Time/7_xmin.txt', Xmin, fmt='%.5f')
np.savetxt(path + '/../Time/8_ymax.txt', Ymax, fmt='%.5f')
np.savetxt(path + '/../Time/9_ymin.txt', Ymin, fmt='%.5f')
np.savetxt(path + '/../Time/10_dy.txt', dY, fmt='%.5f')
np.savetxt(path + '/../Time/11_Perimeter.txt', Perimeter, fmt='%.10f')
错误如下:
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
? File "H:\MOOSE18_data\anisotropic\data_analysis01.py", line 66, in <module>
? ? Perimeter[t] =Perimeter
ValueError: setting an array element with a sequence.
我不知道该怎么解决,网上查找的方式感觉没用,求教,如果解决的话会打赏的,谢谢