I have trouble reading and writing my bipartite graphs as sparse matrix in python. I want to read a mat file back in python but I have trouble going back to a graph, because the mat file gives a numpy.ndarray type file and I need a sparse matrix to reconstruct my graph. Any idea how to make my code work? I think it has to do with loadmat 'object' specification but I don't understand how to go further...
import setuptools
import math
from networkx.utils import powerlaw_sequence
from networkx import *
import networkx as nx
from scipy import sparse, io #
G= nx.Graph()
stockGraph = []
G =bipartite.configuration_model([1, 1], [1, 1], create_using=None, seed=None)
stockGraph.append(G) # stock new graph
G =bipartite.configuration_model([0, 1], [1, 0], create_using=None, seed=None)
stockGraph.append(G) # stock new graph
graphToMatlab=[] #then export all my graphs as biadjacency matrix
n=2
for i in range(n):
myGraphToMatlab=bipartite.biadjacency_matrix(stockGraph[i], row_order=bipartite.sets(stockGraph[i])[0], column_order=bipartite.sets(stockGraph[i])[1], weight='weight', format='csr')
graphToMatlab.append(myGraphToMatlab) # stacks adjacency matrix.
io.savemat('manygraphs.mat', dict(graphToMatlab=graphToMatlab)) # save all matrices
mat_contents=io.loadmat('manygraphs.mat', mdict=None, appendmat=True) # then open biadjacency matrices : where the problem lies !!
mat1 = mat_contents['graphToMatlab']
m1=mat1[0,0] # remove the object part WITH THIS LINE, IT WORKS !!
bipartite.from_biadjacency_matrix(mat1[1], create_using=None, edge_attribute='weight')# recreate the first graph
I get this:
Traceback (most recent call last):
in from_biadjacency_matrix
n, m = A.shape
AttributeError: 'numpy.ndarray' object has no attribute 'format'