首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >进口混合(有向和无向类型在同一图形)网络-如何?

进口混合(有向和无向类型在同一图形)网络-如何?
EN

Stack Overflow用户
提问于 2015-12-16 11:38:57
回答 1查看 2K关注 0票数 1

我有一个图,其中一些边是有向的,有些是无向的,但我不能让Gephi在同一图中确认这两种边类型。

我目前正在使用R操作图形,然后使用'rgexf‘包将图形编码为一个可读的.gexf文件,如下所示:

代码语言:javascript
运行
复制
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包含一个字符串类型列(“有向”和“无定向”)。edgesAtt看起来像:

代码语言:javascript
运行
复制
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

但是,当我打开gexf并打开gexf文件时,gexf并不会将这个' type‘列作为边缘类型。相反,它只是像对待任何其他任意边缘属性一样对待“type”列,并添加了一个名为“type”的新列,其中填充了我在打开数据集时选择的默认边缘类型。当我导入数据时,选择“混合”并不会改变这一点。然而,盖菲成功地将“权重”列解读为边缘权重。

我如何使伤寒看到这两种边缘类型?

编辑将defaultedgetype属性更改为“混合”或“互”也不起作用,也不能将每个无向边缘变成两个面向相反方向的边。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-17 17:46:12

基于Yannis .关于.gexf文件如何以混合类型存储“type”的说明,我构建了一个快速的Python3脚本来解决这个问题。剧本在下面。它假设您有一个.gexf文件,并且您已经将“type”存储在一个名为“type”的edgesAtt列中,该列包含字符串“定向”和“无定向”。脚本读取这些内容,并将它们写到“边缘”中的正确位置,以便these能够识别它们。在Gephi中打开新脚本生成的.gexf文件时,只需在导入时将类型设置为“混合型”。

注意到:它是一个快速脚本,可以避免错误处理和对其操作on...use的任何测试,并且/或添加您自己的catches...but,如果您和我一样被困在这上面,那么这应该会让您开始工作。

代码语言:javascript
运行
复制
#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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34311067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档