def estimate_Kriging(self,p,coord): ## Pow=self.SensorPowers Pos=self.get_sensor_locations() A, B= generate_matrices(p, self.PairwiseDist, Pos, coord) W = solve(A, B) W=W[0:Pow.shape[1],0].T Pow_est = np.sum(np.sum(W*Pow)) return Pow_est
def estimate_map(Prx, Locations): Pow = Prx tdist, tmean = find_pairwise_distance(Locations) Dist = tdist ## pbp method rij = np.zeros((Pow.shape[1], Pow.shape[1])) for i in range(0, Pow.shape[1]): rij[i, :] = 0.5 * (Pow[0, i] - Pow) * (Pow[0, i] - Pow) dij = Dist # # calculate variogram directly dd=dij.reshape(1,Pow.shape[1]*Pow.shape[1]) semi=rij.reshape(1,Pow.shape[1]*Pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] # ### bin method # VarResolution = 1 # rang = Range # MaxDist = get_distance(np.array([rang[0], rang[2]]), np.array([rang[1], rang[3]])) # bins = np.arange(0, MaxDist + VarResolution, VarResolution) + VarResolution # Pow = Pow # Variogram, Bin_bound = find_variogram(Dist, Pow, bins) # get coefficients for the Gaussian model #, bounds=([0,0,0],[np.inf,np.inf,np.inf]),method='trf' pg1, pg2, pg3 = opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0,0,0],[np.inf,np.inf,np.inf]),method='trf')[0]#bounds=([-np.inf,0,-np.inf,], [np.inf,np.inf,np.inf]) #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] rang = PixelRange # to get the estimated map img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) # Kriging_method # Pow = Prx Pos = Locations A, B = generate_matrices(p, Dist, Pos, coord) W = solve(A, B) W = W[0:Pow.shape[1], 0].T # print(np.sum(np.sum(W))) Pow_est = np.sum(np.sum(W * Pow)) img[i, j] = Pow_est Img = img return Img
def estimate_map_nearst_method(self, N): # for each point, use nearst N sensors self.update_sensor_powers() Pos = self.get_sensor_locations() Pow = self.SensorPowers rang = self.Clust.PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] flag=0 for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) sen_dis.append(dis) sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] pg1, pg2, pg3 = \ opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) self.Img = img
def estimate_map_nearst_method(self, N): # for each point, use nearst N sensors self.update_sensor_powers() Pos = self.get_sensor_locations() Pow = self.SensorPowers rang = self.Clust.PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] flag=0 for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) # if dis==0: # flag=1 # img[i,j]=Pow[0,ii] # break #if dis==0: # dis=0.00000000001 sen_dis.append(dis) # if flag==1: # continue # sen_dis = np.array(sen_dis) # sen_dis1 = np.sort(sen_dis) # pos = np.zeros((N, 2)) # pow = np.zeros((1, N)) # # for jj in range(0, N): # lo = np.argwhere(sen_dis == sen_dis1[jj])[0][0] # #print(pos) # pos[jj,:] = Pos[lo,:] # # pow[0,jj] = Pow[0,lo] # tdist, tmean = find_pairwise_distance(pos) # dij = tdist sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = dd # Variogram = semi # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] pg1, pg2, pg3 = \ opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) self.Img = img
def estimate_map(Prx, Dist, Locations, method,vari): Pow = Prx if vari=='pbp': rij = np.zeros((Pow.shape[1], Pow.shape[1])) for i in range(0, Pow.shape[1]): rij[i, :] = 0.5 * (Pow[0, i] - Pow) * (Pow[0, i] - Pow) dij = Dist dd = [] semi = [] for n in range(0, dij.shape[0]): for m in range(0, dij.shape[1]): if n == m: continue else: lo = np.argwhere(dij == dij[n, m]) dd.append(dij[n, m]) se = [] for l in range(0, lo.shape[0]): se.append(rij[lo[l, 0], lo[l, 1]]) semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = np.sort(dd) # Variogram = np.sort(semi) Bin_bound = dd Variogram = semi elif vari=='bin': VarResolution = 1 rang = Range MaxDist = get_distance(np.array([rang[0], rang[2]]), np.array([rang[1], rang[3]])) bins = np.arange(0, MaxDist + VarResolution, VarResolution) + VarResolution Pow = Pow Variogram, Bin_bound = find_variogram(Dist, Pow, bins) # plt.figure(30) # plt.plot(Bin_bound, Variogram, 'b.-') pg1, pg2, pg3 = opt.curve_fit(Gaussian_modul, Bin_bound, Variogram)[0] # pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] rang = PixelRange img = np.zeros((int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) if method == 'kriging': # Kriging_method # Pow = Prx Pos = Locations A, B = generate_matrices(p, Dist, Pos, coord) W = solve(A, B) W = W[0:Pow.shape[1], 0].T Pow_est = sum(sum(W * Pow)) img[i, j] = Pow_est elif method == 'shepard': Pos = Locations PowerFactor = 2 flag = 1 n = Pow.shape[1] W = np.zeros((1, n)) for nn in range(0, n): td = get_distance(Pos[nn, :], coord) if td == 0: flag = 0 Pow_est = Pow[0, nn] break W[0, nn] = 1 / (td ** PowerFactor) if flag == 1: Pow_est = sum(sum(W * Pow)) / sum(sum(W)) img[i, j] = Pow_est elif method == 'neighbour': Pos = Locations Pow = Prx grid_z0 = griddata(Pos, Pow.T, coord, method='nearest') # grid_z0 = naturalneighbor.griddata(Pos, Pow.T, coord) img[i, j] = grid_z0 Img = img return Img
def estimate_map(Prx, Dist, Locations, method, vari): Pow = Prx if vari == 'pbp': rij = np.zeros((Pow.shape[1], Pow.shape[1])) for i in range(0, Pow.shape[1]): rij[i, :] = 0.5 * (Pow[0, i] - Pow) * (Pow[0, i] - Pow) dij = Dist # # # calculate mean-value of variogram that under same distance # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound=dd # Variogram=semi # # calculate variogram directly dd = dij.reshape(1, Pow.shape[1] * Pow.shape[1]) semi = rij.reshape(1, Pow.shape[1] * Pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] plt.figure(30) plt.plot(Bin_bound, Variogram, 'b.-') plt.xlabel('distance/m') plt.ylabel('semivariogram') plt.title('equal space method') plt.show() elif vari == 'bin': VarResolution = 1 rang = Range MaxDist = get_distance(np.array([rang[0], rang[2]]), np.array([rang[1], rang[3]])) bins = np.arange(0, MaxDist + VarResolution, VarResolution) + VarResolution Pow = Pow Variogram, Bin_bound = find_variogram(Dist, Pow, bins) plt.figure(30) plt.plot(Bin_bound, Variogram, 'b.-') plt.xlabel('distance/m') plt.ylabel('semivariogram') plt.title('equal space method') plt.show() # sometimes the last value of Variogram is smaller than the first one. # Then it can't fit'RuntimeError: Optimal parameters not found: # The maximum number of function evaluations is exceeded.' # if Variogram[len(Variogram)-1]<Variogram[0]: # print(Variogram) # print(Bin_bound) # Variogram=Variogram[0:len(Variogram)-1] # Bin_bound=Bin_bound[0:len(Bin_bound)-1] pg1, pg2, pg3 = opt.curve_fit( Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[ 0] #bounds=([-np.inf,0,-np.inf,], [np.inf,np.inf,np.inf]) #pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] #pg1, pg2, pg3 = opt.curve_fit(Exponent_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] #print(p) rang = PixelRange img = np.zeros( (int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) if method == 'kriging': # Kriging_method # Pow = Prx Pos = Locations A, B = generate_matrices(p, Dist, Pos, coord) W = np.linalg.solve(A, B) W = W[0:Pow.shape[1], 0].T Pow_est = sum(sum(W * Pow)) img[i, j] = Pow_est elif method == 'shepard': Pos = Locations PowerFactor = 2 flag = 1 n = Pow.shape[1] W = np.zeros((1, n)) for nn in range(0, n): td = get_distance(Pos[nn, :], coord) if td == 0: flag = 0 Pow_est = Pow[0, nn] break W[0, nn] = 1 / (td**PowerFactor) if flag == 1: Pow_est = sum(sum(W * Pow)) / sum(sum(W)) img[i, j] = Pow_est elif method == 'neighbour': Pos = Locations Pow = Prx grid_z0 = griddata(Pos, Pow.T, coord, method='nearest') # grid_z0 = naturalneighbor.griddata(Pos, Pow.T, coord) img[i, j] = grid_z0 Img = img return Img
def estimate_map_nearst_method( N, Locations, Prx, PixelRange): # for each point, use nearst N sensors Pos = Locations flag = 0 Pow = Prx rang = PixelRange img = np.zeros( (int(rang[1]) - int(rang[0]) + 1, int(rang[3]) - int(rang[2]) + 1)) for i in range(0, img.shape[0]): tx = rang[0] + i for j in range(0, img.shape[1]): ty = rang[2] + j pxcoord = np.array([tx, ty]) coord = pixel2coord(pxcoord) sen_dis = [] for ii in range(0, Pos.shape[0]): dis = get_distance(coord, Pos[ii, :]) # if dis==0: # flag=1 # img[i,j]=Pow[0,ii] # break # if dis==0: # dis=0.00000000001 sen_dis.append(dis) # if flag==1: # continue sen_dis = np.array(sen_dis) num = np.argsort(sen_dis) pos = np.zeros((N, 2)) pow = np.zeros((1, N)) for kk in range(0, N): pos[kk, :] = Pos[num[kk], :] pow[0, kk] = Pow[0, num[kk]] dij, tmean = find_pairwise_distance(pos) rij = np.zeros((pow.shape[1], pow.shape[1])) for k in range(0, pow.shape[1]): rij[k, :] = 0.5 * (pow[0, k] - pow) * (pow[0, k] - pow) # # # calculate mean-value of variogram that under same distance # dd = [] # semi = [] # for n in range(0, dij.shape[0]): # for m in range(0, dij.shape[1]): # if n == m: # continue # else: # lo = np.argwhere(dij == dij[n, m]) # dd.append(dij[n, m]) # se = [] # for l in range(0, lo.shape[0]): # se.append(rij[lo[l, 0], lo[l, 1]]) # semi.append(np.sum(se) / lo.shape[0]) # Bin_bound = dd # Variogram = semi # # calculate variogram directly dd = dij.reshape(1, pow.shape[1] * pow.shape[1]) semi = rij.reshape(1, pow.shape[1] * pow.shape[1]) Bin_bound = dd[0] Variogram = semi[0] ########### # if Variogram[len(Variogram) - 1] < Variogram[0]: # Variogram = Variogram[0:len(Variogram) - 1] # Bin_bound = Bin_bound[0:len(Bin_bound) - 1] ########### pg1, pg2, pg3 = opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[0] # pg1, pg2, pg3 = opt.curve_fit(Spherical_modul, Bin_bound, Variogram)[0] p = [pg1, pg2, pg3] A, B = generate_matrices(p, dij, pos, coord) W = solve(A, B) W = W[0:pow.shape[1], 0].T img[i, j] = sum(sum(W * pow)) return img