Skip to content

Cannot convert networkx graph to igraph using ig.Graph.from_networkx #566

@mcleantom

Description

@mcleantom

Describe the bug
Code which used ig.Graph.from_networkx worked in 0.9.11 fails in 0.10.0

To reproduce
Steps or minimal example code to reproduce the problem.

Run the following code:

import networkx as nx
import igraph as ig
import numpy as np

coords = np.array(
    [
        [-7.25, 49.42],
        [-7.99451822, 49.40710344],
        [-8.73857413, 49.38941589],
        [-9.48202378, 49.36694374],
        [-10.22472414, 49.33969506],
        [-10.96653322, 49.30767965],
        [-11.70731035, 49.27090898],
        [-12.44691629, 49.22939616],
        [-13.18521351, 49.18315596],
        [-13.92206632, 49.13220475],
        [-14.65734104, 49.07656048],
    ]
)
speeds = np.ones((len(coords),))
bearings = np.ones_like(speeds)

G = nx.DiGraph()
G.add_edges_from(
    (
        (
            (0, 0),
            (1, 1),
            {
                "weight": float("inf"),
                "coords": coords,
                "speed": speeds,
                "bearing": bearings,
                "delta_time": 1,
            },
        ),
        (
            (0, 0),
            (1, 0),
            {
                "weight": float("inf"),
                "coords": coords,
                "speed": speeds,
                "bearing": bearings,
                "delta_time": 1,
            },
        ),
        (
            (0, 0),
            (1, -1),
            {"weight": 1, "coords": coords, "speed": speeds, "bearing": bearings, "delta_time": 1},
        ),
        (
            (1, 1),
            (2, 0),
            {"weight": 1, "coords": coords, "speed": speeds, "bearing": bearings, "delta_time": 1},
        ),
        (
            (1, 0),
            (2, 0),
            {"weight": 1, "coords": coords, "speed": speeds, "bearing": bearings, "delta_time": 1},
        ),
        (
            (1, -1),
            (2, 0),
            {"weight": 1, "coords": coords, "speed": speeds, "bearing": bearings, "delta_time": 1},
        ),
        (
            (2, 0),
            (3, 0),
            {"weight": 3, "coords": coords, "speed": speeds, "bearing": bearings, "delta_time": 1},
        ),
    )
)
igG = ig.Graph.from_networkx(G)

And the following error occurs:

usr/local/lib/python3.10/dist-packages/BATWeatherRouting/Graph/graph_component.py:323: in __init__
    igG = ig.Graph.from_networkx(G)
usr/local/lib/python3.10/dist-packages/igraph/io/libraries.py:101: in _construct_graph_from_networkx
    if len(g) and "_igraph_index" in g.nodes[0]:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = NodeView(((0, 0), (1, 1), (1, 0), (1, -1), (2, 0), (3, 0))), n = 0

    def __getitem__(self, n):
        if isinstance(n, slice):
            raise nx.NetworkXError(
                f"{type(self).__name__} does not support slicing, "
                f"try list(G.nodes)[{n.start}:{n.stop}:{n.step}]"
            )
>       return self._nodes[n]
E       KeyError: 0

Version information

igraph==0.10.0
networkx==2.8.6
numpy==1.23.2

Metadata

Metadata

Assignees

Labels

todoTriaged for implementation in some unspecified future version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions