Exemple #1
0
def reliability_hnh(hnh_array, save_path=None, title="可靠性图"):
    '''
    根据中间结果计算
    :param th:
    :param save_path:
    :return:
    '''
    total_grade_num = hnh_array[:, 0]
    observed_grade_num = hnh_array[:, 1]
    ngrade = len(total_grade_num)
    grade = 1 / ngrade
    total_num = np.sum(total_grade_num)
    under = np.zeros_like(total_grade_num)
    under[:] = total_grade_num[:]
    under[total_grade_num == 0] = 1
    ob_rate = observed_grade_num / under
    ob_rate[total_grade_num == 0] = IV
    ob_rate_noIV = set_plot_IV(ob_rate)
    ob_rate[total_grade_num == 0] = np.nan
    index_iv = np.where(total_grade_num == 0)
    line_x = np.arange(0, 1.00, grade)
    prefect_line_y = np.arange(0, 1.00, grade)
    climate_line_y = np.ones_like(line_x) * np.sum(
        observed_grade_num) / total_num
    x = np.arange(grade / 2, 1, grade)
    fig = plt.figure(figsize=(5, 5.1))
    grid_plt = plt.GridSpec(5, 1, hspace=0)
    ax1 = plt.subplot(grid_plt[0:4, 0])
    plt.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k")
    x_iv = x[index_iv[0]]
    ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]]
    plt.plot(x_iv, ob_rate_noIV_iv, "x", color='k')
    plt.plot(x, ob_rate, marker=".", markersize="10", label="实际预报", color="r")
    plt.plot(line_x, prefect_line_y, '--', label="完美预报", color="k")
    plt.plot(line_x, climate_line_y, ':', label="无技巧预报", color="k")
    plt.setp(ax1.get_xticklabels(), visible=False)
    plt.ylim(0.0, 1)
    plt.ylabel("实况的发生比例")
    plt.legend(loc=2)
    plt.title(title)
    ax2 = plt.subplot(grid_plt[4, 0], sharex=ax1)
    plt.bar(x, total_grade_num, width=0.03)
    #plt.setp(ax2.get_xticklabels())

    plt.ylabel("样本数")
    plt.xlim(0.0, 1.0)
    plt.xticks(np.arange(0.1, 1.01, 0.1))
    plt.xlabel("预测的概率")

    if save_path is None:
        plt.show()
    else:
        plt.savefig(save_path)
        print("检验结果已以图片形式保存至" + save_path)
    plt.close()
