Gephi:导入混合(同一图中的有向和无向类型)网络——如何?
Gephi: Import Mixed (Directed and Undirected Type in same graph) Network -- How?
我有一个图,其中一些边是有向的,一些边是无向的,但我无法让 Gephi 在同一个图中识别两种边类型。
我目前正在使用 R 来操作图形,然后使用 'rgexf' 包将图形编码为 Gephi 可读的 .gexf 文件,如下所示:
write.gexf(nodes = nodes_df, edges = edges_df, edgesWeight = E(gD)$weight,
nodesAtt = nodes_att, edgesAtt = edges_att, nodesVizAtt = nodes_att_viz,
edgesVizAtt = edges_att_viz,output = "plag_c_d3.gexf")
此处,edgesAtt 包含一个字符串类型列('Directed' 和 'Undirected')。 edgesAtt 看起来像:
Type weight sourcedate targetdate repost_date_diff
Directed 100 1361424992 1361426157 0.0134838
Undirected 100 1362140722 1362140722 0.0000000
Directed 54 1365403984 1365465600 0.7131481
但是,当我打开 Gephi 并打开 gexf 文件时,Gephi 不会将此 'Type' 列读取为边缘类型。相反,它只是像对待任何其他任意边缘属性一样对待 'Type' 列,并且它添加了一个名为 'Type' 的新列,其中填充了我在打开数据集时选择的默认边缘类型。当我导入数据时选择 'mixed' 不会改变这一点。但是,Gephi 确实成功读取了 'weight' 列作为边权重。
如何让 Gephi 看到两种边类型?
Edit 将 defaultedgetype 属性更改为 'mixed' 或 'mutual' 也不起作用,也无法将每个无向边变成两个面向的有向边相反的方式。
根据 Yannis P. 关于 .gexf 文件如何以混合类型存储 'type' 的注释,我构建了一个快速解决问题的 Python 3 脚本。脚本如下。它假定您有一个 .gexf 文件,并且您已将 'type' 存储在名为 'type' 的 edgesAtt 列中,该列包含字符串 'Directed' 和 'Undirected'。该脚本读取这些并将它们写入 'edges' 中的正确位置,以便 Gephi 能够识别它们。当你在Gephi中打开新的脚本生成的.gexf文件时,只需在导入时将类型设置为'mixed'即可。
注意:这是一个快速脚本,它避免了错误处理和对其运行的任何测试......使用风险自负 and/or 添加你自己的捕获...但如果你像我一样坚持这个,那么这应该让你开始。
#Call script like:
# python this_script.py foo.gexf
#Assumes that in foo.gexf, Type ('Directed' or 'Undirected') is stored as a string column in edgesAtt
#Script outputs a file foo_mixed.gexf that is identical except that mixed Directed and Undirected edge types will be specified in edge elements so that Gephi can read them
import sys
import argparse
def work (args):
linecount = 0
notinnodes = False # nodes att comes first in gexf file
# Read the whole .gexf input file into memory as a list
with open (args.fname, 'r') as infile:
gexf = infile.readlines()
infile.close()
# Create output gexf file
outfname = args.fname.split('.')[0]+'_mixed'+'.'+args.fname.split('.')[1]
with open (outfname, 'w') as outgexf:
for line in gexf:
# First, ignore the node attributes that come before edge atts in .gexf files
if '<attributes class=\"edge\"' in line:
notinnodes = True
# Get the edge attribute number that contains 'Type' or 'type'
if notinnodes and 'title=\"type\"' in line.lower():
Type_attnumber = int(line.split('id=\"att')[1].split('\"')[0])
break
# Edit every line that contains an edge element and add the 'type' to it from the attributes listed below it
for line in gexf:
if not '<edge id=' in line:
outgexf.writelines(line)
else:
edgeLine = line.split('\"')
Type = gexf[linecount + 1 + Type_attnumber].split('value=')[1].split('/')[0]
outgexf.writelines( '\"'.join(edgeLine[0:6])+'\" '+'type='+Type +'\"'.join(edgeLine[6:]) )
linecount = linecount+1
def main():
# Parser to grab name of file we're currently working on
parser = argparse.ArgumentParser()
parser.add_argument("fname")
args = parser.parse_args()
work(args)
if __name__ == "__main__":
main()
我有一个图,其中一些边是有向的,一些边是无向的,但我无法让 Gephi 在同一个图中识别两种边类型。
我目前正在使用 R 来操作图形,然后使用 'rgexf' 包将图形编码为 Gephi 可读的 .gexf 文件,如下所示:
write.gexf(nodes = nodes_df, edges = edges_df, edgesWeight = E(gD)$weight,
nodesAtt = nodes_att, edgesAtt = edges_att, nodesVizAtt = nodes_att_viz,
edgesVizAtt = edges_att_viz,output = "plag_c_d3.gexf")
此处,edgesAtt 包含一个字符串类型列('Directed' 和 'Undirected')。 edgesAtt 看起来像:
Type weight sourcedate targetdate repost_date_diff
Directed 100 1361424992 1361426157 0.0134838
Undirected 100 1362140722 1362140722 0.0000000
Directed 54 1365403984 1365465600 0.7131481
但是,当我打开 Gephi 并打开 gexf 文件时,Gephi 不会将此 'Type' 列读取为边缘类型。相反,它只是像对待任何其他任意边缘属性一样对待 'Type' 列,并且它添加了一个名为 'Type' 的新列,其中填充了我在打开数据集时选择的默认边缘类型。当我导入数据时选择 'mixed' 不会改变这一点。但是,Gephi 确实成功读取了 'weight' 列作为边权重。
如何让 Gephi 看到两种边类型?
Edit 将 defaultedgetype 属性更改为 'mixed' 或 'mutual' 也不起作用,也无法将每个无向边变成两个面向的有向边相反的方式。
根据 Yannis P. 关于 .gexf 文件如何以混合类型存储 'type' 的注释,我构建了一个快速解决问题的 Python 3 脚本。脚本如下。它假定您有一个 .gexf 文件,并且您已将 'type' 存储在名为 'type' 的 edgesAtt 列中,该列包含字符串 'Directed' 和 'Undirected'。该脚本读取这些并将它们写入 'edges' 中的正确位置,以便 Gephi 能够识别它们。当你在Gephi中打开新的脚本生成的.gexf文件时,只需在导入时将类型设置为'mixed'即可。
注意:这是一个快速脚本,它避免了错误处理和对其运行的任何测试......使用风险自负 and/or 添加你自己的捕获...但如果你像我一样坚持这个,那么这应该让你开始。
#Call script like:
# python this_script.py foo.gexf
#Assumes that in foo.gexf, Type ('Directed' or 'Undirected') is stored as a string column in edgesAtt
#Script outputs a file foo_mixed.gexf that is identical except that mixed Directed and Undirected edge types will be specified in edge elements so that Gephi can read them
import sys
import argparse
def work (args):
linecount = 0
notinnodes = False # nodes att comes first in gexf file
# Read the whole .gexf input file into memory as a list
with open (args.fname, 'r') as infile:
gexf = infile.readlines()
infile.close()
# Create output gexf file
outfname = args.fname.split('.')[0]+'_mixed'+'.'+args.fname.split('.')[1]
with open (outfname, 'w') as outgexf:
for line in gexf:
# First, ignore the node attributes that come before edge atts in .gexf files
if '<attributes class=\"edge\"' in line:
notinnodes = True
# Get the edge attribute number that contains 'Type' or 'type'
if notinnodes and 'title=\"type\"' in line.lower():
Type_attnumber = int(line.split('id=\"att')[1].split('\"')[0])
break
# Edit every line that contains an edge element and add the 'type' to it from the attributes listed below it
for line in gexf:
if not '<edge id=' in line:
outgexf.writelines(line)
else:
edgeLine = line.split('\"')
Type = gexf[linecount + 1 + Type_attnumber].split('value=')[1].split('/')[0]
outgexf.writelines( '\"'.join(edgeLine[0:6])+'\" '+'type='+Type +'\"'.join(edgeLine[6:]) )
linecount = linecount+1
def main():
# Parser to grab name of file we're currently working on
parser = argparse.ArgumentParser()
parser.add_argument("fname")
args = parser.parse_args()
work(args)
if __name__ == "__main__":
main()