我正在探索 Snakemake 将我的数据分析定义为 DAG 并可重复地对其进行评估。我将其安装在 Windows 10 上的 WSL2 上的单独环境中。在开始实际项目之前,我想在一个简单的项目上尝试一下,以了解它的工作原理。
我想在 3 个 txt 文件(2 列数字,逗号分隔)上使用 shell 和 python 进行测试。第一条规则通过 shell 命令将文件作为 .csv 复制到单独的文件夹“intermediate”。然后,第二条规则加载一个 python 脚本来读取中间文件并使用 matplotlib 绘制它的图表。我想在单独的 conda 环境中运行 python 脚本。这对于我想做的事情来说并不是绝对必要的,但我可以看到它的好处。
DAG正常运行并输出文件。然而涉及 conda/python 的规则需要 90 秒。这似乎没有必要太长,从命令行我预计它会在一秒钟左右运行。我做错了什么吗?是不是后台发生了一些我不知道的事情?即使有必要,我想如果我知道发生了什么,会更容易接受。
Activating conda environment: ../.snakemake/conda/a060898bb3a415a46236eba6c4b6b5fa_
所以我认为这是激活花了很长时间。我的蛇文件如下所示。
工作流程/蛇文件
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...
谢谢!
事实证明问题出现在一个意想不到的地方:当我要求 python 脚本打印它时,它运行得足够快。问题在于 WSL 中没有图形后端。在
a_script.py
顶部添加以下内容解决了问题。
import matplotlib
matplotlib.use('Agg')