diff --git a/other/pythonExample/README.md b/other/pythonExample/README.md new file mode 100755 index 00000000..e19e05a2 --- /dev/null +++ b/other/pythonExample/README.md @@ -0,0 +1,26 @@ +# Working from Python Example + + +### :warning: ALPHA: Package under construction (pre-release) :warning: +Until I add tests, I make no guarantees as to the suitability of this code. + +While working from R meets the workflow of many people if you have a large +amount of existing python code, or simply feel more productive working from +python, then using these network comparison tools can be difficult. + +In the example file in this directory, we have included a simple way of calling the NetEMD code from python. + +It should be noted that it is not fast (as data has to be passed between the +runtimes), nor is it memory efficient as there are duplicate versions of data. +However, for networks with a relatively small of nodes, it is a simple and +relatively pain-free way of experimenting with this code. + +To run this code the following python packages are required: + +* networkx - A python network library +* numpy - Python matrix and array library +* rpy2 - A library which allows R functions to be called from inside of python. + +Note: If you are using python 2.7, then you cannot install the current version +of rpy2 as it is python 3 only, however the final version that is python 2.7 +compatible will be fine. diff --git a/other/pythonExample/example.py b/other/pythonExample/example.py new file mode 100644 index 00000000..cf4db649 --- /dev/null +++ b/other/pythonExample/example.py @@ -0,0 +1,33 @@ +import numpy as np +import networkx as nx +import rpy2.robjects.numpy2ri +rpy2.robjects.numpy2ri.activate() +from rpy2 import robjects +from rpy2.robjects.packages import importr +import rpy2.robjects.numpy2ri +orca=importr("orca") +igraph = importr('igraph') +netdist =importr('netdist') +graphConstructor=rpy2.robjects.r['graph.adjacency'] + +def makeRgdd(G): + B=nx.to_numpy_matrix(G) + B=np.array(B) + nr,nc = B.shape + Br = robjects.r.matrix(B, nrow=nr, ncol=nc) + Gr=graphConstructor(Br,mode ="undirected") + Ggdd=netdist.gdd(Gr) + return Ggdd + +G1=nx.Graph() +G1.add_edges_from([[0,1],[1,2],[2,3]]) +G1gdd=makeRgdd(G1) + + +G2=nx.Graph() +G2.add_edges_from([[0,1],[1,2],[2,3],[3,4]]) +G2gdd=makeRgdd(G2) + +print netdist.net_emd(G1gdd,G1gdd) +print netdist.net_emd(G2gdd,G2gdd) +print netdist.net_emd(G1gdd,G2gdd)