assigning vertex properties when adding edges

Hi,

I was wondering if there is any way to assign vertex properties while
adding edges to the graph. for example using "add_edge_list" I can assign
edge properties but later I have to iterate through all vertices again to
assign their properties.

I know this is not a problem when the vertex property is of the type "int"
or "float" because then one can use "vprop.a = values", but in case of
"string" and "object" this method doesn't work

What would be the best/fastest way to handle this situation.

I guess it would be very helpful to extend the "add_edge_list" function to
accept vertex property in some way.

cheers,

attachment.html (917 Bytes)

You can create new property maps with initialized values of any kind with

    p = g.new_vertex_property("string", vals=values)

Best,
Tiago

Simple example using add_edge_list()

edges = [["A", "B", 10],
         ["A", "C", 10],
         ["B", "C", 10],
         ["C", "D", 1],
         ["B", "F", 1],
         ["A", "E", 1],
         ["D", "E", 10],
         ["D", "F", 10],
         ["E", "F", 10]]

g = Graph()
eweight = g.new_edge_property("int")
eprops = [eweight]
g.add_edge_list(edges, eprops=eprops, hashed=True)

How do I recover the vertex names ("A", "B", "C", etc), so that after I fit
an SBM I can collect membership of vertices?

Now, if I had loaded the same data set from a .csv using
load_graph_from_csv() and run this:

g = load_graph_from_csv('edges.csv', hashed = True)

Then the following keep track of both edge weights and vertex names:

weights = g.edge_properties['c1']
vnames = g.vertex_properties

However, if I fit an SBM to g using edge weights using this method, I get an
error message:

AttributeError: 'str' object has no attribute 'key_type'

Simple example using add_edge_list()

edges = [["A", "B", 10],
         ["A", "C", 10],
         ["B", "C", 10],
         ["C", "D", 1],
         ["B", "F", 1],
         ["A", "E", 1],
         ["D", "E", 10],
         ["D", "F", 10],
         ["E", "F", 10]]

g = Graph()
eweight = g.new_edge_property("int")
eprops = [eweight]
g.add_edge_list(edges, eprops=eprops, hashed=True)

How do I recover the vertex names ("A", "B", "C", etc), so that after I fit
an SBM I can collect membership of vertices?

Read the manual! The docstring of g.add_edge_list() explains exactly
this, i.e. a property map is returned which contains the names.

Now, if I had loaded the same data set from a .csv using
load_graph_from_csv() and run this:

g = load_graph_from_csv('edges.csv', hashed = True)

Then the following keep track of both edge weights and vertex names:

weights = g.edge_properties['c1']
vnames = g.vertex_properties

The above is obviously not what you intended, as vnames points to the
whole property dictionary, not any particular property map.

However, if I fit an SBM to g using edge weights using this method, I get an
error message:

AttributeError: 'str' object has no attribute 'key_type'

Since you have not specified what you have actually done by showing us
the code, it's difficult to say what the problem is. Most likely, the
edge property map passed had the incorrect type.

If you want us to help you, please remember to always provide a minimal
and self-contained example that shows the problem. Giving error messages
without the context tells us almost nothing.

Best,
Tiago

Tiago Peixoto wrote

The docstring of g.add_edge_list() explains exactly
this, i.e. a property map is returned which contains the names.

So when you run g.add_edge_list() with the hashed = True option, it adds the
edges to the graph and returns a vertex property map.

For anyone else as clueless as me that might read this in the future, here's
the code:

As for the .csv method, here's what the .csv file looks like

and here's the code:

It seems to work!