由于 numpy.float64 值,将 NetworkX 图导出为 graphml 会引发异常
Exporting NetworkX graph as graphml throws exception due to numpy.float64 value
我正在使用以下代码分析加权化学反应网络:
import networkx as nx
import pandas as pd
edge_data = pd.read_table('VULCAN 800.dat', sep=',')
edge_list=[]
edge_data = edge_data.fillna(value=0.1)
col1=edge_data['Column_1']
col2=edge_data['Column_2']
col3=edge_data['Column_3']
col4=edge_data['Column_4']
col5=edge_data['Column_5']
col6=edge_data['Column_6']
for i in range(1,560):
edge_list.append((col1.iloc[i],col2.iloc[i],col5.iloc[i]))
if pd.isnull(col3.iloc[i]) != True:
edge_list.append((col1.iloc[i],col3.iloc[i],col5.iloc[i]))
edge_list.append((col2.iloc[i],col3.iloc[i],col5.iloc[i]))
if pd.isnull(col4.iloc[i]) != True:
edge_list.append((col1.iloc[i],col4.iloc[i],col5.iloc[i]))
edge_list.append((col2.iloc[i],col4.iloc[i],col5.iloc[i]))
if pd.isnull(col3.iloc[i]) != True and pd.isnull(col4.iloc[i]) != True:
edge_list.append((col3.iloc[i],col4.iloc[i],col5.iloc[i]))
G=nx.Graph()
G.add_weighted_edges_from(edge_list)
G.remove_node(0.1)
nx.write_graphml(G, '/home/tessa/Git/Network_biosignatures/VULCAN 800k.graphml')
我想将生成的图形导出为 graphml 文件,以便我可以在 Cytoscape 中对其进行可视化,但由于某种原因,它抛出了错误消息:
Traceback (most recent call last):
File "topo_measure_pn_hot jupiter_weighted.py", line 196, in <module>
nx.write_graphml(G, '/home/tessa/Git/Network_biosignatures/VULCAN 800k.graphml')
File "<decorator-gen-202>", line 2, in write_graphml
File "/usr/lib/python2.7/dist-packages/networkx/utils/decorators.py", line 220, in _open_file
result = func(*new_args, **kwargs)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 82, in write_graphml
writer.add_graph_element(G)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 351, in add_graph_element
self.add_edges(G,graph_element)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 325, in add_edges
self.add_attributes("edge", edge_element, data, default)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 300, in add_attributes
scope=scope, default=default_value)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 288, in add_data
'%s as data values.'%element_type)
networkx.exception.NetworkXError: GraphML writer does not support <type 'numpy.float64'> as data values.
显然,graphml 编写器不能很好地处理图形的简单属性以外的任何事情,我猜它是引发错误的权重。但是,我不确定 哪里 权重值被分配为 numpy.float64,或者如何从这个分配中删除它。
读入的数据格式如下:
1,H,H2O,OH,H2,1.75116588E-16,,,,,,,,,,,,,,,,,,,,,,,,,,,
2,H,H2O,OH,H2,4.00975292E-13,,,,,,,,,,,,,,,,,,,,,,,,,,,
3,O,H2,OH,H,9.25180896E-14,,,,,,,,,,,,,,,,,,,,,,,,,,,
4,O,H2,OH,H,1.04176774E-13,,,,,,,,,,,,,,,,,,,,,,,,,,,
5,O,H2O,OH,OH,1.04560994E-15,,,,,,,,,,,,,,,,,,,,,,,,,,,
6,O,H2O,OH,OH,2.6959031E-12,,,,,,,,,,,,,,,,,,,,,,,,,,,
感谢任何和所有建议。谢谢!
这似乎是 networkx 中的错误。转换为基本 python 类型可以解决问题。
import numpy as np
import networkx as nx
total_nodes = 20
nodes = range(total_nodes)
p = 0.1
total_edges = int(p * total_nodes ** 2 )
sources = np.random.choice(nodes, total_edges)
targets = np.random.choice(nodes, total_edges)
weights = np.random.rand(total_edges)
# edge_list = zip(sources, targets, weights) # doesn't work
edge_list = [(int(s), int(t), float(w)) for s, t, w in zip(sources, targets, weights)] # works
G = nx.Graph()
G.add_weighted_edges_from(edge_list)
nx.write_graphml(G, 'test.graphml')
编辑:
显然,他们很清楚这个问题 (https://github.com/networkx/networkx/issues/1556),但尚未解决。
我正在使用以下代码分析加权化学反应网络:
import networkx as nx
import pandas as pd
edge_data = pd.read_table('VULCAN 800.dat', sep=',')
edge_list=[]
edge_data = edge_data.fillna(value=0.1)
col1=edge_data['Column_1']
col2=edge_data['Column_2']
col3=edge_data['Column_3']
col4=edge_data['Column_4']
col5=edge_data['Column_5']
col6=edge_data['Column_6']
for i in range(1,560):
edge_list.append((col1.iloc[i],col2.iloc[i],col5.iloc[i]))
if pd.isnull(col3.iloc[i]) != True:
edge_list.append((col1.iloc[i],col3.iloc[i],col5.iloc[i]))
edge_list.append((col2.iloc[i],col3.iloc[i],col5.iloc[i]))
if pd.isnull(col4.iloc[i]) != True:
edge_list.append((col1.iloc[i],col4.iloc[i],col5.iloc[i]))
edge_list.append((col2.iloc[i],col4.iloc[i],col5.iloc[i]))
if pd.isnull(col3.iloc[i]) != True and pd.isnull(col4.iloc[i]) != True:
edge_list.append((col3.iloc[i],col4.iloc[i],col5.iloc[i]))
G=nx.Graph()
G.add_weighted_edges_from(edge_list)
G.remove_node(0.1)
nx.write_graphml(G, '/home/tessa/Git/Network_biosignatures/VULCAN 800k.graphml')
我想将生成的图形导出为 graphml 文件,以便我可以在 Cytoscape 中对其进行可视化,但由于某种原因,它抛出了错误消息:
Traceback (most recent call last):
File "topo_measure_pn_hot jupiter_weighted.py", line 196, in <module>
nx.write_graphml(G, '/home/tessa/Git/Network_biosignatures/VULCAN 800k.graphml')
File "<decorator-gen-202>", line 2, in write_graphml
File "/usr/lib/python2.7/dist-packages/networkx/utils/decorators.py", line 220, in _open_file
result = func(*new_args, **kwargs)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 82, in write_graphml
writer.add_graph_element(G)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 351, in add_graph_element
self.add_edges(G,graph_element)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 325, in add_edges
self.add_attributes("edge", edge_element, data, default)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 300, in add_attributes
scope=scope, default=default_value)
File "/usr/lib/python2.7/dist-packages/networkx/readwrite/graphml.py", line 288, in add_data
'%s as data values.'%element_type)
networkx.exception.NetworkXError: GraphML writer does not support <type 'numpy.float64'> as data values.
显然,graphml 编写器不能很好地处理图形的简单属性以外的任何事情,我猜它是引发错误的权重。但是,我不确定 哪里 权重值被分配为 numpy.float64,或者如何从这个分配中删除它。
读入的数据格式如下:
1,H,H2O,OH,H2,1.75116588E-16,,,,,,,,,,,,,,,,,,,,,,,,,,,
2,H,H2O,OH,H2,4.00975292E-13,,,,,,,,,,,,,,,,,,,,,,,,,,,
3,O,H2,OH,H,9.25180896E-14,,,,,,,,,,,,,,,,,,,,,,,,,,,
4,O,H2,OH,H,1.04176774E-13,,,,,,,,,,,,,,,,,,,,,,,,,,,
5,O,H2O,OH,OH,1.04560994E-15,,,,,,,,,,,,,,,,,,,,,,,,,,,
6,O,H2O,OH,OH,2.6959031E-12,,,,,,,,,,,,,,,,,,,,,,,,,,,
感谢任何和所有建议。谢谢!
这似乎是 networkx 中的错误。转换为基本 python 类型可以解决问题。
import numpy as np
import networkx as nx
total_nodes = 20
nodes = range(total_nodes)
p = 0.1
total_edges = int(p * total_nodes ** 2 )
sources = np.random.choice(nodes, total_edges)
targets = np.random.choice(nodes, total_edges)
weights = np.random.rand(total_edges)
# edge_list = zip(sources, targets, weights) # doesn't work
edge_list = [(int(s), int(t), float(w)) for s, t, w in zip(sources, targets, weights)] # works
G = nx.Graph()
G.add_weighted_edges_from(edge_list)
nx.write_graphml(G, 'test.graphml')
编辑:
显然,他们很清楚这个问题 (https://github.com/networkx/networkx/issues/1556),但尚未解决。