igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] python load attributes


From: Tuan Q. Phan
Subject: Re: [igraph] python load attributes
Date: Wed, 1 Jun 2011 17:50:42 -0400

Thanks Tamas, yeah, I was afraid of that.  I'm dealing with millions of vertices.  I'll play around with it.

Tuan

On Wed, Jun 1, 2011 at 5:42 PM, Tamás Nepusz <address@hidden> wrote:
Hi Tuan,

That's a tricky business; as you see, neither GraphML nor GML support lists as attribute values. I think you have two options here:

1. If you are using your graphs only from Python, you can save them in Python's pickle format. This is a Python-specific serialization format, and it is not supported by R or any other language; on the other hand, it allows any Python object as an attribute value, including lists, dictionaries, NumPy arrays, anything that itself is serializable by the pickle module of Python.

2. If your lists contain only simple values like strings or numbers, I'd store them as strings and use a post-processing function that converts them into lists after having loaded them from GraphML or GML. In Python, the "eval" function would probably do to convert simple lists of numbers or strings from a string representation to a list itself. So, for instance, assume that you have a list of numbers like [2, 3, 5, 7, 11]. You store it in GraphML as a string ("[2, 3, 5, 7, 11]") and then call eval("[2,3,5,7,11]") to get your list back after you have loaded the GraphML object. If you want to read the same graph from R, you would also get the same string there as the value of the vertex attribute, but you can turn that into an R list by replacing [...] with (...), adding a "c" in front of it and then asking R to evaluate "c(2,3,5,7,11)" as follows:

eval(parse(text="c(2,3,5,7,11)"))

-- 
T.

On Wednesday, 1 June 2011 at 22:34, Tuan Q. Phan wrote:

Thanks Tamas for the comments.  What's the best format for ragged array of vertex attributes?  That is, I've been creating lists of attributes for each vertex, but gml and graphml writes it as a string.  I'd like to eventually easily import it into R and/or python for quick access.  There are a large number of sparse attributes, so the traditional way where each attribute has a name will require too much memory.

Thanks,
Tuan




On Sun, May 22, 2011 at 3:30 PM, Tamás Nepusz <address@hidden> wrote:
Dear Tuan,

Neither the NCOL nor the edgelist format support attributes; your best bet is probably to use a different graph format if you want to store and load vertex/edge attributes together with the graph. The GML format is fairly easy to create and many other software packages support it. GraphML is another alternative but it's not very human-readable.

Alternatively, it takes only a few lines of code to facilitate loading vertex attributes from a CSV file. E.g.:

import csv
from itertools import izip

reader = csv.reader(open("attributes.csv"))
headers = reader.next()
for row in reader:
    vertex_id, attrs = row[0], row[1:]
    for attr_name, attr_value in izip(headers, attrs):
        graph.vs[vertex_id][attr_name] = attr_value

The above code assumes that you have a CSV file where the first column is the vertex ID, and the first row contains the names of the attributes you wish to add.

Even if you decide to do this, I'd strongly suggest to save your graph afterwards in GraphML or GML format to keep the attributes and the graph itself in the same file.

-- 
T.

On Saturday, 21 May 2011 at 02:52, Tuan Q. Phan wrote:

Is there a python function to load vertex attributes?  I'm loading an edgelist/ncol, and would be nice to have a built-in attribute loading rather than doing it in python.

Thanks,
Tuan
_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help


_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help




--
Tuan Q. PHAN
Doctor of Business Administration (Harvard)
cell: (213) 804-9432
skype: tuanqphan
address@hidden
http://www.tuanqphan.com


_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help


_______________________________________________
igraph-help mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/igraph-help




--
Tuan Q. PHAN
Doctor of Business Administration (Harvard)
cell: (213) 804-9432
skype: tuanqphan
address@hidden
http://www.tuanqphan.com



reply via email to

[Prev in Thread] Current Thread [Next in Thread]