考虑以下脚本,我在其中测试两种方法,对通过以下方式获得的生成器执行某些计算itertools.tee:
#!/usr/bin/env python3
from sys import argv
from itertools import tee
from multiprocessing import Process
def my_generator():
for i in range(5):
print(i)
yield i
def double(x):
return 2 * x
def compute_double_sum(iterable):
s = sum(map(double, iterable))
print(s)
def square(x):
return x * x
def compute_square_sum(iterable):
s = sum(map(square, iterable))
print(s)
g1, g2 = tee(my_generator(), 2)
try:
processing_type = argv[1]
except IndexError:
processing_type = "no_multi"
if processing_type == "multi":
p1 = Process(target=compute_double_sum, args=(g1,))
p2 = Process(target=compute_square_sum, args=(g2,))
print("p1 starts") …Run Code Online (Sandbox Code Playgroud) 我知道如何使用三种颜色(黑色、白色和灰色)机制来检测图表中的循环。对于那些不知道这一点的人,请按照以下步骤操作: http://www.geeksforgeeks.org/detect-cycle-direct-graph-using-colors/
简而言之,白色节点是未发现的节点,灰色是正在处理的节点,黑色节点是已发现的节点。
几天前,一位资深人士问我为什么需要三种颜色才能达到这一目的。为什么我们不能只用黑白来做到这一点?为什么真正需要灰色节点?可惜我能回答他这个问题。你们谁能告诉我他是在问我一个有效的问题还是只是在测试我?
我有两个熊猫数据框df1,df2我想要它们的“合并索引”。
我的意思是当我这样做时获得的索引df1.add(df2, fill_value=0).index(基本上,行名称的联合)。这种计算(此处为add)在单独的脚本中执行,我不想在这些脚本中计算“合并索引”,但是当我只对“合并索引”。
有没有更“直接”(希望有效)的方法来做到这一点?
我的目标实际上是将“标签”与索引元素相关联。我有几对数据框。每一对对应一个“标签”并且可能有重叠的索引。不同的对对应不同的标签,并且它们应该没有重叠的索引。
基本上,我正在寻找一个associate_tag函数的有效实现,该函数的工作方式如下:
dfA_1:
idA_1 2 0
idA_2 1 0
idA_3 0 2
Run Code Online (Sandbox Code Playgroud)
dfA_2:
idA_1 3 2 1
idA_3 2 6 2
idA_4 4 0 2
Run Code Online (Sandbox Code Playgroud)
merge_A = associate_tag((dfA_1, dfA_2), "A"):
idA_1 A
idA_2 A
idA_3 A
idA_4 A
Run Code Online (Sandbox Code Playgroud)
dfB_1:
idB_1 2 2 1
idB_2 3 0 0
idB_3 3 1 3
Run Code Online (Sandbox Code Playgroud)
dfB_2:
idB_1 0
idB_2 3
idB_4 2
Run Code Online (Sandbox Code Playgroud)
merge_B = associate_tag((dfB_1, …
我benchmark在snakemake工作流程中包括了一些规则的指令,并且生成的文件具有以下标头:
s h:m:s max_rss max_vms max_uss max_pss io_in io_out mean_load
Run Code Online (Sandbox Code Playgroud)
我找到的唯一文档提到了“基准txt文件(它将在MiB中包含运行时间和内存使用情况的制表符分隔的表)”。
我可以猜测,第1列和第2列是显示执行规则所花费时间的两种不同方式(以秒为单位,并转换为小时,分钟和秒)。
io_in并io_out可能与磁盘的读取及写入活动,但在被他们测量什么单位?
还有什么?这是在某处记录的吗?
我在中找到了以下代码/snakemake/benchmark.py,这很可能是基准数据的来源:
s h:m:s max_rss max_vms max_uss max_pss io_in io_out mean_load
Run Code Online (Sandbox Code Playgroud)
因此,这似乎来自所提供的功能psutil。
我正在尝试首先为LETTERS x NUMS组合生成4个文件,然后通过NUMS进行汇总,以获得LETTERS中每个元素的一个文件:
LETTERS = ["A", "B"]
NUMS = ["1", "2"]
rule all:
input:
expand("combined_{letter}.txt", letter=LETTERS)
rule generate_text:
output:
"text_{letter}_{num}.txt"
shell:
"""
echo "test" > {output}
"""
rule combine text:
input:
expand("text_{letter}_{num}.txt", num=NUMS)
output:
"combined_{letter}.txt"
shell:
"""
cat {input} > {output}
"""
Run Code Online (Sandbox Code Playgroud)
执行此snakefile会导致以下错误:
WildcardError in line 19 of /tmp/Snakefile:
No values given for wildcard 'letter'.
File "/tmp/Snakefile", line 19, in <module>
Run Code Online (Sandbox Code Playgroud)
似乎部分expand是不可能的.这是一个限制expand吗?如果是这样,我该如何规避呢?
假设我有以下文件,我想使用snakemake自动对其进行一些处理:
test_input_C_1.txt
test_input_B_2.txt
test_input_A_2.txt
test_input_A_1.txt
Run Code Online (Sandbox Code Playgroud)
以下snakefile用于expand确定所有可能的最终结果文件:
rule all:
input: expand("test_output_{text}_{num}.txt", text=["A", "B", "C"], num=[1, 2])
rule make_output:
input: "test_input_{text}_{num}.txt"
output: "test_output_{text}_{num}.txt"
shell:
"""
md5sum {input} > {output}
"""
Run Code Online (Sandbox Code Playgroud)
执行上面的snakefile会导致以下错误:
test_input_C_1.txt
test_input_B_2.txt
test_input_A_2.txt
test_input_A_1.txt
Run Code Online (Sandbox Code Playgroud)
其中的原因错误是expand使用itertools.product引擎盖下产生的通配符的组合,其中一些发生在对应于丢失的文件。
如何过滤掉不想要的通配符组合?
python wildcard python-itertools higher-order-functions snakemake