dpeerlab / Palantir

Single cell trajectory detection
https://palantir.readthedocs.io
GNU General Public License v2.0
203 stars 45 forks source link

Error while running plot_palantir_results #130

Closed Yansr3 closed 5 months ago

Yansr3 commented 5 months ago

Hello,

I was trying to run palantir on my dataset. When try to visualize the result, I got this error.

palantir.plot.plot_palantir_results(pr_res, tsne)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[/var/folders/7q/9l9bx0fx6ml2c_ld_zmbtkw00000gt/T/ipykernel_76740/2417268260.py](https://file+.vscode-resource.vscode-cdn.net/var/folders/7q/9l9bx0fx6ml2c_ld_zmbtkw00000gt/T/ipykernel_76740/2417268260.py) in ?()
----> 1 palantir.plot.plot_palantir_results(pr_res, tsne)

[~/miniconda3/envs/JFD_scRNAseq_env/lib/python3.9/site-packages/palantir/plot.py](https://file+.vscode-resource.vscode-cdn.net/Users/siruiyan/Workspace/EryProject/DilworthPipeline_scRNAseq/~/miniconda3/envs/JFD_scRNAseq_env/lib/python3.9/site-packages/palantir/plot.py) in ?(data, pr_res, embedding_basis, pseudo_time_key, entropy_key, fate_prob_key, **kwargs)
    509             )
    510     else:
    511         embedding_data = data
    512 
--> 513     n_branches = pr_res.branch_probs.shape[1]
    514     n_cols = 6
    515     n_rows = int(np.ceil(n_branches [/](https://file+.vscode-resource.vscode-cdn.net/) n_cols))
    516     plt.figure(figsize=[2 * n_cols, 2 * (n_rows + 2)])

[~/miniconda3/envs/JFD_scRNAseq_env/lib/python3.9/site-packages/pandas/core/generic.py](https://file+.vscode-resource.vscode-cdn.net/Users/siruiyan/Workspace/EryProject/DilworthPipeline_scRNAseq/~/miniconda3/envs/JFD_scRNAseq_env/lib/python3.9/site-packages/pandas/core/generic.py) in ?(self, name)
   5898             and name not in self._accessors
   5899             and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5900         ):
   5901             return self[name]
-> 5902         return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'branch_probs'

I checked pr_res.branch_probs and it looks fine.

pr_res.branch_probs.shape
(15906, 2)

May I ask if there is any advice for plotting the result?

Thank you!

katosh commented 5 months ago

Of course! You might find the docstring of the function helpful:

plot_palantir_results(
    data: Union[anndata._core.anndata.AnnData, pandas.core.frame.DataFrame],
    pr_res: Optional[palantir.presults.PResults] = None,
    embedding_basis: str = 'X_umap',
    pseudo_time_key: str = 'palantir_pseudotime',
    entropy_key: str = 'palantir_entropy',
    fate_prob_key: str = 'palantir_fate_probabilities', **kwargs
)
    Plot Palantir results on t-SNE or UMAP plots.

    Parameters
    ----------
    data : Union[sc.AnnData, pd.DataFrame]
        Either a Scanpy AnnData object or a DataFrame of tSNE or UMAP results.
    pr_res : Optional[PResults]
        Optional PResults object containing Palantir results. If None, results are expected to be found in the provided AnnData object.
    embedding_basis : str, optional
        The key to retrieve UMAP results from the AnnData object. Defaults to 'X_umap'.
    pseudo_time_key : str, optional
        Key to access the pseudotime from obs of the AnnData object. Default is 'palantir_pseudotime'.
    entropy_key : str, optional
        Key to access the entropy from obs of the AnnData object. Default is 'palantir_entropy'.
    fate_prob_key : str, optional
        Key to access the fate probabilities from obsm of the AnnData object. Default is 'palantir_fate_probabilities'.
    **kwargs
        Additional keyword arguments passed to `ax.scatter`.

    Returns
    -------
    matplotlib.pyplot.Figure
        A matplotlib Figure object representing the plot of the Palantir results.

It seems you might only have to flip the order of arguments to

palantir.plot.plot_palantir_results(tsne, pr_res)

Please let me know if this resolved the issue.

Yansr3 commented 5 months ago

Thank you very much, the issue is resolved.

katosh commented 5 months ago

You are very welcome!