Files
zoeyyyzou ae654e1dc8 update
2021-09-23 16:10:08 +08:00

85 lines
3.0 KiB
Python

import csv
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
# 绘制实验0的时延曲线
# 折线图
def getXY(fileName: str, target: str, faceId: str, quota: str):
x = []
y = []
fileName = os.getcwd() + '/data/' + fileName
with open(fileName, "r") as file:
lines = file.readlines()
lastCount = 0;
beta = 0.6
for line in lines[1:]:
item = line.split("\t")
if len(item) < 5:
continue
if item[1] == target and item[2] == faceId and item[4] == quota:
capTime = float(item[0])
if int(capTime / 0.1) != lastCount:
lastCount = int(capTime / 0.1)
else:
continue
x.append(lastCount * 0.1)
if len(y) == 0:
# 第一次添加
y.append(float(item[5]) * 1000)
else:
# 指数加权平均
y.append(beta * y[len(y) - 1] + (1 - beta) * float(item[5]) * 1000)
return x, y
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: python delay.py <filename> <title>")
exit(0)
filename = sys.argv[1]
delayType = "LastDelay"
x1, y1 = getXY(filename, "C1", "0", delayType)
x2, y2 = getXY(filename, "C2", "0", delayType)
x3, y3 = getXY(filename, "C3", "0", delayType)
x4, y4 = getXY(filename, "C4", "0", delayType)
print(f"C1: mean->{np.mean(y1).round(2)}, std->{np.std(y1).round(2)}")
print(f"C2: mean->{np.mean(y2).round(2)}, std->{np.std(y2).round(2)}")
print(f"C3: mean->{np.mean(y3).round(2)}, std->{np.std(y3).round(2)}")
print(f"C4: mean->{np.mean(y4).round(2)}, std->{np.std(y4).round(2)}")
res = ""
for num in [
np.mean(y1).round(2), np.std(y1).round(2),
np.mean(y2).round(2), np.std(y2).round(2),
np.mean(y3).round(2), np.std(y3).round(2),
np.mean(y4).round(2), np.std(y4).round(2),
]:
res += f"& {num} "
print(res)
# # 箱线图代码
# plt.boxplot([y1, y2, y3, y4], labels=('C1', 'C2', 'C3', 'C4'), showfliers=False) # 箱线图 by qjm
# plt.title(sys.argv[2])
# plt.ylim(63,68)
# plt.ylabel('Delay(ms)')
l1 = plt.plot(x1, y1, 'r', label='C1', linestyle='-.', marker='+', alpha=1)
l2 = plt.plot(x2, y2, 'g', label='C2', linestyle='-.', marker='x', alpha=1)
l3 = plt.plot(x3, y3, 'b', label='C3', linestyle='-.', marker='d', alpha=0.2)
l4 = plt.plot(x4, y4, 'aqua', label='C4', linestyle='-.', marker='*', alpha=1)
plt.title(sys.argv[2])
plt.xlabel('Time(s)')
plt.ylabel('Delay(ms)')
plt.xlim(0)
plt.ylim(60, 80) # 实验0专用
plt.legend(loc=0)
# save pictures
full_path = os.getcwd() + "/pictures_delay/" + filename + ".png" # 将图片保存到当前目录,记得斜杠;可更改文件格式(.tif),不写的话默认“.png ”
plt.savefig(full_path, dpi=960) # 存储路径+设置图片分辨率dpi=960