我是否遗漏了什么,还是Gephi脚本控制台中存在奇怪的错误?
控制台显示边缘但没有节点。
例如:
>>> len(g.edges)
4314
>>> len(g.nodes)
1
>>> g.edges
set([e8926, e8794, e7024 ......])
>>> g.nodes
set([None])
您可以使用Gephi提供的数据集Power Grid.gml来复制错误。 我在这里举个例子测试了几个数据集,都出现了相同的错误。
我做错了什么吗?
我是否遗漏了什么,还是Gephi脚本控制台中存在奇怪的错误?
控制台显示边缘但没有节点。
例如:
>>> len(g.edges)
4314
>>> len(g.nodes)
1
>>> g.edges
set([e8926, e8794, e7024 ......])
>>> g.nodes
set([None])
有一个名为“Data Table”的插件,安装后可以查看数据集的结构。 我曾经遇到过类似的问题,我了解到节点ID是字符串而不是数字。如果您想在脚本插件中执行g.nodes()命令并查看插件之间的差异,可以从“Data Table”插件中看到新创建节点的ID是数字而不是字符串。当您在Gephi控制台中执行g.nodes或len(g.nodes)时,您可以看到新创建的节点。 我通过以下方式解决了这个问题: 我安装了一个名为“Data Table”的插件,在其中可以选择“导出表格”,它会告诉您需要导出哪些列,您可以选择任何您想要的列但不包括ID,然后选择一个分隔符并按下“确定”,它将保存。创建一个新项目,打开“Data Table”插件,然后单击“导入电子表格”,从这里您可以插入带有名为“Id”的新列的数据集,Gephi会自动将其添加到您的数据集中。
现在距离最初的问题已经过去两年了,在Gephi的Jython控制台中仍存在一个bug,其中:
>>> g.nodes
set([None])
>>> graph = g.getUnderlyingGraph()
>>> nodes = [node for node in graph.nodes]
>>> nodes
[n0, n1, n2, n3, n4, n5, n6, n7, ...
>>> node = nodes[0]
>>> attr = node.attributes
>>> value = attr.getValue('attribute_name')
>>> new_value = do_something(value)
>>> attr.setValue('attribute_name', new_value)
这是我用Python编写的脚本,如果您在完成用户1290329在此处所做的操作后无法将边缘放回原位,则可以使用它[https://dev59.com/k3DXa4cB1Zd3GeqP_mLO#15827459]
基本上,它将把您的新创建的Gephi整数ID列映射到一个边缘表中。
import pandas as pd
# Once you have re-imported your CSV, and your ID is an Int,
# but your edge table is still messed up
nodes = pd.read_csv('nodes_table.csv')
edges = pd.read_csv('edges_table.csv')
# delete any unnecessary cols
del edges['Label']
# Create a dictionary with your Node name as the key,
# and its Gephi int Id as the value
# To do this Set index to be col you want the dict keys to be
# and the dict values will be col you specifiy in the brackets after 'ie ['Id']
node_dict = nodes.set_index('Label')['Id'].to_dict()
# Then use the map function, col you are mapping with should have they keys
# And will fill with value of key when matched
# In this case we just over-write the Source and Target cols
edges['Source'] = edges['Source'].map(node_dict)
edges['Target'] = edges['Target'].map(node_dict)
edges.to_csv('edges_formatted_for_gephi.csv', index=False)
edges.head()