-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKLDclasses.py
159 lines (135 loc) · 7.12 KB
/
KLDclasses.py
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
from Ok_Uk_Module import *
from scipy.special import kl_div
from scipy.interpolate import griddata
class SpacialSensitivityAnalysisOK(OrdinaryKrigning):
def __init__(self, Points, Zvals, Variogram='gaussian',DiverganceModel='KLD', radius=10):
super().__init__(Points, Zvals, Variogram)
self.DivModel=DiverganceModel
self.radius=radius
if self.DivModel == 'KLD':
def DivModel(p, q):
m = 0.5 * (p + q)
kld=np.mean(0.5 * np.sum(kl_div(p, m)) + 0.5 * np.sum(kl_div(q, m)))
return kld
self.DivModel = np.vectorize(DivModel,otypes=[np.float64])
def remove_neighbors(self, index, radius):
#_______________To do_______________________
#catch if r is too big and deletes all points and if deletes too many return warning
point = self.points[index]
distances = np.linalg.norm(self.points - point, axis=1)
neighbors_indices = np.where(distances <= radius)[0]
return np.delete(self.points, neighbors_indices, axis=0), np.delete(self.zvals, neighbors_indices)
def DiverganceLOO(self,step=10,manualbounds=None):
#set up the initial kriging model
params=[self.C, self.a, self.nugget, self.anisotropy_factor]
self.divscores=[]
if manualbounds==None:
bounds=[np.max(self.points[:,0]),np.min(self.points[:,0]),np.max(self.points[:,1]),np.min(self.points[:,1])]
else:
bounds=manualbounds
#LOO cross validation loop
for i in range(len(self.points)):
print('Iterations: ',i+1,'/',len(self.points)+1)
new_points, new_zvals = self.remove_neighbors(i, self.radius)
model = OrdinaryKrigning(new_points, new_zvals,Variogram=self.variogram)
estimate = model.AutoKrige(step=step,bounds=bounds)
self.divscores.append((np.mean(self.DivModel(np.abs(self.zarray/np.sum(self.zarray)),np.abs(estimate/np.sum(estimate))))))
np.save('div_scores.npy', self.divscores)
return self.divscores
def plot_div(self,resolution=1000,saveplot=False,powerscale=1):
plt.style.use('ggplot')
#check if divscores file exists if so load it if not save it
try:
self.divscores=np.load('div_scores.npy')
except:
ValueError('div_scores.npy does not exist')
#interpolate the divergance scores in 3d space with x and y as the input and divscores as the output
x=np.linspace(np.min(self.points[:,0]),np.max(self.points[:,0]),resolution)
y=np.linspace(np.min(self.points[:,1]),np.max(self.points[:,1]),resolution)
X,Y=np.meshgrid(x,y)
Z=griddata(self.points,self.divscores,(X,Y),method='linear')
#plot the interpolated divergance scores
im = plt.imshow(Z**powerscale, cmap='YlOrRd', interpolation='bilinear', origin='lower',aspect='auto', extent=[np.min(self.points[:,0]),np.max(self.points[:,0]),np.min(self.points[:,1]),np.max(self.points[:,1])])
plt.scatter(self.points[:,0],self.points[:,1],c='k',s=15)
#plot configuration
plt.title('LOO Divergance Values')
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar(im)
plt.grid(color='black', linestyle='-', linewidth=0.5,alpha=.25)
#set x and y lims to 0
plt.xlim(0)
plt.ylim(0)
plt.show()
plt.close()
if saveplot==True:
plt.savefig('div_scores.png')
plt.show()
plt.close()
return Z
class SpacialSensitivityAnalysisUK(UniversalKriging):
def __init__(self, Points, Zvals, Variogram='gaussian',DiverganceModel='KLD',trendfunc='linear',radius=10):
super().__init__(Points, Zvals, Variogram,trendfunc)
self.DivModel=DiverganceModel
self.radius=radius
if self.DivModel == 'KLD':
def DivModel(p, q):
m = 0.5 * (p + q)
kld=np.mean(0.5 * np.sum(kl_div(p, m)) + 0.5 * np.sum(kl_div(q, m)))
return kld
self.DivModel = np.vectorize(DivModel,otypes=[np.float64])
def remove_neighbors(self, index, radius):
#_______________To do_______________________
#catch if r is too big and deletes all points and if deletes too many return warning
point = self.points[index]
distances = np.linalg.norm(self.points - point, axis=1)
neighbors_indices = np.where(distances <= radius)[0]
return np.delete(self.points, neighbors_indices, axis=0), np.delete(self.zvals, neighbors_indices)
def DiverganceLOO(self,step=10,manualbounds=None):
#set up the initial kriging model
params=[self.C, self.a, self.nugget, self.anisotropy_factor]
self.divscores=[]
if manualbounds==None:
bounds=[np.min(self.points[:,0]),np.max(self.points[:,0]),np.min(self.points[:,1]),np.max(self.points[:,1])]
else:
bounds=manualbounds
#LOO cross validation loop
for i in range(len(self.points)):
print('Iterations: ',i+1,'/',len(self.points)+1)
new_points, new_zvals = self.remove_neighbors(i, self.radius)
model = UniversalKriging(new_points, new_zvals,Variogram=self.variogram)
estimate = model.AutoKrige(step=step,bounds=bounds)
self.divscores.append((np.mean(self.DivModel(np.abs(self.zarray/np.sum(self.zarray)),np.abs(estimate/np.sum(estimate))))))
np.save('div_scores.npy', self.divscores)
return self.divscores
def plot_div(self,resolution=1000,saveplot=False,powerscale=1):
plt.style.use('ggplot')
#check if divscores file exists if so load it if not save it
try:
self.divscores=np.load('div_scores.npy')
except:
ValueError('div_scores.npy does not exist')
#interpolate the divergance scores in 3d space with x and y as the input and divscores as the output
x=np.linspace(np.min(self.points[:,0]),np.max(self.points[:,0]),resolution)
y=np.linspace(np.min(self.points[:,1]),np.max(self.points[:,1]),resolution)
X,Y=np.meshgrid(x,y)
Z=griddata(self.points,self.divscores,(X,Y),method='linear')
#plot the interpolated divergance scores
im = plt.imshow(Z**powerscale, cmap='YlOrRd', interpolation='bilinear', origin='lower',aspect='auto', extent=[np.min(self.points[:,0]),np.max(self.points[:,0]),np.min(self.points[:,1]),np.max(self.points[:,1])])
plt.scatter(self.points[:,0],self.points[:,1],c='k',s=15)
#plot configuration
plt.title('LOO Divergance Values')
plt.xlabel('X')
plt.ylabel('Y')
plt.colorbar(im)
plt.grid(color='black', linestyle='-', linewidth=0.5,alpha=.25)
#set x and y lims to 0
plt.xlim(0)
plt.ylim(0)
plt.show()
plt.close()
if saveplot==True:
plt.savefig('div_scores.png')
plt.show()
plt.close()
return Z