An error occurred while loading the file. Please try again.
-
Interdonato Roberto authoredef6ef559
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from geo2net import *
from matplotlib.pyplot import figure,imshow,plot,hist,show as pltshow, draw as pltdraw
from skimage.future.graph import show_rag
from skimage.exposure import rescale_intensity
sys.path.append('C:\\Program Files\\QGIS 2.18\\apps\\Python27\\Lib\\site-packages')
from osgeo import gdal
import numpy as np
import networkx as nx
import os
from utilities import multilayer_format_geo2net
import matplotlib.pyplot as plt
import glob
import network_analysis
from os.path import basename
fld = './testdata'
# cropmask
ds = gdal.Open(os.path.join(fld,'koumbia_cropland.tif'))
crop = ds.ReadAsArray()
ds = None
# seg singola immagine
segfile = os.path.join(fld,'koumbia_seg.tif')
ds = gdal.Open(segfile)
seg = ds.ReadAsArray()
ds = None
communities_file = ".\\output\\infomap\\mlnet_K20_cmeans.clu"
def readCommunities(clu_file):
coms = {}
f = open(clu_file, 'r')
for line in f:
if not line.startswith("#"):
vals = line.split(' ')
com = int(vals[1])
coms[int(vals[0])]=com
return coms
def readCommunities_list(clu_file):
coms = {}
f = open(clu_file, 'r')
for line in f:
if not line.startswith("#"):
vals = line.split(' ')
com = int(vals[1])
if com not in coms:
coms[com]=[]
coms[com].append(int(vals[0]))
return coms
def histo(coms,seg,crop):
#print(len(coms),seg.shape,crop.shape)
classes = {}
comset=set()
for i, j in np.ndindex(seg.shape): #for each pixel
if seg[i,j]!=2246:
curr_com = coms[seg[i,j]]
comset.add(curr_com)
if curr_com not in classes:
classes[curr_com]=np.zeros(2)
classes[curr_com][crop[i,j]-1]+=1
N = len(comset)
crop = []
nat = []
for c in classes:
crop.append(classes[c][0])
nat.append(classes[c][1])
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, crop, width,)
p2 = plt.bar(ind, nat, width,
bottom=crop)
#plt.ylabel('Scores')
#plt.title('Scores by group and gender')
#plt.xticks(ind, ('C1', 'C2', 'C3', 'C4', 'C5'))
plt.yticks(np.arange(0, 81, 10))
plt.legend((p1[0], p2[0]), ('Cropland', 'Natural Areas'))
plt.savefig("hist"+str(len(classes))+".png")
#plt.show()
def percs(coms, seg, crop):
classes = {}
comset = set()
for i, j in np.ndindex(seg.shape): # for each pixel
if seg[i, j] != 2246:
curr_com = coms[seg[i, j]]
comset.add(curr_com)
if curr_com not in classes:
classes[curr_com] = np.zeros(2)
classes[curr_com][crop[i, j] - 1] += 1
for c in classes:
tot = np.sum(classes[c])
classes[c]/=tot
#print(np.max(classes[c]),c)
return classes
# Implementazione multislice modularity definita in :
#8. Mucha, P. J., Richardson, T., Macon, K., Porter, M. A. & Onnela, J.-P.
# Community structure in time - dependent, multiscale, and multiplex networks.
# Science 328, 876-8(2010)
def multislice_modularity(layer_graphs,interlayer,communities):
gamma = 1.0 #teoricamente se ne puo definire uno per layer
w = 1.0 #interlayer weight: 1 per i coupling, 0 altrimenti [CHECK]
assignments = {}
c = 0
for D_key in communities:
D = communities[D_key]
for n in D:
assignments[n]=c
c+=1
mu = 0.0 #total edge weight
Q = 0.0
for l in layer_graphs:
for i in layer_graphs[l].nodes():
sum_i = 0.0
ni = layer_graphs[l].neighbors(i)
deg_i = 0
if ni is not None:
deg_i = len(list(ni))
for j in layer_graphs[l].neighbors(i):
if i in assignments and j in assignments:
if assignments[i]==assignments[j]: #teoricamente gli assegn. possono essere diversi per ogni layer
deg_j = len(list(layer_graphs[l].neighbors(j)))
P_ij = float(deg_i*deg_j)/float(2*len(layer_graphs[l].edges()))
#P_ij = float(deg_i-deg_j)/float(2*len(layer_graphs[l].edges()))
sum_i+= 1.0-gamma*P_ij
if i in interlayer:
if j in interlayer[i]:
sum_i += w # contribution interlayer edge
Q+=sum_i
mu+=deg_i+(len(layer_graphs.keys())-1)*w # grado su ogni layer + peso coupling edges (uno per layer)
Q=float(Q)/float(2*mu)
return Q
if __name__=='__main__':
coms = readCommunities(".\\output\\infomap\\mlnet_K10_cmeans_pc50.clu")
histo(coms,seg,crop)
exit()
pr = percs(coms, seg, crop)
maxes = []
for c in pr:
maxes.append(np.max(pr[c]))
print maxes
print len(maxes),np.max(maxes),np.min(maxes),np.mean(maxes),np.std(maxes)
coms = readCommunities_list(".\\output\\infomap\\mlnet_K10_cmeans_pc50.clu")
layer_graphs, node_layers, interlayer = network_analysis.readNet("D:\\Mes Donnees\\Amoris\\geo2net-master\\geo2net-master\\ml_nets\\mlnet_K10_cmeans_pc50.ncol")
m = multislice_modularity(layer_graphs, interlayer, coms)
print(m)
exit()
os.chdir("D:\\Mes Donnees\\Amoris\\geo2net-master\\geo2net-master\\ml_nets\\")
files = glob.glob("*.ncol")
for f in files:
name = basename(f).split('.')[0]
comfile = "D:\\Mes Donnees\\Amoris\\geo2net-master\\geo2net-master\\output\\infomap\\"+name+".clu"
coms = readCommunities_list(comfile)
layer_graphs, node_layers, interlayer = network_analysis.readNet(f)
m = multislice_modularity(layer_graphs,interlayer,coms)
print name,m
#files = glob.glob(".\\output\\infomap\\*.clu")
#for f in files:
# coms = readCommunities(f)
#coms = readCommunities(communities_file)
#histo(coms,seg,crop)
#percs(coms,seg,crop)
files = glob.glob(".\\output\\infomap\\*.clu")
for f in files:
coms = readCommunities(f)
histo(coms,seg,crop)
pr = percs(coms, seg, crop)
maxes = []
for c in pr:
maxes.append(np.max(pr[c]))
print f,maxes
#print f,len(maxes),np.max(maxes),np.min(maxes),np.mean(maxes),np.std(maxes)