gpt4 book ai didi

python - networkX 缺少节点错误

转载 作者:行者123 更新时间:2023-12-01 02:41:57 25 4
gpt4 key购买 nike

我有一些函数用于计算 networkX 图的一些值,以下是它们的代码:

def signal_path_counter(G, node, inputs, outputs):
c = 0
paths = []
for input, output in itertools.product(inputs, outputs):
for path in all_simple_paths(G, input, output):
if node in path:
c += 1
return c

def feedback_loop_counter(G, node):
neighbors = G.neighbors(node)
cycles = []
for neighbor in neighbors:
path = all_simple_paths(G, neighbor, node)
if path not in cycles:
cycles.append(path)
return len(cycles)

def sigFluxCalc(G, node, inputs, outputs):
numerator = signal_path_counter(G, node, inputs, outputs) +
feedback_loop_counter(G, node)
denominator = 0
for n in G.nodes():
temp = signal_path_counter(G, n, inputs, outputs) + feedback_loop_counter(G, n)
denominator += temp
return numerator/denominator

这是我的输入图:

molecules = ["TNF", "RIP1", "FASL", "clAP", "CASP8", "ROS", "MPT", "MOMP", 
"NFkB", "ATP", "CASP3", "Survival", "NonACD", "Apoptosis"]
TNF = [("TNF", "RIP1"), ("TNF", "CASP8")]
FASL = [("FASL", "RIP1"), ("FASL", "CASP8")]
RIP1 = [("RIP1", "NFkB"), ("RIP1", "ROS")]
CASP8 = [("CASP8", "RIP1"), ("CASP8", "MOMP")]
cIAP = [("cIAP", "cIAP"), ("cIAP", "NFkB")]
NFkB = [("NFkB", "cIAP"),("NFkB", "Survival"), ("NFkB", "ROS"), ("NFkB", "CASP8"), ("NFkB", "MOMP"), ("NFkB", "MPT"), ("NFkB", "CASP3")]
ROS = [("ROS", "MPT")]
MPT = [("MPT", "MOMP"), ("MPT", "ATP"), ("MPT", "ROS")]
MOMP = [("MOMP", "cIAP"), ("MOMP", "CASP3")]
ATP = [("ATP", "NonACD"), ("ATP", "CASP3")]
CASP3 = [("CASP3", "Apoptosis"), ("CASP3", "NFkB"), ("CASP3", "CASP8")]
edges = TNF + FASL + RIP1 + CASP8 + cIAP + NFkB + ROS + MPT + MOMP + ATP + CASP3
G.add_nodes_from(molecules)
G.add_edges_from(edges)
sources = ["TNF", "FASL"]
targets = ["Survival", "NonACD", "Apoptosis"]

如果你看不出来,这是一个代表人体细胞的网络。我试图使用图表上的函数为每个输出计算一次网络中每个节点的“SigFlux”(所以 3 次)。这是我的代码,应该做到这一点:

for output in targets:
print("SigFlux calculations for " + output + " as output:")
for node in G.nodes():
if(node != "Survival" or node != "NonACD" or node != "Apoptosis"):
print(node + ": " + str(sigFluxCalc(G, node, sources, output)))

但是,运行脚本时出现此错误:

Traceback (most recent call last):
File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 200, in <module>
print(node + ": " + str(sigFluxCalc(G, node, sources, output)))
File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 144, in sigFluxCalc
numerator = signal_path_counter(G, node, inputs, outputs) + feedback_loop_counter(G, node)
File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 129, in signal_path_counter
for path in all_simple_paths(G, input, output):
File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 69, in all_simple_paths
raise nx.NetworkXError('target node %s not in graph'%target)
networkx.exception.NetworkXError: target node S not in graph

无法理解问题所在。如果您想自己运行完整的脚本:https://pastebin.com/jBeX7EHs

最佳答案

您的代码有这一行:

for input, output in itertools.product(inputs, outputs)

但是当调用它时,outputs是字符串 'Survival' 。因此它会迭代 ' Survival' 中的所有值,即:'S' , 'u' ,...

您可以通过编辑函数或更改发送到函数的参数来解决此问题。所以替换sigFluxCalc(G, node, sources, output)通过sigFluxCalc(G, node, sources, [output])会起作用的。

<小时/>

作为注释,我认为您的这行代码:

if(node != "Survival" or node != "NonACD" or node != "Apoptosis"):

读起来更好:

if node not in targets:

关于python - networkX 缺少节点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com