Ejemplo n.º 1
0
    def addRBF(self, newXt, n=1000, qhull_opts="", output=True, makeHull=True):
        sampled=False; x=1.0;i=0
        while not(sampled):
            diff=self.Xt-newXt
            diff[:,2]*=self.vref*x
            diff*=diff
            diff=diff.sum(1)
            diff=np.sqrt(diff)
            index=np.argpartition(diff, n)[:n] #provide the indices of the n closest points
            points=self.array[index,:]

            rsampled=len(set(points[:,0]))>5
            zsampled=len(set(points[:,1]))>5
            tsampled=len(set(points[:,2]))>5
            
            if rsampled and zsampled and tsampled:
                sampled=True
            elif not(rsampled and zsampled):
                x*=2;i+=1
            else:
                x/=1.3;i+=1
            if i>=100:
                sampled=True
                print "max iterations reached, forcing convergence"
            

        if output: print("creating RBF at ", newXt)
        
        def dist_func(X1,X2):
            X=X1[:-1,...]-X2[:-1,...]
            T=(X1[-1,...]-X2[-1,...])*self.vref
            return np.sqrt((X**2).sum(0)+T**2)
        
        RBFu=Rbf(points[:,0], points[:,1], points[:,2], points[:,3],\
                 norm=dist_func, smooth=0, epsilon=5e2)

        RBFw=Rbf(points[:,0], points[:,1], points[:,2], points[:,4],\
                 norm=dist_func, smooth=0, epsilon=5e2)

        if makeHull:
            hull=Delaunay(RBFu.xi.T, qhull_options="QJ10000 "+qhull_opts) #creates a convex hull around the points used to construct the RBF
            RBFu.hull=hull           #a point cant be tested to be interior to the hull with:
            RBFw.hull=hull           #RBF.hull.find_simplex([r,z,t])>=0
            
            RBFu.centre=points.mean(0)
            RBFw.centre=points.mean(0)
        
        self.RBFs.appendleft([RBFu, RBFw])
Ejemplo n.º 2
0
    def run(self):
        while not self.kill_received:

            # get a task
            try:
                y_range, x_range, back_size = self.work_queue.get_nowait()
            except Queue.Empty:
                break

            # the actual processing
            print 'Worker running: ', multiprocessing.current_process()
            #			print "Computing the sky - XRANGE=", y_range, " - YRANGE=", x_range

            im_size = np.asarray(shared_im.shape)

            im_pad = back_size
            y_pad = [
                0 if y_range[0] == 0 else -im_pad[0],
                0 if y_range[1] == (im_size[0] - 1) else im_pad[0]
            ]
            x_pad = [
                0 if x_range[0] == 0 else -im_pad[1],
                0 if x_range[1] == (im_size[1] - 1) else im_pad[1]
            ]

            im_data = shared_im[y_range[0] + y_pad[0]:y_range[1] + y_pad[1],
                                x_range[0] + x_pad[0]:x_range[1] + x_pad[1]]
            yextra = im_data.shape[0] % back_size[0]
            xextra = im_data.shape[1] % back_size[1]

            ypad, xpad = 0, 0
            if yextra > 0: ypad = back_size[0] - yextra
            if xextra > 0: xpad = back_size[1] - xextra
            pad_width = ((0, ypad), (0, xpad))
            im_data_pad = np.pad(im_data,
                                 pad_width,
                                 mode='constant',
                                 constant_values=[np.nan])

            ny, nx = im_data_pad.shape
            ny_box, nx_box = back_size
            y_nbins = int(ny /
                          ny_box)  # always integer because data were padded
            x_nbins = int(nx /
                          nx_box)  # always integer because data were padded
            im_data_rebin = np.swapaxes(
                im_data_pad.reshape(y_nbins, ny_box, x_nbins, nx_box), 1,
                2).reshape(y_nbins, x_nbins, ny_box * nx_box)

            grid_data = np.median(im_data_rebin, axis=2)
            grid_x, grid_y = np.meshgrid(
                np.arange(back_size[0] / 2, nx, back_size[0]),
                np.arange(back_size[1] / 2, ny, back_size[1]))
            grid_gv = np.isfinite(grid_data)

            im_x, im_y = np.meshgrid(np.arange(0, nx), np.arange(0, ny))

            print "y_nbins, x_nsbins = ", y_nbins, x_nbins
            print "Shape of grid_x ", grid_x.shape
            print "Shape of im_x ", im_x.shape
            print np.sum(grid_gv)

            #			im_data_inter= scipy.interpolate.griddata((grid_y[grid_gv].flatten(),grid_x[grid_gv].flatten()), grid_data[grid_gv].flatten() , (im_y, im_x),method='cubic')
            if np.sum(grid_gv) > 2:
                im_rbf = Rbf(grid_y[grid_gv],
                             grid_x[grid_gv],
                             grid_data[grid_gv],
                             function='thin_plate',
                             smooth=0.0)
                im_data_inter = im_rbf(im_y, im_x)
                shared_nim[y_range[0]:y_range[1],
                           x_range[0]:x_range[1]] = im_data_inter[
                               -y_pad[0]:y_range[1] - y_range[0] - y_pad[0],
                               -x_pad[0]:x_range[1] - x_range[0] - x_pad[0]]


#			print "Shape of original image ", im_data.shape
#			print "Shape of padded image ", im_data_pad.shape
#			print "Shape of interpolated image ", im_data_inter.shape
#			print "Range of shared_nim y_range=", y_range[0], y_range[1], " x_range=", x_range[0], x_range[1]
#			print "Range of im_data_inter y_range=", -y_pad[0], y_range[1]-y_range[0]-y_pad[0], " x_range=", -y_pad[0], y_range[1]-y_range[0]-y_pad[0]

            print 'Worker done: ', multiprocessing.current_process()
            self.result_queue.put(id)
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp #debemos de importar scipy
from scipy.interpolate import Rbf

x = np.random.rand(5)
y = np.random.rand(5)
pet = 2+2*np.random.rand(5)
rbfi =Rbf(x, y, pet)

xi = np.linspace(0,1)
yi = np.linspace(0,1)
XI, YI = np.meshgrid(xi,yi) # gridded locations
di = rbfi(XI, YI) # interpolated values
plt.imshow(di, extent=(0,1,0,1), origin='lower')
plt.scatter(x,y, color='k')
plt.xlabel('X')
plt.ylabel('Y')
plt.axis((0,1,0,1))
plt.savefig('rbf.png')
Ejemplo n.º 4
0
import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
from matplotlib import cm
import mpl_toolkits.mplot3d

PLOT3D = False

mu1 = [1, 0]                # mean of rbf 1 (red)   -> (w[0])
mu2 = [-1, 0]               # mean of rbf 2 (blue)  -> (w[1])
w = np.array([10, -10])     # weight vector
stddev = 1

ti = np.linspace(-2.0, 2.0, 100)
xx, yy = np.meshgrid(ti, ti)
rbf1 = Rbf(mu1[0], mu1[1], 1, epsilon=stddev, function='gaussian')
rbf2 = Rbf(mu2[0], mu2[1], 1, epsilon=stddev, function='gaussian')
phi = np.stack([rbf1(xx, yy), rbf2(xx, yy)])      # create featuremap tensor
X, Y, Z = xx, yy, np.tensordot(w, phi, axes=1)    # calculate costfield

if not PLOT3D:
    plt.xlabel('x')
    plt.ylabel('y')
    plt.pcolor(X, Y, Z, cmap=cm.jet)
    plt.plot(mu1[0], mu1[1], 'o', ms=10, color='r')
    plt.plot(mu2[0], mu2[1], 'o', ms=10, color='b')
    plt.colorbar()
else:
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(
Ejemplo n.º 5
0
lats_orig, lons_orig = extractPositions(images)

doses_wrapped, lats_wrapped, lons_wrapped = wrapAround(doses, lats_orig,
                                                       lons_orig)
doses_log_wrapped, lats_wrapped, lons_wrapped = wrapAround(
    doses_log, lats_orig, lons_orig)

#{ RBF interpolation

# create meshgrid for RBF
x_meshgrid, y_meshgrid = createMeshGrid(150)

# calculate RBF from log data
rbf_lin = Rbf(lats_wrapped,
              lons_wrapped,
              doses_wrapped,
              function='multiquadric',
              epsilon=1,
              smooth=1)
doses_rbf_lin = rbf_lin(x_meshgrid, y_meshgrid)

# calculate RBF from lin data
rbf_log = Rbf(lats_wrapped,
              lons_wrapped,
              doses_log_wrapped,
              function='multiquadric',
              epsilon=1,
              smooth=1)
doses_rbf_log = rbf_log(x_meshgrid, y_meshgrid)

#} end of RBF interpolation
Ejemplo n.º 6
0
def forecast(timeSeries, waveletStr, predictMonths, plotGraphs=True):
    tsLength = len(timeSeries)
    wavelet = pywt.Wavelet(waveletStr)

    cA5, cD5, cD4, cD3, cD2, cD1 = pywt.wavedec(timeSeries, wavelet, level=5)

    predict = [
        predictMonths, predictMonths / 2, predictMonths / 4, predictMonths / 8,
        predictMonths / 16, predictMonths / 32
    ]

    # <codecell>
    if plotGraphs:
        print cA5.shape
        print cD5.shape
        print cD4.shape
        print cD3.shape
        print cD2.shape
        print cD1.shape

        # <codecell>

        rcParams['figure.figsize'] = 7, 4
        plt.plot(cA5)
        plt.show()

        # <codecell>

        plt.plot(cD5)
        plt.show()

        # <codecell>

        plt.plot(cD4)
        plt.show()

        # <codecell>

        plt.plot(cD3)
        plt.show()

        # <codecell>

        plt.plot(cD2)
        plt.show()

        # <codecell>

        plt.plot(cD1)
        plt.show()

    # <headingcell level=2>

    # Forecasting

    # <rawcell>

    # Extend the samples of cA5 using an interpolated univariate spline. This, by fitting this spline, we extended the
    # data to values [0:349] rather than [0:336]. Wavelet reconstruct the signal using the interpolated approx

    # <codecell>
    # Sample extension using RBF method. Wavelet reconstruct the signal using the interpolated approx

    t0 = np.linspace(0, tsLength - 1, cA5.shape[0]).astype(int)
    if (predict[4] > 0):
        ius_cA5 = InterpolatedUnivariateSpline(t0[:-predict[4]],
                                               cA5[:-predict[4]])
        rbf_cA5 = Rbf(t0[:-predict[4]], cA5[:-predict[4]])
    else:
        ius_cA5 = InterpolatedUnivariateSpline(t0, cA5)
        rbf_cA5 = Rbf(t0, cA5)
    ti = np.linspace(0, tsLength - 1, cA5.shape[0]).astype(int)
    cA5i1 = ius_cA5(ti)
    datai1 = pywt.waverec([cA5i1, cD5, cD4, cD3, cD2, cD1], waveletStr)

    cA5i2 = rbf_cA5(ti)
    datai2 = pywt.waverec([cA5i2, cD5, cD4, cD3, cD2, cD1], waveletStr)

    # <rawcell>

    # Comparison of the forecast based only on extrapolation of approximate wavelet decomposition part (IUS and RBF) with the true data

    # <codecell>
    if plotGraphs:
        rcParams['figure.figsize'] = 18, 10
        p1, = plot(timeSeries[tsLength - predictMonths - 20:], 'r')
        p2, = plot(datai1[tsLength - predictMonths - 20:], 'b')
        p3, = plot(datai2[tsLength - predictMonths - 20:], 'g')
        p4, = plot(data[tsLength - predictMonths - 20:, 1], 'y')

        legend([p3, p2, p1], ["RBF method", "Univariate Spline", "Oil Price"])

        plt.show()

    # <rawcell>

    # We are going to model the detail part of the wavelet decomposition using a trigonometric fitting/sine fitting. So, first we get the Fourier transform of each of these waveforms

    # <codecell>

    if plotGraphs:
        rcParams['figure.figsize'] = 7, 4

        Cd1 = np.fft.fft(cD1)
        plot(abs(Cd1))
        plt.show()

        # <codecell>

        Cd2 = np.fft.fft(cD2)
        plot(abs(Cd2))
        plt.show()

        # <codecell>

        Cd3 = np.fft.fft(cD3)
        plot(abs(Cd3))
        plt.show()

        # <codecell>

        Cd4 = np.fft.fft(cD4)
        plot(abs(Cd4))
        plt.show()

        # <codecell>

        Cd5 = np.fft.fft(cD5)
        plot(abs(Cd5))
        plt.show()

    # <codecell>

    def mysine(x, a1, a2, a3):
        return a1 * np.sin(a2 * x + a3)

    def sinefit(yReal, xReal):
        yhat = fftpack.rfft(yReal)
        idx = (yhat**2).argmax()
        freqs = fftpack.rfftfreq(len(xReal),
                                 d=(xReal[1] - xReal[0]) / (2 * pi))
        frequency = freqs[idx]

        amplitude = abs(yReal.max())
        guess = [amplitude, frequency, 0.]

        (amplitude, frequency,
         phase), pcov = optimize.curve_fit(mysine, xReal, yReal, guess)
        period = 2 * pi / frequency
        return [amplitude, frequency, phase]

    testSig = mysine(np.linspace(0, 10, 5000), 2, 200, 56)

    params = sinefit(testSig, np.linspace(0, 10, 5000))
    if plotGraphs:
        print params

    # <codecell>
    if predict[4] > 0:
        paramsD5 = sinefit(
            cD5[:-predict[4]],
            np.linspace(0, tsLength, cD5.shape[0])[:-predict[4]])
        cD5i = np.append(
            cD5[:-predict[4]],
            mysine(
                np.linspace(cD5.shape[0] - predict[4], cD5.shape[0],
                            predict[4]), paramsD5[0], paramsD5[1],
                paramsD5[2]))
    else:
        paramsD5 = sinefit(cD5, np.linspace(0, tsLength, cD5.shape[0]))
        cD5i = np.append(
            cD5,
            mysine(np.linspace(cD5.shape[0], cD5.shape[0], 0), paramsD5[0],
                   paramsD5[1], paramsD5[2]))
    if predict[3] > 0:
        paramsD4 = sinefit(
            cD4[:-predict[3]],
            np.linspace(0, tsLength, cD4.shape[0])[:-predict[3]])
        cD4i = np.append(
            cD4[:-predict[3]],
            mysine(
                np.linspace(cD4.shape[0] - predict[3], cD4.shape[0],
                            predict[3]), paramsD4[0], paramsD4[1],
                paramsD4[2]))
    else:
        paramsD4 = sinefit(cD4, np.linspace(0, tsLength, cD4.shape[0]))
        cD4i = np.append(
            cD4,
            mysine(np.linspace(cD4.shape[0], cD4.shape[0], 0), paramsD4[0],
                   paramsD4[1], paramsD4[2]))
    if predict[2] > 0:
        paramsD3 = sinefit(
            cD3[:-predict[2]],
            np.linspace(0, tsLength, cD3.shape[0])[:-predict[2]])
        cD3i = np.append(
            cD3[:-predict[2]],
            mysine(
                np.linspace(cD3.shape[0] - predict[2], cD3.shape[0],
                            predict[2]), paramsD3[0], paramsD3[1],
                paramsD3[2]))
    else:
        paramsD3 = sinefit(cD3, np.linspace(0, tsLength, cD3.shape[0]))
        cD3i = np.append(
            cD3,
            mysine(np.linspace(cD3.shape[0], cD3.shape[0], 0), paramsD3[0],
                   paramsD3[1], paramsD3[2]))
    if predict[1] > 0:
        paramsD2 = sinefit(
            cD2[:-predict[1]],
            np.linspace(0, tsLength, cD2.shape[0])[:-predict[1]])
        cD2i = np.append(
            cD2[:-predict[1]],
            mysine(
                np.linspace(cD2.shape[0] - predict[1], cD2.shape[0],
                            predict[1]), paramsD2[0], paramsD2[1],
                paramsD2[2]))
    else:
        paramsD2 = sinefit(cD2, np.linspace(0, tsLength, cD2.shape[0]))
        cD2i = np.append(
            cD2,
            mysine(np.linspace(cD2.shape[0], cD2.shape[0], 0), paramsD2[0],
                   paramsD2[1], paramsD2[2]))
    if predict[0] > 0:
        paramsD1 = sinefit(
            cD1[:-predict[0]],
            np.linspace(0, tsLength, cD1.shape[0])[:-predict[0]])
        cD1i = np.append(
            cD1[:-predict[0]],
            mysine(
                np.linspace(cD1.shape[0] - predict[0], cD1.shape[0],
                            predict[0]), paramsD1[0], paramsD1[1],
                paramsD1[2]))
    else:
        paramsD1 = sinefit(cD1, np.linspace(0, tsLength, cD1.shape[0]))
        cD1i = np.append(
            cD1,
            mysine(np.linspace(cD1.shape[0], cD1.shape[0], 0), paramsD1[0],
                   paramsD1[1], paramsD1[2]))

    datai1 = pywt.waverec([cA5i1, cD5i, cD4i, cD3i, cD2i, cD1i], waveletStr)
    datai2 = pywt.waverec([cA5i2, cD5i, cD4i, cD3i, cD2i, cD1i], waveletStr)

    # <headingcell level=2>

    # Forecast using all the interpolated signals

    # <codecell>
    if plotGraphs:
        rcParams['figure.figsize'] = 18, 10
        p1, = plot(timeSeries[tsLength - predictMonths - 20:], 'r')
        p2, = plot(datai1[tsLength - predictMonths - 20:], 'b')
        p3, = plot(datai2[tsLength - predictMonths - 20:], 'g')
        p4, = plot(data[tsLength - predictMonths - 20:, 1], 'y')

        legend([p4, p3, p2, p1],
               ["Contract 1", "RBF method", "Univariate Spline", "Oil Price"])

        plt.show()
    return datai2
Ejemplo n.º 7
0
def interpolate(x,
                y,
                data,
                grid_xy=None,
                interp_type='linear',
                hres=50000,
                minimum_neighbors=3,
                num_pass=1,
                gamma=0.25,
                kappa_star=5.052,
                search_radius=None,
                rbf_func='linear',
                rbf_smooth=0,
                op_param=None,
                comp_err=0):
    r"""Interpolate given (x,y), observation (z) pairs to a grid based on given parameters.

    Parameters
    ----------
    x: array_like
        x coordinate
    y: array_like
        y coordinate
    data: array_like
        observation value
    grid_xy: (grid_x, grid_y) tuple of (N, 2) ndarrays
        Meshgrid for the resulting interpolation in the x and y dimension respectively
        Default None (calculated from x and y)
    interp_type: str
        What type of interpolation to use. Available options include:
        1) "linear", "nearest", "cubic", or "rbf" from Scipy.interpolate.
        2) "natural_neighbor", "barnes", or "cressman" from Metpy.mapping .
        Default "linear".
    hres: float
        The horizontal resolution of the generated grid. Default 50000 meters.
    min_neighbors: int
        Minimum number of neighbors needed to perform barnes or cressman interpolation for a
        point. Default is 3.
    gamma: float
        Adjustable smoothing parameter for the barnes interpolation. Default 0.25.
    kappa_star: float
        Response parameter for barnes interpolation, specified nondimensionally
        in terms of the Nyquist. Default 5.052
    search_radius: float
        A search radius to use for the barnes and cressman interpolation schemes.
        If search_radius is not specified, it will default to the average spacing of
        observations.
    rbf_func: str
        Specifies which function to use for Rbf interpolation.
        Options include: 'multiquadric', 'inverse', 'gaussian', 'linear', 'cubic',
        'quintic', and 'thin_plate'. Defualt 'linear'. See scipy.interpolate.Rbf for more
        information.
    rbf_smooth: float
        Smoothing value applied to rbf interpolation.  Higher values result in more smoothing.

    Returns
    -------
    grid_xy: tuple of two (N, 2) ndarrays
        Meshgrid for the resulting interpolation in the x and y dimensions respectively
    img: (M, N) ndarray
        2-dimensional array representing the interpolated values for each grid.
    """

    # set up resulting grid
    if grid_xy is None:

        grid_x, grid_y = intp.points.generate_grid(
            hres, intp.points.get_boundary_coords(x, y))
    else:
        if len(grid_xy) != 2:
            raise ValueError("when specified, grid_xy must have 2 elements")
        else:
            grid_x, grid_y = grid_xy

    if grid_x.shape != grid_y.shape:
        raise ValueError("grid_x and grid_y must have the same shape")

    # interpolate
    if interp_type in ['linear', 'nearest', 'cubic']:
        points_zip = np.array(list(zip(x, y)))
        img = griddata(points_zip, data, (grid_x, grid_y), method=interp_type)

    elif interp_type == 'natural_neighbor':
        img = intp.natural_neighbor(x, y, data, grid_x, grid_y)

    elif interp_type in ['cressman', 'barnes', 'bratseth']:

        if interp_type == 'cressman':

            img = intp.inverse_distance(x,
                                        y,
                                        data,
                                        grid_x,
                                        grid_y,
                                        search_radius,
                                        minimum_neighbors,
                                        kind=interp_type)

        elif interp_type == 'barnes':

            ave_spacing = np.mean((cdist(list(zip(x, y)), list(zip(x, y)))))

            kappa = intp.calc_kappa(ave_spacing, kappa_star)

            img = intp.inverse_distance(x,
                                        y,
                                        data,
                                        grid_x,
                                        grid_y,
                                        search_radius,
                                        minimum_neighbors,
                                        gamma=gamma,
                                        kappa=kappa,
                                        num_pass=num_pass,
                                        kind=interp_type)
    elif interp_type == 'rbf':

        # 3-dimensional support not yet included.
        # Assign a zero to each z dimension for observations.
        h = np.zeros((len(x)))

        rbfi = Rbf(x, y, h, data, function=rbf_func, smooth=rbf_smooth)

        # 3-dimensional support not yet included.
        # Assign a zero to each z dimension grid cell position.
        hi = np.zeros(grid_x.shape)
        img = rbfi(grid_x, grid_y, hi)

    elif interp_type == 'optimal':

        grid_points = intp.points.generate_grid_coords(grid_x, grid_y)

        if op_param == None:
            raise ValueError(
                'Parameters must be set as input with argument op_param=:')
        elif np.size(op_param) != 4:
            raise ValueError('Argument op_param most be length 4')
        else:
            parameters = np.concatenate((op_param, [data.max()], [data.min()]))

        #else: Interpolate with user specified parameters ...
        args, spect_err = intp.optimal_parameters(x,
                                                  y,
                                                  data,
                                                  grid_points,
                                                  parameters,
                                                  comp_err=comp_err)

        img = intp.optimal_interp(args).reshape(grid_x.shape)
        if comp_err == 1:
            spect_err = spect_err.reshape(grid_x.shape)

    else:
        raise ValueError(
            'Interpolation option not available. '
            'Try: linear, nearest, cubic, natural_neighbor, '
            'optimal', 'barnes, cressman, rbf')

    if grid_xy == None:
        return (grid_x, grid_y), img
    else:
        if comp_err:
            return img, spect_err
        else:
            return img
    d2_doses_subset_log, d2_subset_lats, d2_subset_lons)