Exemple #2
0
def comprehensive_hnh(hnh_array,  member_list=None,vmax = None,log_y = False, save_path=None,show = False,dpi = 300, title="概率预报综合检验图"):
    '''

    :param th_array:
    :param save_path:
    :return:
    '''
    sup_fontsize = 10
    fig = plt.figure(figsize=(8, 5.6),dpi = dpi)
    grade_count = hnh_array.shape[-2]
    grade = 1 / grade_count
    if grade_count < 1:
        print('grade_count输入错误,不能小于1')
        return
    grade_list = np.arange(0, 1, grade).tolist()
    grade_list.append(1.1)
    shape = list(hnh_array.shape)
    new_th_array = hnh_array.reshape((-1, len(grade_list) - 1, 2))
    new_th_array_shape = new_th_array.shape
    label = []
    legend_num = new_th_array_shape[0]
    if member_list is None:
        if legend_num == 1:
            label.append('预报')
        else:
            for i in range(legend_num):
                label.append('预报' + str(i + 1))
    else:
        label.extend(member_list)
    color_list = meteva.base.tool.color_tools.get_color_list(legend_num)
    bar_width = 1.0 / (grade_count * (legend_num + 3))
    ymax = -9999
    mark_line_x = []
    mark_line_y = []
    for line in range(legend_num):
        total_grade_num = new_th_array[line, :, 0]
        observed_grade_num = new_th_array[line, :, 1]
        total_num = np.sum(total_grade_num)

        under = np.zeros_like(total_grade_num)
        under[:] = total_grade_num[:]
        under[total_grade_num == 0] = 1
        ob_rate = observed_grade_num / under
        ob_rate[total_grade_num == 0] = IV
        ob_rate_noIV = set_plot_IV(ob_rate)
        ob_rate[total_grade_num == 0] = np.nan
        index_iv = np.where(total_grade_num == 0)

        not_observed_grade_num = total_grade_num - observed_grade_num

        ngrade = len(total_grade_num)
        grade = 1 / ngrade
        x = np.arange(grade / 2, 1, grade)

        line_x = np.arange(0, 1.01, 0.1)
        prefect_line_y = np.arange(0, 1.01, 0.1)
        climate_line_y = np.ones_like(line_x) * np.sum(observed_grade_num) / total_num

        grid_plt = plt.GridSpec(6, 2)
        if line == 0:
            plt.suptitle(title,fontsize= sup_fontsize,y=0.95)
            plt.subplots_adjust(wspace=0.2, hspace=1)
            ax3 = plt.subplot(grid_plt[0:4, 1])
            ax4 = plt.subplot(grid_plt[4:, :])
            ax1 = plt.subplot(grid_plt[0:4, 0])
        ax1.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k")
        x_iv = x[index_iv[0]]
        ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]]

        ax1.plot(x_iv, ob_rate_noIV_iv, "x", color='k',label = None)
        if line == 0:
            ax1.plot(line_x, prefect_line_y, '--', label="完美", color="k")
            ax1.plot(line_x, climate_line_y, ':', label="无技巧", color="k")
        ax1.plot(x, ob_rate, marker=".", markersize="10", color=color_list[line],label = None)

        x1 = x + (line -legend_num / 2 + 0.5) * bar_width
        total_hap = np.sum(observed_grade_num)
        hfmc = np.zeros((len(total_grade_num), 4))
        for i in range(ngrade):
            hfmc[i, 0] = np.sum(observed_grade_num[i:])
            hfmc[i, 1] = np.sum(total_grade_num[i:]) - hfmc[i, 0]
            hfmc[i, 2] = total_hap - hfmc[i, 0]
            hfmc[i, 3] = total_num - (hfmc[i, 0] + hfmc[i, 1] + hfmc[i, 2])

        far = [1]
        far.extend(pofd_hfmc(hfmc).tolist())
        far.append(0)
        pod = [1]
        pod.extend(pod_hfmc(hfmc).tolist())
        pod.append(0)
        far = np.array(far)
        pod = np.array(pod)

        if line == 0:
            ax3.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧")
        ax3.plot(far, pod, color=color_list[line], linewidth=2, label=label[line])
        #ax4.bar(x1, observed_grade_num, width=bar_width*0.8, color=color_list[line],label = label[line])
        ax4.bar(x1, (not_observed_grade_num+ observed_grade_num), width=bar_width*0.8,edgecolor=color_list[line],label =label[line])

        ax4.set_xlabel("预测的概率",fontsize= sup_fontsize *0.9)
        ax4.set_ylabel("样本数",fontsize= sup_fontsize *0.9)
        ymax = max(np.max(observed_grade_num+not_observed_grade_num), ymax)
        mark_line_x.append(x1)
        mark_line_y.append((not_observed_grade_num+ observed_grade_num)* x)

    ax1.set_xlim(0.0, 1)
    ax1.set_ylim(0.0, 1)
    ax1.set_xlabel("预测的概率",fontsize= sup_fontsize *0.9)
    ax1.set_ylabel("实况的发生比例",fontsize= sup_fontsize *0.9)
    ax1.legend(loc=2,fontsize= sup_fontsize *0.9)
    ax3.set_xlabel("空报率",fontsize= sup_fontsize *0.9)
    ax3.set_ylabel("命中率",fontsize= sup_fontsize *0.9)
    ax3.set_ylim(0.0, 1.0)
    ax3.set_xlim(0.0, 1.0)
    ax3.legend(loc=4,fontsize= sup_fontsize *0.9)
    ax4.set_xlim(0.0, 1)
    ax4.set(title='\n')
    ax4.set_xticks(np.arange(0,1.01,1/grade_count))
    #ax4.legend(loc="upper right", ncol=2)

    #mark_line_x = np.array(mark_line_x)
    #mark_line_y = np.array(mark_line_y)
    #mark_line_x = mark_line_x.T.flatten()
    #mark_line_y = mark_line_y.T.flatten()
    #ax4.plot(mark_line_x, mark_line_y, '.', color='k',markersize = bar_width * 300)
    #lines = ax4.get_children()
    #ax4.legend([lines[0], lines[grade_count],lines[grade_count * legend_num * 2 ]], ['观测正例', '观测负例',"合理比例"],
    #                     loc='upper center', bbox_to_anchor=(0.5, 1.0), ncol=3, prop={'size': 6},fontsize= sup_fontsize *0.9)

    if log_y: ax4.set_yscale('log')
    if vmax is None:
        ax4.set_ylim(0.0, ymax * 1.5)
    else:
        ax4.set_ylim(0.0, vmax)
    if save_path is None:
        show = True
    else:
        plt.savefig(save_path,bbox_inches='tight')
        print("检验结果已以图片形式保存至" + save_path)
    if show is True:
        plt.show()
    plt.close()
