2

I am trying to convert a Python networkx graph to a Gephi compatible file. However, I am running into some issues which I don't understand how to solve:

Questions:

  1. How can we incorporate node feature vectors into gexf files? I keep getting the error: "TypeError: attribute value type is not allowed: <class 'numpy.ndarray'>". If I remove the node features code below, then it works, but ideally I would like to include node features.

  2. When I load the .gexf file into Gephi, nothing is showing up for some reason - why might this be happening? I managed to get a gexf file when I removed the node feature part of the code below. (The app starts, I can visualize the other test data-sets, but when I load the file below, it shows the nodes and edges counter in the top right corner, but the actual graph doesn't show up... Do I need to press something else? I watched some of the YouTube tutorials and the graph always loads for those people)

I know there are lots of posts, but after looking through quite a few and trying the solutions I just decided to make a post.

Example code: I have made some mock-up code to showcase what I am doing:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline

# Make the networkx graph
G = nx.Graph()

# Add some cars (just do 4 for now)
G.add_nodes_from([
      (1, {'y': 0}),
      (2, {'y': 1}),
      (3, {'y': 2}),
      (4, {'y': 3}),
      (5, {'y': 4}),
])

# Add some edges --> A = [(0, 1, 0, 1, 1), (1, 0, 1, 1, 0), (0, 1, 0, 0, 1), (1, 1, 0, 0, 0), (1, 0, 1, 0, 0)]
G.add_edges_from([
                  (1, 2), (1, 4), (1, 5),
                  (2, 3), (2, 4),
                  (3, 2), (3, 5), 
                  (4, 1), (4, 2),
                  (5, 1), (5, 3)
])

# add the price data to the graph as node features
for node in G.nodes():
  G.nodes[node]['x'] = np.random.rand(5) * 5

# This code mounts the google drive
from google.colab import drive
drive.mount('/content/drive')

# convert the graph to Gephi
nx.write_gexf(G, '/content/drive/MyDrive/cars_test.gexf', version="1.2draft")

Then I get the error: "TypeError: attribute value type is not allowed: <class 'numpy.ndarray'>"

Any help would be appreciated.

Rocky the Owl
  • 325
  • 1
  • 2
  • 11
  • Did you ever find a solution to this? I'm having the same issue. Frustratingly, it initially managed to open the graph generated this way. But now it does not. I had installed some plugins, so I wondered if that was the issue, but uninstalling them does not resolve it. – Dr Xorile Jun 07 '22 at 17:39
  • I eventually went Window - Reset Windows and that fixed it – Dr Xorile Jun 07 '22 at 17:43
  • 1
    @DrXorile - sorry for late reply. I think what I ended up doing was creating a csv file for the node and edge lists. Then I input those into Gephi (which can be done through the table). – Rocky the Owl Jun 09 '22 at 12:38
  • Thanks. It's probably worth giving the networkX export another try. It turns out that the Window - Reset Windows fixed it and it's working beautifully now. – Dr Xorile Jun 09 '22 at 17:24

1 Answers1

0

It appears there is no numpy support at the moment, so one way around this is to store data as lists:

# add the price data to the graph as node features
for node in G.nodes():
    G.nodes[node]["x"] = {"price": list(np.random.rand(5) * 5)}

Note that the node attribute is stored as a dictionary.

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • 1
    Thanks for the help! So this does work in terms of allowing me to convert the networkx graph to gexf. However, do you know what might be causing the issue that nothing shows up when I load the graph into Gephi? That is, I open Gephi, click Open --> select the .gexf file from my downloads --> the top right of the app shows the number of nodes and edges that we defined in the example above, but then there is no visualization showing up... – Rocky the Owl Dec 29 '21 at 23:16