从文本中提取数据实现数据可视化
在很多译码实验的程序中输出的很多都是误码率的数字,并不能直观的看出程序的性能,所以数据的可视化就很显得很重要。我们在python中一般使用的是matplotlib这个函数来进行画图,实现数据的可视化。
通常我们将每次实验的结果是进行单独存储的,对数据的分析就要涉及到python对linux文件系统的操作,这里是用到了os库函数。
话不多说,直接上源码,有什么更好的优化欢迎志同道合的朋友一起讨论。
import sys
import os
import shutil
import matplotlib.pyplot as plt
import matplotlib
from matplotlib import font_manager
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('SNRs')
plt.ylabel('BERs')
plt.ylim(1e-7, 1)
plt.grid(True, axis="y", which="both", linestyle='-.')
plt.grid(True, axis="x", linestyle='-.')
plt.ion()
# cnt = 0
for cnt in range(len(BERs)):
for i in range(1, len(BERs) + 1):
SNRs.append(i)
print(SNRs)
plt.semilogy(SNRs, BERs, provided_decoder_fatmat, label=provided_decoder_type)
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
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):
dirs = findfolder(os.getcwd())
dirs.sort()
old_datas = []
for dir in dirs:
os.chdir(dir)
filename = findfilename(os.getcwd(), name)
data = filetolistfloat(filename)
old_datas.append(data)
os.chdir('../')
new_datas = []
for k in range(len(old_datas)):
data = []
for l in range(len(old_datas)):
data.append(old_datas)
new_datas.append(data)
encoder_type = ['Min-sum', 'FNSPA', 'RNSPA', 'FNOMS', 'FNNMS', 'RNOMS', 'RNNMS']
fatmat = generate_fatmat(len(new_datas))
num_iterations = []
for num in range(5, 31, 5):
num_iterations.append(str(num))
for j in range(len(encoder_type)):
draw(encoder_type, new_datas, fatmat, num_iterations)
def data_anaa(name):
dirs = findfolder(os.getcwd())
dirs.sort()
old_datas = []
for dir in dirs:
os.chdir(dir)
filename = findfilename(os.getcwd(), name)
data = filetolistfloat(filename)
old_datas.append(data)
os.chdir('../')
new_datas = []
for k in range(len(old_datas)):
data = []
for l in range(len(old_datas)):
if l == 1 or l ==2:
pass
else:
data.append(old_datas)
new_datas.append(data)
encoder_type = ['Min-sum-R', 'FNOMS-R', 'FNNMS-R', 'RNOMS-R', 'RNNMS-R']
fatmat = generate_fatmat(len(new_datas))
num_iterations = []
for num in range(5, 31, 5):
num_iterations.append(str(num))
for j in range(len(num_iterations)):
draw('BCH_63_36 '+num_iterations, new_datas, fatmat, encoder_type)
os.chdir('test_result/')
data_ana('BERs.txt')
data_anaa('BERs.txt')
'''
dirs = findfolder(os.getcwd())
print(dirs)
for dir in dirs:
os.chdir(dir)
folder_draws(os.getcwd())
filemv(os.getcwd(), 'png')
os.chdir('../')
'''
有没有文件示例,初学,看不懂,要是能有示例文件最好了。
页:
[1]