Exemple #1
0
def cvtLCH_to_HTML(LCH_1channel):
    lch_img = np.ones((2, 2, 3), dtype=np.float32) * LCH_1channel
    BGR_img = color_changer.cvtLCH2BGR(lch_img)
    RGB_img = cv2.cvtColor(BGR_img, cv2.COLOR_BGR2RGB)
    RGB_1channel = RGB_img[0, 0]
    return cvtRGB_to_HTML(RGB_1channel)
def main(root_dir, exp_name):
    """関連フォルダ・ファイルの存在確認"""
    if not os.path.exists(root_dir):
        print "root_dir", root_dir, "is not exist"
        exit()
    exp_dir = os.path.join(root_dir, exp_name)
    if not os.path.exists(exp_dir):
        print "exp_dir", exp_dir, "is not exist"
        exit()

    with open(os.path.join(exp_dir, "instance.pkl")) as fi:
        lda = pickle.load(fi)

    dim = 2
    K = lda.K
    #values=lda.phi()*(lda.theta().sum(axis=0)[np.newaxis].T)
    values = lda.phi()
    #values=lda.theta()
    """要素間距離の算出"""
    delta = np.zeros((values.shape[0], values.shape[0], values.shape[1]),
                     dtype=values.dtype)  #x値とy値の差の行列を格納
    for i in range(values.shape[1]):
        delta[:, :, i] = values[:, i, None] - values[:, i]  #Noneは縦ベクトルにしているだけ
    # distance between points
    distance = np.sqrt((delta**2).sum(axis=-1))

    pca = decomposition.PCA(dim)
    pca.fit(values)
    phi_pca = pca.transform(values)

    fig = plt.figure()

    if dim == 1:
        ax = fig.add_subplot(111)
        ax.scatter(phi_pca, phi_pca, s=1000, c=COLORLIST[:K])
        #for k in range(K):
        #	ax.annotate(unicode(k+1),phi_pca[k])
        """主成分分析の結果をもとに,トピックごとの色リストを定義"""
        l = 100
        c = 1.
        reg_phi_pca = (phi_pca - phi_pca.min()) / (
            phi_pca.max() - phi_pca.min())  #0~1に正規化
        #reg_phi_pca=reg_phi_pca*2-1#-1~1に変換
        h_values = reg_phi_pca * np.pi

        use_color_list = []
        for k in range(K):
            lch = np.array((l, c, h_values[k]), dtype=np.float32)
            lch_img = np.ones((2, 2, 3), dtype=np.float32) * lch
            BGR_img = color_changer.cvtLCH2BGR(lch_img)
            RGB_img = cv2.cvtColor(BGR_img, cv2.COLOR_BGR2RGB)
            RGB_1channel = RGB_img[0, 0]

            use_color_list.append(cvtRGB_to_HTML(RGB_1channel))

        fig2 = plt.figure()
        ax2 = fig2.add_subplot(1, 1, 1)
        labels = [unicode(x + 1) for x in range(lda.K)]
        sample = [0.1 for i in range(lda.K)]
        plt.rcParams['font.size'] = 20.0
        ax2.pie(sample,
                colors=use_color_list,
                labels=labels,
                startangle=90,
                radius=0.2,
                center=(0.5, 0.5),
                frame=True,
                counterclock=False)
        #ax.set_aspect((ax.get_xlim()[1] - ax.get_xlim()[0]) / (ax.get_ylim()[1] - ax.get_ylim()[0]))
        plt.axis("off")
        plt.axis('equal')

    elif dim == 2:
        ax = fig.add_subplot(111)
        ax.scatter(phi_pca[:, 0], phi_pca[:, 1], s=1000, c=COLORLIST[:K])
        for k in range(K):
            ax.annotate(unicode(k + 1), phi_pca[k])
        center, size, angle = cv2.fitEllipse(phi_pca.astype(np.float32))
        ell = Ellipse(xy=center, width=size[0], height=size[1], angle=angle)
        ell.set_facecolor('none')
        ax.add_artist(ell)  # fitted curve

        #xx=np.random.rand(10,2)
        #xx[5,0]=10
        #plt.scatter(xx[:,0], xx[:,1]) # raw data with randomness
        #center,size,angle=cv2.fitEllipse(xx.astype(np.float32))

        #ell = Ellipse(xy=center, width=size[0], height=size[1], angle=angle)
        #ell.set_facecolor('none')
        #ax.add_artist(ell) # fitted curve

    elif dim == 3:
        ax = fig.add_subplot(111, projection="3d")
        ax.scatter(phi_pca[:, 0],
                   phi_pca[:, 1],
                   phi_pca[:, 2],
                   s=1000,
                   c=COLORLIST[:K])

    plt.show()