我是否遗漏了什么或者这是 Gephi 脚本控制台中的一个奇怪的错误?
控制台显示边缘但没有节点
例如
>>> len(g.edges)
4314
>>> len(g.nodes)
1
>>> g.edges
set([e8926, e8794, e7024 ......])
>>> g.nodes
set([None])
您可以使用Gephi提供的数据集Power Grid.gml来复制错误。例如,我从这里对几个数据集进行了测试,得到了同样的错误。
难道我做错了什么 ?
有一个名为“Data Table”的插件,当你安装它时,你可以看到你的数据集的结构。我有一个完全一样的问题,我理解节点 id 是一个字符串而不是一个数字。如果您想查看脚本插件在控制台脚本插件中执行 g.nodes() 命令的区别,您可以看到(从“数据表”插件中)新创建的节点的 id 是数字而不是字符串。当你在 Gephi 控制台中执行 g.nodes 或 len(g.nodes) 时,你可以看到新创建的节点。我用这种方式解决它:我安装了一个名为“数据表”的插件,在“导出表”中,选择它,它告诉你需要导出哪些列,你选择任何你想要的但不是Id,然后选择一个分隔符然后按确定它会保存它。创建一个新项目,打开“数据表”插件,然后单击“
现在,在最初的问题两年后,错误仍然存在于 Gephi 的 Jython 控制台中的位置:
>>> 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 中编写的一个脚本,如果您在完成 user1290329 在这里所做的操作后无法将边缘恢复原位 [ https://stackoverflow.com/a/15827459/1645451]
这基本上会将您的新 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()
现在在 gephi 数据实验室中,导入电子表格,确保选择边缘选项,然后单击选择“edges_formatted_for_gephi.csv”,取消选中创建缺失节点,您的边缘应该回到您的 gephi 图中。:)