import csv
import os
import numpy as np
import random
from scipy.optimize import leastsq
TotalVehicleFlow_str = []
AvgSharr_dstr = []
csv_open_file = r'C:\Users\psqk\Desktop\my_query.csv'
#load original data , format:string
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
TotalVehicleFlow_str = [row['TotalVehicleFlow'] for row in reader]
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
AvgSharr_str = [row['AvgSharr'] for row in reader]
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
RealTime_str = [row['RealTime'] for row in reader]
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
Speed_str = [row['AvgSpeed'] for row in reader]
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
EquID_str = [row['EquID'] for row in reader]
with open(csv_open_file,'r') as inputfile:
reader = csv.DictReader(inputfile)
Lane_str = [row['Lane'] for row in reader]
#TotalVehicleFlow_tuple = tuple(TotalVehicleFlow_str)
#AvgSharr_tuple = tuple(AvgSharr_str)
#print (TotalVehicleFlow_str[-10:])
#print (AvgSharr_str[-10:])
#convert the format from string into integer & float
TotalVehicleFlow_num = []
for n in TotalVehicleFlow_str:
TotalVehicleFlow_num.append(int(n))
AvgSharr_num = []
for n in AvgSharr_str:
AvgSharr_num.append(float(n))
print(len(RealTime_str),len(AvgSharr_num))
print('EquID: {}\n'.format(EquID_str[1]),'Lane: {}'.format(Lane_str[1]))
#print (TotalVehicleFlow_num[-10:])
#print (AvgSharr_num[-10:])
#plt.scatter(AvgSharr_num,TotalVehicleFlow_num,c=['r','g','b'],s=10,marker='*')
#plt.show()
#add to np.array
AvgSharr_np = np.array(AvgSharr_num)
TotalVehicleFlow_np = np.array(TotalVehicleFlow_num)
import numpy as np
import matplotlib.pyplot as plt
# y = 2 + 3x + 4x^2
X = AvgSharr_np
Y = TotalVehicleFlow_np
#拟合曲线的参数
parameter_k = 6
parameter_b = -16
_X = np.arange(0, 24, 0.1)
_Y = np.array([parameter_b + parameter_k*x for x in _X])
#设置画布大小
plt.figure(figsize=(12, 8))
axes = plt.axes()
axes.spines['top'].set_visible(False)
axes.spines['right'].set_visible(False)
#axes.set_xticks([0,5,10,15,20,25,30,35,40,45])
plt.xlim(0,80)
plt.ylim(0,120)
font = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 20,}
plot1=plt.plot(X, Y, 'r*',label='original values')
plot2=plt.plot(_X, _Y, 'b',linewidth = 2,label='curve_fit values')
plt.tick_params(labelsize=20)
plt.xlabel('OCCUPANCY, %',font)
plt.ylabel('FLOW RATE',font)
#plt.plot(X, Y, 'ro', _X, _Y, 'b', linewidth=5)
#plt.title("y = {} + {}x + {}$x^2$ ".format(a0, a1, a2))
#plt.title("y = {}$x^2$ + {}x + ({})".format(a2, a1, a0),fontsize=20,)
plt.title("MC Master",fontsize=20,)
plt.axvline(24,ls='--',linewidth = 1,color='purple')
savefig_name = "{}-lane{}.png".format(EquID_str[1][-4:],Lane_str[1])
plt.savefig(savefig_name)
plt.show()
#create csv file 生成一个csv文件
def create_csv(path):
with open(path, "w+", newline='') as file:
csv_file = csv.writer(file)
head = ["time","avgsharr","vehicleflow","speed","congestion_status"]
csv_file.writerow(head)
#append csv file 追加行数据
def append_csv(path,data):
with open(path, "a+", newline='') as file: # 处理csv读写时不同换行符 linux:\n windows:\r\n mac:\r
csv_file = csv.writer(file)
datas = data
csv_file.writerows(datas)
output_csv = 'csv_{}-lane{}_output.csv'.format(EquID_str[1][-4:],Lane_str[1])
#创建一个csv文件,会自动替换之前的csv文件
create_csv(output_csv)
for n in range(0,len(TotalVehicleFlow_str)):
data_time_csv = '{}'.format(RealTime_str[n])
data_sharr_csv = '{}'.format(AvgSharr_num[n])
data_vflow_csv = '{}'.format(TotalVehicleFlow_num[n])
data_speed_csv = '{}'.format(Speed_str[n])
if parameter_k*X[n] + parameter_b > Y[n] and X[n]<25:
#print("\033[0;34mlight | {:19}, | {:5.2f}, |{:5d}, | {:8} |".format(RealTime_str[n],X[n],Y[n],Speed_str[n]))
data_congestion_csv = '{}'.format('light')
elif parameter_k*X[n] + parameter_b > Y[n] and X[n]>25:
#print("\033[1;31mserious | {:19}, | {:6.2f}, |{:5d}, | {:8} |\033[0m".format(RealTime_str[n],X[n],Y[n],Speed_str[n]))
data_congestion_csv = '{}'.format('serious')
else:
n=0
data_congestion_csv = '{}'.format('normal')
append_csv(output_csv,zip([data_time_csv],[data_sharr_csv],[data_vflow_csv],[data_speed_csv],[data_congestion_csv]))
# print("\033[1;30mnormal | {:19}, | {:5.2f}, |{:5d}, | {:8} |\033[0m".format(RealTime_str[n],X[n],Y[n],Speed_str[n]))
print("end")
'''
create_csv('output_csv.csv')
for n in range(0,len(TotalVehicleFlow_str)):
data_time_csv = '{}'.format(RealTime_str[n])
data_sharr_csv = '{}'.format(AvgSharr_num[n])
data_vflow_csv = '{}'.format(TotalVehicleFlow_num[n])
data_speed_csv = '{}'.format(Speed_str[n])
append_csv('output_csv.csv',zip([data_time_csv],[data_sharr_csv],[data_vflow_csv],[data_speed_csv]))
'''
Python之csv数据读取、创建和追加
最新推荐文章于 2023-09-01 14:00:43 发布