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

R Gephi:如何导入混合(同一图形中的有向和无向类型)网络?,r,gephi,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_

我有一个图,其中一些边是有向的,一些是无向的,但我不能让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”的新列,其中填充了我在打开数据集时选择的默认边缘类型。导入数据时选择“混合”不会改变这一点。但是,Gephi确实成功地将“权重”列读取为边权重

如何使Gephi同时看到这两种边类型


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

根据Yannis p.关于.gexf文件如何在混合类型中存储“type”的说明,我构建了一个快速的Python 3脚本来解决这个问题。脚本如下。它假定您有一个.gexf文件,并且您已将“type”存储在名为“type”的edgesAtt列中,该列包含字符串“Directed”和“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()
#调用脚本如下:
#python this_script.py foo.gexf
#假设在foo.gexf中,类型('Directed'或'Undirected')作为字符串列存储在edgesAtt中
#脚本输出一个文件foo_mixed.gexf,该文件相同,只是将在边缘元素中指定混合定向和无定向边缘类型,以便Gephi可以读取它们
导入系统
导入argparse
def工作(args):
行数=0
notinnodes=False#节点att在gexf文件中位于第一位
#将整个.gexf输入文件作为列表读取到内存中
以open(args.fname,'r')作为填充:
gexf=infle.readlines()
infle.close()
#创建输出gexf文件
OutName=args.fname.split('.')[0]+''混合'+'.'.+args.fname.split('.')[1]
将open(outname,'w')作为outgexf:
对于gexf中的行:
#首先,忽略.gexf文件中位于边缘ATT之前的节点属性

如果“基于Yannis p.关于.gexf文件如何以混合类型存储“type”的注释,我构建了一个快速的Python 3脚本来解决这个问题。脚本如下。它假定您有一个.gexf文件,并且您已将“type”存储在名为“type”的edgesAtt列中,该列包含字符串“Directed”和“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()
#调用脚本如下:
#python this_script.py foo.gexf
#假设在foo.gexf中,类型('Directed'或'Undirected')作为字符串列存储在edgesAtt中
#脚本输出一个文件foo_mixed.gexf,该文件相同,只是将在边缘元素中指定混合定向和无定向边缘类型,以便Gephi可以读取它们
导入系统
导入argparse
def工作(args):
行数=0
notinnodes=False#节点att在gexf文件中位于第一位
#将整个.gexf输入文件作为列表读取到内存中
以open(args.fname,'r')作为填充:
gexf=infle.readlines()
infle.close()
#创建输出gexf文件
OutName=args.fname.split('.')[0]+''混合'+'.'.+args.fname.split('.')[1]
将open(outname,'w')作为outgexf:
对于gexf中的行:
#首先,忽略.gexf文件中位于边缘ATT之前的节点属性

如果'
write.gexf
有一个参数
defaultedgetype
。如果您尝试设置
defaultedgetype='mixed'
,会怎么样?我尝试了设置defaultedgetype='mixed'和defaultedgetype='mutual'。当我将结果文件导入Gephi时,我得到
错误:“无法识别默认边缘类型“mixed”。设置为默认“mixed”。
当我尝试defaultedgetype='mutual'时,我得到
错误:“无法识别默认边缘类型“double”。设置为默认“混合”。
在这两种情况下,Gephi都无法识别我的edgesAtt“Type”列,并创建一个新列。我还尝试将图中的无向边切换为双向边。然后,Gephi给出了一个错误,即“平行边还不受支持”。看看结果:。问题是,类型不应该是edge属性,而是XML元素
edge
的属性。我希望你能理解其中的区别。因此,应该有一个选项来定义数据框中的边的类型,在该数据框中定义边,然后Gephi将自动检测图形类型,或者在
rgexf
中有一个bug,这很有帮助。但是,当我尝试向
边缘
数据帧添加“Type”列时,我得到错误:
write.gexf(…:-edges-应该有两列,而不是3
。此外,我注意到,即使
权重
边缘
,它也显示为