How to create a Spektral graph with edge weights and edge features - python

I want to create a Graph in Spektral which has both edged weights and edge labels. I have three matrices: a nxn adjacency matrix, an nx4 feature matrix for the nodes, and an nxnx2 matrix which represents the label for each edge. I do not know how to incorporate this third matrix into my Graph so that the edges will be labeled. I would also be happy to label the edges manually, but I don't know how to do that either. Thanks!

The spektral.data.Graph data structure has an attribute called e to store edge attributes.
So, if you have your data stored into numpy arrays you can do:
g = Graph(x=x, a=a, e=e)
where e is your edge attributes matrix.
They will be picked up automatically by the data loaders, if you use them.
Check out the documentation here.

Related

How to apply same transform on differrent set of unequal no. of vertices

I want to apply the transformation from one 3D mesh to another mesh using code (not in meshlab or blender). In my case, I have a 3D face model and a 3D facemask (with vertex and face information).
3D face model
3D face mask
My approach is below:
First, get ids of all the vertices from the 3D mask mesh (VA)
Get ids of all the vertices from the 3D face-model mesh (VB)
Find correspondence vertices to apply the transformation on (VB)
Find transformation required to get the same shape
Now, what would be the best way to find the correspondence vertices when the total no. of vertices are not equal in both the mesh.
What would be the right way to achieve this?
For each vertex v in VA, can't you simply find the closest vertex v' in VB, using minimum pairwise distance? Of course VA and VB need to be in the same coordinate system first.

How to create a graph with vertices weight in python with igraph?

I searched but found there are many examples about how to create a graph with edges weight, but none of them shows how to create a graph with vertices weight. I start to wonder if it is possible.
If a vertices-weighted graph can be created with igraph, then is it possible to calculate the weighted independence or other weighted numbers with igraph?
As far as I know, there are no functions in igraph that accept arguments for weighted vertices. However, the SANTA package that is a part of the Bioconductor suite for R does have routines for weighted vertices, if you are willing to move to R for this. (Seems like maybe you can run bioconductor in python.)
Another hacky option is the use (when possible) unweighted routines from igraph and then back in the weights. E.g. something like this for weighted maximal independent sets:
def maxset(graph,weight):
ms = g.maximal_independent_vertex_sets()
w = []
t = []
for i in range(0, 150):
m = weights.loc[weights['ids'].isin(ms[i]),"weights"]
w.append(m)
s = sum(w[i])
t.append(s)
return(ms[t.index(max(t))])
maxset(g,weights)
(Where weights is a two column data frame with column 1 = vertex ids and column 2 = weights). This gets the maximal independent set taking vertex weights into consideration.
You want to use vs class to define vertices and their attributes in igraph.
As example for setting weight on vertices, taken from documentation:
http://igraph.org/python/doc/igraph.VertexSeq-class.html
g=Graph.Full(3) # generate a full graph as example
for idx, v in enumerate(g.vs):
v["weight"] = idx*(idx+1) # set the 'weight' of vertex to integer, in function of a progressive index
>>> g.vs["weight"]
[0, 2, 6]
Note that a sequence of vertices are called through g.vs, here g the instance of your Graph object.
I suggested you this page, I found it practical to look for iGraph methods here:
http://igraph.org/python/doc/identifier-index.html

Add vertex attributes to a weighted igraph Graph in python

I am learning python-igraph, and having difficulty in handling a graph which is divided to components (which are unconnected between them). When I apply one of the clustering algorithms on this graph it doesn't seem to work properly, and so I need to apply the algorithms to each subgraph (component) separately. So in order to maintain the identification of the vertices, I would like to add a vertex attribute that give me the id number in the original graph. My graph is constructed from a weighted adjacency matrix:
import numpy as np
import igraph
def symmetrize(a):
return a + a.T - 2*np.diag(a.diagonal())
A = symmetrize(np.random.random((100,100)))
G = igraph.Graph.Adjacency(A.tolist(),attr="weight",mode="UPPER")
I see that there should be a way to add vertex attributes, but I don't understand how to use it..
Adding a vertex attribute to all of the vertices works like this:
G.vs["attr"] = ["id1", "id2", "id3", ...]
You can also attach a vertex attribute to a single vertex:
G.vs[2]["attr"] = "id3"
For instance, if you simply need a unique identifier to all your vertices, you can do this:
G.vs["original_id"] = list(range(G.vcount()))
(You don't need the list() part if you are on Python 2.x as range() already produces a list).

Build a graph as a subset of another, larger, graph [iGraph, Python]

I need to compute the density of a subgraph made of vertices belonging to the same attribute "group".
ie., let g be an iGraph graph,
g.vs.select(group = 1)
gives me an object with all vertices belonging to group 1
Is there any way to compute density on the graph which is formed by these vertices and the connections between them?
In a fashion similar to this maybe?
g2.vs(g2.vs.select(group = i)).density()
Try this:
g.vs.select(group=1).subgraph().density()

Separating two graphs based on connectivity and coordinates

I would like to separate existing data of vertices and edges into two or more graphs that are not connected. I would like to give the following as example:
Imagine two hexagons on top of each other but are lying in different Z.
Hexagon 1 has the following vertices A(0,0,1), B(1,0,2), C(2,1,2), D(1,2,1), E(0,2,1), F(-1,2,1). The connectivity is as following: A-B, B-C, C-D, D-E, E-F, F-A. This part of Graph 1 as all the vertices are connected in this layer.
Hexagon2 has the following vertices A1(0,0,6), B1(1,0,7), C1(2,1,7), D1(1,2,8), E1(0,2,7), F1(-1,2,6). The connectivity is as following: A1-B1, B1-C1, C1-D1, D1-E1, E1-F1, F1-A1. This is part of Graph 2
My data is in the following form: list of Vertices and list of Edges that i can form graphs with. I would like to eliminate graph 2 and give only vertices and connectivity of graph 1 to polygon determination part of my algorithm. My real data contains around 1000 connected polygons as graph 1 and around 100 (much larger in area) polygons as graph 2. I would like to eliminate graph 2.
The problem you're describing relates to connected components.
The Python Networkx module has functions for dealing with this type of graph problems. You're looking for the connected_components function which returns all of the components, you can then pick the appropriate one (possible by number of vertices).

Categories

Resources