d4_doses_subset_log_wrapped, d4_subset_lats_wrapped, d4_subset_lons_wrapped = wrapAround(
    d4_doses_subset_log, d4_subset_lats, d4_subset_lons)

d2_doses_log_wrapped, d2_lats_wrapped, d2_lons_wrapped = wrapAround(
    d2_doses_log, d2_lats_original, d2_lons_original)
d4_doses_log_wrapped, d4_lats_wrapped, d4_lons_wrapped = wrapAround(
    d4_doses_log, d4_lats_original, d4_lons_original)

# create meshgrid for RBF
x_meshgrid, y_meshgrid = createMeshGrid(100)

# calculate RBF from lin data
d2_subset_rbf_log = Rbf(d2_subset_lats_wrapped,
                        d2_subset_lons_wrapped,
                        d2_doses_subset_log_wrapped,
                        function='multiquadric',
                        epsilon=0.1,
                        smooth=0.1)
d2_subset_doses_rbf_log = d2_subset_rbf_log(x_meshgrid, y_meshgrid)

d2_rbf_log = Rbf(d2_lats_wrapped,
                 d2_lons_wrapped,
                 d2_doses_log_wrapped,
                 function='multiquadric',
                 epsilon=0.1,
                 smooth=0.1)
d2_doses_rbf_log = d2_rbf_log(x_meshgrid, y_meshgrid)

