Gephi:导入混合(同一图中的有向和无向类型)网络 - 如何?

时间:2015-12-16 11:38:57

标签: r gephi

我有一个图表,其中一些边是指向的,一些是无向的,但我不能让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包含一个Type字符串列('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”列作为边缘类型读取。相反,它只是像任何其他任意边缘属性一样处理“类型”列,并添加一个名为“类型”的新列,填充我在打开数据集时选择的默认边缘类型。导入数据时选择“混合”不会改变这一点。然而,Gephi成功地将“权重”列读作边缘权重。

如何让Gephi看到两种边缘类型?

编辑将defaultedgetype属性更改为'mixed'或'mutual'也不起作用,也不会将每个无向边成为面向相反方向的两个方向边。

1 个答案:

答案 0 :(得分:0)

根据Yannis P。关于.gexf文件如何存储'类型'的说明。在混合类型中,我构建了一个快速的Python 3脚本来解决这个问题。脚本如下。它假设您有一个.gexf文件,并且您已经存储了'键入'在edgeAtt列中命名为' type'包含字符串'定向'并且' Undirected'。脚本会读取这些内容并将它们写入“边缘”中的正确位置。这样Gephi就会认出它们。当您在Gephi中打开新脚本生成的.gexf文件时,只需将类型设置为' mixed'导入时。

注意:它是一个快速的脚本,可以避免错误处理以及对其操作的任何测试......使用风险和/或添加自己的捕获量......但如果你像我一样坚持这个,那么这应该让你开始。

#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()