Commit 87358dbc authored by Pokiros's avatar Pokiros
Browse files

Update ged4py :

 - create subpackage for GED algorithms
 - create subpackage for graph kernels
 - ADD VEO and Vertex Ranking algorithm
parent 4eaadc56
No related merge requests found
Showing with 124 additions and 70 deletions
+124 -70
# coding = utf-8
\ No newline at end of file
File moved
from ged4py.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
import sys
from ged4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
class EdgeEditDistance(AbstractGraphEditDistance):
"""
......
# -*- coding: UTF-8 -*-
from ged4py.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
from ged4py.algorithm.edge_edit_dist import EdgeEditDistance
from ged4py.graph.edge_graph import EdgeGraph
import networkx as nx
import sys
import networkx as nx
from ged4py.algorithm.edge_edit_dist import EdgeEditDistance
from ged4py.ged.algorithm.abstract_graph_edit_dist import AbstractGraphEditDistance
from ged4py.ged.graph.edge_graph import EdgeGraph
def compare(g1, g2, print_details=False):
ged = GraphEditDistance(g1, g2,print_details)
......
......@@ -3,7 +3,7 @@
# coding = utf-8
from ged4py.bipartite_graph_matching_2 import BP_2
from ged4py.ged.bipartite_graph_matching_2 import BP_2
from ged4py.utils import *
......
# coding = utf-8
from ged4py.algorithm.graph_edit_dist import GraphEditDistance
from helpers.gazeteer_helpers import get_data
from shapely.geometry import Point
import numpy as np
from ged4py.ged.algorithm.graph_edit_dist import GraphEditDistance
from ged4py.utils import *
_cache_g_info={}
......
# coding = utf-8
from ged4py.hausdorff_edit_distance import HED
from ged4py.ged.hausdorff_edit_distance import HED
from ged4py.utils import *
class GeoHED(HED):
......
File moved
File moved
# coding = utf-8
from ged4py.algorithm.graph_edit_dist import GraphEditDistance
import numpy as np
from ged4py.ged.algorithm.graph_edit_dist import GraphEditDistance
class GreedyEditDistance(GraphEditDistance):
"""
Implementation of the Greedy Edit Distance presented in :
......
# coding = utf-8
"""Shortest-Path graph kernel.
"""
Shortest-Path graph kernel.
Python implementation based on: "Shortest-path kernels on graphs", by
Borgwardt, K.M.; Kriegel, H.-P., in Data Mining, Fifth IEEE
International Conference on , vol., no., pp.8 pp.-, 27-30 Nov. 2005
......@@ -17,7 +18,8 @@ class ShortestPathGraphKernel:
"""
Shorthest path graph kernel.
"""
def compare(self, g_1, g_2, verbose=False):
@staticmethod
def compare( g_1, g_2, verbose=False):
"""Compute the kernel value (similarity) between two graphs.
Parameters
----------
......@@ -53,24 +55,9 @@ class ShortestPathGraphKernel:
return np.sum(v1 * v2)
def compare_normalized(self, g_1, g_2, verbose=False):
"""Compute the normalized kernel value between two graphs.
A normalized version of the kernel is given by the equation:
k_norm(g1, g2) = k(g1, g2) / sqrt(k(g1,g1) * k(g2,g2))
Parameters
----------
g1 : networkx.Graph
First graph.
g2 : networkx.Graph
Second graph.
Returns
-------
k : The similarity value between g1 and g2.
"""
return self.compare(g_1, g_2) / (np.sqrt(self.compare(g_1, g_1) *
self.compare(g_2, g_2)))
def compare_list(self, graph_list, verbose=False):
@staticmethod
def compare_list(graph_list, verbose=False):
"""Compute the all-pairs kernel values for a list of graphs.
This function can be used to directly compute the kernel
matrix for a list of graphs. The direct computation of the
......@@ -89,7 +76,7 @@ class ShortestPathGraphKernel:
k = np.zeros((n, n))
for i in range(n):
for j in range(i, n):
k[i, j] = self.compare(graph_list[i], graph_list[j])
k[i, j] = ShortestPathGraphKernel.compare(graph_list[i], graph_list[j])
k[j, i] = k[i, j]
k_norm = np.zeros(k.shape)
......
......@@ -19,7 +19,7 @@ import copy
class WeisfeleirLehmanKernel(object):
@staticmethod
def compare(self,graph_list,h=2):
def compare(graph_list,h=2):
"""Compute the all-pairs kernel values for a list of graphs.
This function can be used to directly compute the kernel
matrix for a list of graphs. The direct computation of the
......@@ -40,7 +40,7 @@ class WeisfeleirLehmanKernel(object):
K: numpy.array, shape = (len(graph_list), len(graph_list))
The similarity matrix of all graphs in graph_list.
"""
self.graphs = graph_list
n = len(graph_list)
k = [0] * (h + 1)
n_nodes = 0
......@@ -57,7 +57,7 @@ class WeisfeleirLehmanKernel(object):
if (n_max < graph_list[i].number_of_nodes()):
n_max = graph_list[i].number_of_nodes()
phi = np.zeros((n_max, n), dtype=np.uint64)
phi = np.zeros((n_nodes, n), dtype=np.uint64)
# INITIALIZATION: initialize the nodes labels for each graph
# with their labels or with degrees (for unlabeled graphs)
......@@ -69,7 +69,6 @@ class WeisfeleirLehmanKernel(object):
# 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()
# It is assumed that the graph has an attribute
......@@ -85,6 +84,7 @@ class WeisfeleirLehmanKernel(object):
labels[i][j] = label_lookup[nodes[j]]
# labels are associated to a natural number
# starting with 0.
phi[labels[i][j], i] += 1
graph_list[i]=nx.relabel_nodes(graph_list[i],label_lookup)
......
# coding = utf-8
import numpy as np
import networkx as nx
def intersect(a, b):
return list(set(a) & set(b))
class VertexEdgeOverlap():
""""""
@staticmethod
def compare(listgs):
n = len(listgs)
comparison_matrix = np.zeros((n, n))
for i in range(n):
for j in range(i,n):
g1 = listgs[i]
g2 = listgs[j]
inter_ver,inter_ed = VertexEdgeOverlap.intersect_graph(g1,g2)
comparison_matrix[i,j]=2*(len(inter_ver)+len(inter_ed))/(len(g1)+len(g2)+len(g1.edges(data=True))+len(g2.edges(data=True))) # Data = True --> For nx.MultiDiGraph
comparison_matrix[j,i]=comparison_matrix[i,j]
return comparison_matrix
@staticmethod
def intersect_edges(g1,g2):
ed1 = VertexEdgeOverlap.transform_edges(g1.edges(data=True))
ed2 = VertexEdgeOverlap.transform_edges(g2.edges(data=True))
inter_ed=[]
for e1 in ed1:
for e2 in ed2:
if e1 == e2:
inter_ed.append(e1)
return inter_ed
@staticmethod
def intersect_nodes(g1,g2):
return intersect(g1.nodes(),g2.nodes())
@staticmethod
def intersect_graph(g1,g2):
return VertexEdgeOverlap.intersect_nodes(g1,g2),VertexEdgeOverlap.intersect_edges(g1,g2)
@staticmethod
def transform_edges(ed):
for e in range(len(ed)):
if "id" in ed[e][-1]:
del ed[e][-1]["id"]
return ed
# coding = utf-8
import numpy as np
import networkx as nx
from scipy.stats import pearsonr,spearmanr
def intersect(a, b):
return list(set(a) & set(b))
class VertexRanking():
@staticmethod
def compare(listgs):
n = len(listgs)
comparison_matrix = np.zeros((n,n))
page_r=[nx.pagerank(nx.DiGraph(g)) for g in listgs]
for i in range(n):
for j in range(i,n):
node_intersection=intersect(list(page_r[i].keys()),list(page_r[j].keys()))
X,Y=[],[]
for node in node_intersection:
X.append(page_r[i][node])
Y.append(page_r[j][node])
comparison_matrix[i,j] = spearmanr(X,Y)[0]
comparison_matrix[j,i] = comparison_matrix[i,j]
return comparison_matrix
# coding: utf-8
import glob
import json
import numpy as np
from progressbar import ProgressBar, Timer, Bar, ETA
# Disa
from disambiguator.geodict_gaurav import *
from ged4py.exception import NotFoundDistance
from ged4py.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 progressbar import ProgressBar, Timer, Bar, ETA
# Disa
from disambiguator.geodict_gaurav import *
from ged4py.exception import NotFoundDistance
from ged4py.kernels.weisfeiler_lehman import *
from pipeline import *
from pos_tagger.tagger import Tagger
......@@ -60,9 +58,8 @@ def compareBP2(id_,graphs):
return sc
def compareSubTreeKernel(id_,graphs):
global grap_kernel_results, graph_lookup
h=WeisfeleirLehmanKernel()
j=0
sc = np.zeros(len(graphs))
if len(grap_kernel_results)<1:
......@@ -70,7 +67,7 @@ def compareSubTreeKernel(id_,graphs):
for i,g in graphs.items():
graphs_array[i]=g
grap_kernel_results=h.compare(graphs_array,h=3)
grap_kernel_results=WeisfeleirLehmanKernel.compare(graphs_array,h=3)
for i in range(abs(id_-len(grap_kernel_results))):
sc[id_ + i] = 1 - grap_kernel_results[id_ + i,id_]
......@@ -223,8 +220,8 @@ gra=graphs[447].edges(data=True)
selected_documents_=json.load(open("data/random_selected_doc.json"))
from ged4py.algorithm import graph_edit_dist as ged
from ged4py.bipartite_graph_matching_2 import BP_2
from ged4py.ged.algorithm import graph_edit_dist as ged
from ged4py.ged.bipartite_graph_matching_2 import BP_2
......
# coding: utf-8
from ner.gate_annie import GateAnnie
from ner.nltk import NLTK
from pipeline import *
from pos_tagger.tagger import Tagger
# Disa
from disambiguator.pagerank import *
from disambiguator.geodict_gaurav import *
import glob
from ged4py.geo_bp2 import GeoBP2
# Graph Edit Distance Algorithm Import
from ged4py.algorithm import graph_edit_dist as ged
from ged4py.geo_ged import GeoGED
from ged4py.geo_hed import GeoHED
from ged4py.hausdorff_edit_distance import HED
from ged4py.bipartite_graph_matching_2 import BP_2
from ged4py.greedy_edit_distance import GreedyEditDistance
from ged4py.geo_bp2 import GeoBP2
from ged4py.hausdorff_edit_distance import HED
from progressbar import ProgressBar, Timer, Bar, ETA
# Disa
from disambiguator.geodict_gaurav import *
from ged4py.exception import NotFoundDistance
from pipeline import *
from pos_tagger.tagger import Tagger
import numpy as np
import glob, json, argparse
from progressbar import ProgressBar,Timer,Bar,ETA
# Similarity Function between graph and a set of graphs
......@@ -198,10 +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.algorithm import graph_edit_dist as ged
from ged4py.bipartite_graph_matching_2 import BP_2
from ged4py.ged.algorithm import graph_edit_dist as ged
from ged4py.ged.bipartite_graph_matching_2 import BP_2
......
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