d4_subset_rbf_log = Rbf(d4_subset_lats_wrapped,
                        d4_subset_lons_wrapped,
                        d4_doses_subset_log_wrapped,
Ejemplo n.º 9
0
def scale_spectra(common_text, list_photfiles, prefix_str='f', plot=False):
    """
    Scales spectra acccording to the values in the array 'scale_array'. Basically, this step applies
    flux calibration on the spectra.
    Args:
        common_text     : Common text of 1-D Spectra files whose spectroscopic fluxes are to be scaled
        list_photfiles  : Text list of files containing different broadband photometric magnitudes
        prefix_str      : Prefix to distinguish the scaled 1-D spectra from the original
        plot            : Boolean describing whether the scaled spectra has to be plotted
    Returns:
        None
    """
    list_files = group_similar_files('', common_text=common_text)

    for file_name in list_files:
        dict_spec = read_specflux(file_name)
        dict_phot = read_photflux(list_photfiles=list_photfiles,
                                  julian_day=read_jd(file_name))
        dict_phot = get_zflux(dict_phot, cntrl_wav=7500)
        dict_scale = dict(
            (key, str(float(dict_phot[key]) / float(dict_spec[key])))
            for key in dict_phot.keys() if key in dict_spec.keys())

        if len(dict_scale.keys()) > 3:
            del dict_scale[7500]

        # if len(dict_scale.keys()) > 4:
        #     order = 4
        # else:
        #     order = len(dict_scale.keys()) - 1

        series = pd.Series(dict_scale, dtype='float64')
        spline = CubicSpline(series.index.values,
                             series.values,
                             bc_type='natural',
                             extrapolate=True)
        spline2 = Rbf(series.index.values, series.values)

        wave_data, flux_data = read_1dspec(file_name)
        scale_data = spline(wave_data)
        scale_data[scale_data < 0] = 0

        flux_moddata = np.multiply(np.asarray(flux_data), scale_data)
        write_1dspec(ref_filename=file_name,
                     flux_array=flux_moddata,
                     prefix_str=prefix_str)

        if plot:
            fig = plt.figure(figsize=(8, 6))
            ax = fig.add_subplot(111)

            wavenew = np.linspace(float(wave_data[0]), float(wave_data[-1]),
                                  10000)
            ax.plot(series.index.values,
                    series.values,
                    'o',
                    label='Data Points')
            ax.plot(wavenew, spline(wavenew), 'k', label='CubicSpline')
            ax.plot(wavenew, spline2(wavenew), 'r', label='Rbf')

            ax.legend()
            ax.grid()
            plt.show()
            plt.close(fig)
Ejemplo n.º 10
0
groups = warp.split()
list_old_x, list_old_y, list_new_x, list_new_y, list_old_dist, list_new_dist = (
    [] for i in range(6))
for group in groups:
    coords = group.split(',')
    list_old_x.append(float(coords[0]))
    list_old_y.append(float(coords[1]))
    list_new_x.append(float(coords[2]))
    list_new_y.append(float(coords[3]) + random.random() / 1000000)
    list_old_dist.append(dist(list_old_x[-1], list_old_y[-1], 0.5, 0.5))
    list_new_dist.append(dist(list_new_x[-1], list_new_y[-1], 0.5, 0.5))

interpType = 'linear'
interpFunctionOldDist = Rbf(list_new_x,
                            list_new_y,
                            list_old_dist,
                            function=interpType)
interpFunctionNewDist = Rbf(list_old_x,
                            list_old_y,
                            list_new_dist,
                            function=interpType)

ringDist = oneMinuteDistance * ringMinutes

frameCount = 0
pTimeProgress = 0
numRows = mapResolution * len(framesToRender)
rowsDone = 0
startTime = datetime.now()
for frameNumber in framesToRender:
    progress = float(frameNumber) / frames
Ejemplo n.º 11
0
    df_ori = df_mid.copy().sample(frac=0.05,
                                  random_state=1).reset_index(drop=True)

    df_mid = df_mid.sample(frac=0.05,
                           random_state=constant).reset_index(drop=True)

    df_test = df_mid[0:int(0.1 * df_mid.shape[0])]
    df_train = df_mid[int(0.1 * df_mid.shape[0]):]

    a, b, c, d, e, vals = df_train.to_numpy().T

    now = time.time()
    try:
        W = joblib.load("weight_linear.pickle")
    except:
        rbfi = Rbf(a, b, c, d, e, vals, function="thin_plate")
        print("Training time -----------------------> %s seconds" %
              (time.time() - now))
        print("ATTRIBUTES RBF")

        print(rbfi.nodes)

        joblib.dump(rbfi.nodes, "weight_linear.pickle", protocol=2)

        W = rbfi.nodes

    a_test, b_test, c_test, d_test, e_test = df_ori[
        df_ori.columns[0:5]].to_numpy().T

    X_train = df_train[df_train.columns[0:5]].to_numpy()
Ejemplo n.º 12
0
def plot_heatmap(
    env_name: str,
    algo_name: str,
    param_names: List,
    env_pairs_pass_probabilities: List[Tuple[Tuple, float]],
    first_param_limits: List[float],
    second_param_limits: List[float],
    plot_points: bool = False,
    save_plot: bool = False,
    show_plot: bool = False,
    file_path: str = None,
    interpolation_function: str = "linear",
    smooth: float = 0.0,
) -> float:

    assert interpolation_function in [
        "multiquadric",
        "inverse",
        "gaussian",
        "linear",
        "cubic",
        "quintic",
        "thin_plate",
    ], "interpolation_function should have one of these values: {}".format(
        ["multiquadric", "inverse", "gaussian", "linear", "cubic", "quintic", "thin_plate"]
    )

    dist = None

    first_param_values = []
    second_param_values = []
    pass_probabilities = []

    for env_pair_pass_probability in env_pairs_pass_probabilities:
        mutated_pair = env_pair_pass_probability[0]
        pass_probability = env_pair_pass_probability[1]
        first_param_values.append(round(mutated_pair[0], 1))
        second_param_values.append(round(mutated_pair[1], 1))
        pass_probabilities.append(pass_probability)

    dict_for_df = {
        param_names[0]: first_param_values,
        param_names[1]: second_param_values,
        "pass_probability": pass_probabilities,
    }
    df = pd.DataFrame(dict_for_df)
    heatmap_data = pd.pivot_table(df, values="pass_probability", index=[param_names[1]], columns=param_names[0])

    first_param = Param(
        **load_env_params(algo_name=algo_name, env_name=env_name, param_name=param_names[0]), id=0, name=param_names[0]
    )
    second_param = Param(
        **load_env_params(algo_name=algo_name, env_name=env_name, param_name=param_names[1]), id=1, name=param_names[1]
    )

    min_first_param = first_param_limits[0] if len(first_param_limits) == 2 else first_param.get_low_limit()
    max_first_param = first_param_limits[1] if len(first_param_limits) == 2 else first_param.get_high_limit()

    min_second_param = second_param_limits[0] if len(second_param_limits) == 2 else second_param.get_low_limit()
    max_second_param = second_param_limits[1] if len(second_param_limits) == 2 else second_param.get_high_limit()

    extent = [min_first_param, max_first_param, min_second_param, max_second_param]
    # Create regular grid
    xi, yi = (
        np.linspace(min_first_param, max_first_param, heatmap_data.shape[1]),
        np.linspace(min_first_param, max_second_param, heatmap_data.shape[0]),
    )
    xi, yi = np.meshgrid(xi, yi)
    # Interpolate missing data
    matrix_is_singular_error = True
    while matrix_is_singular_error:
        try:
            rbf = Rbf(
                first_param_values, second_param_values, pass_probabilities, function=interpolation_function, smooth=smooth
            )
            matrix_is_singular_error = False
        except LinAlgError:
            smooth += 0.1
            print("Matrix is singular: increasing smooth value to", smooth)
            matrix_is_singular_error = True

    zi = rbf(xi, yi)

    if zi.shape == heatmap_data.shape:
        dist = 1 / (1 + np.linalg.norm(zi - heatmap_data.values))

    if show_plot or save_plot:

        fig = plt.figure()
        fig.suptitle(env_name + ", " + algo_name)

        ax1 = fig.add_subplot(211)
        cmap = LinearSegmentedColormap.from_list(name="test", colors=["red", "gold", "green"])
        sns.heatmap(data=heatmap_data, cmap=cmap, linewidths=0.3, cbar_kws={"label": "Pass probability"})
        ax1.invert_yaxis()

        ax2 = fig.add_subplot(212)
        hm = ax2.imshow(zi, interpolation="none", cmap=cmap, extent=extent, origin="lower", aspect="auto", vmin=0.0, vmax=1.0)
        if plot_points:
            ax2.scatter(first_param_values, second_param_values)
        cbar = fig.colorbar(hm, ax=ax2)
        cbar.ax.set_ylabel("Pass probability")
        ax2.set_xlabel(param_names[0])
        ax2.set_ylabel(param_names[1])

        fig = plt.gcf()
        fig.set_size_inches(12, 9)

        if show_plot:
            plt.show()

        if save_plot:
            if file_path:
                plt.savefig(file_path, format="pdf")
            else:
                abs_prefix = os.path.abspath("../")
                file_prefix = 0
                file_suffix = "heatmap_" + interpolation_function + "_" + env_name + "_" + algo_name + "_"
                file_name = file_suffix + str(file_prefix) + ".pdf"
                while os.path.exists(os.path.join(abs_prefix, file_name)):
                    file_prefix += 1
                    file_name = file_suffix + str(file_prefix) + ".pdf"

                plt.savefig(os.path.join(abs_prefix, file_name), format="pdf")

    return dist
Ejemplo n.º 13
0
def plot_HK_map(flder='.',
                lonrange=[120., 125.],
                latrange=[-34., -30.],
                info_file='/home/christian/info_file',
                z='H',
                gravity=False,
                smooth=0.1,
                eps=0.33,
                Ps=False,
                pval=False,
                save=False,
                mode='color'):
    """
    plots an interpolated map of crustal thickness (or vp/vs) from a bunch of HK-stacking derived values
    Ps=True only works with z='H'
    mode = 'color' or 'contour' or 'both' (the last only works if gravity=False)
    """
    # get dictionary, pick maxima from matrices
    if Ps:
        # read values from file, multiply with 8
        xvals = []
        yvals = []
        zvals = []
        if pval:
            infile = open('all_checked', 'r')
        else:
            infile = open('Ps_all', 'r')
        dtt = infile.readlines()
        infile.close()

        for j in dtt:
            if pval:
                sta, stat_lat, stat_lon, H, K, dum = j.strip('\n').split(None)
                print(sta)
            else:
                sta, Ps = j.strip('\n').split(None)
            # stat_lat,stat_lon,stat_elev = util.get_stat_coords(sta,info_file)

            if float(stat_lat) >= latrange[0] and float(stat_lat) <= latrange[
                    1] and float(stat_lon) >= lonrange[0] and float(
                        stat_lon) <= lonrange[1]:
                if z == 'K' and K == '----':
                    continue
                xvals.append(float(stat_lon))
                yvals.append(float(stat_lat))
                if pval:
                    if z == 'H':
                        zvals.append(float(H))
                    elif z == 'K':
                        zvals.append(float(K))
                else:
                    zvals.append(float(Ps) * 8)

    else:
        dic = dict_setup(flder=flder)
        dic_picked = pick_max(dic)

        # pkl.dump(dic_picked,open('storage_dic','wb'))
        # dic_picked = pkl.load(open('storage_dic','rb'))

        # now station-wise...get coordinates, store in 4-tuples (lat,lon,H,K)
        xvals = []
        yvals = []
        zvals = []
        for stat in dic_picked.keys():
            lat, lon, elev = util_c_sippl.get_stat_coords(stat, info_file)
            H = dic_picked[stat]['header']['max_HK'][0][1]
            K = dic_picked[stat]['header']['max_HK'][0][0]
            if float(lat) >= latrange[0] and float(lat) <= latrange[1] and float(lon) >= lonrange[0] and float(lon) <= \
                    lonrange[1]:
                xvals.append(lon)
                yvals.append(lat)
                if z == 'H':
                    zvals.append(H)
                elif z == 'K':
                    zvals.append(K)

                    # set control nodes (at 40 km)
    for x in np.arange(lonrange[0], lonrange[1], 0.25):
        for y in [latrange[0] - 0.25, latrange[1] + 0.25]:
            xvals.append(x)
            yvals.append(y)
            if z == 'H':
                zvals.append(37.5)
            elif z == 'K':
                zvals.append(1.73)

    for y in np.arange(latrange[0], latrange[1], 0.25):
        for x in [lonrange[0] - 0.25, lonrange[1] + 0.25]:
            xvals.append(x)
            yvals.append(y)
            if z == 'H':
                zvals.append(37.5)
            elif z == 'K':
                zvals.append(1.73)

    print(xvals, yvals)

    yi = np.linspace(latrange[0], latrange[1], 500)
    xi = np.linspace(lonrange[0], lonrange[1], 500)
    XI, YI = np.meshgrid(xi, yi)

    from mpl_toolkits import basemap
    plt.figure(figsize=(12, 12))
    m = basemap.Basemap(projection='merc',
                        llcrnrlon=lonrange[0],
                        llcrnrlat=latrange[0],
                        urcrnrlon=lonrange[1],
                        urcrnrlat=latrange[1],
                        resolution='h')

    XImap, YImap = m(XI, YI)

    from scipy.interpolate import Rbf

    xvals_map, yvals_map = m(xvals, yvals)
    rbf = Rbf(xvals, yvals, zvals, epsilon=eps, smooth=smooth)
    # rbf = Rbf(xvals_map,yvals_map,zvals)
    ZI = rbf(XI, YI)

    outfile = open('Moho_lowres.txt', 'w')
    for i in range(len(ZI)):
        for j in range(len(ZI[0])):
            outfile.write(
                str(round(XI[i][j], 3)) + ' ' + str(round(YI[i][j], 3)) + ' ' +
                str(round(ZI[i][j], 3)) + '\n')

    m.drawcoastlines()
    # plt.ylim([latrange[0],latrange[1]])
    # plt.xlim([lonrange[0],lonrange[1]])
    if gravity:
        # read in gravity grid, plot contours on top
        gravdat = np.loadtxt('/home/sippl/sandbox/gravity.txt')

        # convert to regular grid
        gravxi = np.arange(120., 128.01, 0.01)
        gravyi = np.arange(-34., -30.01, 0.01)
        gravzi = np.griddata(gravdat[:, 0],
                             gravdat[:, 1],
                             gravdat[:, 2],
                             gravxi,
                             gravyi,
                             interp='linear')

        gravlon, gravlat = np.meshgrid(gravxi, gravyi)
        gravx, gravy = m(gravlon, gravlat)

        m.contour(gravx,
                  gravy,
                  gravzi, [-800, -400, -200, 0, 150, 300],
                  colors=['b', 'b', 'r', 'r', 'r', 'r'],
                  linestyles='-')
    if mode == 'color':
        m.pcolor(XImap, YImap, ZI, cmap='jet', vmin=32, vmax=52)

    elif mode == 'contour':
        m.contour(XImap, YImap, ZI)

    elif mode == 'both':
        m.pcolor(XImap, YImap, ZI, cmap='jet')
        m.contour(XImap, YImap, ZI)

    m.scatter(xvals_map, yvals_map, 120, zvals, cmap='jet', vmin=32, vmax=52)

    lat_draw = np.arange(latrange[0], latrange[1], 1)
    lon_draw = np.arange(lonrange[0], lonrange[1], 1)

    m.drawmeridians(lon_draw, labels=[0, 0, 0, 1])
    m.drawparallels(lat_draw, labels=[1, 0, 0, 0])

    if z == 'H':
        plt.colorbar(shrink=0.7, label='Moho depth [km]')
    elif z == 'K':
        plt.colorbar(shrink=0.7, label='vp/vs')

    if save:
        plt.savefig('Mohomap_Vers1.pdf', dpi=300)
Ejemplo n.º 14
0
def grid_query_unstruct(uvs, values, grid_res, method=None):
    r"""Grid queries unstructured data given by coordinates and their values.

    If you are looking to grid query structured data, such as an image, check
    out :func:`grid_query_img`.

    This function interpolates values on a rectangular grid given some sparse,
    unstrucured samples. One use case is where you have some UV locations and
    their associated colors, and you want to "paint the colors" on a UV canvas.

    Args:
        uvs (numpy.ndarray): N-by-2 array of UV coordinates where we have
            values (e.g., colors). See
            :func:`xiuminglib.blender.object.smart_uv_unwrap` for the UV
            coordinate convention.
        values (numpy.ndarray): N-by-M array of M-D values at the N UV
            locations, or N-array of scalar values at the N UV locations.
            Channels are interpolated independently.
        grid_res (array_like): Resolution (height first; then width) of
            the query grid.
        method (dict, optional): Dictionary of method-specific parameters.
            Implemented methods and their default parameters:

            .. code-block:: python

                # Default
                method = {
                    'func': 'griddata',
                    # Which SciPy function to call.

                    'func_underlying': 'linear',
                    # Fed to `griddata` as the `method` parameter.

                    'fill_value': (0,), # black
                    # Will be used to fill in pixels outside the convex hulls
                    # formed by the UV locations, and if `max_l1_interp` is
                    # provided, also the pixels whose interpolation is too much
                    # of a stretch to be trusted. In the context of "canvas
                    # painting," this will be the canvas' base color.

                    'max_l1_interp': np.inf, # trust/accept all interpolations
                    # Maximum L1 distance, which we can trust in interpolation,
                    # to pixels that have values. Interpolation across a longer
                    # range will not be trusted, and hence will be filled with
                    # `fill_value`.
                }

            .. code-block:: python

                method = {
                    'func': 'rbf',
                    # Which SciPy function to call.

                    'func_underlying': 'linear',
                    # Fed to `Rbf` as the `method` parameter.

                    'smooth': 0, # no smoothing
                    # Fed to `Rbf` as the `smooth` parameter.
                }

    Returns:
        numpy.ndarray: Interpolated values at query locations, of shape
        ``grid_res`` for single-channel input or ``(grid_res[0], grid_res[1],
        values.shape[2])`` for multi-channel input.
    """
    if values.ndim == 1:
        values = values.reshape(-1, 1)
    assert values.ndim == 2 and values.shape[0] == uvs.shape[0]

    if method is None:
        method = {'func': 'griddata'}

    h, w = grid_res
    # Generate query coordinates
    grid_x, grid_y = np.meshgrid(np.linspace(0, 1, w), np.linspace(0, 1, h))
    # +---> x
    # |
    # v y
    grid_u, grid_v = grid_x, 1 - grid_y
    # ^ v
    # |
    # +---> u

    if method['func'] == 'griddata':
        from scipy.interpolate import griddata

        func_underlying = method.get('func_underlying', 'linear')
        fill_value = method.get('fill_value', (0, ))
        max_l1_interp = method.get('max_l1_interp', np.inf)

        fill_value = np.array(fill_value)
        if len(fill_value) == 1:
            fill_value = np.tile(fill_value, values.shape[1])
        assert len(fill_value) == values.shape[1]

        if max_l1_interp is None:
            max_l1_interp = np.inf  # trust everything

        # Figure out which pixels can be trusted
        has_value = np.zeros((h, w), dtype=np.uint8)
        ri = ((1 - uvs[:, 1]) * (h - 1)).astype(int).ravel()
        ci = (uvs[:, 0] * (w - 1)).astype(int).ravel()
        in_canvas = np.logical_and.reduce(
            (ri >= 0, ri < h, ci >= 0,
             ci < w))  # to ignore out-of-canvas points
        has_value[ri[in_canvas], ci[in_canvas]] = 1
        dist2val = cv2.distanceTransform(1 - has_value, cv2.DIST_L1, 3)
        trusted = dist2val <= max_l1_interp

        # Process each color channel separately
        interps = []
        for ch_i in range(values.shape[1]):
            v_fill = fill_value[ch_i]
            v = values[:, ch_i]
            interp = griddata(uvs,
                              v, (grid_u, grid_v),
                              method=func_underlying,
                              fill_value=v_fill)
            interp[~trusted] = v_fill
            interps.append(interp)
        interps = np.dstack(interps)

    elif method['func'] == 'rbf':
        from scipy.interpolate import Rbf

        func_underlying = method.get('func_underlying', 'linear')
        smooth = method.get('smooth', 0)

        # Process each color channel separately
        interps = []
        for ch_i in range(values.shape[1]):
            v = values[:, ch_i]
            rbfi = Rbf(uvs[:, 0],
                       uvs[:, 1],
                       v,
                       function=func_underlying,
                       smooth=smooth)
            interp = rbfi(grid_u, grid_v)
            interps.append(interp)
        interps = np.dstack(interps)

    else:
        raise NotImplementedError(method['func'])

    if interps.shape[2] == 1:
        return interps[:, :, 0].squeeze()
    return interps
    df.loc[i, 'easting'] = list(utm.from_latlon(df.loc[i, 'lat'], df.loc[i, 'lon'], zone, 'U'))[0]  # easting
    df.loc[i, 'northing'] = list(utm.from_latlon(df.loc[i, 'lat'], df.loc[i, 'lon'], zone, 'U'))[1]  # northing
    df.loc[i, 'zone'] = list(utm.from_latlon(df.loc[i, 'lat'], df.loc[i, 'lon'], zone, 'U'))[2]  # zone
    df.loc[i, 'zone_letter'] = list(utm.from_latlon(df.loc[i, 'lat'], df.loc[i, 'lon'], zone, 'U'))[3]  # zone letter

df = df[['prcs', 'lon', 'lat', 'easting', 'northing']]

df.rename(columns={'easting': 'x',  # depending on the format of input data 
                   'northing': 'y'}, inplace=True) 

step = 2500
extent = x_min, x_max, y_min, y_max = [100161.17256633355, 598195.012411898, 5839614.174655632, 6654300.485022815]
grid_x, grid_y = np.mgrid[x_min:x_max:step, y_min:y_max:step]

rbfi = Rbf(df.x, df.y, df.prcs, function='linear', smooth=200000)
di = rbfi(grid_x, grid_y)

import utm

coords = zip(grid_x.flatten(), grid_y.flatten())
df_interp = pd.DataFrame(coords, columns=['easting', 'northing'])

# recalculate interpolated values with utm coordinates back in lat/lon
zone = 11
for c in df_interp.index:
    df_interp.at[c, 'lat'] = list(utm.to_latlon(df_interp.at[c, 'easting'], df_interp.at[c, 'northing'], zone, 'U'))[0]
    df_interp.at[c, 'lat'] = list(utm.to_latlon(df_interp.at[c, 'easting'], df_interp.at[c, 'northing'], zone, 'U'))[1]

mapped = zip(df_interp.lon, df_interp.lat, df_interp.easting, df_interp.northing, di.flatten())
mapped = set(mapped)
Ejemplo n.º 16
0
    'function': 'thin_plate',
    }
    #############

    # get params (x) and cost (z)
    x, z = xyz.T[:,:-1], xyz.T[:,-1]

    #HACK: remove any duplicate points by adding noise
    _x = x + np.random.normal(scale=1e-8, size=x.shape)

    if len(z) > N:
        N = max(int(round(len(z)/float(N))),1)
        print "for speed, sampling {} down to {}".format(len(z),len(z)/N)
        x, _x, z = x[::N], _x[::N], z[::N]

    f = Rbf(*np.vstack((_x.T, z)), **args)
    f.__doc__ = model.__doc__
    # convert to 'model' format (i.e. takes a parameter vector)
    _model = lambda x: f(*x).tolist()
    _model.__doc__ = f.__doc__

    mz = np.argmin(z)
    print "min: {}; min@f: {}".format(z[mz], f(*x[mz]))
    mz = np.argmax(z)
    print "max: {}; max@f: {}".format(z[mz], f(*x[mz]))

