Esempio n. 1
0
def rank_progress(X, Y, Z, m, n, R, k=0, greys=True, rgb=False):
    """
    Plots the partial sums of rank one terms corresponding to the k-th slice of the CPD. The last image should match the
    original CPD. Use rgb=True only for tensors of the form (m, n, 3) encoding RGB format. The program will display the
    red rank one terms, then it will add the green rank one terms and then the blue rank one terms. This ordering may
    cause some distortions on the final image.

    Inputs
    ------
    X, Y, Z: 2-D arrays
        Their are the CPD of some third order tensor.
    m, n, p, R: int
    k: int
        Slice we want to visualize.
    greys: bool
        If True (default), it will show all slices in gray scale. Otherwise it will show the RGB evolution of the
        slices. In this case the parameter 'rgb' should be set to True.
    rgb: bool
        If True, it will show all the RGB evolution of the slices. False is default.    
    """

    if greys:
        temp = zeros((m, n))
        sections = mlinalg.rank1(X, Y, Z, m, n, R, k)
        for r in range(R):
            temp = temp + sections[:, :, r]
            plt.imshow(temp, cmap='gray')
            name = 'factor_' + str(r + 1) + '.png'
            plt.savefig(name)
            plt.show()

    elif rgb:
        count = 0
        temp = zeros((m, n, 3))
        for color_choice in [0, 1, 2]:
            sections = mlinalg.rank1(X, Y, Z, m, n, R, color_choice)
            for r in range(R):
                temp[:, :,
                     color_choice] = temp[:, :, color_choice] + sections[:, :,
                                                                         r]
                plt.imshow(array(temp, dtype=uint8))
                name = 'factor_' + str(count) + '.png'
                plt.savefig(name)
                plt.show()
                count += 1

    else:
        return

    return
Esempio n. 2
0
def rank1_plot(X,
               Y,
               Z,
               m,
               n,
               R,
               k=0,
               num_rows=5,
               num_cols=5,
               greys=True,
               rgb=False,
               save=False):
    """
    This function generates an image with the frontal sections of all rank one terms (in coordinates) of some CPD. It
    also saves the image in a file.
    Warning: this functions uses a lot of memory.

    Inputs
    ------
    X, Y, Z: 2-D arrays
        Their are the CPD of some third order tensor.
    m, n, R: int
    k: int
        Slice we want to visualize.
    num_rows, num_cols: int
        The dimensions of the grid of subplots. We recommend using squares grids in order to maximize the size of each 
        subplot. Some blank squares may be left at the end.
    greys: bool
        If True (default), it will show all slices in gray scale. Otherwise it will show the RGB evolution of the
        slices.
        In this case the parameter 'rgb' should be set to True.
    rgb: bool
        If True, it will show all the RGB evolution of the slices. Default is rgb=False.
    """

    sections = mlinalg.rank1(X, Y, Z, m, n, R, k)
    r = 0
    count = 0

    while r < R:
        fig, ax = plt.subplots(num_rows,
                               num_cols,
                               figsize=(30, 30),
                               sharex='col',
                               sharey='row')
        for i in range(num_rows):
            for j in range(num_cols):
                ax[i, j].xaxis.set_major_locator(plt.NullLocator())
                ax[i, j].yaxis.set_major_locator(plt.NullLocator())
                if r < R:
                    if greys:
                        temp = sections[:, :, r]
                        ax[i, j].imshow(temp, cmap='gray')
                    elif rgb:
                        temp = zeros((m, n, 3))
                        temp[:, :, k] = sections[:, :, r]
                        ax[i, j].imshow(array(temp, dtype=uint8))
                    else:
                        return
                r += 1

        if save:
            name = 'fig_' + str(count) + '.png'
            plt.savefig(name)
        count += 1

    return