On 11/03/2013 04:07 AM, Alan Williams wrote:
Hi there, sorry for the newb-ish question. I have read the docs for an hour :)
I have a graph as a "dot" text file. Nodes are numbered consecutively from 1 to N. example below. I have used the /minimise_blockmodel_dl /method, which returns a PropertyMap b. My question is how to save a map from the dotfile's Node ID to community number as a txt file. I have saved the propertymap array b.get_array() using numpy.savetxt and it appears to be a one-dimensional array.
b, mdl = minimize_blockmodel_dl(g,max_B=100,min_B=200,verbose=True)
(Side note: This is not right, you must have max_B >= min_B. I assume it is a typo.)
array = b.get_array() np.savetxt('1m_output_blocks_array.txt',array,'%i')
I kind of assumed that the PropertyMap would be returned in order so I could just lay a 1-N column beside b.get_array() and that would be what I want. However, I'm not getting results that make sense to me and I wanted to find out if this assumption is correct or not.
I'd also like to know the proper way of doing this for the general case where nodes could be arbitrarily labelled in the original dotfile.
In the dot format the labels are arbitrary strings, so the ordering assumed in graph-tool is (as you guessed in your second message) lexicographic, not according to the line order in the file or the numeric value of the labels. The names of the nodes are stored as strings in a internal property map called "vertex_name": label = g.vp["vertex_name"] for v in g.vertices(): print(label[v]) This should print something like: 0 1 10 100 ... The property map arrays are in same order, so using "vertex_name" you should be able to store the mapping to a file. Note that you could also store the block assignments to an internal property map and save that to a file together with the whole network: g.vp["b"] = b g.save("foo.dot") Let me advise you, however, that the dot format is not the best one to keep stored metadata, since it has no type information on the stored properties. So they are always read as strings, and you need to convert them later: g = load_graph("foo.dot") b = g.vp["b"] # this is always a string property map b = b.copy(value_type="int") # converts to int As I mention in the documentation, a much better choice is to use the graphml format, which stores the graph and all its internal property maps perfectly: g.save("foo.xml") g = load_graph("foo.xml") b = g.vp["b"] # already has int type I recommend using the dot or gml formats _only_ if you need to pass to or read from another program/library which uses this format, otherwise you should stick to graphml. Cheers, Tiago -- Tiago de Paula Peixoto <tiago@skewed.de>