#   print "TOOK: %s" % (time.time() - start)

    # plot
    #############
    # specify 2-of-N dim (with bounds) and (N-2) with values
Ejemplo n.º 17
0
    def create_contour(self,
                       fields=None,
                       time=None,
                       function='cubic',
                       subplot_index=(0, ),
                       grid_delta=(0.01, 0.01),
                       grid_buffer=0.1,
                       **kwargs):
        """
        Extracts, grids, and creates a contour plot. If subplots have not been
        added yet, an axis will be created assuming that there is only going
        to be one plot.

        Parameters
        ----------
        fields : dict
            Dictionary of fields to use for x, y, and z data.
        time : datetime
            Time in which to slice through objects.
        function : string
            Defaults to cubic function for interpolation.
            See scipy.interpolate.Rbf for additional options.
        subplot_index : 1 or 2D tuple, list, or array
            The index of the subplot to set the x range of.
        grid_delta : 1D tuple, list, or array
            x and y deltas for creating grid.
        grid_buffer : float
            Buffer to apply to grid.
        **kwargs : keyword arguments
            The keyword arguments for :func:`plt.contour`

        Returns
        -------
        ax : matplotlib axis handle
            The matplotlib axis handle of the plot.

        """
        # Get x, y, and z data by looping through each dictionary
        # item and extracting data from appropriate time
        x = []
        y = []
        z = []
        for ds in self._arm:
            obj = self._arm[ds]
            field = fields[ds]
            x.append(obj[field[0]].sel(time=time).values.tolist())
            y.append(obj[field[1]].sel(time=time).values.tolist())
            z.append(obj[field[2]].sel(time=time).values.tolist())

        # Create a meshgrid for gridding onto
        xs = np.arange(
            np.min(x) - grid_buffer,
            np.max(x) + grid_buffer, grid_delta[0])
        ys = np.arange(
            np.min(y) - grid_buffer,
            np.max(y) + grid_buffer, grid_delta[1])
        xi, yi = np.meshgrid(xs, ys)

        # Use scipy radial basis function to interpolate data onto grid
        rbf = Rbf(x, y, z, function=function)
        zi = rbf(xi, yi)

        # Create contour plot
        self.contourf(xi, yi, zi, subplot_index=subplot_index, **kwargs)

        return self.axes[subplot_index]
Ejemplo n.º 18
0
def save_single_plot(layout_fn, csv_fn, output_fn):

    layout = imread(layout_fn)
    a = pd.read_csv(csv_fn)

    image_width = len(layout[0])
    image_height = len(layout)

    print("Image Width: %d; Image Height: %d" % (image_width, image_height))

    grid_width = image_width
    grid_height = image_height

    num_x = image_width // 8
    num_y = num_x // (image_width // image_height)

    print("Resolution: %0.2f x %0.2f" % (num_x, num_y))

    x = np.linspace(0, grid_width, num_x)
    y = np.linspace(0, grid_height, num_y)

    gx, gy = np.meshgrid(x, y)
    gx, gy = gx.flatten(), gy.flatten()

    # -80dBm: Unstable; -30dBm: Perfect signal
    # Source: https://eyesaas.com/wi-fi-signal-strength/
    levels = [-85, -80, -75, -70, -65, -60, -55, -50, -45, -40, -35, -30, -25]

    interpolate = True

    f = pp.figure()

    # Use AxesGrid to create subplots
    # Subplots not used, but useful for multiple floors
    image_grid = AxesGrid(f,
                          111,
                          nrows_ncols=(1, 1),
                          axes_pad=0.1,
                          label_mode="1",
                          share_all=True,
                          cbar_location="right",
                          cbar_mode="single",
                          cbar_size="3%")

    for beacon, i in zip(sources, range(len(sources))):
        # Hide labels
        image_grid[i].xaxis.set_visible(False)
        image_grid[i].yaxis.set_visible(False)

        if interpolate:
            # Interpolate the data
            rbf = Rbf(a['Drawing X'],
                      a['Drawing Y'],
                      a[beacon],
                      function='linear')  #possibly logarithmic?

            # Run rbf() on the data to find the interpolated value
            z = rbf(gx, gy)
            z = z.reshape((num_y, num_x))

            # Render the interpolated data (red, yellow, blue)
            image = image_grid[i].imshow(z,
                                         vmin=-85,
                                         vmax=-25,
                                         extent=(0, image_width, image_height,
                                                 0),
                                         cmap='RdYlBu_r',
                                         alpha=1)

        else:
            # If value not interpolated
            z = ml.griddata(a['Drawing X'], a['Drawing Y'], a[beacon], x, y)

            c = image_grid[i].contourf(x, y, z, levels, alpha=0.5)

        image_grid[i].imshow(layout, interpolation='bicubic', zorder=100)

    # colorbar setup
    image_grid.cbar_axes[0].colorbar(image)
    image_grid.cbar_axes[0].set_yticks(levels)

    # Add titles
    for ax, im_title in zip(image_grid, sources):
        t = add_inner_title(ax, "Layout Name: %s" % layout_fn, loc=3)

        t.patch.set_alpha(0.5)

    # pp.show()
    pp.savefig(output_fn, bbox_inches='tight', transparent=True)
