Commit 02c1bea1 authored by Fize Jacques's avatar Fize Jacques

Graph Class stable ! problem with MultiDiGraph and no attr selected fixed

parent 91253007
......@@ -5,6 +5,8 @@ cdef class Base:
cdef int type_alg
cdef bint normalized
cdef int cpu_count
cdef str node_attr_key
cdef str edge_attr_key
## Methods
cpdef np.ndarray compare(self,list graph_list, list selected)
cpdef np.ndarray compare_old(self,list listgs, list selected)
......@@ -13,6 +15,8 @@ cdef class Base:
cpdef bint isAccepted(self,G,index,selected)
cpdef np.ndarray get_selected_array(self,selected,size_corpus)
cpdef set_attr_graph_used(self, str node_attr_key, str edge_attr_key)
cpdef intersection(G,H)
cpdef union_(G,H)
......@@ -119,7 +119,7 @@ cdef class Base:
self.type_alg=0
self.normalized=False
def __init__(self,type_alg,normalized):
def __init__(self,type_alg,normalized,node_attr_key="",edge_attr_key=""):
"""
Constructor of Base
......@@ -141,7 +141,13 @@ cdef class Base:
self.type_alg=type_alg
self.normalized=normalized
self.cpu_count=multiprocessing.cpu_count()
self.node_attr_key=node_attr_key
self.edge_attr_key=edge_attr_key
cpdef set_attr_graph_used(self, str node_attr_key, str edge_attr_key):
self.node_attr_key=node_attr_key
self.edge_attr_key=edge_attr_key
cpdef np.ndarray get_selected_array(self,selected,size_corpus):
cdef double[:] selected_test = np.zeros(size_corpus)
if not selected == None:
......
......@@ -113,7 +113,7 @@ cdef class AbstractGraphEditDistance(Base):
cpdef np.ndarray compare(self,list listgs, list selected):
cdef int n = len(listgs)
cdef double[:,:] comparison_matrix = np.zeros((n, n))
listgs=parsenx2graph(listgs)
listgs=parsenx2graph(listgs,self.node_attr_key,self.edge_attr_key)
cdef long[:] n_nodes = np.array([g.size() for g in listgs])
cdef double[:] selected_test = np.array(self.get_selected_array(selected,n))
cdef int i,j
......
......@@ -47,7 +47,7 @@ cdef class HED(Base):
cpdef np.ndarray compare(self,list listgs, list selected):
cdef int n = len(listgs)
cdef list new_gs=parsenx2graph(listgs)
cdef list new_gs=parsenx2graph(listgs,self.node_attr_key,self.edge_attr_key)
cdef double[:,:] comparison_matrix = np.zeros((n, n))
cdef double[:] selected_test = np.array(self.get_selected_array(selected,n))
cdef int i,j
......
......@@ -16,24 +16,23 @@ cdef class Graph:
self.is_multi = G.is_multigraph()
self.is_node_attr=(True if node_attr_key else False)
self.is_edge_attr=(True if edge_attr_key else False)
if len(G) ==0:
self.nodes_list,self.nodes_attr_list,self.nodes_hash,self.nodes_weight,self.attr_nodes=[],[],[],[],[]
self.nodes_degree,self.nodes_degree_in,self.nodes_degree_out,self.nodes_degree_weighted,self.nodes_degree_in_weighted,self.nodes_degree_out_weighted=np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long)
self.nodes_idx,self.degree_per_attr,self.degree_per_attr_weighted={},{},{}
self.nodes_hash_set=set([])
self.number_of_nodes = 0
self.number_of_edges = 0
self.edges_list=[]
self.edges_attr_list =[]
self.edges_hash_idx = {}
self.edges_hash = []
self.edges_hash_set= set([])
self.edges_weight={}
self.edges_hash_map={}
self.attr_edges=[]
if self.is_multi and not self.is_edge_attr:
if not len(nx.get_edge_attributes(G,"id")) == len(G.edges(data=True)):
i=0
for id1 in G.adj:
for id2 in G.adj[id1]:
for id3 in G.adj[id1][id2]:
G._adj[id1][id2][id3]["id"]=i
i+=1
self.is_edge_attr = True
edge_attr_key = "id"
# for ed in
#len(nx.get_edge_attributes(G1,"id")) == len(G1.edges(data=True))
if len(G) ==0:
self.__init_empty__()
else:
a,b=list(zip(*list(G.nodes(data=True))))
......@@ -162,13 +161,19 @@ cdef class Graph:
return "{0}".format(n1)
cpdef str hash_edge(self,str n1,str n2):
return "_".join(sorted([n1,n2]))
if not self.is_directed:
return "_".join(sorted([n1,n2]))
return "_".join([n1,n2])
cpdef str hash_node_attr(self,str n1, str attr_value):
return "_".join(sorted([n1,attr_value]))
cpdef str hash_edge_attr(self,str n1,str n2, str attr_value):
return "_".join([n1,n2,attr_value])
if not self.is_directed:
return "_".join([n1,n2,attr_value])
ed=sorted([n1,n2])
ed.extend(attr_value)
return "_".join(ed)
## EXIST FUNCTION
cpdef bint has_node(self,str n_id):
......@@ -364,4 +369,21 @@ cdef class Graph:
return Graph(G,self.node_attr_key,self.edge_attr_key)
def __init_empty__(self):
self.nodes_list,self.nodes_attr_list,self.nodes_hash,self.nodes_weight,self.attr_nodes=[],[],[],[],[]
self.nodes_degree,self.nodes_degree_in,self.nodes_degree_out,self.nodes_degree_weighted,self.nodes_degree_in_weighted,self.nodes_degree_out_weighted=np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long),np.array([],dtype=np.long)
self.nodes_idx,self.degree_per_attr,self.degree_per_attr_weighted={},{},{}
self.nodes_hash_set=set([])
self.number_of_nodes = 0
self.number_of_edges = 0
self.edges_list=[]
self.edges_attr_list =[]
self.edges_hash_idx = {}
self.edges_hash = []
self.edges_hash_set= set([])
self.edges_weight={}
self.edges_hash_map={}
self.attr_edges=[]
\ No newline at end of file
from ..graph cimport Graph
import networkx as nx
def parsenx2graph(list_gs):
def parsenx2graph(list_gs,node_attr_key="",edge_attr_key=""):
new_gs=[nx.relabel_nodes(g,{node:str(node) for node in list(g.nodes)},copy=True) for g in list_gs]
new_gs=[Graph(g) for g in new_gs]
new_gs=[Graph(g,node_attr_key,edge_attr_key) for g in new_gs]
return new_gs
......@@ -41,7 +41,7 @@ cdef class Jaccard(Base):
cpdef np.ndarray compare(self,list listgs, list selected):
cdef int n = len(listgs)
cdef list new_gs=parsenx2graph(listgs)
cdef list new_gs=parsenx2graph(listgs,self.node_attr_key,self.edge_attr_key)
cdef double[:,:] comparison_matrix = np.zeros((n, n))
cdef long[:] n_nodes = np.array([g.size() for g in new_gs])
cdef long[:] n_edges = np.array([g.density() for g in new_gs])
......@@ -71,6 +71,6 @@ cdef class Jaccard(Base):
else:
comparison_matrix[i][j] = 0.
comparison_matrix[j][i] = comparison_matrix[i][j]
comparison_matrix[j][i] = comparison_matrix[i][j]
return np.array(comparison_matrix)
......@@ -32,7 +32,7 @@ cdef class MCS(Base):
cdef int n = len(listgs)
cdef double [:,:] comparison_matrix = np.zeros((n, n))
cdef double[:] selected_test = np.array(self.get_selected_array(selected,n))
cdef list new_gs=parsenx2graph(listgs)
cdef list new_gs=parsenx2graph(listgs,self.node_attr_key,self.edge_attr_key)
cdef long[:] n_nodes = np.array([g.size() for g in new_gs])
cdef double [:,:] intersect_len_nodes = np.zeros((n, n))
cdef int i,j
......@@ -47,7 +47,11 @@ cdef class MCS(Base):
comparison_matrix[i][j] = intersect_len_nodes[i][j]/max(n_nodes[i],n_nodes[j])
else:
comparison_matrix[i][j] = 0.
if i==j:
comparison_matrix[i][j]=1
comparison_matrix[j][i] = comparison_matrix[i][j]
return np.array(comparison_matrix)
def s_mcs(self,G, H):
......
......@@ -41,7 +41,7 @@ cdef class VertexEdgeOverlap(Base):
cpdef np.ndarray compare(self,list listgs, list selected):
cdef int n = len(listgs)
cdef list new_gs=parsenx2graph(listgs)
cdef list new_gs=parsenx2graph(listgs,self.node_attr_key,self.edge_attr_key)
cdef double[:,:] comparison_matrix = np.zeros((n, n))
cdef int denom,i,j
cdef long[:] n_nodes = np.array([g.size() for g in new_gs])
......@@ -62,12 +62,12 @@ cdef class VertexEdgeOverlap(Base):
if n_nodes[i] > 0 and n_nodes[j] > 0 and selected_test[i] == 1:
denom=n_nodes[i]+n_nodes[j]+\
n_edges[i]+n_edges[j]
if denom == 0:
continue
comparison_matrix[i][j]=(2*(intersect_len_nodes[i][j]
if denom > 0:
comparison_matrix[i][j]=(2*(intersect_len_nodes[i][j]
+intersect_len_edges[i][j]))/denom # Data = True --> For nx.MultiDiGraph
comparison_matrix[i][j] = comparison_matrix[i][j]
if i==j:
comparison_matrix[i][j]=1
comparison_matrix[j][i] = comparison_matrix[i][j]
return np.array(comparison_matrix)
......
Markdown is supported
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