2017-08-09 92 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