def draw_function():
    if btn7.get() == '默认':
        mark2 = 0
        mmin = None
    else:
        mark2 = 1
        mmin = float(btn7.get())
    if btn8.get() == '默认':
        mark3 = 0
        mmax = None
    else:
        mark3 = 1
        mmax = float(btn8.get())
    #------------警告提示框---------
    if mark1 == 0 and mark2 == 0 and mark3 == 0:
        pass
    elif mark1 == 0 and (mark2 == 1 or mark3 == 1):
        tk.messagebox.showwarning('警告', '色阶默认情况下,图例最小值和图例最大值均应为“默认”')
        return
    else:
        tk.messagebox.showwarning('警告', '色阶非默认情况下,需同时自定义色阶级数、图例最小值和图例最大值')
        return

    path0 = 'DTool/dishi.shp'
    #    path1 = 'C:/Users/zhanLf/Desktop/Python/DTool/shengjie.shp'
    #    a = gpd.GeoDataFrame.from_file(r'C:\Users\zhanlf\Desktop\Python\DTool\dishi.shp')

    file = shapefile.Reader(path0)
    rec = file.shapeRecords()
    polygon = list()
    for r in rec:
        polygon.append(Polygon(r.shape.points))
    poly = cascaded_union(polygon)  #并集
    ext = list(poly.exterior.coords)  #外部点
    codes = [Path.MOVETO] + [Path.LINETO] * (len(ext) - 1) + [Path.CLOSEPOLY]
    #    codes += [Path.CLOSEPOLY]
    ext.append(ext[0])  #起始点
    path = Path(np.array(ext), codes)
    patch = PathPatch(path, facecolor='None')
    df = pd.read_csv(file_path, sep=',', encoding='GB2312')
    x, y = df['经度'], df['纬度']
    xi = np.arange(113, 118.5, 0.01)
    yi = np.arange(24, 31, 0.01)
    olon, olat = np.meshgrid(xi, yi)

    # 空间插值计算
    func = Rbf(x, y, z, function='linear')
    rain_data_new = func(olon, olat)

    ax = plt.axes(projection=ccrs.PlateCarree())
    box = [113.4, 118.7, 24.1, 30.4]
    ax.set_extent(box, crs=ccrs.PlateCarree())
    ax.add_patch(patch)
    shp = list(shpreader.Reader(path0).geometries())
    ax.add_geometries(shp,
                      ccrs.PlateCarree(),
                      edgecolor='black',
                      facecolor='none',
                      alpha=1,
                      linewidth=0.5)  #加底图
    '''    不可取方法,太耗时间    
    
    #for i in range(0,olat.shape[1]):
    #    print(i)
    #    for j in range(0,olat.shape[0]):
    #        if geometry.Point(np.array([olon[0,i],olat[j,0]])).within(geometry.shape(shps)): 
    #            continue
    #        else:
    #            rain_data_new[j,i] = np.nan
    '''

    if mark1 == 1 and mark2 == 1 and mark3 == 1 and btn10.get() != 'CMA_Rain':
        v = np.linspace(mmin, mmax, num=mnum, endpoint=True)  #设置显示数值范围和级数
        if color_mark == 1:
            pic = plt.contourf(olon, olat, rain_data_new, v, cmap=plt.cm.jet)
        elif color_mark == 2:
            pic = plt.contourf(olon,
                               olat,
                               rain_data_new,
                               v,
                               cmap=plt.cm.rainbow)
        elif color_mark == 3:
            pic = plt.contourf(olon,
                               olat,
                               rain_data_new,
                               v,
                               cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            pic = plt.contourf(olon, olat, rain_data_new, v, cmap=plt.cm.OrRd)
        cbar = plt.colorbar(pic)
        cbar.set_label(btn9.get(), fontproperties='SimHei')  #图例label在右边
    # elif mark1 == 1 and mark2 == 0 and mark3 == 0 and btn10.get() != 'CMA_Rain':
    #     # if color_mark == 1:
    #     #     pic = plt.contourf(olon, olat, rain_data_new, mnum, cmap = plt.cm.jet)
    #     # elif color_mark == 2:
    #     #     pic = plt.contourf(olon, olat, rain_data_new, mnum, cmap = plt.cm.rainbow)
    #     # elif color_mark == 3:
    #     #     pic = plt.contourf(olon, olat, rain_data_new, mnum, cmap = plt.cm.gist_rainbow)
    #     # elif color_mark == 4:
    #     #     pic = plt.contourf(olon, olat, rain_data_new, mnum, cmap = plt.cm.OrRd)
    #     # v = np.linspace(mmin, mmax, num = mnum, endpoint = True)
    #     pic = plt.contourf(olon, olat, rain_data_new, mnum, cmap = plt.cm.jet)
    #     cbar = plt.colorbar(pic)
    #     cbar.set_label(btn9.get(),fontproperties='SimHei')
    elif btn10.get() == 'CMA_Rain':
        rain_levels = [0, 0.1, 10, 25, 50, 100, 250, 2500]
        rain_colors = [
            '#FFFFFF', '#A6F28F', '#38A800', '#61B8FF', '#0000FF', '#FA00FA',
            '#730000', '#400000'
        ]
        pic = plt.contourf(olon,
                           olat,
                           rain_data_new,
                           levels=rain_levels,
                           colors=rain_colors)
        cbar = plt.colorbar(pic, ticks=[0, 0.1, 10, 25, 50, 100, 250])
        cbar.set_label(btn9.get(), fontproperties='SimHei')  #图例label在右边
        # cbar.make_axes(locations='top')
        # cbar.ax.set_xlabel(btn9.get(),fontproperties='SimHei')
    else:
        if color_mark == 1:
            pic = plt.contourf(olon, olat, rain_data_new, cmap=plt.cm.jet)
        elif color_mark == 2:
            pic = plt.contourf(olon, olat, rain_data_new, cmap=plt.cm.rainbow)
        elif color_mark == 3:
            pic = plt.contourf(olon,
                               olat,
                               rain_data_new,
                               cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            pic = plt.contourf(olon, olat, rain_data_new, cmap=plt.cm.OrRd)
        cbar = plt.colorbar(pic)
        cbar.set_label(btn9.get(), fontproperties='SimHei')  #图例label在右边

    for collection in pic.collections:
        collection.set_clip_path(patch)  #设置显示区域

    plt.scatter(x, y, marker='.', c='k', s=10)

    # cbar.ax.set_xlabel(btn9.get(),fontproperties='SimHei') #图例label在下边

    fig = plt.gcf()
    fig.set_size_inches(5, 4)
    plt.savefig('pics.tif', bbox_inches='tight')
    plt.close()
    wifi_img = Image.open('pics.tif')
    img = ImageTk.PhotoImage(wifi_img)
    window.img = img  # to prevent the image garbage collected.
    canvas.create_image(200, 180, anchor='center', image=img)
Ejemplo n.º 20
0
Use scipy to interpolate the value of a scalar known on a set of points
on a new set of points where the scalar is not defined.
Two interpolation methods are possible: Radial Basis Function, Nearest Point.
'''
from scipy.interpolate import Rbf, NearestNDInterpolator as Near
import numpy as np
# np.random.seed(0)


# a small set of points for which the scalar is given
x, y, z = np.random.rand(3, 20)

scals = z                            # scalar value is just z component

# build the interpolator
itr = Rbf(x, y, z, scals)              # Radial Basis Function interpolator
# itr = Near(list(zip(x,y,z)), scals) # Nearest-neighbour interpolator

# generate a new set of points
t = np.linspace(0, 7, 100)
xi, yi, zi = [np.sin(t)/10+.5, np.cos(t)/5+.5, (t-1)/5]  # an helix

# interpolate scalar values on the new set
scalsi = itr(xi, yi, zi)


from vtkplotter import Plotter, Points, Text
vp = Plotter(verbose=0, bg='w')
vp.add(Points([x, y, z], r=10, alpha=0.5)).pointColors(scals)
vp.add(Points([xi, yi, zi])).pointColors(scalsi)
Ejemplo n.º 21
0
    def changeToRebinedMjuAveraging(self,
                                    rebinType='spline',
                                    dE1=1,
                                    dE2=0.1,
                                    dK=0.01,
                                    s=0):
        # rebinType possible values:
        #    'spline'
        #    'rbf-smooth'
        self.energyOriginal = copy(self.energy)
        self.mjuOriginal = copy(self.mju)

        # sort array in there are decreasing points
        sortIndexes = self.energy.argsort()
        sortedEnergy = self.energy[sortIndexes]
        sortedMju = self.mju[sortIndexes]

        dE = dE1
        newE = []
        newMju = []

        #take data before E1
        E1Region = where(sortedEnergy < self.E1)
        E1Energy = sortedEnergy[E1Region]
        E1Mju = sortedMju[E1Region]
        newE.append(E1Energy[0])
        newMju.append(E1Mju[0])
        eCenter = E1Energy[0] + dE / 2
        while eCenter + dE / 2 < self.E1:
            reg = where(
                logical_and(sortedEnergy > eCenter - dE / 2,
                            sortedEnergy < eCenter + dE / 2))
            valE = sortedEnergy[reg].sum() / len(sortedEnergy[reg])
            val = sortedMju[reg].sum() / len(sortedMju[reg])
            newE.append(valE)
            newMju.append(val)
            eCenter = eCenter + dE

        dE = dE2

        #take data before E0+50
        E0Region = where(
            logical_and(sortedEnergy > self.E1, sortedEnergy < self.E0 + 50))
        E0Energy = sortedEnergy[E0Region]
        E0Mju = sortedMju[E0Region]
        newE.append(E0Energy[0])
        newMju.append(E0Mju[0])
        eCenter = E0Energy[0] + dE / 2
        while eCenter + dE / 2 < self.E0 + 50:
            reg = where(
                logical_and(sortedEnergy > eCenter - dE / 2,
                            sortedEnergy < eCenter + dE / 2))
            #            print("reg", len(reg), reg)
            #            if reg==[]:
            #                eCenter = eCenter + dE
            if len(reg[0]) == 1:
                valE = sortedEnergy[reg][0]
                val = sortedMju[reg][0]
                newE.append(valE)
                newMju.append(val)
            if len(reg[0]) > 1:
                valE = sortedEnergy[reg].sum() / len(sortedEnergy[reg])
                val = sortedMju[reg].sum() / len(sortedMju[reg])
                newE.append(valE)
                newMju.append(val)
            eCenter = eCenter + dE

        dk = dK

        #take data after E0
        E3Region = where(
            logical_and(sortedEnergy > self.E0 + 50, sortedEnergy < self.E3))
        E3Energy = sortedEnergy[E3Region]
        E3Mju = sortedMju[E3Region]
        newE.append(E3Energy[0])
        newMju.append(E3Mju[0])

        #        kExafsMin = sqrt(  (2*me/hbar**2) * (self.E0+50-self.E0) * 1.602*10**-19  ) *10**-10
        #        kExafsMax = sqrt(  (2*me/hbar**2) * (self.E3-self.E0) * 1.602*10**-19  ) *10**-10

        kScale = sqrt((2 * me / hbar**2) *
                      (E3Energy - self.E0) * 1.602 * 10**-19) * 10**-10
        #        eScale = kScale**2 * 10**20 / (1.602*10**-19 * (2*me/hbar**2)) + self.E0
        kCenter = kScale[0] + dk / 2
        while kCenter + dk / 2 < kScale[-1]:
            reg = where(
                logical_and(kScale > kCenter - dE / 2,
                            kScale < kCenter + dE / 2))
            #            print("reg", len(reg), reg)
            #            if reg==[]:
            #                eCenter = eCenter + dE
            if len(reg[0]) == 1:
                valE = E3Energy[reg][0]
                val = E3Mju[reg][0]
                newE.append(valE)
                newMju.append(val)
            if len(reg[0]) > 1:
                valE = E3Energy[reg].sum() / len(E3Energy[reg])
                val = E3Mju[reg].sum() / len(E3Mju[reg])
                newE.append(valE)
                newMju.append(val)
            kCenter = kCenter + dk

        newE = asarray(newE)
        newMju = asarray(newMju)

        kExafsMin = sqrt((2 * me / hbar**2) *
                         (self.E0 + 50 - self.E0) * 1.602 * 10**-19) * 10**-10
        kExafsMax = sqrt((2 * me / hbar**2) *
                         (self.E3 - self.E0) * 1.602 * 10**-19) * 10**-10

        kScale = arange(kExafsMin, kExafsMax, dk, dtype='float64')
        eScale = kScale**2 * 10**20 / (1.602 * 10**-19 *
                                       (2 * me / hbar**2)) + self.E0

        a1 = arange(self.energy[0], self.E1, self.dE1, dtype='float64')
        a2 = arange(self.E1, self.E0 + 50, self.dE2, dtype='float64')
        self.energyRebined = concatenate((a1, a2, eScale.astype('float64')))

        if rebinType == 'spline':
            uniqueEnergy, uniqueIndexes = unique(newE, return_index=True)

            newE = newE[uniqueIndexes]
            newMju = newMju[uniqueIndexes]

            newE = newE[~isnan(newE)]
            newMju = newMju[~isnan(newMju)]

            spl = InterpolatedUnivariateSpline(newE, newMju, k=1)

        if rebinType == 'rbf-smooth':
            spl = Rbf(
                newE,
                newMju,
                function='multiquadric',
                epsilon=3,  #epsilon 3 woks fine
                smooth=s)

        self.mjuRebined = spl(self.energyRebined)

        self.mjuRebined = self.mjuRebined[1:-2]
        self.energyRebined = self.energyRebined[1:-2]

        self.energy = copy(self.energyRebined)
        self.mju = copy(self.mjuRebined)
        self.mjuDerivative = gradient(self.mju)

        self.redoExtraction()
Ejemplo n.º 22
0
def makeplot(x, y, z, this_slice, this_bestval, xlabel, ylabel, zlabel, title,
             pngoutfile):
    fig = plt.figure(figsize=(7.5, 6))
    ax = plt.gca()
    sliceindexes = np.where(this_slice == this_bestval)
    slicex = x[sliceindexes]
    slicey = y[sliceindexes]
    slicez = z[sliceindexes]
    slicex = np.array(slicex)
    slicey = np.array(slicey)
    slicez = np.array(slicez)

    if len(slicez) > 3:
        # Set up a regular grid of interpolation points
        xi, yi = np.linspace(slicex.min(), slicex.max(),
                             60), np.linspace(slicey.min(), slicey.max(), 60)
        xi, yi = np.meshgrid(xi, yi)
        # Interpolate using Rbf
        rbf = Rbf(slicex, slicey, slicez, function='cubic')
        zi = rbf(xi, yi)

        q = [0.999]
        vmax = np.quantile(slicez, q)
        zi[zi > vmax] = vmax

        # replace nan with vmax (using workaround)
        val = -99999.9
        zi[zi == 0.0] = val
        zi = np.nan_to_num(zi)
        zi[zi == 0] = vmax
        zi[zi == val] = 0.0

        # plot
        pl2 = plt.imshow(
            zi,
            vmin=slicez.min(),
            vmax=slicez.max(),
            origin='lower',
            extent=[slicex.min(),
                    slicex.max(),
                    slicey.min(),
                    slicey.max()],
            aspect='auto',
            cmap=cmap)
        ax.set_xlabel(xlabel, fontsize=18)
        ax.set_ylabel(ylabel, fontsize=18)
        clb = fig.colorbar(pl2)
        clb.set_label(label=zlabel, size=16)
        clb.ax.tick_params(labelsize=18)
    #####################################

    fig.subplots_adjust(left=0.13,
                        bottom=0.12,
                        right=0.93,
                        top=0.94,
                        wspace=0,
                        hspace=0)
    fig = gcf()

    fig.suptitle(title, fontsize=18, y=0.99)

    ax.tick_params(axis='both', which='major', labelsize=16)
    ax.tick_params(axis='both', which='minor', labelsize=16)

    fig.savefig(pngoutfile, bbox_inches='tight')
    plt.close()
Ejemplo n.º 23
0
def draw_function():
    if markclick == 0:  # mclick 如果不存在,则鼠标未点击色阶级数,则给mark1和mnum赋初始值
        mark1 = 0
        mnum = None
    else:
        mark1 = mark1_1
        mnum = mnum_1
    if btn_legendmin.get() == '默认':
        mark2 = 0
        mmin = None
    else:
        mark2 = 1
        mmin = float(btn_legendmin.get())
    if btn_legendmax.get() == '默认':
        mark3 = 0
        mmax = None
    else:
        mark3 = 1
        mmax = float(btn_legendmax.get())
        if mmax <= mmin:
            tm.showwarning('警告', '图例最大值应大于最小值!')

    # ------------警告提示框---------
    if (mark1 == 0 or mark1 == 1) and mark2 == 0 and mark3 == 0:
        pass
    elif color_mark == 6:
        pass
    elif mark1 == 0 and (mark2 == 1 or mark3 == 1):
        tm.showwarning('警告', '色阶级数默认情况下,图例最小值和图例最大值均应为“默认”。')
        return
    elif mark1 == 1 and mark2 == 1 and mark3 == 1:
        pass
    else:
        tm.showinfo('提示', '色阶级数非默认情况下,需同时自定义设置图例最小值和图例最大值;否则图例最大值和最小值以默认值绘出。')
        pass

    path0 = 'DTool/dishi.shp'
    file = shapefile.Reader(path0)
    rec = file.shapeRecords()
    polygon = list()
    for r in rec:
        polygon.append(Polygon(r.shape.points))
    poly = cascaded_union(polygon)  # 并集
    ext = list(poly.exterior.coords)  # 外部点
    codes = [Path.MOVETO] + [Path.LINETO] * (len(ext) - 1) + [Path.CLOSEPOLY]
    #    codes += [Path.CLOSEPOLY]
    ext.append(ext[0])  # 起始点
    path = Path(np.array(ext), codes)
    patch = PathPatch(path, facecolor='None')

    x, y = df['经度'], df['纬度']
    xi = np.arange(113, 118.5, 0.01)
    yi = np.arange(24, 31, 0.01)
    olon, olat = np.meshgrid(xi, yi)

    # Rbf空间插值
    func = Rbf(x, y, z, function='linear')
    oz = func(olon, olat)

    # 克里金插值
    #ok = OrdinaryKriging(x, y, z, variogram_model='linear')
    #oz, ss = ok.execute('grid', xi, yi)

    ax = plt.axes(projection=ccrs.PlateCarree())
    box = [113.4, 118.7, 24.1, 30.4]
    ax.set_extent(box, crs=ccrs.PlateCarree())
    ax.add_patch(patch)
    shp = list(shpreader.Reader(path0).geometries())
    ax.add_geometries(shp,
                      ccrs.PlateCarree(),
                      edgecolor='black',
                      facecolor='none',
                      alpha=0.3,
                      linewidth=0.5)  # 加底图
    if mark1 == 1 and mark2 == 1 and mark3 == 1 and btn_style.get(
    ) != 'CMA_Rain':
        v = np.linspace(mmin, mmax, num=mnum, endpoint=True)  # 设置显示数值范围和级数
        if color_mark == 1:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.jet)
        elif color_mark == 2:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.rainbow)
        elif color_mark == 3:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.OrRd)
    elif btn_style.get() == 'CMA_Rain':
        # 应加入超出levels的提示
        try:
            rain_levels = [0, 0.1, 10, 25, 50, 100, 250, 2500]
            rain_colors = [
                '#FFFFFF', '#A6F28F', '#38A800', '#61B8FF', '#0000FF',
                '#FA00FA', '#730000', '#400000'
            ]
            pic = plt.contourf(olon,
                               olat,
                               oz,
                               levels=rain_levels,
                               colors=rain_colors)
            # cbar = plt.colorbar(pic, ticks=[0, 0.1, 10, 25, 50, 100, 250])
            # position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
            # plt.colorbar(pic, ticks=[0, 0.1, 10, 25, 50, 100, 250], cax=position, orientation='vertical')
            # cbar.set_label(btn9.get(), fontproperties='SimHei')  # 图例label在右边
        except:
            if np.min(z) < 0:
                tm.showinfo(message='存在负数,超出降水图例范围!请换其他颜色样式。')
            elif np.max(z) > 2500:
                tm.showinfo(message='降水量过大,请何查数据!或请换其他颜色样式。')
                # cbar.make_axes(locations='top')
        # cbar.ax.set_xlabel(btn9.get(),fontproperties='SimHei')
    else:
        if color_mark == 1:
            if mark1 == 0:  # 未设置级数
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.jet)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)  # 设置显示数值范围和级数
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.jet)
        elif color_mark == 2:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.rainbow)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.rainbow)
        elif color_mark == 3:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.gist_rainbow)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.gist_rainbow)
        elif color_mark == 4:
            if mark1 == 0:
                pic = plt.contourf(olon, olat, oz, cmap=plt.cm.OrRd)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=mnum,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, cmap=plt.cm.OrRd)
        elif color_mark == 6:  # 自定义颜色
            # 判断出自定义颜色级数
            jishu_colors = []
            for index, r in enumerate(rgb):
                if r == None:
                    num_color = index
                    break
                else:
                    jishu_colors.append(r)

            if mark2 == 1 and mark3 == 1:  # mark2最小值;mark3最大值
                v = np.linspace(mmin, mmax, num=num_color + 1,
                                endpoint=True)  # 设置显示数值范围和级数
                pic = plt.contourf(olon, olat, oz, v, colors=jishu_colors)
            else:
                v = np.linspace(np.min(oz),
                                np.max(oz),
                                num=len(jishu_colors) + 1,
                                endpoint=True)
                pic = plt.contourf(olon, olat, oz, v, colors=jishu_colors)

    for collection in pic.collections:
        collection.set_clip_path(patch)  # 设置显示区域

    plt.scatter(x, y, marker='.', c='k', s=10)  # 绘制站点

    # 添加显示站名、数值等标签
    for i in range(len(z)):
        plt.text(x[i], y[i] + 0.05, df['站名'][i], size=5.5, weight=2, wrap=True)
    # 添加单位标注
    plt.text(117.75, 27.1, btn_legendunit.get(), size=8, weight=2)
    fig = plt.gcf()
    fig.set_size_inches(6, 4)  # 设置图片大小
    plt.axis('off')  # 去除四边框框
    if btn_style.get() == 'CMA_Rain':
        position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
        plt.colorbar(pic,
                     ticks=[0, 0.1, 10, 25, 50, 100, 250],
                     cax=position,
                     orientation='vertical')
    else:
        position = fig.add_axes([0.65, 0.15, 0.03, 0.3])  # 位置
        plt.colorbar(pic, cax=position, orientation='vertical')

    plt.savefig('pics_dpi100.png', dpi=100, bbox_inches='tight')
    plt.savefig('pics_dpi300.png', dpi=300, bbox_inches='tight')
    plt.close()
    wifi_img = Image.open('pics_dpi100.png')
    img = ImageTk.PhotoImage(wifi_img)
    window.img = img  # to prevent the image garbage collected.
    canvas.create_image(200, 180, anchor='center', image=img)  # 设置生成的图片位置
Ejemplo n.º 24
0
    print "Calculated values for {0} files.".format(len(alts))

alts = np.array(alts)
azs = np.array(azs)
powers = np.array(powers)

import matplotlib.pyplot as plt

print "Plotting..."

x = azs
y = alts
z = powers
xi, yi = np.linspace(0, 360, 360), np.linspace(0, 90, 90)
xi, yi = np.meshgrid(xi, yi)
rbf = Rbf(x, y, z, function='linear')

#zi = griddata((x,y), z, (xi,yi), method='cubic')

zi = rbf(xi, yi)

plt.imshow(zi,
           vmin=z.min(),
           vmax=z.max(),
           origin='lower',
           extent=[0, 360, 0, 90],
           aspect='auto')
plt.colorbar()
plt.scatter(x, y, c=z)
plt.title("Total Power from {0} to {1} MHz".format(fmin, fmax))
plt.xlabel("Azimuth of Observation (degrees)")
Ejemplo n.º 25
0
c_easting = [v['easting'] for v in clusters.values()]
c_northing = [v['northing'] for v in clusters.values()]
c_elevation = [v['elevation'] for v in clusters.values()]
stiffness = [v["stiffness"] for v in clusters.values()]
stress = [v["stress"] for v in clusters.values()]
strain = [v["strain"] for v in clusters.values()]

point_convex_hull = ConvexHull(xyz)

points = np.vstack([c_easting, c_northing, c_elevation]).T

cluster_convex_hull = ConvexHull(points)
stress_int = Rbf(c_easting,
                 c_northing,
                 c_elevation,
                 np.log10(stress),
                 function='inverse',
                 smooth=0.4)
strain_int = Rbf(c_easting,
                 c_northing,
                 c_elevation,
                 np.log10(strain),
                 function='inverse',
                 smooth=0.4)
stiffness_int = Rbf(c_easting,
                    c_northing,
                    c_elevation,
                    np.log10(stiffness),
                    function='inverse',
                    smooth=0.4)
Ejemplo n.º 26
0
def eeg_topoplot(topography,
                 sensorlocations,
                 plotsensors=False,
                 resolution=151,
                 contour=False,
                 masked=True,
                 plothead=True,
                 method='Rbf',
                 plothead_kwargs=None,
                 **kwargs):
    """Plot distribution to a head surface, derived from some sensor locations. 
 		The sensor locations (polar coordinates; theta, radius) are plotted on top of a scalp
 		Largely based on the plotHeadTopography and plotHeadOutline functions as part of the PyMVPA package (http://www.pymvpa.org)
 		source: https://github.com/PyMVPA/PyMVPA/blob/master/mvpa2/misc/plot/topo.py
 		
	Args:
		topography : array; a vector containing the values corresponding to each of the sensors. 
        sensorlocations : (nsensors x 2) array; first value is theta (in degree) and the second is the radius 
			The order of the sensors has to match those of the `topography` vector. 
		plotsensors: bool; If True, the sensor will be plotted on their projected coordinates. No sensor are shown otherwise. 
 		plothead: bool;  If True, a head outline is plotted. 
 		plothead_kwargs: dict; Additional keyword arguments passed to `plotHeadOutline()`. 
		resolution: int; Number of surface samples along both x and y-axis. 
 		contour: bool; If True, contour lines are shown on top of the map
 		masked: bool; If True, all surface sample extending to head outline will be masked.
 		method: 'Rbf' (default) or 'Grid'; Rbf allows for extrapolation outside the data points (scipy.interpolate.Rbf); Grid does not allow this
		**kwargs: All additional arguments will be passed to the `pylab.imshow()` instance showing the topography. 

	Returns: 
		(maps,maps_contourlines,head,sensors) 
		The corresponding matplotlib objects are returned if plotted, ie. if plothead is set to `False`, `head` will be `None`. 
		maps : The colormap that makes the actual plot, a matplotlib.image.AxesImage instance. 
 		maps_contourlines : the contourlines, a matplotlib.contour.QuadContourSet instance
 		head : the outline of the head as returned by `eeg_plotHeadOutline()`, a matplotlib.lines.Line2d instance
 		sensors : The dots marking the electrodes, a matplotlib.lines.Line2d instance. """

    from scipy.interpolate import Rbf
    if plothead_kwargs is None:
        plothead_kwargs = {}

    th = sensorlocations[:, 0]
    rd = sensorlocations[:, 1]
    # theta values assumed to be in degrees; if already in radians, comment out next line
    th = pi * th / 180.0
    y = rd * np.cos(th)
    x = rd * np.sin(th)
    r = np.max(rd)

    # size of each square
    ssh = float(r) / resolution  # half-size
    ss = ssh * 2.0  # full-size
    cx, cy = 0, 0

    # Generate a grid and interpolate using either the griddata module (w/o extrapolation) or scipy.interpolate.Rbf (with extrapolation outside data-points)
    xi = np.arange(cx - r, cx + r, ss) + ssh
    yi = np.arange(cy - r, cy + r, ss) + ssh
    xi, yi = meshgrid(xi, yi)
    if method == 'grid':
        topo = griddata(x, y, topography, xi, yi)
    else:
        rbf = Rbf(x, y, topography, epsilon=0.05)
        topo = rbf(xi, yi)

    # mask values outside the head
    if masked:
        notinhead = np.greater_equal((xi - cx)**2 + (yi - cy)**2, (1.0 * r)**2)
        topo = np.ma.masked_where(notinhead, topo)

# show topography + contourlines or only the topography
    if contour:
        maps = plt.imshow(topo,
                          origin="lower",
                          extent=(-r, r, -r, r),
                          cmap=plt.cm.jet,
                          **kwargs)
        levels = 5
        maps_contourlines = plt.contour(xi,
                                        yi,
                                        topo,
                                        levels,
                                        linewidths=2,
                                        colors='grey',
                                        **kwargs)
        # We don't need dashed contour lines to indicate negative
        # regions, so let's turn them off.
        for c in maps_contourlines.collections:
            c.set_linestyle('solid')

        #for filled & discrete contours instead of the 256-color map, use this:
        #maps_contourlines = plt.contourf(xi,yi,topo,levels,origin="lower",cmap=plt.cm.jet, extent=(-r, r, -r, r), **kwargs)

    else:
        maps = plt.imshow(topo,
                          origin="lower",
                          extent=(-r, r, -r, r),
                          **kwargs)
        maps_contourlines = None

    if plothead:
        #make head slightly smaller than r so that e.g. fp1 and fp2 are on the outline of the head
        head = eeg_plot_head_outline(scale=0.75 * r,
                                     shift=(cx / 2.0, cy / 2.0),
                                     **plothead_kwargs)
    else:
        head = None

    if plotsensors:
        # plot projected sensor locations
        sensors = plt.plot(x,
                           y,
                           c='black',
                           marker='o',
                           lw=0,
                           ms=6,
                           mew=1,
                           mec='black')
    else:
        sensors = None

    plt.axis('off')
    plt.axis('equal')
    return maps, maps_contourlines, head, sensors
Ejemplo n.º 27
0
Two interpolation methods are possible:
Radial Basis Function (used here), and Nearest Point."""
import numpy as np
from vedo import *
from scipy.interpolate import Rbf, NearestNDInterpolator as Near

mesh = Mesh(dataurl + "bunny.obj").normalize()
pts = mesh.points()

# pick a subset of 100 points where a scalar descriptor is known
ptsubset = pts[:100]

# assume the descriptor value is some function of the point coord y
x, y, z = np.split(ptsubset, 3, axis=1)
desc = 3 * sin(4 * y)

# build the interpolator to determine the scalar value
#  for the rest of mesh vertices:
itr = Rbf(x, y, z, desc)  # Radial Basis Function interpolator
#itr = Near(ptsubset, desc)       # Nearest-neighbour interpolator

# interpolate desciptor on the full set of mesh vertices
xi, yi, zi = np.split(pts, 3, axis=1)
interpolated_desc = itr(xi, yi, zi)

mesh.cmap('rainbow', interpolated_desc).addScalarBar(title='3sin(4y)')
rpts = Points(ptsubset, r=8, c='white')

show(mesh, rpts, __doc__, axes=1).close()
Ejemplo n.º 28
0
 def interp_RZ(self, var):
     x = np.average(self.xs, axis=1)
     y = np.average(self.ys, axis=1)
     d = self.vars[var]
     return Rbf(x, y, d)
Ejemplo n.º 29
0
def interp_rbf(data,
               sphere_origin,
               sphere_target,
               function='multiquadric',
               epsilon=None,
               smooth=0.1,
               norm="angle"):
    """Interpolate data on the sphere, using radial basis functions.

    Parameters
    ----------
    data : (N,) ndarray
        Function values on the unit sphere.
    sphere_origin : Sphere
        Positions of data values.
    sphere_target : Sphere
        M target positions for which to interpolate.

    function : {'multiquadric', 'inverse', 'gaussian'}
        Radial basis function.
    epsilon : float
        Radial basis function spread parameter. Defaults to approximate average
        distance between nodes.
    a good start
    smooth : float
        values greater than zero increase the smoothness of the
        approximation with 0 as pure interpolation. Default: 0.1
    norm : str
        A string indicating the function that returns the
        "distance" between two points.
        'angle' - The angle between two vectors
        'euclidean_norm' - The Euclidean distance

    Returns
    -------
    v : (M,) ndarray
        Interpolated values.

    See Also
    --------
    scipy.interpolate.Rbf

    """
    from scipy.interpolate import Rbf

    def angle(x1, x2):
        xx = np.arccos(np.clip((x1 * x2).sum(axis=0), -1, 1))
        return np.nan_to_num(xx)

    def euclidean_norm(x1, x2):
        return np.sqrt(((x1 - x2)**2).sum(axis=0))

    if norm == "angle":
        norm = angle
    elif norm == "euclidean_norm":
        w_s = "The Euclidean norm used for interpolation is inaccurate "
        w_s += "and will be deprecated in future versions. Please consider "
        w_s += "using the 'angle' norm instead"
        warnings.warn(w_s, DeprecationWarning)
        norm = euclidean_norm

    # Workaround for bug in older versions of SciPy that don't allow
    # specification of epsilon None:
    if epsilon is not None:
        kwargs = {
            'function': function,
            'epsilon': epsilon,
            'smooth': smooth,
            'norm': norm
        }
    else:
        kwargs = {'function': function, 'smooth': smooth, 'norm': norm}

    rbfi = Rbf(sphere_origin.x, sphere_origin.y, sphere_origin.z, data,
               **kwargs)
    return rbfi(sphere_target.x, sphere_target.y, sphere_target.z)
Ejemplo n.º 30
0
        # normalize
        mean = x.mean()
        std = x.std()
        x = (x - mean) / std
        timeseries.append(x)

        # compute PDF
        g, y = statsmodels_univariate_kde(x,
                                          kernel=KERNEL,
                                          clip=CLIP,
                                          bw=BW,
                                          gridsize=GRIDSIZE,
                                          cut=CUT,
                                          cumulative=CUMULATIVE)
        # interp to the same grid
        f = Rbf(g, y, function="multiquadric")
        ikde = f(GRID)
        PDFs.append(ikde)
    PDFs = np.array(PDFs)

    # --- Cluster offshore data ---

    M = GaussianMixture(3,
                        n_init=200,
                        init_params="random",
                        random_state=42,
                        verbose=0,
                        covariance_type="full")
    M.fit(of[["Hm0", "Tm01"]])
    labels = M.predict(of[["Hm0", "Tm01"]])
    C = []
Ejemplo n.º 31
0
    'function': 'thin_plate',
    }
    #############

    # get params (x) and cost (z)
    x, z = xyz.T[:,:-1], xyz.T[:,-1]

    #HACK: remove any duplicate points by adding noise
    _x = x + np.random.normal(scale=1e-8, size=x.shape)

    if len(z) > N:
        N = max(int(round(len(z)/float(N))),1)
        print("for speed, sampling {} down to {}".format(len(z),len(z)/N))
        x, _x, z = x[::N], _x[::N], z[::N]

    f = Rbf(*np.vstack((_x.T, z)), **args)
    f.__doc__ = model.__doc__
    # convert to 'model' format (i.e. takes a parameter vector)
    _model = lambda x: f(*x).tolist()
    _model.__doc__ = f.__doc__

    mz = np.argmin(z)
    print("min: {}; min@f: {}".format(z[mz], f(*x[mz])))
    mz = np.argmax(z)
    print("max: {}; max@f: {}".format(z[mz], f(*x[mz])))

#   print("TOOK: %s" % (time.time() - start))

    # plot
    #############
    # specify 2-of-N dim (with bounds) and (N-2) with values
Ejemplo n.º 32
0
    def plot_vectors_from_spd_dir(self,
                                  fields,
                                  time=None,
                                  subplot_index=(0, ),
                                  mesh=False,
                                  function='cubic',
                                  grid_delta=(0.01, 0.01),
                                  grid_buffer=0.1,
                                  **kwargs):
        """
        Extracts, grids, and creates a contour plot.
        If subplots have not been added yet, an axis will be created
        assuming that there is only going to be one plot.

        Parameters
        ----------
        fields : dict
            Dictionary of fields to use for x, y, and z data.
        time : datetime
            Time in which to slice through objects.
        mesh : boolean
            Set to True to interpolate u and v to grid and create wind barbs.
        function : string
            Defaults to cubic function for interpolation.
            See scipy.interpolate.Rbf for additional options.
        grid_delta : 1D tuple, list, or array
            x and y deltas for creating grid.
        grid_buffer : float
            Buffer to apply to grid.
        **kwargs : keyword arguments
            The keyword arguments for :func:`plt.barbs`

        Returns
        -------
        ax : matplotlib axis handle
            The matplotlib axis handle of the plot.

        """
        # Get x, y, and z data by looping through each dictionary
        # item and extracting data from appropriate time
        x = []
        y = []
        wspd = []
        wdir = []
        for ds in self._arm:
            obj = self._arm[ds]
            field = fields[ds]
            x.append(obj[field[0]].sel(time=time).values.tolist())
            y.append(obj[field[1]].sel(time=time).values.tolist())
            wspd.append(obj[field[2]].sel(time=time).values.tolist())
            wdir.append(obj[field[3]].sel(time=time).values.tolist())

        # Calculate u and v
        tempu = -np.sin(np.deg2rad(wdir)) * wspd
        tempv = -np.cos(np.deg2rad(wdir)) * wspd

        if mesh is True:
            # Create a meshgrid for gridding onto
            xs = np.arange(
                min(x) - grid_buffer,
                max(x) + grid_buffer, grid_delta[0])
            ys = np.arange(
                min(y) - grid_buffer,
                max(y) + grid_buffer, grid_delta[1])
            xi, yi = np.meshgrid(xs, ys)

            # Use scipy radial basis function to interpolate data onto grid
            rbf = Rbf(x, y, tempu, function=function)
            u = rbf(xi, yi)

            rbf = Rbf(x, y, tempv, function=function)
            v = rbf(xi, yi)
        else:
            xi = x
            yi = y
            u = tempu
            v = tempv

        self.barbs(xi, yi, u, v, **kwargs)

        return self.axes[subplot_index]