繁体   English   中英

python-networkX缺少节点错误

[英]python - missing node error with networkX

我有一些函数可以用来计算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:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM