【发布时间】:2016-08-17 17:12:59
【问题描述】:
H2 > H2
H2 > H2
H2 > H2*
H2 > H2*
H2 > H + H
H2 > H2^
H2 > H* + H
H2 > H + H
H2^ > H2^
H2^ > H^ + H
H2^ > H + H
H3^ > H3^
H3^ > H^ + H2
H3^ > H + H2
H > H
H > H*
H > H^
H^ > H^
H^ > H
H + H2^ > H2 + H^
H2 + H2^ > H + H3^
CF4 > CF4
CF4 > F- + CF3
我希望我的程序在地图上为化学中的每个物种创建节点,并在反应物和产物之间绘制路径,其中一个物种在地图上只出现一次,并且地图上有线条来表示反应中每个反应物的反应反应中的每个产物。
我已经编写了以下代码,但是该代码只是将每个反应绘制在地图上,并没有将它们连接起来,我不确定如何最好地继续连接反应中的常见物种。
import os
import sys
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import copy
try:
data = sys.argv[1]
except IndexError:
print('Insert filename')
sys.exit(1)
def parse_line(line):
new=line.split('>')
reactants=new[0].split('+')
products=new[1].split('+')
return reactants,products
all_edges=[]
edge_labels={}
all_reactants=[]
all_products=[]
with open(data) as fi:
for line in fi.readlines():
line = line.strip()
if not line or line[0] in '#%!':
# skip blank and comment lines
continue
reactants,products = parse_line(line)
for i in np.arange(len(reactants)):
other_reactants=copy.copy(reactants)
other_reactants.remove(reactants[i])
other_reactants=', '.join(other_reactants)
for j in np.arange(len(products)):
edge=(reactants[i],products[j])
all_edges.append(edge)
edge_labels[edge]=other_reactants
gr=nx.DiGraph()
gr.add_edges_from(all_edges)
pos=nx.random_layout(gr)
nx.draw_networkx_nodes(gr,pos,node_size=2000,node_shape='o',node_color='0.75',alpha=10)
nx.draw_networkx_edges(gr,pos, width=0.05,edge_color='b')
nx.draw_networkx_labels(gr, pos,font_size=12, font_color='k', font_weight='normal', alpha=1.0, ax=None)
nx.draw_networkx_edge_labels(gr,pos,edge_labels=edge_labels, label_pos=0.01, ax=None, rotate=False)
plt.show()
任何人都可以建议我进行此操作的最佳方法吗,我需要一个函数来识别所有反应中的反应物和产物中的常见物种,并为每个独特的物种创建一个节点,并从每个反应物创建一条线到每个反应的每个产物。
任何帮助将不胜感激
非常感谢
【问题讨论】:
-
化工背景出身,文字还是挺难理解的;有没有一种方法可以在没有化学术语的情况下概括这一点?我从来没有用过
networkx,所以我不能半途而废,但我认为这在目前的状态下很难回答。 -
从本质上讲,我认为您要求的是 有向图,其中
>左侧的所有内容都指向与 @987654325 右侧相关联的所有内容@?但是H2 > H2到底是什么意思,为什么会出现两次?另外,您认为(我认为是)激进的H*与H不同?我认为这些问题可能会成为答案的障碍......出于我自己的好奇心,^是什么? -
非常感谢您的关注和阐明问题。我将首先回答您的第二个问题,因为我将为您的第二个问题准备一些 .png。有两种类型的反应物“电子碰撞反应”和“重粒子反应”。在电子碰撞反应中,电子没有打印出来,因为它们不会出现在化学图上。所以 H2 > H2 实际上是 e + H2 > e + H2 这称为弹性反应。这些反应不应该出现在地图上,但我可以输入一些代码来消除它们。 H* 是 H 的“亚稳态”,它是不同的。
-
而 H^ 仅表示 H+,即离子