3

I have two graph objects made from scratch with Networkx (Python 3.8). To help understanding the situation this is a summary snippet:

G = nx.Graph()
Z = nx.Graph()

#some work here to get data for nodes and edges

G.add_nodes_from([(data["id"], {"origin": data["origin"], "text": data["text"]})])

#some other work in order to hash data for improved nodes privacy

G.add_edge(h.hexdigest(), data["x"])
Z.add_edge(h.hexdigest(), data["x"], weight=polarity) #here polarity is a simple double value

Now, the problem is that I want to export those objects in order to do some work with RStudio and the package igraph. Until now I've tried the following things (without any luck):

  1. Export to a straightforward adjacency list (but in RStudio I get an error, something related to the fact that it expects a square matrix, I don't remember well)
  2. Export to an edge list (but I get another type of error, it seems that i cannot use the weight representation)
  3. Use a naive approach, export in gml format, then install python-igraph, read the gml file with the function Read_Adjacency() and then re-export with the function Write_Adjacency() (hoping that with this intermediate step it somehow make the format understandable by igraph)
  4. I tried the solutions proposed here but also this time without success

How can I do ?

This is the code I use in RStudio:

ADJACENCY=read.csv("myadjlist.csv",header=FALSE,row.names=NULL,sep=";")
ADJACENCY=as.matrix(ADJACENCY)
#then we create the graph
G=graph_from_adjacency_matrix(ADJACENCY, mode="undirected")

And the error:

Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, : At structure_generators.c:274 : Non-square matrix, Non-square matrix

This is my adjacency list:

link

docdev
  • 943
  • 1
  • 7
  • 17
  • Please provide some additional detail. For instance, share the adjacency list you are using and post the specific error you received. If we cannot see the kind of objects involved and the errors, it's hard to give help. – nicola Nov 18 '20 at 09:42
  • It looks like as `ADJACENCY` is actually not an adjacency matrix. Please, share it with `dput` so we can check it. – nicola Nov 18 '20 at 10:14
  • Ok, now I edited – docdev Nov 18 '20 at 10:14
  • That file doesn't look like an adjacency list. Did you see it? How should it be interpreted? For instance a row is: `820976985387313;d54fd34cbddc270cafac961fa721b4406cc6de97;1c0e32c2bebad24ad7a06350a909788643ed13aa;ec13d5dd8f9e011e3114bd23404fe1a50da875aa`. What is supposed to mean? – nicola Nov 18 '20 at 10:33
  • Well, I just used Networkx in python with the function `nx.write_adjlist(G, "my csv")`. So I expect the output to be an adj list. My interpretation of that line is that node `820976985387313` is connected with `d54fd34cbddc270cafac961fa721b4406cc6de97` and so on until the newline character is reached – docdev Nov 18 '20 at 11:02

1 Answers1

2

igraph nowadays support direct conversion from and to networkx objects. See https://igraph.org/python/doc/api/igraph.Graph.html#from_networkx for more details. If you first convert to an igraph object (in Python), write it to a file, and then read it back in using igraph (in R), this should work correctly.

Big Brother
  • 107
  • 1
  • 9
Vincent Traag
  • 660
  • 4
  • 6