コード例 #1
0
    def fit_variogram(self): # calculate semivariogram using bin_bound ##
        VarResolution = 1
        range = self.Clust.Range
        MaxDist = get_distance(np.array([range[0], range[2]]), np.array([range[1], range[3]]))
        bins=np.arange(0,MaxDist+VarResolution,VarResolution)+VarResolution

        Pow=self.SensorPowers
        self.update_pairwise_distance()
        Variogram, Bin_bound = find_variogram(self.PairwiseDist, Pow, bins)

        self.Variogram=Variogram
        pg1, pg2, pg3 = \
        opt.curve_fit(Exponent_modul, Bin_bound, Variogram, bounds=([0, 0, 0], [np.inf, np.inf, np.inf]), method='trf')[
            0]
        self.p=[pg1,pg2,pg3]
コード例 #2
0
    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