-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCORR.py
More file actions
177 lines (138 loc) · 4.68 KB
/
CORR.py
File metadata and controls
177 lines (138 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def Correspondence(df,df2,hist_dist_Ga,hist_dist_Gb):
import networkx as nx
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import operator
import random as rd
import networkx as nx
from matplotlib import pyplot as plt
aa=hist_dist_Ga
bb=hist_dist_Gb
#ns=10
#nb=0.5
df_np=np.array(df)
#np.shape(df_np)[0]
adj = np.zeros([np.shape(df_np)[0],np.shape(df_np)[0]])
dist1=adj.copy()
for i in range((np.shape(df_np)[0])):
for j in range((np.shape(df_np)[0])):
dist=np.linalg.norm(df_np[i] - df_np[j])
dist1[i,j]=dist
if dist < aa:
adj[i,j]=1
import sys
np.set_printoptions(threshold=sys.maxsize)
adj
dist2=dist1.flatten()
plt.hist(dist2)
plt.show()
adj_df=pd.DataFrame(data=adj)
G = nx.from_pandas_adjacency(adj_df)
G.name = "Graph from pandas adjacency matrix"
print(nx.info(G))
nx.draw(G,with_labels=True,font_color='red',font_size=0,node_size=70,node_color='red')
#plot a graph
plt.show()
######
df_np_2=np.array(df2)
#np.shape(df_np_2)[0]
adj_2 = np.zeros([np.shape(df_np_2)[0],np.shape(df_np_2)[0]])
dist1_2=adj.copy()
for i in range((np.shape(df_np_2)[0])):
for j in range((np.shape(df_np_2)[0])):
dist_2=np.linalg.norm(df_np_2[i] - df_np_2[j])
dist1_2[i,j]=dist_2
if dist_2 < bb:
adj_2[i,j]=1
import sys
np.set_printoptions(threshold=sys.maxsize)
adj_2
dist2_2=dist1_2.flatten()
plt.hist(dist2_2)
plt.show()
import networkx as nx
from matplotlib import pyplot as plt
adj_df_2=pd.DataFrame(data=adj_2)
G_2 = nx.from_pandas_adjacency(adj_df_2)
G.name_2 = "Graph from pandas adjacency matrix"
print(nx.info(G_2))
nx.draw(G_2,with_labels=True,font_color='green',font_size=0,node_size=65,node_color='green')
#plot a graph
plt.show()
# -*- coding: utf-8 -*-
"""Page Rank Algorithm.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1oUC_418I6e2nv_2xBQ0sgXZtDfA98zuH
"""
graph=G
graph1=G_2
# created a directed graph
#graph=nx.gnp_random_graph(ns,nb,directed=False)
#draw a graph
#nx.draw(graph,with_labels=True,font_color='red',font_size=10,node_color='yellow')
#plot a graph
#plt.show()
#number of nodes for graph
count=graph.number_of_nodes()
# #Page Rank Algorithm-Calculating random walk score
rank_dict={}
#Page rank by networkx library
pagerank=nx.pagerank(graph)
#pagerank= nx.closeness_centrality(graph) #close_centrality
#sorting both dictionaries based on items
pagerank_sorted=sorted(pagerank.items(),key=lambda v:(v[1],v[0]),reverse=True)
#sorting the rank_dict based on values
rank_dict_sorted=sorted(rank_dict.items(),key=lambda v:(v[1],v[0]),reverse=True)
# created a directed graph
#graph1=nx.gnp_random_graph(ns,nb,directed=False)
#draw a graph
#nx.draw(graph1,with_labels=True,font_color='black',font_size=10,node_color='yellow')
#plot a graph
#plt.show()
#number of nodes for graph
count1=graph1.number_of_nodes()
# #Page Rank Algorithm-Calculating random walk score
rank_dict1={}
#Page rank by networkx library
pagerank1=nx.pagerank(graph1)
#pagerank1= nx.closeness_centrality(graph1) #close_centrality
#sorting both dictionaries based on items
pagerank_sorted1=sorted(pagerank1.items(),key=lambda v1:(v1[1],v1[0]),reverse=True)
#sorting the rank_dict based on values
rank_dict_sorted1=sorted(rank_dict1.items(),key=lambda v1:(v1[1],v1[0]),reverse=True)
# print("\n\nThe order for Graph is\n")
# for i in pagerank_sorted:
# print(i[0],end=" ")
# print("\n\nThe order Graph1 is\n")
# for j in pagerank_sorted:
# print(j[0],end=" ")
#def pgr_sort(self,rank_dict_sorted,rank_dict_sorted1):
pr=[]
for i in pagerank_sorted:
pr.append(i[0])
pr1=[]
for j in pagerank_sorted1:
pr1.append(j[0])
cm1=np.shape(pr)[0]
cm2=np.shape(pr1)[0]
cm = np.zeros([cm1,cm2])
#np.shape(cm)
cmm=min(cm1,cm2)
for i in range(0,cmm):
cm[pr[i],pr1[i]]=1
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.imshow(cm)
ax.set_aspect('equal')
cax = fig.add_axes([0.3, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()
return cm