예제 #1
0
def bilateralSmoothing(image):
    sigma_xy = 0.1
    xy = positionFeatures(image) / sigma_xy
    Lab = LabFeatures(image)
    foreground = foreGroundFeatures(image)

    Labxy = np.concatenate((Lab, xy), axis=1)[foreground, :]
    sigma_L = 1.0
    Labxy[:, 0] = Labxy[:, 0] / sigma_L
    Labxy_sparse = shuffle(Labxy, random_state=0)[:1000]

    Lab_smooth = np.array(Lab)

    smooth = 0.0

    L_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                sigma_L * Labxy_sparse[:, 0],
                function='linear',
                smooth=smooth)

    a_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                Labxy_sparse[:, 1],
                function='linear',
                smooth=smooth)
    b_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                Labxy_sparse[:, 2],
                function='linear',
                smooth=smooth)

    #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4])
    Lab_smooth[foreground, 0] = L_rbf(*(Labxy.T))
    Lab_smooth[foreground, 1] = a_rbf(*(Labxy.T))
    Lab_smooth[foreground, 2] = b_rbf(*(Labxy.T))

    h, w = image.shape[:2]
    Lab_smooth = Lab_smooth.reshape((h, w, 3))

    rgb_smooth = Lab2rgb(np.float32(Lab_smooth))
    rgb_smooth_8U = to8U(rgb_smooth)
    rgb_smooth_8U = setAlpha(rgb_smooth_8U, alpha(image))
    return rgb_smooth_8U
    def fit_border_through_pixels(self, edge=None):
        """
        :param edge: if provided, given set of points is smoothed (implies that the points are sorted in some way).
        Otherwise one of the attributes is picked, based on the value of self.is_endo:
        True -> lv_endo_sorted_edge
        False -> lv_epi_sorted_edge
        :return: list of points of the smooth contour, with resolution controlled by smoothing_resolution
        """
        _smoothing_resolution = self.smoothing_resolution
        if edge is not None:
            border = edge
        elif self.is_endo:
            border = self.endo_sorted_edge
        else:
            border = self.epi_sorted_edge
            _smoothing_resolution *= 5

        print('Interpolating')

        x_orig = np.array([x[0] for x in border])
        y_orig = np.array([y[1] for y in border])

        positions = np.arange(len(
            border))  # strictly monotonic, number of points in single trace
        rbf_x = Rbf(positions,
                    x_orig,
                    smooth=np.power(10, 5),
                    function='quintic')  # Heavy smoothing due to pixel effect
        rbf_y = Rbf(positions,
                    y_orig,
                    smooth=np.power(10, 5),
                    function='quintic')  # Heavy smoothing due to pixel effect

        # Interpolate based on the RBF model
        interpolation_target_n = np.linspace(0,
                                             len(border) - 1,
                                             _smoothing_resolution)
        x_interpolated = rbf_x(interpolation_target_n)
        y_interpolated = rbf_y(interpolation_target_n)

        if self.plot_smoothing_results:
            # Plot if you want to see the results
            plt.plot(x_orig, y_orig, '.-')
            plt.plot(x_interpolated[::2], y_interpolated[1::2], 'r')
            plt.show()

        # Return interpolated trace
        fitted_points = [
            (point_x, point_y)
            for point_x, point_y in zip(x_interpolated, y_interpolated)
        ]

        return fitted_points, border
예제 #3
0
    def _illuminationFromColorDifference(self, Cs):
        I0s = np.average(Cs, axis=1)

        I0_ids = self._I_ids(I0s)

        C_map = np.zeros((self._map_size, Cs.shape[1]))
        hist = np.zeros((self._map_size))
        C_map[I0_ids, :] += Cs[:, :]
        hist[I0_ids] += 1.0

        hist_positive = hist > 0

        for ci in xrange(3):
            C_map[hist_positive, ci] *= 1.0 / hist[hist_positive]

        I_map = np.zeros((self._map_size))

        I_min, I_max = self._Iminmax

        Ims = np.linspace(I_min, I_max, num=self._map_size)

        for ci in xrange(3):
            C_map[:, ci] = Rbf(Ims[hist_positive],
                               C_map[hist_positive, ci],
                               smooth=0.0005)(Ims)

        sigma = 0.02
        for mi in xrange(self._map_size):
            c = C_map[mi]
            dc = normVectors(self._map - c)

            dc_i = np.argmin(dc)
            wc = np.exp(-(dc**2) / (sigma**2))

            I = np.dot(wc, Ims) / np.sum(wc)

            #I_map[mi] = I_min + (I_max - I_min) * dc_i / float(self._map_size - 1)
            I_map[mi] = I

        Im_max = 0.0
        for mi in xrange(self._map_size):
            Im_max = max(I_map[mi], Im_max)
            I_map[mi] = Im_max

        I_map = Rbf(Ims, I_map, smooth=0.0005)(Ims)

        #         I_map[np.max(I0_ids):] = I_max

        return I_map[I0_ids]
예제 #4
0
def scipy_fun_plot(n, x, y, xs, epsilon_arr, img_name):

    # draw plot here
    fig = pl.figure(figsize=(20, 5))

    draw = fig.add_subplot(111)
    draw.set_title("Radial Basis Function Interpolation")
    draw.set_ylim([-2, 4])
    draw.set_xlim([-0.5, 20.5])
    draw.plot(x, y, 'bo', label="data")
    draw.plot(x, y, 'r', label="linear")

    plot_lines = ['orange', 'gray', 'brown', 'black']
    function_name = 'gaussian'

    for indx in range(0, 4, 1):
        rbf = Rbf(x, y, function=function_name, epsilon=epsilon_arr[indx])
        bim = rbf(xs)

        draw.plot(xs,
                  bim,
                  plot_lines[indx],
                  linewidth=1.3,
                  label=r"$sigma$=" + str(indx))

    draw.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                loc=3,
                ncol=6,
                mode="expand",
                borderaxespad=0.0)
    draw.plot()

    pl.savefig(img_name, bbox_inches='tight')
    pl.show()
예제 #5
0
def test_default_construction():
    """Check that the Rbf class can be constructed with the default
    multiquadric basis function. Regression test for ticket #1228."""
    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y)
    yi = rbf(x)
    assert_array_almost_equal(y, yi)
예제 #6
0
def test_function_is_callable():
    """Check that the Rbf class can be constructed with function=callable."""
    x = linspace(0, 10, 9)
    y = sin(x)
    linfunc = lambda x: x
    rbf = Rbf(x, y, function=linfunc)
    yi = rbf(x)
    assert_array_almost_equal(y, yi)
예제 #7
0
def check_rbf1d_interpolation(function):
    # Check that the Rbf function interpolates through the nodes (1D)
    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y, function=function)
    yi = rbf(x)
    assert_array_almost_equal(y, yi)
    assert_almost_equal(rbf(float(x[0])), y[0])
예제 #8
0
def test_rbf_epsilon_none_collinear():
    # Check that collinear points in one dimension doesn't cause an error
    # due to epsilon = 0
    x = [1, 2, 3]
    y = [4, 4, 4]
    z = [5, 6, 7]
    rbf = Rbf(x, y, z, epsilon=None)
    assert_(rbf.epsilon > 0)
예제 #9
0
def check_rbf2d_interpolation(function):
    # Check that the Rbf function interpolates through the nodes (2D).
    x = random.rand(50, 1) * 4 - 2
    y = random.rand(50, 1) * 4 - 2
    z = x * exp(-x**2 - 1j * y**2)
    rbf = Rbf(x, y, z, epsilon=2, function=function)
    zi = rbf(x, y)
    zi.shape = x.shape
    assert_array_almost_equal(z, zi)
예제 #10
0
def check_rbf1d_regularity(function, atol):
    # Check that the Rbf function approximates a smooth function well away
    # from the nodes.
    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y, function=function)
    xi = linspace(0, 10, 100)
    yi = rbf(xi)
    msg = "abs-diff: %f" % abs(yi - sin(xi)).max()
    assert_(allclose(yi, sin(xi), atol=atol), msg)
