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: