I have a graph represented as a numpy boolean array (G.adj.dtype == bool). This is homework in writing my own graph library, so I can't use networkx. I want to dump it to a file so that I can fiddle with it, but for the life of me I can't work out how to make numpy dump it in a recoverable fashion.
I've tried G.adj.tofile, which wrote the graph correctly (ish) as one long line of True/False. But fromfile barfs on reading this, giving a 1x1 array, and loadtxt raises a ValueError: invalid literal for int. np.savetxt works but saves the matrix as a list of 0/1 floats, and loadtxt(..., dtype=bool) fails with the same ValueError.
Finally, I've tried networkx.from_numpy_matrix with networkx.write_dot, but that gave each edge [weight=True] in the dot source, which broke networkx.read_dot.
To save:
numpy.savetxt('arr.txt', G.adj, fmt='%s')
To recover:
G.adj = numpy.genfromtxt('arr.txt', dtype=bool)
HTH!
This is my test case:
m = numpy.random(100,100) > 0.5
numpy.savetxt('arr.txt', obj, fmt='%s') creates a 54 kB file.
numpy.savetxt('arr.txt', obj, fmt='%d') creates a much smaller file (20 kB).
cPickle.dump(obj, open('arr.dump', 'w')), which creates a 40kB file,
numpy.savetxt('arr.txt', obj, fmt='%s') 45 ms
numpy.savetxt('arr.txt', obj, fmt='%d') 10 ms
cPickle.dump(obj, open('arr.dump', 'w')), 2.3 ms
use savetxt with text format (%s) if human readability is needed, use savetxt with numeric format (%d) if space consideration are an issue and use cPickle if time is an issue.
The easiest way to save your array including metadata (dtype, dimensions) is to use numpy.save() and numpy.load():
a = array([[False, True, False],
[ True, False, True],
[False, True, False],
[ True, False, True],
[False, True, False]], dtype=bool)
numpy.save("data.npy", a)
numpy.load("data.npy")
# array([[False, True, False],
# [ True, False, True],
# [False, True, False],
# [ True, False, True],
# [False, True, False]], dtype=bool)
a.tofile() and numpy.fromfile() would work as well, but don't save any metadata. You need to pass dtype=bool to fromfile() and will get a one-dimensional array that must be reshape()d to its original shape.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With