[Python] 纯文本查看 复制代码
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[cnt]) + 1):
SNRs.append(i)
print(SNRs)
plt.semilogy(SNRs, BERs[cnt], 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):
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[0])):
data = []
for l in range(len(old_datas)):
data.append(old_datas[l][k])
new_datas.append(data)
encoder_type = ['Min-sum', 'FNSPA', 'RNSPA', 'FNOMS', 'FNNMS', 'RNOMS', 'RNNMS']
fatmat = generate_fatmat(len(new_datas[0]))
num_iterations = []
for num in range(5, 31, 5):
num_iterations.append(str(num))
for j in range(len(encoder_type)):
draw(encoder_type[j], new_datas[j], 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[0])):
if l == 1 or l ==2:
pass
else:
data.append(old_datas[k][l])
new_datas.append(data)
encoder_type = ['Min-sum-R', 'FNOMS-R', 'FNNMS-R', 'RNOMS-R', 'RNNMS-R']
fatmat = generate_fatmat(len(new_datas[0]))
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[j], new_datas[j], 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('../')
'''