不同之处:
保存内容为,以log命名的文件夹内存有一个txt文件+一张对应图像
import os
import argparse
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
def main():
#####################命令行获取输入输出文件路径############################
parser = argparse.ArgumentParser("Demo of argparse")
parser.add_argument('log_path', type=str,
default=" ", help="path to log file")
parser.add_argument('out_path',type=str,
default=" ",help ="path to result file")
args = parser.parse_args()
log_path = args.log_path
out_path=args.out_path
current_path = os.path.abspath(log_path)
current_outpath = os.path.abspath(out_path)
print("输入文件路径为: {}".format(current_path))
print("输出结果文件保存在: {}".format(current_outpath))
files = os.listdir(log_path)
os.mkdir(out_path)
#os.mkdir(out_path+'\\figures')
##########################批量读取#################################
for file_name in files:
fname,fename=os.path.splitext(file_name)
os.mkdir(out_path+'\\'+fname)
print(fname)
input = open(log_path+"\\"+file_name, 'r')
fpsNumArr = []
coutArr=[]
inttimeArr=[]
for line in input:
line = line.split()
if 'fps->' in line:
#print(line)
line[-1]=line[-1].lstrip('"')
fpsNumArr.append(int(line[-1].rstrip('"')))
#提取时间关键词
coutArr.append(line[1])
#print(fpsNumArr.shape())
###########################将时间转为十进制##########################
#print(coutArr)
for i in range(len(coutArr)):
strings=coutArr[i]
int_hour=int(strings[0:2])
int_minute=int(strings[3:5])
int_second=int(strings[6:8])
#print(str_hour)
#print(str_minute)
time_s=int_hour*60+int_minute*60+int_second
#print(time_s)
inttimeArr.append(time_s)
#print(inttimeArr)
#x=np.array(shape=[0,2],dtype=str)
#x=x=np.append(x,[coutArr],axis=1)
#print(x)
input.close()
#######################列表转为数组##################################
fpsNumArr_np = np.array(fpsNumArr)
inttimeArr_np = np.array(inttimeArr)
#print(fpsNumArr_np.shape)
#print(inttimeArr_np.shape)
##五分钟统计,获取符合要求的索引序号
templist=[]#新建数组
#print(inttimeArr)
for j in range(len(inttimeArr_np)):
num_zero=inttimeArr_np[0]
if (inttimeArr_np[j]-num_zero)%60 == 0:
#print(j)
templist.append(j)
#print(templist)#符合要求的索引值0~286为前五分钟
########################均值与方差计算,输出打印####################
#超出三倍方差
mean=[]
std=[]
threshold1_list=[]
#threshold2_list=[]
for k in range(len(templist)-1):
#mean_num=np.mean(fpsNumArr_np[templist[k]:templist[k+1]])
#std_num=np.std(fpsNumArr_np[templist[k]:templist[k+1]])
mean_num=np.around(np.mean(fpsNumArr_np[templist[k]:templist[k+1]]),1)
std_num=np.around(np.std(fpsNumArr_np[templist[k]:templist[k+1]]),1)
threshold1 = mean_num - 3 * std_num
#threshold2 = mean_num + 3 * std_num
threshold1_list.append(threshold1)
#threshold2_list.append(threshold2)
mean.append(mean_num)
std.append(std_num)
#print('\n每一分钟平均值为:',mean)
#print('\n每一分钟标准差为:',std)
outlier = [] #将异常值保存
outlier_x = []
for i in range(len(templist)-1):
for j in range(templist[i],templist[i+1]):
if (fpsNumArr[j] < threshold1_list[i]):
outlier.append(fpsNumArr[j])
outlier_x.append(j)
else:
continue
#print('\n异常数据如下:\n')
#print('\n异常数据值为:',outlier)
#print('\n异常数据的索引值为:',outlier_x)
'''
plt.plot(i, fpsNumArr)
plt.plot(outlier_x, outlier, 'ro')
for j in range(len(outlier)):
plt.annotate(outlier[j], xy=(outlier_x[j], outlier[j]), xytext=(outlier_x[j],outlier[j]))
plt.show()
'''
#################可视化##################################
#plt.errorbar(mean, std, fmt="o")
#plt.show()
#fig = plt.figure()
#fig = plt.gcf()
x = np.arange(len(mean))+1
y=mean
plt.errorbar(x,y, std, fmt="o",label='mean and std')
figure_path=out_path+'\\'+fname+"\\"+"figures_"+fname+".png"
#print(figure_path)
plt.xlabel('每一分钟',fontsize=14)
#设置x轴标签及其字号
plt.ylabel('fps',fontsize=14)
plt.grid(True)
#存储图片到路径下
plt.savefig(figure_path)
#plt.show()
'''
num_list = mean
num_list1 = std
x =list(range(len(num_list)))
total_width, n = 0.8, 2
width = total_width / n
plt.bar(x, num_list, width=width, label='mean',fc = 'y')
for i in range(len(x)):
x[i] = x[i] + width
plt.bar(x, num_list1, width=width, label='std',fc = 'r')
plt.legend()
#plt.show()
'''
'''
data_error={
'error data':outlier,
"error data 's key":outlier_x,
}
df=pd.DataFrame(data_error)
fig,ax=plt.subplots(figsize=(6,6))
ax.axis=("off")
#ax.axis=("tight")
ax.table(
cellText=df.values,
colLabels=df.columns,
bbox=[0,0,1,1]
)
'''
#print("*****************统计数组*******************")
#print(fpsNumArr)
length =len(fpsNumArr)
a,b,c,d,e=0,0,0,0,0
for i in range(length):
if (fpsNumArr[i]>=1 and fpsNumArr[i]<=7):
a+=1
elif(fpsNumArr[i]<=10 and fpsNumArr[i]>=8):
b+=1
elif(fpsNumArr[i]<=13 and fpsNumArr[i]>=11):
c+=1
elif(fpsNumArr[i]<=15 and fpsNumArr[i]>=14):
d+=1
elif(fpsNumArr[i]>=16):
e+=1
####################输出打印#########################
print("*******************************************")
print("当前读取的文件是:",file_name)
print("检测到fps个数为:",length)
print("fps范围在1~7的个数有: ",a)
print("fps范围在8~10的个数有: ",b)
print("fps范围在11~13的个数有: ",c)
print("fps范围在14~15的个数有: ",d)
print("fps范围大于16的个数有: ",e)
#print('\n每一分钟平均值为:',mean)
#print('\n每一分钟标准差为:',std)
#print('\n异常数据如下:\n')
print('\n异常数据个数为:',len(outlier))
print('异常数据占总数百分比:',format((len(outlier)/length),'.3f'))
#print('\n异常数据值为:',outlier)
#print('\n异常数据的索引值为:',outlier_x)
save_result_file=out_path+'\\'+fname+"\\"+"result_"+file_name
with open(save_result_file,"w") as f:
f.write("检测到fps个数为:"+str(length)+"\n")
f.write("fps范围在1~7的个数有:"+str(a)+"\n"
+"fps范围在8~10的个数有:"+str(b)+"\n"
+"fps范围在11~13的个数有:"+str(c)+"\n"
+"fps范围在14~15的个数有:"+str(d)+"\n"
+"fps范围大于16个数有:"+str(e)+"\n")
f.write("统计数组:"+str(fpsNumArr)+"\n")
f.write('异常数据个数为:'+str(len(outlier))+"\n")
f.write('异常数据占总数百分比:'+str(format((len(outlier)/length),'.3f')))
f.write(
"每一分钟平均值为:"+str(mean)+"\n"
+"每一分钟标准差为:"+str(std)+"\n"
+"异常值数据为:"+str(outlier)+"\n"
+"异常数据的索引值为:"+str(outlier_x)+"\n"
)
if __name__ == '__main__':
main()