Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plot cp factors #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

caglayantuna
Copy link
Member

I will submit a PR soon to add two plot cp factor functions. I wanted to add a notebook here to show their outputs with different options.

@MarieRoald
Copy link

MarieRoald commented Apr 15, 2022

Cool! @yngvem and I have also been working on visualisation for TensorLy! We were unsure if it was the right scope for the main TensorLy repository, so we made it as a standalone package. It is still somewhat lacking in the documentation, but I think it covers most of what we are interested in implementation-wise. We hope to have it ready for release by the end of April :)

We just got it up on ReadTheDocs, and you can see the plotting functions here and some example scripts here if you're interested.

Edit: Updated URLs

@caglayantuna
Copy link
Member Author

It seems very nice, I didn't know about your package. As we talked with Jean before, he is eager to have some visualization functions in tensorly. I was thinking to add only following function to cp_tensor ;

def plot_cp_factors(cp_tensors, title=None, mode_info=None, permute_factors=False, x_lim=None, y_lim=None, print=False):
    """
    Plots each factors columnwise as line plots.
    Several cp_tensors may be provided as input, in which case the factors of each cp_tensor are plot in subplots.
    Parameters
    ----------
    cp_tensor : cp_tensors or list of cp_tensors
        The input factors as cp_tensor to be visualized.
    title: list of string
        Information about the each cp_tensor in order to name the figures properly.
        Default=None
    mode_info: list of string
        Information about the each mode
        Default:None
    permute_factors : bool
        Permutes factors by using first cp tensor as a reference for permutation.
        Default:False
    x_lim: list
        Range for x axis
        Default:None
    y_lim: list
        Range for y axis
        Default:None
    print: bool
        Set to True if, on top of returning the figure and the axes, this function should show the figures while running.
    Returns
    -------
    fig : matplotlib figure
    axs : ndarray
    """
    if permute_factors:
        n_cp_tensors = len(cp_tensors)
        if n_cp_tensors == 1:
            raise ValueError('Only one cp tensor is not enough for permutation')
        permuted_tensors, _ = cp_permute_factors(cp_tensors[0], cp_tensors[1:])
        if n_cp_tensors == 2:
            cp_tensors[1] = permuted_tensors.cp_copy()
        else:
            for i in range(n_cp_tensors - 1):
                cp_tensors[i + 1] = permuted_tensors[i].cp_copy()

    if isinstance(cp_tensors, CPTensor):
        cp_tensors = [cp_tensors]
    colors = ['b', 'g', 'r', "y", "c", "m"]
    if print is False:
        plt.ioff()
    if title:
        if len(cp_tensors) != len(title):
            raise ValueError('{0} title is given but there are {1} cp tensors as an input'.format(len(title), len(cp_tensors)))
    if mode_info:
        if len(mode_info) != len(cp_tensors[0].factors):
            raise ValueError('{0} mode_info is given but there are {1} modes in the given cp tensors'.format(len(mode_info), len(cp_tensors[0].factors)))
    for i in range(len(cp_tensors)):
        factor = cp_tensors[i].factors
        rank = T.shape(factor[0])[1]
        fig, axs = plt.subplots(rank, len(factor))
        plt.subplots_adjust(hspace=1.5)
        fig.set_size_inches(15, fig.get_figheight(), forward=True)
        for j in range(len(factor)):
            if title:
                fig.suptitle(str(title[i]))
            else:
                fig.suptitle(str(i + 1) + ". tensor")
            for r in range(rank):
                axs[r, j].plot(factor[j][:, r], color=colors[j])
                if x_lim:
                    axs[r, j].set_xlim(x_lim)
                if y_lim:
                    axs[r, j].set_ylim(y_lim)
                if mode_info:
                    axs[r, j].set_title(str(j + 1) + ". factor" + ' ' + str(r + 1) + ". rank:" + ' ' + str(mode_info[j]))
                else:
                    axs[r, j].set_title(str(j + 1) + ". factor" + ' ' + str(r + 1) + ". rank")
    return fig, axs

Your package has wider perspective (core heatmap etc), we can have separate visualization module with your functions in Tensorly. What is your opinion about it @JeanKossaifi ?

@JeanKossaifi
Copy link
Member

As we discussed, visualization is something we really need in TensorLy and your project does that really well @MarieRoald so would love to have it in TensorLy!

@cohenjer
Copy link

cohenjer commented May 2, 2022

Hey @JeanKossaifi @MarieRoald @caglayantuna,
Great to see how developed Marie's package is, it is a great piece of software really needed in tensorly, not just for visualisation but also many kind of post-processing/validation.

I was wondering if we could still push forward with the small visualisation functions we have been working on with @caglayantuna; I suspect merging @MarieRoald's code will take some time considering the number of functionalities it has, while our contribution is more modest and could serve as a quick visualisation patch while we work out the full thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants