Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions other/pythonExample/README.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions other/pythonExample/example.py
Original file line number Diff line number Diff line change
@@ -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)