theislab / scvelo

RNA Velocity generalized through dynamical modeling
https://scvelo.org
BSD 3-Clause "New" or "Revised" License
408 stars 103 forks source link

scv.pl.velocity_embedding_stream can't save pdf format. Solved. #1184

Closed wy-gi closed 6 months ago

wy-gi commented 7 months ago

This problem stems from matplotlib. When using matplotlib to draw a line graph, if the line width is set to np.nan or 0, the line will not be visible. However, if it is set to np.nan, it will not be saved as pdf. The problem that velocity_embedding_stream cannot be saved as pdf comes from this.

Inspecting the code of the file located at site-packages/scvelo/plotting/velocity_embedding_stream.py reveals (probably at line 155):

         lengths = np.sqrt((V_grid**2).sum(0))
         linewidth = 1 if linewidth is None else linewidth
         linewidth *= 2 * lengths / lengths[~np.isnan(lengths)].max()

linewidth is an array list containing nan, so the solution is to replace the nan value. This is my code to replace nan linewidth = np.where(np.isnan(linewidth), 0, linewidth) Just add it below the code above.

The final code looks like

         lengths = np.sqrt((V_grid**2).sum(0))
         linewidth = 1 if linewidth is None else linewidth
         linewidth *= 2 * lengths / lengths[~np.isnan(lengths)].max()

         linewidth = np.where(np.isnan(linewidth), 0, linewidth)

This is the text via Google Translate, thank you.


这个问题源于matplotlib。 当使用matplotlib画线图的时候,线宽设置为 np.nan 和 0,都将看不到线,但是,如果设置为 np.nan,将无法保存为pdf。 velocity_embedding_stream 无法保存为pdf的问题源自于此。

检查位置位于site-packages/scvelo/plotting/velocity_embedding_stream.py 的文件的代码可以发现(大概位于第155行):

        lengths = np.sqrt((V_grid**2).sum(0))
        linewidth = 1 if linewidth is None else linewidth
        linewidth *= 2 * lengths / lengths[~np.isnan(lengths)].max()

linewidth 是一个array列表,其中含有 nan,故解决方案是替换掉其中 nan 值。 这是我替换nan的代码 linewidth = np.where(np.isnan(linewidth), 0, linewidth) 将其添加到上边代码的下方即可。

最终代码看起来像

        lengths = np.sqrt((V_grid**2).sum(0))
        linewidth = 1 if linewidth is None else linewidth
        linewidth *= 2 * lengths / lengths[~np.isnan(lengths)].max()

        linewidth = np.where(np.isnan(linewidth), 0, linewidth)

这是通过谷歌翻译得到的文本,谢谢。