Python Snakemake:如何让shell命令在规则中使用不同的参数(整数)运行?

Python Snakemake:如何让shell命令在规则中使用不同的参数(整数)运行?,python,shell,integer,snakemake,Python,Shell,Integer,Snakemake,我正试图为我的增强决策树训练研究最佳超参数。以下是两个实例的代码: user = '/home/.../BDT/' nestimators = [1, 2] rule all: input: user + 'AUC_score.pdf' rule testing: output: user + 'AUC_score.csv' shell: 'python bdt.py --nestimators {}'.format(nestimators[

我正试图为我的增强决策树训练研究最佳超参数。以下是两个实例的代码:

user = '/home/.../BDT/'

nestimators = [1, 2]

rule all:
        input: user + 'AUC_score.pdf'

rule testing:
        output: user + 'AUC_score.csv'
        shell: 'python bdt.py --nestimators {}'.format(nestimators[i] for i in range(2))

rule plotting:
        input: user + 'AUC_score.csv'
        output: user + 'AUC_score.pdf'
        shell: 'python opti.py
计划如下: 我想用一系列不同的超参数来并行化BDT的训练(开始时我只想从nestimators开始)。因此,我尝试使用shellcommand来训练bdt。bdt.py获取训练参数,训练并将hyperparameters+训练分数保存在csv文件中。在csv文件中,我可以查看哪些超参数给出了最好的分数。是的


不幸的是,它不是那样工作的。我试图使用输入函数,但因为我想给出一个整数,所以它不起作用。我按照您在上面看到的方式进行了尝试,但知道我收到了一条“错误消息”:“python bdt.py--nestimators”。我理解为什么这也不起作用,但我不知道从这里开始该怎么办。

代码中的问题是,范围(2)中的I的表达式
nestimators[I]不是一个列表(如您所想)。这是一个生成器,它不会生成任何值,除非您显式地这样做。例如,此代码:

'python bdt.py--nestimators{}.格式(列表(范围(2)中i的nestimators[i]))
生成结果“python bdt.py--nestimators[1,2]”

实际上,您根本不需要生成器,因为此代码生成完全相同的输出:

'python bdt.py--nestimators{}。格式(nestimators)
此格式可能不是脚本所期望的格式。例如,如果希望获得如下命令行:
python bdt.py--nestimators 1,2
,可以使用以下表达式:

'python bdt.py--nestimators{}.format(“,”.join(map(str,nestimators)))
如果可以使用f字符串,则可以减少最后一个表达式:

f'python bdt.py--nestimators{“,”.join(map(str,nestimators))}

出现错误是因为
{}
被生成器对象替换,也就是说,它不是先被
1
替换,然后被
2
替换,而是被
nestimators
上的迭代器替换

即使您在规则
测试中更正了python表达式
。如果我正确理解你的目标,可能会有一个更根本的问题。 因此,函数测试将只调用一次,但您可能希望为每个超参数分别调用规则

解决方案是在输出文件名中添加hyperparameter。 大概是这样的:

user = '/home/.../BDT/'

nestimators = [1, 2]

rule all:
        input: user + 'AUC_score.pdf'

rule testing:
        output: user + 'AUC_score_{hyper}.csv'
        shell: 'python bdt.py --nestimators {wildcards.hyper}'

rule plotting:
        input: expand(user + 'AUC_score_{hyper}.csv', hyper=nestimators)
        output: user + 'AUC_score.pdf'
        shell: 'python opti.py'
最后,不要使用
shell:
调用python脚本。您可以直接使用
脚本:
,如文档中所述:

请注意,在@jmai的原始帖子中,
opti.py
后面缺少一个引号(但我无法编辑一个字符)。