2017-08-09 96 views
0

丢失节点的错误,我有我用来计算一些数值断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”为每个节点一次,对于每一输出(SO 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

回答

1

您的代码具有此行:

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: 
+0

我会如何“生存”和目标的其余部分表示为节点,某种形式的演员?对字符串对象相同的代码并没有在字符串逐个字符事先 – witcheR

+0

如果您运行的代码,其中'outputs'是目标列表,它会通过每个迭代迭代左右。但是,如果您在'outputs'是单个输出(这是一个字符串)的地方发送它,它会假定您要遍历字符串。迭代字符串的方法是逐字符。如果确保'sigFluxCalc'发送的东西看起来像输出列表而不是单个输出,那么可以绕过它。做到这一点的一种方法是简单地发送它[输出]。 (见编辑) – Joel

+0

似乎有效,谢谢。这是输出我现在得到:https://pastebin.com/XvL2tThY – witcheR