Exemple #3
0
def reliability_hnh(hnh_array,  member_list=None,vmax = None,log_y = False, save_path=None,show = False,dpi = 300, title="可靠性图"):
    '''
    根据中间结果计算
    :param th:
    :param save_path:
    :return:
    '''
    sup_fontsize = 10
    grade_count = hnh_array.shape[-2]
    grade = 1 / grade_count
    if grade_count < 1:
        print('grade_count输入错误,不能小于1')
        return
    grade_list = np.arange(0, 1, grade).tolist()
    grade_list.append(1.1)
    hnh_array = hnh_array.reshape((-1, len(grade_list) - 1, 2))
    new_hnh_array_shape = hnh_array.shape
    label = []
    legend_num = new_hnh_array_shape[0]
    if member_list is None:
        if legend_num== 1:
            label.append('预报')
        else:
            for i in range(legend_num):
                label.append('预报' + str(i + 1))
    else:
        label.extend(member_list)

    color_list = meteva.base.tool.color_tools.get_color_list(legend_num)

    for line in range(new_hnh_array_shape[0]):
        total_grade_num = hnh_array[line, :, 0]
        observed_grade_num = hnh_array[line, :, 1]
        ngrade = len(total_grade_num)
        grade = 1 / ngrade
        total_num = np.sum(total_grade_num)
        under = np.zeros_like(total_grade_num)
        under[:] = total_grade_num[:]
        under[total_grade_num == 0] = 1
        ob_rate = observed_grade_num / under
        ob_rate[total_grade_num == 0] = IV
        ob_rate_noIV = set_plot_IV(ob_rate)
        ob_rate[total_grade_num == 0] = np.nan
        index_iv = np.where(total_grade_num == 0)
        line_x = np.arange(0, 1.00, grade)
        prefect_line_y = np.arange(0, 1.00, grade)
        climate_line_y = np.ones_like(line_x) * np.sum(observed_grade_num) / total_num
        x = np.arange(grade / 2, 1, grade)

        if line == 0:
            fig = plt.figure(figsize=(5.5, 6.1),dpi = dpi)
            grid_plt = plt.GridSpec(5, 1, hspace=0)
            ax1 = plt.subplot(grid_plt[0:4, 0])
            plt.plot(line_x, prefect_line_y, '--', label="完美", color="k")
            plt.plot(line_x, climate_line_y, ':', label="无技巧", color="k")
        plt.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k")
        x_iv = x[index_iv[0]]
        ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]]
        plt.plot(x_iv, ob_rate_noIV_iv, "x", color='k')
        plt.plot(x, ob_rate, marker=".", markersize="10", label=label[line], color=color_list[line])

        plt.setp(ax1.get_xticklabels(), visible=False)
        plt.ylim(0.0, 1)
        plt.ylabel("正样本比例",fontsize = sup_fontsize * 0.9)
        plt.legend(loc=2,fontsize = sup_fontsize * 0.9)
        plt.title(title,fontsize = sup_fontsize)
    bar_width = 0.8 / (grade_count * (legend_num+2))
    for line in range(new_hnh_array_shape[0]):
        total_grade_num = hnh_array[line, :, 0]
        observed_grade_num = hnh_array[line, :, 1]
        ngrade = len(total_grade_num)
        grade = 1 / ngrade
        under = np.zeros_like(total_grade_num)
        under[:] = total_grade_num[:]
        under[total_grade_num == 0] = 1
        ob_rate = observed_grade_num / under
        ob_rate[total_grade_num == 0] = IV
        ob_rate[total_grade_num == 0] = np.nan

        x = np.arange(grade / 2, 1, grade)
        if line == 0:
            ax2 = plt.subplot(grid_plt[4, 0], sharex=ax1)
        #x1 = x - 0.01 + (line + 1.5) * 0.01

        x1 = x + (line - legend_num/2 + 0.5) * bar_width
        plt.bar(x1, total_grade_num, width=bar_width*0.8,color=color_list[line])
        plt.ylabel("样本数",fontsize = sup_fontsize * 0.9)
        if log_y: plt.yscale('log')
        plt.xlim(0.0, 1.0)
        plt.xticks(np.arange(0.1, 1.01, 0.1))
        plt.xlabel("预测的概率",fontsize = sup_fontsize * 0.9)
        if vmax is not None:
            plt.ylim(0,vmax)
    if save_path is None:
        show = True
    else:
        plt.savefig(save_path,bbox_inches='tight')
        print("检验结果已以图片形式保存至" + save_path)
    if show is True:
        plt.show()
    plt.close()
