[Python] 纯文本查看 复制代码
import sys
import os
import shutil
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
from matplotlib.ticker import MultipleLocator
fontpath = os.getcwd()
print(fontpath)
def filetolistfloat( input_filename ):
bers = []
cnt = 0
with open(input_filename, "r", encoding="utf-8") as fbers:
for data in fbers.readlines():
data = data.replace("[", "")
data = data.replace("]", "")
data = data.replace("\n", "")
data = data.split(",", -1)
ber = []
for tmp_float in data:
ber.append(float(tmp_float))
bers.append(ber)
cnt +=1
if cnt <= 1:
return ber
else:
return bers
def filetoliststr( input_filename ):
bers = []
cnt = 0
with open(input_filename, "r", encoding="utf-8") as fbers:
for data in fbers.readlines():
data = data.replace("[", "")
data = data.replace("]", "")
data = data.replace(" ", "")
data = data.replace("\'", "")
data = data.replace("\n", "")
data = data.split(",", -1)
bers.append(data)
cnt += 1
if cnt <= 1:
return data
else:
return bers
def findfolder(srcpath):
srcdirs = os.listdir(srcpath)
dirs = []
for dir in srcdirs:
if not os.path.isfile(os.path.join(srcpath, dir)):
dirs.append(dir)
return dirs
def findfile(srcpath):
srcdirs = os.listdir(srcpath)
dirs = []
for dir in srcdirs:
if os.path.isfile(os.path.join(srcpath, dir)):
dirs.append(dir)
return dirs
def draw(input_filename,BERs,provided_decoder_fatmat,provided_decoder_type):
SNRs = []
plt.figure(figsize=(8, 8), dpi=240)
plt.title(input_filename, fontproperties=matplotlib.font_manager.FontProperties( \
fname=fontpath + "/SimHei.ttf"))
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.ylim(0, 1)
plt.grid(True, axis="y", which="both", linestyle='-.')
plt.grid(True, axis="x", linestyle='-.')
x_major_locator = MultipleLocator(1)
# 把x轴的刻度间隔设置为1,并存在变量里
y_major_locator = MultipleLocator(0.1)
# 把y轴的刻度间隔设置为10,并存在变量里
ax = plt.gca()
# ax为两条坐标轴的实例
ax.xaxis.set_major_locator(x_major_locator)
# 把x轴的主刻度设置为1的倍数
ax.yaxis.set_major_locator(y_major_locator)
# 把y轴的主刻度设置为10的倍数
plt.ion()
try:
for cnt in range(len(BERs)):
for i in range(1, len(BERs[cnt]) + 1):
SNRs.append(i)
print(SNRs)
plt.plot(SNRs, BERs[cnt], provided_decoder_fatmat[cnt], label=provided_decoder_type[cnt])
plt.legend(loc='upper right')
SNRs.clear()
except:
cnt = 0
for i in range(1, len(BERs) + 1):
SNRs.append(i)
print(SNRs)
plt.plot(SNRs, BERs, provided_decoder_fatmat[cnt], label=provided_decoder_type[cnt])
plt.legend(loc='upper right')
SNRs.clear()
plt.savefig(input_filename + ".png")
plt.ioff()
plt.close()
def draws (input_filenames,decoder_BERs,provided_decoder_fatmat,provided_decoder_types):
for filename, BERs, decoder_fatmat, decoder_type in input_filenames,decoder_BERs,provided_decoder_fatmat,provided_decoder_types:
draw(filename, BERs, decoder_fatmat, decoder_type)
def findnamefromfile(srcpath,strs):
files = findfile(srcpath)
filenames = []
for file in files:
if (strs in file):
filenames.append(file.replace(strs, ''))
return filenames
def filemv(srcpath,strs):
files = findfile(srcpath)
for file in files:
if (strs in file):
shutil.move(file, "../"+file)
def findfilename(srcpath,strs):
files = findfile(srcpath)
for file in files:
if (strs in file):
return file
def folder_draws(srcpath):
print(srcpath)
input_filenames = findnamefromfile(srcpath, "_BERs.txt")
for input_filename in input_filenames:
decoder_BERs = filetolistfloat(input_filename+"_BERs.txt")
print(input_filename)
provided_decoder_types = filetoliststr(input_filename + "_types.txt") #
print(provided_decoder_types)
provided_decoder_fatmat = filetoliststr(input_filename + "_fatmat.txt") #
print(provided_decoder_fatmat)
draw(input_filename, decoder_BERs, provided_decoder_fatmat, provided_decoder_types)
def GetFileLine(srcfile,num_line):
data = filetolistfloat(srcfile)
return data[num_line]
def generate_fatmat( len ):
linestyles=[
'--', #dashed line style
'-' , #solid line style
'-.', #dash-dot line style
':' , #dotted line style
]
markers=[
'*', # star marker
'.', # point marker
',', # pixel marker
'o', # circle marker
'v', # triangle_down marker
'^', # triangle_up marker
'<', # triangle_left marker
'>', # triangle_right marker
'1', # tri_down marker
'2', # tri_up marker
'3', # tri_left marker
'4', # tri_right marker
's', # square marker
'p', # pentagon marker
'h', # hexagon1 marker
'H', # hexagon2 marker
'+', # plus marker
'x', # x marker
'D', # diamond marker
'd', # thin_diamond marker
'|', # vline marker
'_', # hline marker
]
colors=[
'b', #蓝色
'g', #绿色
'r', #红色
'c', #青色
'm', #品红色
'y', #黄色
'k', #黑色
'w', #白色
]
fatmats = []
cnt = 0
for linestyle in linestyles:
for marker in markers:
for color in colors:
fatmats.append(linestyle+marker+color)
cnt += 1
if cnt >= len : return fatmats
def data_ana(name):
data = filetolistfloat(name+".txt") #数据可视化文本,将文本中的数据转换为列表,每行作为一个独立的数据组,可一行可多行。
encoder_type = [name, 'test_acc', 'RNSPA', 'FNOMS', 'FNNMS', 'RNOMS', 'RNNMS'] #每行数据的类型名称,右上角标注。
fatmat = generate_fatmat(2) #为不同的数据产生不同的线型,可视化中进行区分,数字为产生的线型的数量。
draw(name,data,fatmat,encoder_type) #name为可视化图的名称。
data_ana('train_acc')
data_ana('avg_cost')