Example #1
0
 def testReallyWorks(self, level=1):
     # the solution given masking should be close to the original gradient
     (mx,_,_) = util.lin_regression(self.noisy_grad,
                                    mask=self.gmask, axis=-1)
     (my,_,_) = util.lin_regression(self.noisy_grad,
                                    mask=self.gmask, axis=-2)
     (mz,_,_) = util.lin_regression(self.noisy_grad,
                                    mask=self.gmask, axis=-3)
     assert abs(mx.mean() - self.abg[2]) < .099
     assert abs(my.mean() - self.abg[1]) < .099
     assert abs(mz.mean() - self.abg[0]) < .099
Example #2
0
 def testReallyWorks(self, level=1):
     # the solution given masking should be close to the original gradient
     (mx, _, _) = util.lin_regression(self.noisy_grad,
                                      mask=self.gmask,
                                      axis=-1)
     (my, _, _) = util.lin_regression(self.noisy_grad,
                                      mask=self.gmask,
                                      axis=-2)
     (mz, _, _) = util.lin_regression(self.noisy_grad,
                                      mask=self.gmask,
                                      axis=-3)
     assert abs(mx.mean() - self.abg[2]) < .099
     assert abs(my.mean() - self.abg[1]) < .099
     assert abs(mz.mean() - self.abg[0]) < .099
Example #3
0
    def doLinRegTests(self, mask=None, sigma=None, axis=-1, test_id='basic'):
        gshape = self.noisy_grad.shape
        coef_shape = list(gshape)
        coef_shape.pop(axis)
        m1d, b1d, chi1d = (np.zeros(coef_shape), np.zeros(coef_shape),
                           np.zeros(coef_shape))

        mnd, bnd, chind = util.lin_regression(self.noisy_grad,
                                              mask=mask,
                                              sigma=sigma,
                                              axis=axis)
        # these return with a None dim if axis is not -1
        mnd, bnd, chind = map(lambda x: np.squeeze(x), (mnd, bnd, chind))

        for m in range(coef_shape[0]):
            for n in range(coef_shape[1]):
                sl = [m, n]
                sl.insert(len(gshape) + axis, slice(0, gshape[axis]))
                if sigma is None:
                    sig = sigma
                else:
                    sig = sigma[sl]
                if mask is None:
                    msk = mask
                else:
                    msk = mask[sl]
                (m1d[m, n], b1d[m, n],
                 chi1d[m, n]) = ref_linear_regression(self.noisy_grad[sl],
                                                      sigma=sig,
                                                      mask=msk)
        msg = 'failed with ' + test_id
        assert_array_almost_equal(b1d, bnd, decimal=12, err_msg=msg)
        assert_array_almost_equal(m1d, mnd, decimal=12, err_msg=msg)
        assert_array_almost_equal(chi1d, chind, decimal=12, err_msg=msg)
Example #4
0
    def doLinRegTests(self, mask=None, sigma=None, axis=-1, test_id='basic'):
        gshape = self.noisy_grad.shape
        coef_shape = list(gshape)
        coef_shape.pop(axis)
        m1d,b1d,chi1d = (np.zeros(coef_shape),
                         np.zeros(coef_shape),
                         np.zeros(coef_shape))

        mnd,bnd,chind = util.lin_regression(self.noisy_grad,
                                            mask=mask,
                                            sigma=sigma,
                                            axis=axis)
        # these return with a None dim if axis is not -1
        mnd,bnd,chind = map(lambda x: np.squeeze(x), (mnd,bnd,chind))

        for m in range(coef_shape[0]):
            for n in range(coef_shape[1]):
                sl = [m,n]
                sl.insert(len(gshape)+axis, slice(0,gshape[axis]))
                if sigma is None:
                    sig = sigma
                else:
                    sig = sigma[sl]
                if mask is None:
                    msk = mask
                else:
                    msk = mask[sl]
                (m1d[m,n],
                 b1d[m,n],
                 chi1d[m,n]) = ref_linear_regression(self.noisy_grad[sl],
                                                     sigma=sig,mask=msk)
        msg = 'failed with ' + test_id
        assert_array_almost_equal(b1d, bnd, decimal=12, err_msg=msg)
        assert_array_almost_equal(m1d, mnd, decimal=12, err_msg=msg)
        assert_array_almost_equal(chi1d, chind, decimal=12, err_msg=msg)
Example #5
0
def subsampInterp_TD(ts, c, axis=0):
    T = ts.shape[axis]

    # find ramps and subtract them
    ramps = np.empty_like(ts)
    rax_shape = [1] * len(ts.shape)
    rax_shape[axis] = T
    rax = np.arange(T)
    rax.shape = rax_shape
    (mre, b, r) = lin_regression(ts.real, axis=axis)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes["complex"]:
        (mim, b, r) = lin_regression(ts.imag, axis=axis)
        ramps.imag[:] = rax * mim
    np.subtract(ts, ramps, ts)
    # find biases and subtract them
    ts_mean = ts.mean(axis=axis)
    if len(ts_mean.shape):
        mean_shape = list(ts.shape)
        mean_shape[axis] = 1
        ts_mean.shape = tuple(mean_shape)
    np.subtract(ts, ts_mean, ts)

    if axis != 0:
        ts = np.swapaxes(ts, axis, 0)
    snc_ax = np.arange(T)
    ts_shape = ts.shape
    ts.shape = (T, -1)
    snc_kern = np.sinc(snc_ax[None, :] - snc_ax[:, None] + c)
    ts_tmp = np.dot(snc_kern, ts)
    ts[:] = ts_tmp
    ts.shape = ts_shape
    del ts_tmp
    if axis != 0:
        ts = np.swapaxes(ts, axis, 0)

    # add back biases and analytically interpolated ramps
    np.subtract(rax, c, rax)
    # np.add(rax, c, rax)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes["complex"]:
        ramps.imag[:] = rax * mim
    np.add(ts, ramps, ts)
    np.add(ts, ts_mean, ts)
Example #6
0
def subsampInterp_TD(ts, c, axis=0):
    T = ts.shape[axis]

    # find ramps and subtract them
    ramps = np.empty_like(ts)
    rax_shape = [1] * len(ts.shape)
    rax_shape[axis] = T
    rax = np.arange(T)
    rax.shape = rax_shape
    (mre, b, r) = lin_regression(ts.real, axis=axis)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes['complex']:
        (mim, b, r) = lin_regression(ts.imag, axis=axis)
        ramps.imag[:] = (rax * mim)
    np.subtract(ts, ramps, ts)
    # find biases and subtract them
    ts_mean = ts.mean(axis=axis)
    if len(ts_mean.shape):
        mean_shape = list(ts.shape)
        mean_shape[axis] = 1
        ts_mean.shape = tuple(mean_shape)
    np.subtract(ts, ts_mean, ts)

    if axis != 0:
        ts = np.swapaxes(ts, axis, 0)
    snc_ax = np.arange(T)
    ts_shape = ts.shape
    ts.shape = (T, -1)
    snc_kern = np.sinc(snc_ax[None, :] - snc_ax[:, None] + c)
    ts_tmp = np.dot(snc_kern, ts)
    ts[:] = ts_tmp
    ts.shape = ts_shape
    del ts_tmp
    if axis != 0:
        ts = np.swapaxes(ts, axis, 0)

    # add back biases and analytically interpolated ramps
    np.subtract(rax, c, rax)
    #np.add(rax, c, rax)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes['complex']:
        ramps.imag[:] = rax * mim
    np.add(ts, ramps, ts)
    np.add(ts, ts_mean, ts)
Example #7
0
def subsampInterp(ts, c, axis=-1):
    """Makes a subsample sinc interpolation on an N-D array in a given axis.

    This uses sinc interpolation to return ts(t-a), where a is related to
    the factor c as described below.

    ts : the N-D array with a time series in "axis"
    c : the fraction of the sampling interval such that a = c*dt -- note that
      c is only useful in the interval (0, 1]
    axis : specifies the axis of the time dimension
    """
    To = ts.shape[axis]

    # find ramps and subtract them
    ramps = np.empty_like(ts)
    rax_shape = [1] * len(ts.shape)
    rax_shape[axis] = To
    rax = np.arange(To)
    rax.shape = rax_shape
    (mre, b, r) = lin_regression(ts.real, axis=axis)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes["complex"]:
        (mim, b, r) = lin_regression(ts.imag, axis=axis)
        ramps.imag[:] = rax * mim
    np.subtract(ts, ramps, ts)
    # find biases and subtract them
    ts_mean = ts.mean(axis=axis)
    if len(ts_mean.shape):
        mean_shape = list(ts.shape)
        mean_shape[axis] = 1
        ts_mean.shape = tuple(mean_shape)
    np.subtract(ts, ts_mean, ts)

    # put time series in the last dimension
    if axis != -1:
        ts = np.swapaxes(ts, axis, -1)
    ts_buf = circularize(ts)
    ##     ts_buf = ts.copy()
    T = ts_buf.shape[-1]
    Fn = T / 2 + 1
    # make sure the interpolating filter's dtype is complex!
    filter_dtype = np.dtype(ts.dtype.char.upper())
    phs_shift = np.empty((T,), filter_dtype)
    phs_shift[:Fn] = np.exp(-2.0j * np.pi * c * np.arange(Fn) / float(T))
    phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1 : T - (Fn - 1)]))
    fft(ts_buf, shift=False, inplace=True)
    np.multiply(ts_buf, phs_shift, ts_buf)
    ifft(ts_buf, shift=False, inplace=True)

    ts[:] = ts_buf[..., To - 1 : 2 * To - 1]
    ##     ts[:] = ts_buf[:]
    del ts_buf
    if axis != -1:
        ts = np.swapaxes(ts, axis, -1)

    # add back biases and analytically interpolated ramps
    np.subtract(rax, c, rax)
    # np.add(rax, c, rax)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes["complex"]:
        ramps.imag[:] = rax * mim
    np.add(ts, ramps, ts)
    np.add(ts, ts_mean, ts)
Example #8
0
def subsampInterp(ts, c, axis=-1):
    """Makes a subsample sinc interpolation on an N-D array in a given axis.

    This uses sinc interpolation to return ts(t-a), where a is related to
    the factor c as described below.

    ts : the N-D array with a time series in "axis"
    c : the fraction of the sampling interval such that a = c*dt -- note that
      c is only useful in the interval (0, 1]
    axis : specifies the axis of the time dimension
    """
    To = ts.shape[axis]

    # find ramps and subtract them
    ramps = np.empty_like(ts)
    rax_shape = [1] * len(ts.shape)
    rax_shape[axis] = To
    rax = np.arange(To)
    rax.shape = rax_shape
    (mre, b, r) = lin_regression(ts.real, axis=axis)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes['complex']:
        (mim, b, r) = lin_regression(ts.imag, axis=axis)
        ramps.imag[:] = (rax * mim)
    np.subtract(ts, ramps, ts)
    # find biases and subtract them
    ts_mean = ts.mean(axis=axis)
    if len(ts_mean.shape):
        mean_shape = list(ts.shape)
        mean_shape[axis] = 1
        ts_mean.shape = tuple(mean_shape)
    np.subtract(ts, ts_mean, ts)

    # put time series in the last dimension
    if axis != -1:
        ts = np.swapaxes(ts, axis, -1)
    ts_buf = circularize(ts)
    ##     ts_buf = ts.copy()
    T = ts_buf.shape[-1]
    Fn = T / 2 + 1
    # make sure the interpolating filter's dtype is complex!
    filter_dtype = np.dtype(ts.dtype.char.upper())
    phs_shift = np.empty((T, ), filter_dtype)
    phs_shift[:Fn] = np.exp(-2.j * np.pi * c * np.arange(Fn) / float(T))
    phs_shift[Fn:] = np.conjugate(reverse(phs_shift[1:T - (Fn - 1)]))
    fft(ts_buf, shift=False, inplace=True)
    np.multiply(ts_buf, phs_shift, ts_buf)
    ifft(ts_buf, shift=False, inplace=True)

    ts[:] = ts_buf[..., To - 1:2 * To - 1]
    ##     ts[:] = ts_buf[:]
    del ts_buf
    if axis != -1:
        ts = np.swapaxes(ts, axis, -1)

    # add back biases and analytically interpolated ramps
    np.subtract(rax, c, rax)
    #np.add(rax, c, rax)
    ramps.real[:] = rax * mre
    if ts.dtype.type in np.sctypes['complex']:
        ramps.imag[:] = rax * mim
    np.add(ts, ramps, ts)
    np.add(ts, ts_mean, ts)