In [122]:
import pygraphviz
import igraph
import numpy
import pandas
In [123]:
test_graph = pygraphviz.AGraph("test.dot")
nodes = test_graph.nodes()
edges = test_graph.edges()
test_igraph = igraph.Graph.TupleList(edges)
test_igraph.summary()
Out[123]:
In [124]:
igraph.drawing.plot(test_igraph)
Out[124]:
In [15]:
def markov_cluster(graph):
adj_mat = numpy.matrix(graph.get_adjacency().data)
N = adj_mat.shape[0]
Out[15]:
In [138]:
def markov_cluster(graph):
adj_mat = numpy.matrix(graph.get_adjacency().data)
N = adj_mat.shape[0]
vertex_degrees = numpy.array(graph.degree())
trans_mat_new = (adj_mat + numpy.diag([1]*N)) * numpy.linalg.inv(numpy.diag(vertex_degrees + 1))
trans_mat = numpy.zeros([N, N])
iter_count = 0
while ((numpy.linalg.norm(trans_mat_new - trans_mat, ord="fro")/
numpy.linalg.norm(0.5*(trans_mat_new + trans_mat), ord="fro")) > 0.01 and iter_count < 20):
trans_mat = trans_mat_new
## TODO: take the power of the matrix to two
## TODO: compute the element-wise square of the matrix
## compute column sums as "column_sums"
## repeat column_sums N times as a row vector, to make a matrix "trans_mat_div"
trans_mat_new = numpy.divide(trans_mat_new, trans_mat_div)
iter_count += 1
print("just completed iteration: " + str(iter_count))
trans_mat_new[trans_mat_new < 1e-10]=0
trans_mat_new[trans_mat_new > 1e-10]=1
trans_mat_df = pandas.DataFrame(trans_mat_new.transpose())
cluster_signatures = trans_mat_df.drop_duplicates()
cluster_signatures.index=range(cluster_signatures.shape[0])
return(pandas.merge(cluster_signatures.reset_index(),
trans_mat_df.reset_index(),
left_on=cluster_signatures.columns.tolist(),
right_on=trans_mat_df.columns.tolist())["index_x"].tolist())
In [139]:
markov_cluster(test_igraph)
Out[139]: