吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1389|回复: 1
收起左侧

[其他原创] 从文本中提取数据实现数据可视化

[复制链接]
hpqztsc 发表于 2020-7-28 10:05
在很多译码实验的程序中输出的很多都是误码率的数字,并不能直观的看出程序的性能,所以数据的可视化就很显得很重要。
我们在python中一般使用的是matplotlib这个函数来进行画图,实现数据的可视化。
通常我们将每次实验的结果是进行单独存储的,对数据的分析就要涉及到python对linux文件系统的操作,这里是用到了os库函数。
话不多说,直接上源码,有什么更好的优化欢迎志同道合的朋友一起讨论。
[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('../')

'''

免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

jiguanlang 发表于 2020-11-28 21:10
有没有文件示例,初学,看不懂,要是能有示例文件最好了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 21:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表