예제 #11
0
def check_rbf3d_interpolation(function):
    # Check that the Rbf function interpolates through the nodes (3D).
    x = random.rand(50, 1) * 4 - 2
    y = random.rand(50, 1) * 4 - 2
    z = random.rand(50, 1) * 4 - 2
    d = x * exp(-x**2 - y**2)
    rbf = Rbf(x, y, z, d, epsilon=2, function=function)
    di = rbf(x, y, z)
    di.shape = x.shape
    assert_array_almost_equal(di, d)
예제 #12
0
def check_2drbf1d_interpolation(function):
    # Check that the 2-dim Rbf function interpolates through the nodes (1D)
    x = linspace(0, 10, 9)
    y0 = sin(x)
    y1 = cos(x)
    y = np.vstack([y0, y1]).T
    rbf = Rbf(x, y, function=function, mode='N-D')
    yi = rbf(x)
    assert_array_almost_equal(y, yi)
    assert_almost_equal(rbf(float(x[0])), y[0])
예제 #13
0
def check_2drbf2d_interpolation(function):
    # Check that the 2-dim Rbf function interpolates through the nodes (2D).
    x = random.rand(50, ) * 4 - 2
    y = random.rand(50, ) * 4 - 2
    z0 = x * exp(-x**2 - 1j * y**2)
    z1 = y * exp(-y**2 - 1j * x**2)
    z = np.vstack([z0, z1]).T
    rbf = Rbf(x, y, z, epsilon=2, function=function, mode='N-D')
    zi = rbf(x, y)
    zi.shape = z.shape
    assert_array_almost_equal(z, zi)
예제 #14
0
def test_two_arg_function_is_callable():
    # Check that the Rbf class can be constructed with a two argument
    # function=callable.
    def _func(self, r):
        return self.epsilon + r

    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y, function=_func)
    yi = rbf(x)
    assert_array_almost_equal(y, yi)
예제 #15
0
def check_2drbf3d_interpolation(function):
    # Check that the 2-dim Rbf function interpolates through the nodes (3D).
    x = random.rand(50, ) * 4 - 2
    y = random.rand(50, ) * 4 - 2
    z = random.rand(50, ) * 4 - 2
    d0 = x * exp(-x**2 - y**2)
    d1 = y * exp(-y**2 - x**2)
    d = np.vstack([d0, d1]).T
    rbf = Rbf(x, y, z, d, epsilon=2, function=function, mode='N-D')
    di = rbf(x, y, z)
    di.shape = d.shape
    assert_array_almost_equal(di, d)
예제 #16
0
    def _rbf(self, Is, Cs, smooth=0.00005):
        rbf_list = []
        for ci in xrange(3):
            rbf_list.append(Rbf(Is, Cs[:, ci], smooth=smooth))

        def f(Is_new):
            Cs_new = np.zeros((len(Is_new), Cs.shape[1]))
            for ci in xrange(3):
                Cs_new[:, ci] = rbf_list[ci](Is_new)
            return Cs_new

        return f
예제 #17
0
def check_rbf1d_interpolation(function):
    """Check that the Rbf function interpolates throught the nodes (1D)"""
    olderr = np.seterr(all="ignore")
    try:
        x = linspace(0, 10, 9)
        y = sin(x)
        rbf = Rbf(x, y, function=function)
        yi = rbf(x)
        assert_array_almost_equal(y, yi)
        assert_almost_equal(rbf(float(x[0])), y[0])
    finally:
        np.seterr(**olderr)
예제 #18
0
def check_2drbf1d_regularity(function, atol):
    # Check that the 2-dim Rbf function approximates a smooth function well away
    # from the nodes.
    x = linspace(0, 10, 9)
    y0 = sin(x)
    y1 = cos(x)
    y = np.vstack([y0, y1]).T
    rbf = Rbf(x, y, function=function, mode='N-D')
    xi = linspace(0, 10, 100)
    yi = rbf(xi)
    msg = "abs-diff: %f" % abs(yi - np.vstack([sin(xi), cos(xi)]).T).max()
    assert_(allclose(yi, np.vstack([sin(xi), cos(xi)]).T, atol=atol), msg)
예제 #19
0
def check_rbf2d_interpolation(function):
    """Check that the Rbf function interpolates throught the nodes (2D)"""
    olderr = np.seterr(all="ignore")
    try:
        x = random.rand(50, 1) * 4 - 2
        y = random.rand(50, 1) * 4 - 2
        z = x * exp(-x**2 - 1j * y**2)
        rbf = Rbf(x, y, z, epsilon=2, function=function)
        zi = rbf(x, y)
        zi.shape = x.shape
        assert_array_almost_equal(z, zi)
    finally:
        np.seterr(**olderr)
def bilateralNormalSmoothing(image, normal):
    sigma_xy = 1.0
    xy = positionFeatures(image) / sigma_xy
    Lab = LabFeatures(image)
    foreground = foreGroundFeatures(image)

    N = normal[:, :, :3].reshape(-1, 3)

    LabxyN = np.concatenate((Lab, xy, N), axis=1)[foreground, :]
    sigma_L = 1.0
    LabxyN[:, 0] = LabxyN[:, 0] / sigma_L
    LabxyN_sparse = shuffle(LabxyN, random_state=0)[:100]

    N_smooth = np.array(N)

    smooth = 10.0

    f_x = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 5]))
    f_y = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 6]))
    f_z = np.vstack((LabxyN_sparse[:, :5].T, LabxyN_sparse[:, 7]))

    Nx_rbf = Rbf(*(f_x), function='linear', smooth=smooth)
    Ny_rbf = Rbf(*(f_y), function='linear', smooth=smooth)
    Nz_rbf = Rbf(*(f_z), function='linear', smooth=smooth)

    Labxy = LabxyN[:, :5]

    #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4])
    N_smooth[foreground, 0] = Nx_rbf(*(Labxy.T))
    N_smooth[foreground, 1] = Ny_rbf(*(Labxy.T))
    N_smooth[foreground, 2] = Nz_rbf(*(Labxy.T))

    h, w = image.shape[:2]
    N_smooth = N_smooth.reshape((h, w, 3))

    N_smooth = normalizeImage(N_smooth)
    return N_smooth
예제 #21
0
def check_rbf1d_regularity(function, atol):
    """Check that the Rbf function approximates a smooth function well away
    from the nodes."""
    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y, function=function)
    xi = linspace(0, 10, 100)
    yi = rbf(xi)
    #import matplotlib.pyplot as plt
    #plt.figure()
    #plt.plot(x, y, 'o', xi, sin(xi), ':', xi, yi, '-')
    #plt.title(function)
    #plt.show()
    msg = "abs-diff: %f" % abs(yi - sin(xi)).max()
    assert allclose(yi, sin(xi), atol=atol), msg
예제 #22
0
def check_rbf1d_stability(function):
    # Check that the Rbf function with default epsilon is not subject
    # to overshoot.  Regression for issue #4523.
    #
    # Generate some data (fixed random seed hence deterministic)
    np.random.seed(1234)
    x = np.linspace(0, 10, 50)
    z = x + 4.0 * np.random.randn(len(x))

    rbf = Rbf(x, z, function=function)
    xi = np.linspace(0, 10, 1000)
    yi = rbf(xi)

    # subtract the linear trend and make sure there no spikes
    assert_(np.abs(yi - xi).max() / np.abs(z - x).max() < 1.1)
예제 #23
0
def main_compress():
    x = np.linspace(1, 32, (32 * 5) / 4)
    y = np.random.rand((32 * 5) / 4)
    #y = np.sin(x)
    tck = splrep(x, y, k=3)
    rbf_adj = Rbf(x, y, function='gaussian')
    x2 = np.linspace(2, 33, 32)
    y2 = splev(x2, tck)
    y2_rbf = rbf_adj(x2)
    print(y2)
    print(y2_rbf)
    # y2 has shape 32

    plt.subplot(311)
    plt.plot(x, y)
    plt.subplot(312)
    #plt.plot(x2, y[8:8+16], 'r-', x2, y2, 'g--')
    plt.plot(x2, y2, 'go-')
    #plt.plot(x2,y2)
    plt.subplot(313)
    plt.plot(x2, y2_rbf, 'ro-')
    plt.show()
예제 #24
0
def local_implementation(sigma, img_name):
    # -----------------------------------------------------------------------------
    # local implementation
    fig2 = pl.figure(figsize=(20, 5))

    draw = fig2.add_subplot(111)
    draw.set_title("Radial Basis Function Interpolation")
    draw.set_ylim([-2, 4])
    draw.set_xlim([-0.5, 20.5])
    draw.plot(x, y, 'bo', label="data", markersize=8)
    draw.plot(x, y, 'r', label="linear")

    weights = interpolation_rbf(xs, x, y, sigma)
    bimes = get_ys(xs, x, weights, sigma)
    draw.plot(xs,
              bimes,
              'orange',
              linewidth=3,
              label=r"$sigma$={}, own".format(sigma))

    rbf = Rbf(x, y, function='gaussian', epsilon=0.5)
    bim = rbf(xs)
    draw.plot(xs,
              bim,
              'black',
              linewidth=1.1,
              label=r"$sigma$={}, Scipy".format(sigma))

    draw.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                loc=3,
                ncol=6,
                mode="expand",
                borderaxespad=0.0)
    draw.plot()

    pl.savefig(img_name, bbox_inches='tight')
    pl.show()
def get_all_seq_augmentations_4(X, frame, win_size, num_augmentations=32):
    '''
  Do data augmentation on the given frame in the h5fname for a given
  win_size. This is different than `get_all_seq_augmentations` since it
  returns fixed number of augmentations for all window sizes.
  K:number of augmentations in total. This uses a different algorithm to
  generate augmentations i.e. it involves both compression and expansion
  of given signal.
  '''
    K = num_augmentations
    speed_up = [1.125, 1.25, 1.375]
    all_win_sizes = [int(round(win_size * x)) for x in speed_up]
    points_per_win_size = {
        # 16:[2, 3, 3], 32:[2, 3, 3], 64:[2, 3, 3]
        16: [1, 1, 2],
        32: [1, 1, 2],
        64: [1, 1, 2]
    }
    org_win_size = win_size
    all_aug = np.zeros((K, X.shape[1], org_win_size))

    for feat_idx in range(X.shape[1]):
        all_aug_idx = 0
        win_idx = 0
        for win_size in all_win_sizes:
            start_f = int(frame - org_win_size / 2)
            end_f = start_f + org_win_size
            start_ext_f = int(frame - win_size / 2.0)
            end_ext_f = start_ext_f + win_size
            X_org = X[start_f:end_f, :]
            X_ext, can_compress = None, False
            if start_ext_f >= 0 and end_ext_f < X.shape[0]:
                can_compress = True
                X_ext = X[start_ext_f:end_ext_f]
            else:
                can_compress = True
                X_ext = X[start_ext_f:, :]

            # Loop through all the different feature values used from openface
            # signal
            # Loop through all window sizes (for now we just have 1 win_size)
            y = X_org[:, feat_idx]
            x = np.linspace(1, org_win_size, org_win_size)

            # expansion augmentation
            # increase interpolation scale by 2 times. This will calculate the
            # interpolating signal at the half values i.e. 1.5, 2.5 etc.
            x2 = np.linspace(1, org_win_size, win_size)

            # range(2) since we do 2 kinds of interpolation for now.
            for j in range(2):
                if j == 0:
                    # Cubic interpolation
                    tck = splrep(x, y, k=3)
                    y2 = splev(x2, tck)
                else:
                    rbf_adj = Rbf(x, y, function='gaussian')
                    y2 = rbf_adj(x2)

                if np.abs(np.mean(y) - np.mean(y2)) > 100:
                    assert (False)

                num_interpolations = points_per_win_size[org_win_size][win_idx]
                if win_size - org_win_size == num_interpolations:
                    for i in range(win_size - org_win_size):
                        all_aug[all_aug_idx,
                                feat_idx, :] = y2[i:i + org_win_size]
                        all_aug_idx = all_aug_idx + 1
                else:
                    for i in range(num_interpolations):
                        idx = np.random.randint(0, win_size - org_win_size)
                        all_aug[all_aug_idx,
                                feat_idx, :] = y2[idx:idx + org_win_size]
                        all_aug_idx = all_aug_idx + 1

            assert (can_compress)
            # compression augmentation
            if can_compress:
                y = X_ext[:, feat_idx]
                x = np.linspace(1, win_size, win_size)
                tck = splrep(x, y, k=3)
                rbf_adj = Rbf(x, y, function='gaussian')

                num_interpolations = points_per_win_size[org_win_size][win_idx]
                assert (win_size - org_win_size >= num_interpolations)
                for i in range(num_interpolations):
                    x2 = np.linspace(1 + i, win_size, org_win_size)
                    y2_cubic = splev(x2, tck)
                    # For egocentric videos the mean change is a lot greater than 100px
                    # easily. But for static camera case it is ok.
                    if np.abs(np.mean(y) - np.mean(y2_cubic)) > 500:
                        print("NOTE!! Very high mean difference during interpolation. " \
                            "Most likely some value is inf. Frame# {}".format(
                              frame))
                    all_aug[all_aug_idx, feat_idx, :] = y2_cubic
                    all_aug_idx = all_aug_idx + 1
                    y2_rbf = rbf_adj(x2)
                    if np.abs(np.mean(y) - np.mean(y2_rbf)) > 500:
                        print("NOTE!! Very high mean difference during interpolation. " \
                            "Most likely some value is inf. Frame# {}".format(
                              frame))
                    all_aug[all_aug_idx, feat_idx, :] = y2_rbf
                    all_aug_idx = all_aug_idx + 1

            win_idx = win_idx + 1

        assert (all_aug_idx == K)

    return all_aug
예제 #26
0
import os, PySide2
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
import numpy as np
import pylab as pl
from mpl_toolkits.mplot3d import Axes3D
from scipy.interpolate.rbf import Rbf

# 3 维数据点
x, y = np.mgrid[-np.pi / 2:np.pi / 2:5j, -np.pi / 2:np.pi / 2:5j]
z = np.cos(np.sqrt(x**2 + y**2))
fig = pl.figure(figsize=(12, 6))
ax = fig.gca(projection="3d")
ax.scatter(x, y, z)

# 3 维RBF插值
zz = Rbf(x, y, z)
xx, yy = np.mgrid[-np.pi / 2:np.pi / 2:50j, -np.pi / 2:np.pi / 2:50j]
fig = pl.figure(figsize=(12, 6))
ax = fig.gca(projection="3d")
ax.plot_surface(xx, yy, zz(xx, yy), rstride=1, cstride=1, cmap=pl.cm.jet)

pl.show()
예제 #27
0
import numpy as np
from scipy.interpolate.rbf import Rbf
import pyfits
import pymc

""" Interpolation functions for intrinsic magnitudes """
siess = pyfits.getdata("siess_isochrones.fits", 1)  # Siess et al. (2000)
# Interpolation is performed using linear Radial Basis Functions
siess_Mr = Rbf(siess.field("logMass"), siess.field("logAge"),
               siess.field("Mr_iphas"), function="linear")
siess_Mi = Rbf(siess.field("logMass"), siess.field("logAge"),
               siess.field("Mi_iphas"), function="linear")
siess_Mj = Rbf(siess.field("logMass"), siess.field("logAge"),
               siess.field("Mj"), function="linear")
siess_logR = Rbf(siess.field("logMass"), siess.field("logAge"),
                 siess.field("logRadius"), function="linear")

""" Functions for magnitude offsets due to emission & exctinction """
sim = pyfits.getdata("simulated_iphas_colours_barentsen2011.fits", 1)  # PaperI
# Functions for r'/Ha/i' offsets as a function of colour, extinction and EW
r_offset = Rbf(sim.field("ri_unred"), sim.field("av"), sim.field("logew"),
               sim.field("d_r"), function="linear")
ha_offset = Rbf(sim.field("ri_unred"), sim.field("av"), sim.field("logew"),
                sim.field("d_ha"), function="linear")
i_offset = Rbf(sim.field("ri_unred"), sim.field("av"), sim.field("logew"),
               sim.field("d_i"), function="linear")
# Intrinsic (r'-Ha) colour as a function of intrinsic (r'-i')
intrinsic = (sim.field("av") == 0) & (sim.field("logew") == -1)
rminHa_intrinsic = Rbf(sim.field("ri_unred")[intrinsic],
                       sim.field("rha")[intrinsic], function="linear")
percent = 0.1
minX = minX - (maxX - minX) * percent
maxX = maxX + (maxX - minX) * percent
minY = minY - (maxY - minY) * percent
maxY = maxY + (maxY - minY) * percent

#FUNCTIONS = ('multiquadric', 'inverse multiquadric', 'gaussian', 'cubic', 'quintic', 'thin-plate', 'linear')

# 2-d tests - setup scattered data
tiX = np.linspace(minX, maxX, 500)
tiY = np.linspace(minY, maxY, 500)

XI, YI = np.meshgrid(tiX, tiY)

# use RBF
rbf1 = Rbf(x1, y1, z1, epsilon=2)
rbf2 = Rbf(x2, y2, z2, epsilon=2)
ZI1 = rbf1(XI, YI)
ZI2 = rbf2(XI, YI)

#Union of A and B
ZI5 = np.array(ZI1)
for i in range(0, XI.shape[0]):
    for j in range(0, YI.shape[0]):
        if ZI1[i][j] > ZI2[i][j]:
            ZI5[i][j] = ZI2[i][j]
        else:
            ZI5[i][j] = ZI1[i][j]

#Intersection of A and B
ZI6 = np.array(ZI1)
예제 #29
0
    def makeInterpolation(self, myMethod="sbs", methodVal=None):
        # construct the interpolation grids for all coordinate systems

        # dictionaries with interpolation grids and coordinate systems
        self.interpGrids = {}
        self.interpEdges = {}
        self.interpValues = {}
        self.interpSpline = {}
        self.interpRbf = {}
        self.interpIDW = {}
        self.interpBMedian = {}
        self.interpBNentry = {}
        self.interpBMAD = {}
        self.interpTMean = {}
        self.interpTStd = {}

        # loop over Coordinate systems
        for iCoord in self.coordList:

            # build cell-centers for the interpolation grid
            ny, ylo, yhi, nx, xlo, xhi = self.gridArray[iCoord]
            yGrid, xGrid, yEdge, xEdge = self.makeGrid(ny, ylo, yhi, nx, xlo,
                                                       xhi)
            self.interpGrids[iCoord] = [xGrid, yGrid]
            self.interpEdges[iCoord] = [xEdge, yEdge]

            data = self.pointsArray[iCoord]
            if self.debugFlag:
                print("PointMesh: At ", iCoord, "we have ", data.shape[0],
                      " points")
            npts = data.shape[0]

            # check number of points
            if npts >= 5:
                xData = data[:, 0]
                yData = data[:, 1]
                zData = data[:, 2]

                if myMethod == "sbs":

                    # SmoothBivariateSpline
                    if npts > 600:
                        self.interpSpline[iCoord] = SmoothBivariateSpline(
                            xData,
                            yData,
                            zData,
                            bbox=[xlo, xhi, ylo, yhi],
                            kx=4,
                            ky=4,
                            s=1.e6)
                    elif npts >= 100:
                        self.interpSpline[iCoord] = SmoothBivariateSpline(
                            xData,
                            yData,
                            zData,
                            bbox=[xlo, xhi, ylo, yhi],
                            kx=3,
                            ky=3,
                            s=1.e6)
                    elif npts > 9:
                        self.interpSpline[iCoord] = SmoothBivariateSpline(
                            xData,
                            yData,
                            zData,
                            bbox=[xlo, xhi, ylo, yhi],
                            kx=2,
                            ky=2,
                            s=1.e6)
                    else:
                        self.interpSpline[iCoord] = SmoothBivariateSpline(
                            xData,
                            yData,
                            zData,
                            bbox=[xlo, xhi, ylo, yhi],
                            kx=1,
                            ky=1,
                            s=1.e7)
                    self.interpValues[iCoord] = self.interpSpline[iCoord].ev(
                        xGrid.reshape((ny * nx)), yGrid.reshape(
                            (ny * nx))).reshape((ny, nx))

                elif myMethod == "rbf":

                    self.interpRbf[iCoord] = Rbf(xData, yData, zData)
                    self.interpValues[iCoord] = self.interpRbf[iCoord](
                        xGrid.reshape((ny * nx)), yGrid.reshape(
                            (ny * nx))).reshape((ny, nx))

                elif myMethod == "tmean":

                    # use the truncated mean for each Coord -- very very simple!!
                    zstd = stats.tstd(zData)
                    zmean = stats.tmean(zData)
                    ztmean = stats.tmean(
                        zData, (zmean - 3. * zstd, zmean + 3. * zstd))
                    ztstd = stats.tstd(zData,
                                       (zmean - 3. * zstd, zmean + 3. * zstd))
                    self.interpTMean[iCoord] = ztmean
                    self.interpTStd[iCoord] = ztstd
                    self.interpValues[iCoord] = ztmean * numpy.ones((ny, nx))

                elif myMethod == "bmedian":

                    # use the median for each bin in each Coord
                    self.interpBMedian[iCoord] = numpy.zeros((ny, nx))
                    self.interpBNentry[iCoord] = numpy.zeros((ny, nx))
                    self.interpBMAD[iCoord] = numpy.zeros((ny, nx))
                    zvalL = []
                    xbin = numpy.digitize(xData, xEdge[0, :]) - 1
                    ybin = numpy.digitize(yData, yEdge[:, 0]) - 1
                    for i in range(nx):
                        for j in range(ny):
                            ok = numpy.logical_and.reduce(
                                (xbin == i, ybin == j))
                            zHere = zData[ok]
                            # add nEntry, MAD to the saved variables for the Mesh
                            nEntry = zHere.shape[0]
                            if nEntry >= 1:
                                median_value = numpy.median(zHere)
                                self.interpBMedian[iCoord][j, i] = median_value
                                self.interpBNentry[iCoord][j, i] = nEntry
                                self.interpBMAD[iCoord][j, i] = numpy.median(
                                    numpy.abs(zHere - median_value))
                            else:
                                # need to do something better!
                                self.interpBMedian[iCoord][j, i] = 0.
                    # fill interpValues, need to match order of locations in xGrid and yGrid
                    self.interpValues[iCoord] = self.interpBMedian[
                        iCoord].copy()

                elif myMethod == "grid":
                    Z = griddata((xData, yData), zData, (xGrid.reshape(
                        (ny * nx)), yGrid.reshape((ny * nx))), "linear")
                    self.interpValues[iCoord] = Z.reshape(xGrid.shape)

                elif myMethod == "idw":
                    # July 15, 2013 - change to use epsilon=1.0 (mm) to set a cutoff in the distance
                    # this will make small changes in the results for all Donuts
                    if methodVal != None:
                        usekNN = methodVal[0]
                        useEpsilon = methodVal[1]
                    else:
                        usekNN = 4
                        useEpsilon = 1.0
                    self.interpIDW[iCoord] = IDWInterp(xData,
                                                       yData,
                                                       zData,
                                                       kNN=usekNN,
                                                       epsilon=useEpsilon)
                    self.interpValues[iCoord] = self.interpIDW[iCoord].ev(
                        xGrid.reshape((ny * nx)), yGrid.reshape(
                            (ny * nx))).reshape((ny, nx))

                else:
                    self.interpSpline[iCoord] = None
                    self.interpValues[iCoord] = numpy.zeros(xGrid.shape)

            else:
                self.interpTMean[iCoord] = 0.0
                self.interpBMedian[iCoord] = None
                self.interpBNentry[iCoord] = None
                self.interpBMAD[iCoord] = None
                self.interpTStd[iCoord] = 0.0
                self.interpSpline[iCoord] = None
                self.interpRbf[iCoord] = None
                self.interpValues[iCoord] = numpy.zeros(xGrid.shape)
예제 #30
0
def test_rbf_epsilon_none():
    x = linspace(0, 10, 9)
    y = sin(x)
    rbf = Rbf(x, y, epsilon=None)