multigraph.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:gitnet 作者: networks-lab 项目源码 文件源码
def write_graphml(self, fname):
        """
        Converts a `MultiGraphPlus` object to a graphml file.

        **Parameters** :

        > *fname* :

        >> A string indicating the path or file name to write to. File names which end in `.gz` or `.bz2` will be compressed.

        **Return** : `None`

        > This method will have the side effect of creating a file, specified by fpath.
        > This method cannot use vector attributes within the graphml file. Instead, vector attributes are converted into
        > a semicolon-delimited string. When this occurs, a warning is raised indicating the vector attributes (node
        > attributes are preceded by 'n:' while edge attributes are preceded by 'e:').

        """

        graph = copy.deepcopy(self)

        warning = False
        warning_set = set([])
        for n in graph.nodes():
            for attr in graph.node[n].keys():
                if isinstance(graph.node[n][attr], list):
                    warning = True
                    warning_set = {'n:' + attr} | warning_set
                    graph.node[n][attr] = list_to_scd(graph.node[n][attr])

        for n1, n2, data in graph.edges(data=True):
            for k in data:
                if isinstance(data[k], list):
                    warning = True
                    warning_set = {'e:'+k} | warning_set
                    data[k] = list_to_scd(data[k])

        if warning:
            warnings.warn("The provided graph contained the vector attributes: {}. All values of vector attributes have"
                          " been converted to semicolon-delimited strings. To prevent this, remove vector attributes or"
                          " convert them to atomic attributes prior to calling .write_graphml"
                          .format(warning_set))
        nx.write_graphml(graph, fname)
        print("Success. Wrote GraphML file {} to {}".format(fname, os.getcwd()))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号