Commit 2cf2584a authored by Pokiros's avatar Pokiros
Browse files

Update ged4py :

 - Rename as gmatch4py since we have other graph similarity
 - Add deltaCon0 and deltacon algorithm
parent 87358dbc
No related merge requests found
Showing with 169 additions and 28 deletions
+169 -28
......@@ -2,18 +2,18 @@
import glob
from ged4py.geo_bp2 import GeoBP2
from gmatch4py.ged.geo_bp2 import GeoBP2
# Graph Edit Distance Algorithm Import
from ged4py.geo_ged import GeoGED
from ged4py.geo_hed import GeoHED
from ged4py.greedy_edit_distance import GreedyEditDistance
from ged4py.hausdorff_edit_distance import HED
from gmatch4py.ged.geo_ged import GeoGED
from gmatch4py.ged.geo_hed import GeoHED
from gmatch4py.ged.greedy_edit_distance import GreedyEditDistance
from gmatch4py.ged.hausdorff_edit_distance import HED
from progressbar import ProgressBar, Timer, Bar, ETA
# Disa
from disambiguator.geodict_gaurav import *
from ged4py.exception import NotFoundDistance
from ged4py.kernels.weisfeiler_lehman import *
from gmatch4py.exception import NotFoundDistance
from gmatch4py.kernels.weisfeiler_lehman import *
from pipeline import *
from pos_tagger.tagger import Tagger
......@@ -220,8 +220,8 @@ gra=graphs[447].edges(data=True)
selected_documents_=json.load(open("data/random_selected_doc.json"))
from ged4py.ged.algorithm import graph_edit_dist as ged
from ged4py.ged.bipartite_graph_matching_2 import BP_2
from gmatch4py.ged.algorithm import graph_edit_dist as ged
from gmatch4py.ged.bipartite_graph_matching_2 import BP_2
......
......@@ -2,17 +2,17 @@
import glob
from ged4py.geo_bp2 import GeoBP2
from gmatch4py.ged.geo_bp2 import GeoBP2
# Graph Edit Distance Algorithm Import
from ged4py.geo_ged import GeoGED
from ged4py.geo_hed import GeoHED
from ged4py.greedy_edit_distance import GreedyEditDistance
from ged4py.hausdorff_edit_distance import HED
from gmatch4py.ged.geo_ged import GeoGED
from gmatch4py.ged.geo_hed import GeoHED
from gmatch4py.ged.greedy_edit_distance import GreedyEditDistance
from gmatch4py.ged.hausdorff_edit_distance import HED
from progressbar import ProgressBar, Timer, Bar, ETA
# Disa
from disambiguator.geodict_gaurav import *
from ged4py.exception import NotFoundDistance
from gmatch4py.exception import NotFoundDistance
from pipeline import *
from pos_tagger.tagger import Tagger
......@@ -188,8 +188,8 @@ for file in glob.glob(args.graphs_dir.rstrip("/")+"/*.gexf"):
id=int(re.findall("\d+",file)[0])
graphs[id]=nx.read_gexf(file)
from ged4py.ged.algorithm import graph_edit_dist as ged
from ged4py.ged.bipartite_graph_matching_2 import BP_2
from gmatch4py.ged.algorithm import graph_edit_dist as ged
from gmatch4py.ged.bipartite_graph_matching_2 import BP_2
......
File moved
File moved
# coding = utf-8
import networkx as nx
import numpy as np
import scipy.sparse
class DeltaCon0():
@staticmethod
def compare(list_gs):
n=len(list_gs)
comparison_matrix = np.zeros((n,n))
for i in range(n):
for j in range(i,n):
g1,g2=list_gs[i],list_gs[j]
# S1
epsilon = 1/(1+DeltaCon0.maxDegree(g1))
D, A = DeltaCon0.degreeAndAdjacencyMatrix(g1)
S1 = np.linalg.inv(np.identity(len(g1))+(epsilon**2)*D -epsilon*A)
# S2
D, A = DeltaCon0.degreeAndAdjacencyMatrix(g2)
epsilon = 1 / (1 + DeltaCon0.maxDegree(g2))
S2 = np.linalg.inv(np.identity(len(g2))+(epsilon**2)*D -epsilon*A)
comparison_matrix[i,j] = 1/(1+DeltaCon0.rootED(S1,S2))
comparison_matrix[j,i] = comparison_matrix[i,j]
return comparison_matrix
@staticmethod
def rootED(S1,S2):
return np.sqrt(np.sum((S1-S2)**2)) # Long live numpy !
@staticmethod
def degreeAndAdjacencyMatrix(G):
"""
Return the Degree(D) and Adjacency Matrix(A) from a graph G.
Inspired of nx.laplacian_matrix(G,nodelist,weight) code proposed by networkx
:param G:
:return:
"""
A = nx.to_scipy_sparse_matrix(G, nodelist=G.nodes(), weight="weight",
format='csr')
n, m = A.shape
diags = A.sum(axis=1)
D = scipy.sparse.spdiags(diags.flatten(), [0], m, n, format='csr')
return D, A
@staticmethod
def maxDegree(G):
degree_sequence = sorted(nx.degree(G).values(), reverse=True) # degree sequence
# print "Degree sequence", degree_sequence
dmax = max(degree_sequence)
return dmax
class DeltaCon():
@staticmethod
def relabel_nodes(graph_list):
label_lookup = {}
label_counter = 0
n= len(graph_list)
# label_lookup is an associative array, which will contain the
# mapping from multiset labels (strings) to short labels
# (integers)
for i in range(n):
nodes = graph_list[i].nodes()
for j in range(len(nodes)):
if not (nodes[j] in label_lookup):
label_lookup[nodes[j]] = label_counter
label_counter += 1
graph_list[i] = nx.relabel_nodes(graph_list[i], label_lookup)
return graph_list
@staticmethod
def compare(list_gs, g=3):
n=len(list_gs)
list_gs=DeltaCon.relabel_nodes(list_gs)
comparison_matrix = np.zeros((n,n))
for i in range(n):
for j in range(i,n):
g1,g2=list_gs[i],list_gs[j]
V = g1.nodes()
V.extend(g2.nodes())
V=np.unique(V)
partitions=V.copy()
np.random.shuffle(partitions)
if len(partitions)< g:
partitions=np.array([partitions])
else:
partitions=np.array_split(partitions,g)
partitions_e_1 = DeltaCon.partitions2e(partitions, g1.nodes())
partitions_e_2 = DeltaCon.partitions2e(partitions, g2.nodes())
S1,S2=[],[]
for k in range(len(partitions)):
s0k1,s0k2=partitions_e_1[k],partitions_e_2[k]
# S1
epsilon = 1/(1+DeltaCon0.maxDegree(g1))
D, A = DeltaCon0.degreeAndAdjacencyMatrix(g1)
s1k = np.linalg.inv(np.identity(len(g1))+(epsilon**2)*D -epsilon*A)
s1k=np.linalg.solve(s1k,s0k1).tolist()
# S2
D, A = DeltaCon0.degreeAndAdjacencyMatrix(g2)
epsilon = 1 / (1 + DeltaCon0.maxDegree(g2))
s2k= np.linalg.inv(np.identity(len(g2))+(epsilon**2)*D -epsilon*A)
s2k = np.linalg.solve(s2k, s0k2).tolist()
S1.append(s1k)
S2.append(s2k)
comparison_matrix[i,j] = 1/(1+DeltaCon0.rootED(np.array(S1),np.array(S2)))
comparison_matrix[j,i] = comparison_matrix[i,j]
return comparison_matrix
@staticmethod
def partitions2e( partitions, V):
e = [ [] for i in range(len(partitions))]
for p in range(len(partitions)):
e[p] = []
for i in range(len(V)):
if i in partitions[p]:
e[p].append(1.0)
else:
e[p].append(0.0)
return e
\ No newline at end of file
File moved
File moved
File moved
import sys
from ged4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
from gmatch4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
class EdgeEditDistance(AbstractGraphEditDistance):
......
......@@ -3,10 +3,10 @@
import sys
import networkx as nx
from ged4py.algorithm.edge_edit_dist import EdgeEditDistance
from gmatch4py.algorithm.edge_edit_dist import EdgeEditDistance
from ged4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
from ged4py.ged.graph.edge_graph import EdgeGraph
from gmatch4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
from gmatch4py.ged.graph.edge_graph import EdgeGraph
def compare(g1, g2, print_details=False):
......
......@@ -3,8 +3,8 @@
# coding = utf-8
from ged4py.ged.bipartite_graph_matching_2 import BP_2
from ged4py.utils import *
from gmatch4py.ged.bipartite_graph_matching_2 import BP_2
from gmatch4py.utils import *
class GeoBP2(BP_2):
......
# coding = utf-8
from ged4py.ged.algorithm.graph_edit_dist import GraphEditDistance
from ged4py.utils import *
from gmatch4py.ged.algorithm.graph_edit_dist import GraphEditDistance
from gmatch4py.utils import *
_cache_g_info={}
class GeoGED(GraphEditDistance):
......
# coding = utf-8
from ged4py.ged.hausdorff_edit_distance import HED
from ged4py.utils import *
from gmatch4py.ged.hausdorff_edit_distance import HED
from gmatch4py.utils import *
class GeoHED(HED):
""""""
......
File moved
File moved
# coding = utf-8
import numpy as np
from ged4py.ged.algorithm.graph_edit_dist import GraphEditDistance
from gmatch4py.ged.algorithm.graph_edit_dist import GraphEditDistance
class GreedyEditDistance(GraphEditDistance):
......
File moved
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment