Ejemplo n.º 1
0
def rbf(x, u, x_smooth, u_smooth, variant, smooth):
    u_exp_int = int.Rbf(x, u, function=variant, smooth=smooth)
    u_smooth_int = int.Rbf(x_smooth, u_smooth, function=variant, smooth=smooth)
    xx = np.linspace(-200, -70, 1000)
    u_exp_interpolated = u_exp_int(xx)
    u_smooth_interpolated = u_smooth_int(xx)
    return u_exp_interpolated, u_smooth_interpolated, xx
Ejemplo n.º 2
0
def rbf_interp(fem_coords,exp_coords,data,interp_direc):
    import scipy.interpolate as sp
    from code_settings import pipeline_files
    pf = pipeline_files()
    interp_method = pf.interp_method 
    
    if (interp_direc == 0):
        ref_coords = fem_coords 
        int_coords = exp_coords
        interpolated_data = np.zeros(int_coords.shape)

    else:
        ref_coords = exp_coords 
        int_coords = fem_coords
        interpolated_data = np.zeros(int_coords.shape)
        

    for i in range(ref_coords.shape[0]):

        rbfi_x = sp.Rbf(ref_coords[i,:,0],ref_coords[i,:,1],ref_coords[i,:,2],data[i,:,0],method = interp_method)
        rbfi_y = sp.Rbf(ref_coords[i,:,0],ref_coords[i,:,1],ref_coords[i,:,2],data[i,:,1],method = interp_method)
        rbfi_z = sp.Rbf(ref_coords[i,:,0],ref_coords[i,:,1],ref_coords[i,:,2],data[i,:,2],method = interp_method)

        interpolated_data[i,:,0] = rbfi_x(int_coords[i,:,0],int_coords[i,:,1],int_coords[i,:,2])
        interpolated_data[i,:,1] = rbfi_y(int_coords[i,:,0],int_coords[i,:,1],int_coords[i,:,2])
        interpolated_data[i,:,2] = rbfi_z(int_coords[i,:,0],int_coords[i,:,1],int_coords[i,:,2])
        
    return interpolated_data 
Ejemplo n.º 3
0
def separatrix(points, resolution):

    points = points[:, np.arctan2(points[1, :], points[0, :]).argsort()]

    n = 10
    ext_points = np.hstack((points[:, -n:], points, points[:, :n]))

    phi = np.arctan2(ext_points[1, :], ext_points[0, :])

    phi[:n] -= 2 * np.pi
    phi[-n:] += 2 * np.pi

    sx = scinterp.Rbf(phi, ext_points[0], smooth=200.0)
    sy = scinterp.Rbf(phi, ext_points[1], smooth=200.0)

    h = 2 * np.pi / resolution
    x = np.linspace(-np.pi + h / 2, np.pi + h / 2, resolution)
    x[x > np.pi] -= 2 * np.pi
    sep = np.vstack((sx(x), sy(x))).T

    lengths = np.empty(sep.shape[0])
    lengths[0] = 0.0
    for i in range(1, sep.shape[0]):
        lengths[i] = lengths[i - 1] + np.sqrt(
            np.dot(sep[i - 1] - sep[i], sep[i - 1] - sep[i]))

    return lengths, sep
Ejemplo n.º 4
0
def get_tps_displacement(points, transformed_points, width, height):
    x_dense, y_dense = np.meshgrid(np.arange(0, width), np.arange(0, height))
    x = points[:, 0]
    y = points[:, 1]
    tx = transformed_points[:, 0]
    ty = transformed_points[:, 1]
    u_sparse = x - tx
    v_sparse = y - ty

    rbf_u = interpolate.Rbf(tx,
                            ty,
                            u_sparse,
                            smooth=0,
                            epsilon=100,
                            function='thin_plate')
    rbf_v = interpolate.Rbf(tx,
                            ty,
                            v_sparse,
                            smooth=0,
                            epsilon=100,
                            function='thin_plate')

    u_dense = np.transpose(rbf_u(x_dense, y_dense))
    v_dense = np.transpose(rbf_v(x_dense, y_dense))

    return u_dense, v_dense
Ejemplo n.º 5
0
    def interpolate(self, P1, P2):
        assert len(P1) == len(P2), 'Shapes has different number of points'
        x = [0] * len(P1)
        xs = [0] * len(P1)
        y = [0] * len(P1)
        ys = [0] * len(P1)
        for i in xrange(len(P1)):
            x[i] = P1[i][0]
            xs[i] = P2[i][0]
            y[i] = P1[i][1]
            ys[i] = P2[i][1]

        def U(r):
            res = r**2 * np.log(r**2)
            res[r == 0] = 0
            return res

        SM = 0.1

        fx = scipolate.Rbf(x, xs, function=U, smooth=SM)
        fy = scipolate.Rbf(y, ys, function=U, smooth=SM)

        cx, cy, E, affcost, L = bookenstain(P1, P2, 15)

        return fx, fy, E, float(affcost)
Ejemplo n.º 6
0
def mesh_part(files, axis=[0,1,2], columns=10, rows=10, function='cubic') :
    '''
    Generates a mesh for any part using a similiar process as the wing.
    /!\ Currently does not fit the panair expectations, work in progress.
    
    INPUT :
        files is a list of several filenames :
    files 1-2 are the 2 sides (upper/lower, left/right etc.) of the geometry.
    They will be used to interpolate the points
    files 3-4 are lines defining the edges (front/back, left/right etc.),
    they are needed for the boundaries of the wing.
        axis is a 3-element list defining the order of the axis : the first one
    is the axis of the columns, the second one is the axis of the rows.
        columns is the number of points to be created along the column axis
        rows is the number of points to be created between in every column.
    Each side of the geometry will have this number of points.
        function is the type of function to be used in the Rbf interpolation
    process. Default is 'cubic' but the right one to use really depends on
    the geometry.
    
    OUTPUT : an array of points of shape (c, n, 3), with c being the number 
    of columns, n the number of points in each column, and 3 being
    the x, y, z coordinates in that order.
    '''
    if len(axis) != 3 :
        exit('Number of axis incorrect for generation of mesh, must be 3')
    points_side1 = points_from_stl(files[0])
    points_side2 = points_from_stl(files[1])
    points_line1 = points_from_stl(files[2])
    points_line2 = points_from_stl(files[3])
    
    rbf_side1 = si.Rbf(points_side1[:,axis[0]], points_side1[:,axis[1]], 
                       points_side1[:,axis[2]], function=function, smooth=-100)
    rbf_side2 = si.Rbf(points_side2[:,axis[0]], points_side2[:,axis[1]], 
                       points_side2[:,axis[2]], function=function, smooth=-100)
    
    rbf_line1 = si.Rbf(points_line1[:,axis[0]], points_line1[:,axis[1]], 
                       function='linear')
    rbf_line2 = si.Rbf(points_line2[:,axis[0]], points_line2[:,axis[1]], 
                       function='linear')
    
    mesh_all = []
    
    points_columns = np.linspace(min(points_line1[:,axis[0]]), 
                                 max(points_line1[:,axis[0]]), columns)
    for c in points_columns :
        mesh_c = []
        line_points = [rbf_line1(c), rbf_line2(c)]
        points_rows = np.linspace(min(line_points), max(line_points), rows)
        for r in points_rows :
            mesh_c.append([c, r, rbf_side1(c, r)])
            
        points_rows = np.linspace(max(line_points), min(line_points), rows)
        for r in points_rows :
            mesh_c.append([c, r, rbf_side2(c, r)])
        
        mesh_all.append(mesh_c)
        
    return np.array(mesh_all)
Ejemplo n.º 7
0
def reject_outliers(cross_matched_table, source_uuid):
    """
        To reject matches that don't fit with rbf model
        
        :param cross_matched_table: the list of cross matches with the necessary information
        :param source_uuid: the target source uuid that's been matched

        :return:  an array that represents the relative probability of the target source matching each reference source
        """
    table_copy = copy(cross_matched_table)
    entry_of_interest = table_copy[np.where(
        cross_matched_table['tar_uuid'] == source_uuid)]
    #print('UUID :',source_uuid)
    #print('conditional :',np.where(cross_matched_table['tar_uuid']==source_uuid))
    table_copy.remove_row(
        np.where(cross_matched_table['tar_uuid'] == source_uuid)[0][0])

    matched_offset_ra = entry_of_interest['tar_ra'] - entry_of_interest[
        'ref_ra']
    matched_offset_dec = entry_of_interest['tar_dec'] - entry_of_interest[
        'ref_dec']
    matched_sep = np.sqrt(matched_offset_ra**2 + matched_offset_dec**2)
    matched_angle = np.degrees(
        np.arctan2(matched_offset_dec, matched_offset_ra))

    allowed_pos_error = np.sqrt(entry_of_interest['tar_err_ra']**2 +
                                entry_of_interest['tar_err_dec']**2 +
                                entry_of_interest['ref_err_ra']**2 +
                                entry_of_interest['ref_err_dec']**2)

    d_ra = table_copy['tar_ra'] - table_copy['ref_ra']
    d_dec = table_copy['tar_dec'] - table_copy['ref_dec']

    model_d_ra = interpolate.Rbf(table_copy['tar_ra'],
                                 table_copy['tar_dec'],
                                 d_ra,
                                 function='linear',
                                 smooth=0.032777778)
    model_d_dec = interpolate.Rbf(table_copy['tar_ra'],
                                  table_copy['tar_dec'],
                                  d_dec,
                                  function='linear',
                                  smooth=0.032777778)

    predicted_offset_ra = model_d_ra(entry_of_interest['tar_ra'],
                                     entry_of_interest['tar_dec'])
    predicted_offset_dec = model_d_dec(entry_of_interest['tar_ra'],
                                       entry_of_interest['tar_dec'])
    predicted_sep = np.sqrt(predicted_offset_ra**2 + predicted_offset_dec**2)
    predicted_angle = np.degrees(
        np.arctan2(predicted_offset_dec, predicted_offset_ra))

    if np.abs(matched_angle -
              predicted_angle) > 45. and predicted_sep < matched_sep:
        return 'reject'
    elif matched_sep > predicted_sep + allowed_pos_error:
        return 'reject'
    else:
        return 'accept'
Ejemplo n.º 8
0
def interpolation(noisy, SNR, number_of_pilot, interp):
    noisy_image = np.zeros((40000, 72, 14, 2))

    noisy_image[:, :, :, 0] = np.real(noisy)
    noisy_image[:, :, :, 1] = np.imag(noisy)

    if number_of_pilot == 48:
        idx = [14 * i for i in range(1, 72, 6)] + [4 + 14 * i for i in range(4, 72, 6)] + [7 + 14 * i for i in
                                                                                           range(1, 72, 6)] + [
                  11 + 14 * i for i in range(4, 72, 6)]
    elif number_of_pilot == 16:
        idx = [4 + 14 * i for i in range(1, 72, 9)] + [9 + 14 * i for i in range(4, 72, 9)]
    elif number_of_pilot == 24:
        idx = [14 * i for i in range(1, 72, 9)] + [6 + 14 * i for i in range(4, 72, 9)] + [11 + 14 * i for i in
                                                                                           range(1, 72, 9)]
    elif number_of_pilot == 8:
        idx = [4 + 14 * i for i in range(5, 72, 18)] + [9 + 14 * i for i in range(8, 72, 18)]
    elif number_of_pilot == 36:
        idx = [14 * i for i in range(1, 72, 6)] + [6 + 14 * i for i in range(4, 72, 6)] + [11 + 14 * i for i in
                                                                                           range(1, 72, 6)]

    r = [x // 14 for x in idx]
    c = [x % 14 for x in idx]

    interp_noisy = np.zeros((40000, 72, 14, 2))

    for i in range(len(noisy)):
        z = [noisy_image[i, j, k, 0] for j, k in zip(r, c)]
        if (interp == 'rbf'):
            f = interpolate.Rbf(np.array(r).astype(float), np.array(c).astype(float), z, function='gaussian')
            X, Y = np.meshgrid(range(72), range(14))
            z_intp = f(X, Y)
            interp_noisy[i, :, :, 0] = z_intp.T
        elif (interp == 'spline'):
            tck = interpolate.bisplrep(np.array(r).astype(float), np.array(c).astype(float), z)
            z_intp = interpolate.bisplev(range(72), range(14), tck)
            interp_noisy[i, :, :, 0] = z_intp
        z = [noisy_image[i, j, k, 1] for j, k in zip(r, c)]
        if (interp == 'rbf'):
            f = interpolate.Rbf(np.array(r).astype(float), np.array(c).astype(float), z, function='gaussian')
            X, Y = np.meshgrid(range(72), range(14))
            z_intp = f(X, Y)
            interp_noisy[i, :, :, 1] = z_intp.T
        elif (interp == 'spline'):
            tck = interpolate.bisplrep(np.array(r).astype(float), np.array(c).astype(float), z)
            z_intp = interpolate.bisplev(range(72), range(14), tck)
            interp_noisy[i, :, :, 1] = z_intp

    interp_noisy = np.concatenate((interp_noisy[:, :, :, 0], interp_noisy[:, :, :, 1]), axis=0).reshape(80000, 72, 14,
                                                                                                        1)

    return interp_noisy
Ejemplo n.º 9
0
def diffusercam_svd_xy(stack, rnk, si_mat):
    print("creating matrix")
    Ny, Nx = stack[:, :, 0].shape
    vec = lambda x: x[:].flatten()
    ymat = np.zeros((Ny*Nx, stack.shape[2]))
    for j in range(stack.shape[2]):
        ymat[:, j] = vec(stack[:, :, j])

    print("done")

    print("starting svd")
    tic = time.time()
    u, s, vt = svds(ymat, k=rnk)
    toc = time.time()
    print("SVD took {}".format(tic - toc))

    comps = u.reshape((Ny, Nx, rnk))
    weights = np.zeros((stack.shape[2], rnk))
    for i in range(stack.shape[2]):
        for j in range(rnk):
            weights[i, j] = s[j] * vt[j, i]
    xq = np.arange( -Nx // 2, Nx // 2)
    yq = np.arange(-Ny // 2, Ny // 2)
    Xq, Yq = np.meshgrid(xq, yq)
    weights_interp = np.zeros((Ny, Nx, rnk))
    print("Interpolating")
    for r in range(rnk):
        interpolant_r = interpolate.Rbf(si_mat[1, :].T, si_mat[0, :].T, weights[:, r])
        weights_interp[:, :, r] = np.rot90(interpolant_r(Xq, Yq), 2)
    return weights, weights_interp, comps, u, s, vt
Ejemplo n.º 10
0
Archivo: gps.py Proyecto: vikatelis/gym
 def reset(self):
     """ Reset Gym Env """
     self.gp = 0
     self.loss_func = 0
     self.count = 0
     # choose kernel randomly
     kernel_ind = random.randint(0, len(self.kernels) - 1)
     kernel = self.kernels[kernel_ind]
     # Generate function with Gaussian Process
     self.gp = GaussianProcessRegressor(kernel=kernel)
     # calculate current prior and interpolate between points
     z = self.gp.sample_y(self.grid, n_samples=1, random_state=None)
     #self.loss_func = interpolate.interp2d(self.grid[:,0], self.grid[:,1], z, kind='cubic')
     #self.loss_func = interpolate.RectBivariateSpline(self.grid[:,0], self.grid[:,1], z)
     self.loss_func = interpolate.Rbf(self.grid[:, 0], self.grid[:, 1], z)
     #print("2")
     # init position and shape
     self.state = self.hyper_space.sample()
     # init observation _ need steps to initialize
     self.step(np.zeros(self.ndim))
     f_ = self.gp_eval()
     # step 0.1
     self.step(0.1 * np.ones(self.ndim))
     # TODO: RETURN self ons and not self.state ??
     #  reset counter
     self.count = 0
     return self.obs
Ejemplo n.º 11
0
def kern_smooth(array_in, x, y, method='linear'):
    """

    read numpy array with nans and return interplated and extrapolated 2d array using Radial Basis Function Interpolation / Kernel Smoothing.

    Parameters
    ----------
    array_in : read numpy array with nans

    Returns
    -------
    array_out : return interplated and extrapolated 2d array

    """

    import numpy as np
    import scipy.interpolate as interpolate

    xx, yy = np.meshgrid(x, y)
    vals = ~np.isnan(array_in)

    f = interpolate.Rbf(xx[vals], yy[vals], array_in[vals])
    array_out = f(xx, yy)

    #    plt.imshow(GD1,interpolation='nearest')

    return (array_out)
Ejemplo n.º 12
0
def two_d_estimates(values, name, est_function):
	'''
	Get optimal estimator
	'''
	n_bins = 100
	binsx = np.linspace(np.min(values[1]), np.max(values[1]), n_bins+1)
	binsy = np.linspace(np.min(values[2]), np.max(values[2]), n_bins+1)
	indsx = np.digitize(values[1], binsx)
	indsy = np.digitize(values[2], binsy)
	try:
		est = np.load('theory/{0}_estimates.npy'.format(name))
	except IOError:
		print 'gotta generate the estimator array, grab some coffee...'
		est = np.zeros((3,n_bins,n_bins))
		for i in xrange(n_bins):
			for j in xrange(n_bins):
				px = values[0, np.where((indsx==(i+1)) & (indsy==(j+1)))]
				if px.size == 0:
					est[0,i,j] = np.nan
				else:
					est[0,i,j] = est_function(px)
				est[1,i,j] = (binsx[i]+binsx[i+1])/2
				est[2,i,j] = (binsy[j]+binsy[j+1])/2
		np.save('theory/{0}_estimates.npy'.format(name), est)
	z = est[0].flatten()
	x = est[1].flatten()
	y = est[2].flatten()
	plt.imshow(est[0],
			   extent=[x.min(), x.max(), y.min(), y.max()],
			   interpolation='bilinear', origin='lower', aspect='auto')
	plt.savefig('theory/estimator_raw_{0}.pdf'.format(name))
	plt.clf()
	mask = np.isnan(z)
	rbfi = ip.Rbf(x[~mask], y[~mask], z[~mask], function='linear')
	xx, yy = np.meshgrid(binsx, binsy)
	zz = rbfi(xx.flatten(), yy.flatten())
	plt.imshow(zz.reshape(xx.shape), vmin=0, vmax=1,
			   extent=[binsx.min(), binsx.max(), binsy.min(), binsy.max()],
			   interpolation='bilinear', origin='lower')
	plt.savefig('theory/estimator_{0}.pdf'.format(name))
	plt.clf()

	'''
	Get error from estimator
	'''
	bins = np.linspace(0,1, n_bins+1)
	inds = np.digitize(values[0], bins)
	err = []
	for i in xrange(n_bins):
		print i
		pc1 = values[1, np.where(inds==(i+1))].flatten()
		pc2 = values[2, np.where(inds==(i+1))].flatten()
		x_tru = values[0, np.where(inds==(i+1))].flatten()
		x_std = np.std(rbfi(pc1, pc2) - x_tru)
		x = (bins[i]+bins[i+1])/2
		err.append((x, x_std))
	err = np.array(err)
	plt.plot(err[:,0], err[:,1])
	plt.savefig('theory/error_{0}.pdf'.format(name))
	plt.clf()
Ejemplo n.º 13
0
def interp_aod(aod, plume_mask, logging_path, pp):
    '''
    Interpolate using a radial basis function.  See models
    that shows thta this is the best approach.
    '''

    good_mask = aod != -999

    # build the interpolation grid
    y = np.linspace(0, 1, aod.shape[0])
    x = np.linspace(0, 1, aod.shape[1])
    xx, yy = np.meshgrid(x, y)

    # create interpolated grid (can extend to various methods)
    rbf = interpolate.Rbf(xx[good_mask],
                          yy[good_mask],
                          aod[good_mask],
                          function='linear')
    interpolated_aod = rbf(xx, yy)

    aod[~good_mask] = interpolated_aod[~good_mask]

    if pp['plot']:
        plt.imshow(np.ma.masked_array(aod, ~plume_mask), vmin=0, vmax=2)
        plt.colorbar()
        plt.savefig(os.path.join(logging_path,
                                 'combined_aod_interpolated.png'))
        plt.close()

    return aod[plume_mask]
Ejemplo n.º 14
0
def readRama(path):

    ramas = []
    f = open(path, "r")
    for i in f.readlines():
        if i[0] != '#':
            content = i.strip().split()
            assert len(content) == 22
            ramas.append([float(i) for i in content])
    f.close()
    ramas = np.array(ramas)

    rama_cons = {}
    n_dims = 36 * 36
    res_names = [
        "A", "C", "D", "E", "F", "G", "H", "I", "K", "L", "M", "N", "P", "Q",
        "R", "S", "T", "V", "W", "Y"
    ]
    for rama_id, res_name in zip(range(2, 22), res_names):

        phis = ramas[:, 0]
        psis = ramas[:, 1]
        energy = ramas[:, rama_id]

        rbf = interpolate.Rbf(phis, psis, energy)

        rama_cons[res_name] = {}
        rama_cons[res_name]["xi"] = np.array(rbf.xi.T, dtype=np.float64)
        rama_cons[res_name]["epsilon"] = np.array([rbf.epsilon] * n_dims,
                                                  dtype=np.float64)
        rama_cons[res_name]["nodes"] = np.array(rbf.nodes, dtype=np.float64)
        rama_cons["n_dims"] = n_dims

    return rama_cons
Ejemplo n.º 15
0
    def __init__(self,
                 obsarr,
                 cosmo_params,
                 invcov,
                 fiducial_model,
                 function='multiquadric',
                 smooth=0.0,
                 weights=[0.2, 0.045, 0.1]):

        weighted_params = np.array(cosmo_params).copy()
        norm_mat = np.repeat(np.array([weights]).T,
                             axis=1,
                             repeats=cosmo_params.shape[-1])
        weighted_params /= norm_mat

        self.invcov = invcov
        self.fid = fiducial_model
        self.weights = np.array(weights)

        #         print(obsarr.shape)
        # create a list of Rbf for each independent mode
        spline_interps = [
            interpolate.Rbf(*weighted_params,
                            model,
                            function=function,
                            smooth=smooth) for model in obsarr.T
        ]

        # create a function that applies interpolator to the parameters given, for each mode
        self.interp_func = lambda params: np.array(
            [ii(*(params / self.weights)) for ii in spline_interps])
Ejemplo n.º 16
0
def rbf_interp_2d(in_x, in_t, value, uncertainty, out_x, out_t, debug=False):
    final_sig = []

    excluded_inds = np.isnan(value)
    y = value[~excluded_inds]
    x = in_x[~excluded_inds]
    t = in_t[~excluded_inds]
    err = uncertainty[~excluded_inds]

    n = 100
    inds = np.random.permutation(list(range(len(y))))
    y = y[inds[:n]]
    x = x[inds[:n]]
    t = t[inds[:n]]

    def scale(signal, basis):
        return (signal - min(basis)) / (max(basis) - min(basis))

    t_scaled = scale(t, out_t)
    out_t_scaled = scale(out_t, out_t)

    x_scaled = scale(x, [0, 1])  #out_x)
    out_x_scaled = scale(out_x, [0, 1])  #out_x)

    get_value = interpolate.Rbf(t_scaled,
                                x_scaled,
                                y,
                                function='Gaussian',
                                epsilon=.1)  #,metric='seuclidean')
    import itertools
    coords = np.array(list(itertools.product(out_t_scaled, out_x_scaled))).T
    final_sig = get_value(coords[0], coords[1])
    final_sig = final_sig.reshape((len(out_t_scaled), len(out_x_scaled)))

    return final_sig
Ejemplo n.º 17
0
def load_boat(file='bavaria_match.dat'):
    """ loads a polar diagram with y axis being TWA, x axis being TWS
      returns a scipy function object, with following usage:

      boat_speed = f(TWS, TWA)

      A TWA > 180 needs to be changed to 360-TWA.

      the file needs to be like this (default bavaria match):
      0     6 8  10   12  14  16    20
      52     5.57 6.65 7.15 7.43 7.61 7.72 7.80 1
      60     5.97 6.93 7.45 7.73 7.87 7.95 8.03
      75     6.34 7.15 7.71 8.01 8.17 8.27 8.41
      90     6.36 7.31 7.88 8.08 8.25 8.51 8.79
      110    6.23 7.17 7.80 8.16 8.52 8.77 9.10
      120    5.84 6.93 7.60 8.03 8.41 8.85 9.44
      135    5.03 6.30 7.10 7.69 8.07 8.44 9.33
      150    4.19 5.32 6.35 7.07 7.63 8.02 8.76

    """
    bavaria = np.loadtxt(file)
    boat_data = bavaria[1:, 1:]
    x = bavaria[0]  # tws
    y = np.transpose(bavaria)[0]  # twa
    # a, b, first and second are to interpolate to zero
    a = np.zeros((1, boat_data.shape[1]))
    b = np.zeros((boat_data.shape[0] + 1, 1))
    first = np.concatenate((a, boat_data))
    second = np.concatenate((b, first), axis=1)
    # print(second.shape, x.shape, y.shape)
    # return interp.interp2d(x, y, second, kind='cubic')
    mesh = np.meshgrid(x, y)
    bootf = interp.Rbf(mesh[0], mesh[1], second, function='cubic')
    return mesh, bootf, second
Ejemplo n.º 18
0
def generate_deformation_field_rbf(shape, points, max_deform=DEFORMATION_MAX, nb_bound_points=25):
    """ generate deformation field as thin plate spline  deformation
    in range +/- max_deform

    :param tuple(int,int) shape: tuple of size 2
    :param points: np.array<nb_points, 2> list of landmarks
    :param float max_deform: maximal deformation distance in any direction
    :param int nb_bound_points: number of fix boundary points
    :return: np.array<shape>
    """
    # x_point = points[:, 0]
    # y_point = points[:, 1]
    # generate random shifting
    move = (np.random.random(points.shape[0]) - 0.5) * max_deform

    # fix boundary points
    # set the boundary points
    bound = np.ones(nb_bound_points - 1)
    x_bound = np.linspace(0, shape[0] - 1, nb_bound_points)
    y_bound = np.linspace(0, shape[1] - 1, nb_bound_points)
    x_point = np.hstack((points[:, 0], 0 * bound, x_bound[:-1], (shape[0] - 1) * bound, x_bound[::-1][:-1]))
    y_point = np.hstack((points[:, 1], y_bound[:-1], (shape[1] - 1) * bound, y_bound[::-1][:-1], 0 * bound))
    # the boundary points sex as 0 shift
    move = np.hstack((move, np.zeros(4 * nb_bound_points - 4)))
    # create the interpolation function
    smooth = 0.2 * max_deform
    rbf = interpolate.Rbf(x_point, y_point, move, function='thin-plate', epsilon=1, smooth=smooth)
    # interpolate in regular grid
    x_grid, y_grid = np.mgrid[0:shape[0], 0:shape[1]].astype(np.int32)
    # FIXME: it takes to much of RAM memory, for sample image more that 8GM !
    deform = rbf(x_grid, y_grid)
    return deform
Ejemplo n.º 19
0
def interpolate_function(refgrid: np.ndarray,
                         values: np.ndarray,
                         grid: np.ndarray,
                         function: str = 'gaussian'):
    """ Interpolate some function to an external grid.

    This method assumes that the reference values are
    evaluated on the class' box grid.

    Parameters
    ----------
    refgrid : np.ndarray((n,3), dtype=float)
        Set of points where function was evaluated.
    function : np.ndarray(N, dtype=float)
        Reference function values to create interpolator.
    grid : np.ndarray((n, 3), dtype=float)
        Grid where the interpolant will be evalated.
    function : string
        Name of method for the interpolation. Options are:
        `linear`, `cubic`, `gaussian`.

    """
    # Prepare arrays for interpolator
    xs = refgrid[:, 0]
    ys = refgrid[:, 1]
    zs = refgrid[:, 2]
    interpolator = interpolate.Rbf(xs, ys, zs, values, function=function)
    # Replace values previously stored
    grid[:, 3] = interpolator(grid[:, 0], grid[:, 1], grid[:, 2])
    return grid
Ejemplo n.º 20
0
def build_interp_zack(obs_arr,
                      cosmo_params,
                      function='multiquadric',
                      smooth=0.0):
    '''Build an interpolator:
    Input:
    (1) obs_arr has dimension (Npoints, Nbin), where Npoints = # of 
        cosmological models (=100 here), 
        and Nbins is the number of bins
    (2) cosmo_params has dimension (Npoints, Nparams)
    
    Output:
    spline_interps
    
    Usage:
    spline_interps = build_interp_zack(obs_arr, cosmo_params)
    spline_interps(_nu, Omega_m, A_s)
    '''

    # create a list of Rbf for each independent mode
    spline_interps = [
        interpolate.Rbf(*cosmo_params.T,
                        model,
                        function=function,
                        smooth=smooth) for model in obs_arr.T
    ]

    # return a function that applies Rbf to the parameters given, for each mode
    return lambda params: np.array([ii(*params) for ii in spline_interps])
Ejemplo n.º 21
0
def buildInterpolator(obs_arr, cosmo_params):
    '''Build an interpolator:
    Input:
    (1) obs_arr has dimension (Npoints, Nbin), where Npoints = # of cosmological models (=100 here), and Nbins is the number of bins
    (2) cosmo_params has dimension (Npoints, Nparams), currently Nparams is hard-coded to be 3 (M_nu, Omega_m, A_s)
    
    Output:
    spline_interps
    
    Usage:
    spline_interps = buildInterpolator(obs_arr, cosmo_params)
    spline_interps(_nu, Omega_m, A_s)
    '''
    param1, param2, param3 = cosmo_params.T
    spline_interps = list()
    for ibin in range(obs_arr.shape[-1]):
        model = obs_arr[:, ibin]
        ### Zack: I am using interpolate.Rbf here, but you can also try other ones, check https://docs.scipy.org/doc/scipy/reference/interpolate.html
        iinterp = interpolate.Rbf(param1, param2, param3, model)
        spline_interps.append(iinterp)

    def interp_cosmo(params):
        '''Interpolate the powspec for certain param.
        Params: list of 3 parameters = (M_nu, Omega_m, A_s)
        '''
        mm, wm, sm = params
        gen_ps = lambda ibin: spline_interps[ibin](mm, wm, sm)
        ps_interp = array(map(gen_ps, range(obs_arr.shape[-1])))
        ps_interp = ps_interp.reshape(-1, 1).squeeze()
        return ps_interp

    return interp_cosmo
Ejemplo n.º 22
0
def interpolate_measurements(measurements, interptype='griddata'):
    pts, z = measurements_to_cartesian_points(measurements)
    gridx, gridy = make_mesh_grid(pts)
    if interptype == 'griddata':
        grid = interpolate.griddata(pts,
                                    z, (gridx, gridy),
                                    method='linear',
                                    fill_value=-3e30)
    elif interptype == 'rbf':
        ptx, pty = list(zip(*pts))
        f = interpolate.Rbf(ptx, pty, z, function='linear')
        grid = f(gridy, gridx)
    elif interptype == 'gauss':
        from sklearn.gaussian_process import GaussianProcess
        ptx, pty = list(zip(*pts))
        ptx = np.array(ptx)
        pty = np.array(pty)
        z = np.array(z)
        print(math.sqrt(np.var(z)))
        gp = GaussianProcess(regr='quadratic',
                             corr='cubic',
                             theta0=np.min(z),
                             thetaL=min(z),
                             thetaU=max(z),
                             nugget=0.05)
        gp.fit(X=np.column_stack([pty, ptx]), y=z)
        rr_cc_as_cols = np.column_stack([gridy.flatten(), gridx.flatten()])
        grid = gp.predict(rr_cc_as_cols).reshape((ncol, nrow))
    return gridx, gridy, grid
Ejemplo n.º 23
0
def transsectFromFile(Files):
    """Create 2d transsect from pnt Files"""
    X = []
    F = []
    y = []
    #appen x,y, and force data to 3 arrays
    for file in Files:
        x, f = transsectGetValues(file.data[:, 0], file.data[:, 1])
        surface = numpy.where(x >= file.surface)[0][0]
        ground = numpy.where(x >= file.ground)[0][0]

        X.append(x[surface:ground])
        F.append(f[surface:ground])
        y.append(len(y))

    # Set up a regular grid of interpolation points
    xi, yi = numpy.linspace(X.min(), X.max(),
                            len(X) / len(Files)), numpy.linspace(
                                y.min(), y.max(),
                                len(X) / len(Files))
    xi, yi = numpy.meshgrid(xi, yi)

    # Interpolate
    rbf = interpolate.Rbf(X, y, F, function='linear')
    zi = rbf(xi, yi)

    plt.imshow(zi,
               vmin=F.min(),
               vmax=F.max(),
               origin='lower',
               extent=[X.min(), X.max(), y.min(),
                       y.max()])
    plt.scatter(X, y, c=F)
    plt.colorbar()
    plt.show()
Ejemplo n.º 24
0
    def interpol(self, shpfile, att="ANN_PREC"):
        spatialref = shpfile["spatialref"]
        geomtype = shpfile["geomtype"]
        geomlist = shpfile["geomlist"]
        reclist = shpfile["reclist"]
        ref = spatialref.ExportToPrettyWkt()

        info_ = []

        for i in range(len(geomlist)):
            gmt = geomlist[i]
            rec = reclist[i]
            gmt_ = ogr.CreateGeometryFromWkt(gmt)
            x, y, p = gmt_.GetX(), gmt_.GetY(), rec[att]
            info_.append([x, y, p])

        info = np.array(info_)
        x = info[:, 0]  # x coordinate
        y = info[:, 1]  # y coordinate
        z = info[:, 2]  # precipitation
        arrx = np.linspace(
            np.min(x), np.max(x),
            100)  #create 100 points in range bewtween minx and maxx
        arry = np.linspace(np.min(y), np.max(y), 100)
        xi, yi = np.meshgrid(
            arrx, arry)  #Return coordinate matrices from coordinate vectors
        rbf = interpolate.Rbf(
            x, y, z, epsilon=2)  # radial basis function interpolator instance
        zi = rbf(xi, yi)  # interpolated values
        # zi = zi.tolist() 不好用,改变了维度2维变1维
        orgnarr = [x, y, z]
        rtrnarr = [xi, yi, zi]
        return orgnarr, rtrnarr
        """
Ejemplo n.º 25
0
    def _train_parameter_predictor(self, parameter_names):
        """Train interpolator/emulator for parameter prediction [y = f(vec{x})]."""
        training_data = [None] * (parameter_names.shape[0])
        self._training_parameter_limts = np.zeros(
            (parameter_names.shape[0] - 1, 2))

        for i, parameter_name in enumerate(parameter_names):
            if parameter_name in self.measured_param_names:
                parameter_index = self.measured_param_names[parameter_name]
                training_data_unnorm = self.measured_sample_params[:,
                                                                   parameter_index]
                parameter_limits = self.measured_param_limits[parameter_index]
            elif parameter_name in self.param_names:
                parameter_index = self.param_names[parameter_name]
                training_data_unnorm = self.sample_params[:, parameter_index]
                parameter_limits = self.param_limits[parameter_index]
            else:
                raise ValueError('Parameter name not recognised.')

            if i < (parameter_names.shape[0] - 1):
                training_data[i] = latin_hypercube.map_to_unit_cube_list(
                    np.split(training_data_unnorm, training_data_unnorm.size),
                    parameter_limits.reshape(1, -1))
                self._training_parameter_limts[i, :] = parameter_limits
            else:
                training_data[i] = training_data_unnorm

        predictor = spi.Rbf(*training_data)
        return predictor
Ejemplo n.º 26
0
def interped_fourier_transformer():
    """
    Fourier transforms the combined FID signals of different chemical sites to give a frequency (NMR) spectrum.
    This is done after having used radial basis function interpolation to remove noise and smooth out high frequency
    signals that are not resolvable.
    """
    dat, filename = pick_dat(['t', 'm'], 'RDAT')
    len2 = 2 * len(dat['m'])
    xs = np.linspace(np.min(dat['t']), np.max(dat['t']), len2)
    f = interpolate.Rbf(dat['t'],
                        dat['m'],
                        smooth=3,
                        function='gaussian',
                        epsilon=np.mean(np.diff(xs)) * 3)
    ys = f(xs)
    plt.plot(xs, -ys)
    fig_manager = plt.get_current_fig_manager()
    fig_manager.window.showMaximized()
    plt.show()
    sample_rate = round(1 / np.mean(np.diff(dat['t'])), 11)
    length = len(xs)
    fo = fftpack.fft(-ys)
    freq2 = fftpack.fftfreq(length, 1 / sample_rate)
    halfln = int(length / 2)
    plt.title('{}'.format(filename))
    fig_manager = plt.get_current_fig_manager()
    fig_manager.window.showMaximized()
    plt.plot(dat['t'], dat['m'])
    plt.show()
    plt.title('{} Fourier Transformed'.format(filename))
    plt.plot(freq2[1:halfln], abs(fo[1:halfln]))
    plt.xlim(0, 2000)
    fig_manager = plt.get_current_fig_manager()
    fig_manager.window.showMaximized()
    plt.show()
Ejemplo n.º 27
0
def rbf(local, x_data, num, name, ymax, ymin):
    name_list = [
        'X_displacement ', 'Y_displacement  ', 'pressure', 'x_strain ',
        'Y_strain', 'X_flow ', 'Y_flow', 'concentration ', "Young's modulus ",
        "Poisson's ratio", 'Permeability', 'S'
    ]
    ii = 96
    y = np.int64(local / 1000)
    x = local % 1000
    loc = np.zeros([len(x), 2], np.int64)
    loc = np.zeros([len(x), 2], np.int64)
    loc[:, 0] = np.int64(x - 1)
    loc[:, 1] = np.int64(y - 1)
    new_z = np.zeros([96, 96, 12])
    xx = np.linspace(1, ii, ii)
    yy = np.linspace(1, ii, ii)
    grid_x, grid_y = np.mgrid[1:ii:96j, 1:ii:96j]
    for i in range(12):
        func = interpolate.Rbf(x - 1, y - 1, x_data[:, i], function='linear')
        new_z[:, :, i] = func(grid_x, grid_y)
    for paraa in range(12):
        sc = plt.scatter(x,
                         y,
                         c=(x_data[:, paraa]),
                         vmin=ymin[paraa],
                         vmax=ymax[paraa])
        plt.colorbar(sc)
        plt.title('%s_%s_day_%d' % (name, name_list[paraa], num))
        plt.savefig('e\\%s_%d_%d.png' % (name, paraa, num))
        plt.clf()
    aa = cyka(loc, new_z, 12)
    return aa
Ejemplo n.º 28
0
def plot1020(values, electrodes, axis, head=True):
    ''' Interpolate and plot a color array of the scalp
    values: values to plot, excepts values between 0 and 1 (e.g. p values)
    electrodes: same size as values, the observed scalp electrodes
    axis: axis where the plot will be done
    head: plot the head reference
    returns a plot object that may be used for plt.colorbar
    the colormap used is a diverging map that changes around 0.1 in order to
    mark statistical significance
    '''
    values = np.asarray(values)
    electrodes = np.asarray(electrodes)
    assert (values.size == electrodes.size)

    all_coords = get_1020coords()
    Xe = []
    Ye = []
    Ze = []

    for ei in electrodes:
        x, y, z = all_coords[ei]
        Xe.append(x)
        Ye.append(y)
        Ze.append(z)

    Xe = np.asarray(Xe)
    Ye = np.asarray(Ye)
    Ze = np.asarray(Ze)

    gridpoints = np.linspace(-1, 1, 250)
    Xi, Yi = np.meshgrid(gridpoints, gridpoints)

    rbf = spi.Rbf(Xe, Ye, values, epsilon=0.25)  # RBF ignoring Z coordinates
    Vi = np.ma.masked_array(rbf(Xi, Yi))

    # Mask out of head values
    Vi[Xi**2 + Yi**2 > 1] = np.ma.masked

    #cmap = cm.bwr
    c = mcolors.ColorConverter().to_rgb
    cmap = make_colormap([c('red'), c('white'), 0.1, c('white'), c('blue')])
    cres = axis.pcolor(Xi, Yi, Vi, cmap=cmap, vmin=0, vmax=1)

    if head:
        # plot fake head
        circle = np.linspace(0, 2 * np.pi, 1e3)
        xhead = np.sin(circle)
        yhead = np.cos(circle)
        axis.plot(xhead, yhead, color='k', linewidth=3)

    # plot projection of electrodes on Z=0
    axis.plot(Ye, Xe, 'go')
    for i, elec in enumerate(electrodes):
        axis.text(Ye[i] + .05 * np.sign(Ye[i]), Xe[i] + .08 * np.sign(Xe[i]),
                  elec)
    axis.set_xlim((-1.1, 1.1))
    axis.set_ylim((-1.1, 1.1))

    return cres
Ejemplo n.º 29
0
def map_to_stl(x_axis, y_axis, x, y, z, output_filename=None, show_plots=True):
    '''
    This function interpolates a mesh from a set of 3D coordinates that define a 'map' (a function of two inputs `z = f(x,y)` ) using the Thin-Plate Spline method.

    A standard 3D-printable .stl (STereo-Lithography) file is saved to `output_filename` if provided.

    Args:
        x_axis, y_axis (np.array): 3D map axis
        x, y, z (np.array): Coordinates of points in 3-dimensional space. Coordinates can fall anywhere within the min/max of the provided axes.
        output_filename (str): Name of .stl file
        show_plots (bool): Turn on/off plotting

    '''

    assert np.logical_and(x > min(x_axis), x < max(
        x_axis)).all(), " Coordinates must lie within the axes bounds"
    assert np.logical_and(y > min(y_axis), y < max(
        y_axis)).all(), " Coordinates must lie within the axes bounds"

    # Interpolate between points using the thin plate spline algorithm.
    interp = interpolate.Rbf(x, y, z, function='thin_plate')
    xi, yi = np.meshgrid(x_axis, y_axis)
    zi = interp(xi, yi)

    # Plot the initial interpolated surface.
    fig = plt.figure()
    ax_interp = axes3d.Axes3D(fig)
    ax_interp.plot_surface(xi, yi, zi, alpha=0.5)
    ax_interp.scatter3D(x, y, z, c="red")
    if show_plots:
        plt.show()

    # Begin mesh creation by triangulating the parameter space.
    points = np.array([xi.flatten(), yi.flatten()]).T
    tri = Delaunay(points)

    # Plot the triangulated surface.
    fig = plt.figure()
    ax_trisurf = axes3d.Axes3D(fig)
    ax_trisurf.plot_trisurf(xi.flatten(), yi.flatten(), zi.flatten(),
                            triangles=tri.simplices, cmap=plt.cm.Spectral)
    if show_plots:
        plt.show()

    # Save result to a .stl file.
    if output_filename:

        vertices = points = np.array(
            [xi.flatten(), yi.flatten(), zi.flatten()]).T
        faces = tri.simplices

        map_mesh = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype), )
        for i, f in enumerate(faces):
            for j in range(3):
                map_mesh.vectors[i][j] = vertices[f[j], :]

        map_mesh.save(output_filename)

    return ax_interp, ax_trisurf,
Ejemplo n.º 30
0
    def populateMissingData(self,approach="Smooth",ilog=None):
        '''
        This function is used to interpolate missing data in the image.
        '''
        if approach == 'Smooth':
            # first run a median filter over the array, then smooth the result.
            xmin,xmax,ymin,ymax,zmin,zmax = self.getRange()
            mask = np.array(self.flags,dtype=np.bool)
            
            z = self.getZImage().asMatrix2D()
            median = nd.median_filter(z,size=(15,15))

            mask = mask.flatten()
            z = z.flatten()
            median = median.flatten()

            z[ mask==False ] = median[ mask==False ]
            
            if ilog != None:
                ilog.log(pv.Image(median.reshape(self.width,self.height)),label="Median")
                ilog.log(pv.Image(z.reshape(self.width,self.height)),label="ZMedian")
            
            mask = mask.flatten()
            z = z.flatten()
            median = median.flatten()
            
            for i in range(5):
                tmp = z.copy()
                smooth = nd.gaussian_filter(z.reshape(self.width,self.height),2.0).flatten()
                z[ mask==False ] = smooth[ mask==False ]
                print "Iteration:",i,(z-tmp).max(),(z-tmp).min()
                ilog.log(pv.Image(z.reshape(self.width,self.height)),label="ZSmooth%02d"%i)
                ilog.log(pv.Image((z-tmp).reshape(self.width,self.height)),label="ZSmooth%02d"%i)
                
                
        if approach == 'RBF':
            mask = np.array(self.flags,dtype=np.bool)
            mask = mask.flatten()
    
            x = np.arange(self.width).reshape(self.width,1)
            x = x*np.ones((1,self.height))
            x = x.flatten()
    
            y = np.arange(self.height).reshape(1,self.height)
            y = y*np.ones((self.width,1))
            y = y.flatten()
            
            z = self.z.copy()
            z = z.flatten()
            
            print "Coords:"
            print len(mask)
            print len(x[mask])
            print len(y[mask])
            print len(z[mask])
            
            # this produces an error.  Probably has too much data
            it.Rbf(x[mask],y[mask],z[mask])
            pass