Exemple #4
0
def comprehensive_hnh(th_array, save_path=None, title="概率预报综合检验图"):
    '''

    :param th_array:
    :param save_path:
    :return:
    '''
    total_grade_num = th_array[:, 0]
    observed_grade_num = th_array[:, 1]
    total_num = np.sum(total_grade_num)

    under = np.zeros_like(total_grade_num)
    under[:] = total_grade_num[:]
    under[total_grade_num == 0] = 1
    ob_rate = observed_grade_num / under
    ob_rate[total_grade_num == 0] = IV
    ob_rate_noIV = set_plot_IV(ob_rate)
    ob_rate[total_grade_num == 0] = np.nan
    index_iv = np.where(total_grade_num == 0)

    not_observed_grade_num = total_grade_num - observed_grade_num

    ngrade = len(total_grade_num)
    grade = 1 / ngrade
    x = np.arange(grade / 2, 1, grade)

    line_x = np.arange(0, 1.01, 0.1)
    prefect_line_y = np.arange(0, 1.01, 0.1)
    climate_line_y = np.ones_like(line_x) * np.sum(
        observed_grade_num) / total_num
    fig = plt.figure(figsize=(10, 7))
    title_lines = len(title.split("\n"))
    plt.suptitle(title, y=0.90 + 0.03 * title_lines)
    plt.subplots_adjust(wspace=0.2, hspace=1)
    grid_plt = plt.GridSpec(6, 2)
    ax1 = plt.subplot(grid_plt[0:4, 0])
    plt.plot(x, ob_rate_noIV, "--", linewidth=0.5, color="k")
    x_iv = x[index_iv[0]]
    ob_rate_noIV_iv = ob_rate_noIV[index_iv[0]]
    plt.plot(x_iv, ob_rate_noIV_iv, "x", color='k')
    plt.plot(x, ob_rate, marker=".", markersize="10", label="实际预报", color="r")
    plt.plot(line_x, prefect_line_y, '--', label="完美预报", color="k")
    plt.plot(line_x, climate_line_y, ':', label="无技巧预报", color="k")
    plt.xlim(0.0, 1.0)
    plt.ylim(0.0, 1)
    plt.xlabel("预测的概率")
    plt.ylabel("实况的发生比例")
    plt.legend(loc=2)

    ax2 = plt.subplot(grid_plt[4:, 0])
    plt.bar(x, total_grade_num, width=0.03)
    #plt.setp(ax2.get_xticklabels())
    plt.xlim(0.0, 1.0)
    plt.ylabel("样本数")
    plt.xlabel("预测的概率")

    total_hap = np.sum(observed_grade_num)
    hfmc = np.zeros((len(total_grade_num), 4))
    for i in range(ngrade):
        hfmc[i, 0] = np.sum(observed_grade_num[i:])
        hfmc[i, 1] = np.sum(total_grade_num[i:]) - hfmc[i, 0]
        hfmc[i, 2] = total_hap - hfmc[i, 0]
        hfmc[i, 3] = total_num - (hfmc[i, 0] + hfmc[i, 1] + hfmc[i, 2])

    far = [1]
    far.extend(pofd_hfmc(hfmc).tolist())
    far.append(0)
    pod = [1]
    pod.extend(pod_hfmc(hfmc).tolist())
    pod.append(0)
    far = np.array(far)
    pod = np.array(pod)

    ax3 = plt.subplot(grid_plt[0:4, 1])
    plt.plot(far, pod, color="blue", linewidth=2, label="实际预报")
    plt.plot([0, 1], [0, 1], ":", color="k", linewidth=1, label="无技巧预报")
    plt.xlabel("空报率")
    plt.ylabel("命中率")
    plt.ylim(0.0, 1.0)
    plt.xlim(0.0, 1.0)
    plt.legend(loc=4)

    ax4 = plt.subplot(grid_plt[4:, 1])
    plt.bar(x - 0.01,
            not_observed_grade_num / total_num,
            width=0.02,
            edgecolor='r',
            fill=False,
            label="未发生")
    plt.bar(x + 0.01,
            observed_grade_num / total_num,
            width=0.02,
            color='b',
            label="已发生")

    plt.xlabel("预测的概率")
    plt.ylabel("占总样本数的比例")
    ymax = max(np.max(observed_grade_num / total_num),
               np.max(not_observed_grade_num / total_num)) * 1.5
    plt.ylim(0.0, ymax)
    plt.xlim(0.0, 1.0)

    plt.legend(loc="upper right", ncol=2)

    if save_path is None:
        plt.show()
    else:
        plt.savefig(save_path)
        print("检验结果已以图片形式保存至" + save_path)
    plt.close()