(cls, g)
return graph
Converts the graph from networkx

        Vertex names will be converted to "_nx_name" attribute and the vertices
        will get new ids from 0 up (as standard in igraph).

        @param g: networkx Graph or DiGraph
Converts the graph from networkx
Converts the graph from networkx
def from_networkx(cls, g):
        """Converts the graph from networkx

        Vertex names will be converted to "_nx_name" attribute and the vertices
        will get new ids from 0 up (as standard in igraph).

        @param g: networkx Graph or DiGraph
        """
        # Graph attributes
        gattr = dict(g.graph)

        # Nodes
        vnames = list(g.nodes)
        vattr = {"_nx_name": vnames}
        vcount = len(vnames)
        vd = {v: i for i, v in enumerate(vnames)}

        # NOTE: we do not need a special class for multigraphs, it is taken
        # care for at the edge level rather than at the graph level.
        graph = cls(
            n=vcount, directed=g.is_directed(), graph_attrs=gattr, vertex_attrs=vattr
        )

        # Node attributes
        for v, datum in g.nodes.data():
            for key, val in list(datum.items()):
                graph.vs[vd[v]][key] = val

        # Edges and edge attributes
        eattr_names = {name for (_, _, data) in g.edges.data() for name in data}
        eattr = {name: [] for name in eattr_names}
        edges = []
        for (u, v, data) in g.edges.data():
            edges.append((vd[u], vd[v]))
            for name in eattr_names:
                eattr[name].append(data.get(name))

        graph.add_edges(edges, eattr)

        return graph
def from_networkx ( cls , g ) : # Graph attributes gattr = dict ( g . graph ) # Nodes vnames = list ( g . nodes ) vattr = { "_nx_name" : vnames } vcount = len ( vnames ) vd = { v : i for i , v in enumerate ( vnames ) } # NOTE: we do not need a special class for multigraphs, it is taken # care for at the edge level rather than at the graph level. graph = cls ( n = vcount , directed = g . is_directed ( ) , graph_attrs = gattr , vertex_attrs = vattr ) # Node attributes for v , datum in g . nodes . data ( ) : for key , val in list ( datum . items ( ) ) : graph . vs [ vd [ v ] ] [ key ] = val # Edges and edge attributes eattr_names = { name for ( _ , _ , data ) in g . edges . data ( ) for name in data } eattr = { name : [ ] for name in eattr_names } edges = [ ] for ( u , v , data ) in g . edges . data ( ) : edges . append ( ( vd [ u ] , vd [ v ] ) ) for name in eattr_names : eattr [ name ] . append ( data . get ( name ) ) graph . add_edges ( edges , eattr ) return graph