Snakemake 在 WSL 上花费 90 秒来测试 Snakefile,工作负载非常简单

问题描述 投票:0回答:1

背景

我正在探索 Snakemake 将我的数据分析定义为 DAG 并可重复地对其进行评估。我将其安装在 Windows 10 上的 WSL2 上的单独环境中。在开始实际项目之前,我想在一个简单的项目上尝试一下,以了解它的工作原理。

我想在 3 个 txt 文件(2 列数字,逗号分隔)上使用 shell 和 python 进行测试。第一条规则通过 shell 命令将文件作为 .csv 复制到单独的文件夹“intermediate”。然后,第二条规则加载一个 python 脚本来读取中间文件并使用 matplotlib 绘制它的图表。我想在单独的 conda 环境中运行 python 脚本。这对于我想做的事情来说并不是绝对必要的,但我可以看到它的好处。

问题

DAG正常运行并输出文件。然而涉及 conda/python 的规则需要 90 秒。这似乎没有必要太长,从命令行我预计它会在一秒钟左右运行。我做错了什么吗?是不是后台发生了一些我不知道的事情?即使有必要,我想如果我知道发生了什么,会更容易接受。

到目前为止我已经尝试过的事情

  • 将项目移动到 WSL 磁盘:首先该项目在已安装的 c 上运行,但事实证明这存在速度问题。所以该项目现在位于我的主目录的子目录中。
  • 我第一次运行蛇文件时,在 .snakemake/conda/alphanumeric_env_name 中创建了带有 numpy 和 matplotlib 的环境。这花了一些时间(比使用 yml 中的 conda/mamba 手动创建环境要长一点,如此处所述),但 DAG 使用 --use-conda 工作。运行时,python规则仍然需要很长时间,bash打印的最后一件事是
    Activating conda environment: ../.snakemake/conda/a060898bb3a415a46236eba6c4b6b5fa_
    所以我认为这是激活花了很长时间。
  • 要检查这一点:在我的 Snakemake 环境本身中使用 conda 安装了 numpy 和 pandas,并注释掉了规则的 conda 部分。使用snakemake环境本身不太理想,但我可以忍受。尽管如此,执行速度还是非常慢(见下面的日志)。

代码

我的蛇文件如下所示。

工作流程/蛇文件

samples = "first_data,second_data,third_data"

rule all:
    input:
        expand("graphs/{file}.png", file=samples.split(",")),


rule make_intermediate:
    input:
        "data/{file}.txt",
    output:
        "intermediate/{file}_shell.csv",
    shell:
        "cp {input[0]} {output[0]}"


rule make_graph:
    input:
        "intermediate/{file}_shell.csv",
    output:
        "graphs/{file}.png",
    # conda:
    #    "../envs/data_env.yaml"
    script:
        "../scripts/a_script.py" 

a_script.py

import matplotlib.pyplot as plt
import numpy as np

def make_graph(filename_in, filename_out):
    data = np.loadtxt(filename_in, delimiter=',')
    plt.figure()
    plt.title(filename_in)
    plt.plot(data[:, 0], data[:, 1])
    plt.savefig(filename_out)
    plt.close()
make_graph(snakemake.input[0], snakemake.output[0])

这是相关的日志(我这里没有使用conda单独开环境)。与

snakemake -c 4

Select jobs to execute...
Execute 3 jobs...

[Wed Jan 17 19:58:26 2024]
localrule make_graph:
    input: intermediate/second_data_shell.csv
    output: graphs/second_data.png
    jobid: 3
    reason: Missing output files: graphs/second_data.png
    wildcards: file=second_data
    resources: tmpdir=/tmp

[Wed Jan 17 19:58:26 2024]
localrule make_graph:
    input: intermediate/first_data_shell.csv
    output: graphs/first_data.png
    jobid: 1
    reason: Missing output files: graphs/first_data.png
    wildcards: file=first_data
    resources: tmpdir=/tmp

[Wed Jan 17 19:58:26 2024]
localrule make_graph:
    input: intermediate/third_data_shell.csv
    output: graphs/third_data.png
    jobid: 5
    reason: Missing output files: graphs/third_data.png
    wildcards: file=third_data
    resources: tmpdir=/tmp

[Wed Jan 17 20:00:39 2024]
Finished job 1.
1 of 4 steps (25%) done
[Wed Jan 17 20:00:39 2024]
Finished job 3.
2 of 4 steps (50%) done
[Wed Jan 17 20:00:39 2024]
Finished job 5.
3 of 4 steps (75%) done
Select jobs to execute...
Execute 1 jobs...

谢谢!

conda snakemake
1个回答
0
投票

事实证明问题出现在一个意想不到的地方:当我要求 python 脚本打印它时,它运行得足够快。问题在于 WSL 中没有图形后端。在

a_script.py
顶部添加以下内容解决了问题。

import matplotlib
matplotlib.use('Agg')
© www.soinside.com 2019 - 2024. All rights reserved.