Exemple #1
0
def test_t():
    # Use the default "model" tree (Don't load signal from MDSplus)
    shot = -1

    # Create `Signal` object
    sig = Signal(shot, 1)

    # Now, overwrite properties of `sig` to easily test `_getSlice()` method
    sig.Fs = 1.
    sig._downsample = 1
    sig.t0 = 0
    sig.x = np.arange(10)

    # (1) `sig.t()` is equal to `sig.x` for the above parameters
    np.testing.assert_equal(sig.t(), sig.x)

    # (2) Linear shift
    sig.t0 += 1
    np.testing.assert_equal(sig.t(), sig.x + 1)

    # (3) Double the sampling rate at which signal is retrieved
    sig.Fs *= 2
    np.testing.assert_equal(sig.t(), (sig.x / sig.Fs) + 1)

    # (4) The time base computed by the `sig.t()` method corresponds
    # to the retrieved points in `sig.x`, which may be downsampled
    # from the full digitized record. The downsampling is folded
    # into the sampling rate `sig.Fs` at which the signal is retrieved,
    # however, so altering `sig._downsample` should *not* affect `sig.t()`;
    # that is, `sig.t()` should remain unchanged from (3)
    sig._downsample = 2
    np.testing.assert_equal(sig.t(), (sig.x / sig.Fs) + 1)

    return
Exemple #2
0
def test__getSlice():
    # Use the default "model" tree
    shot = -1

    # Test signal and limiting slicing values
    x = np.arange(10)
    imin = 0
    imax = len(x)

    # Create `Signal` object
    sig = Signal(shot, 1)

    # ValueError tests
    tools.assert_raises(ValueError, sig._getSlice, *[x], **{'tlim': [1]})
    tools.assert_raises(ValueError, sig._getSlice, *[x], **{'tlim': [1, 2, 3]})

    # -------------------------------------------------------------------------
    # (1) No downsampling, trivial `t0_dig`
    # -------------------------------------------------------------------------
    # Now, overwrite properties of `sig` to easily test `_getSlice()` method
    sig.Fs = 1.
    sig._downsample = 1

    # (1a) No actual slicing
    tools.assert_equal(
        sig._getSlice(x, tlim=None, t0_dig=0.),
        slice(imin, imax, 1))

    # (1b) Slicing from both ends, no downsampling
    tlim = [1, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(tlim[0], tlim[1] + 1, 1))

    # (1c) Slicing from lower end only, no downsampling
    tlim = [1, 10 + np.finfo(float).eps]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(tlim[0], imax, 1))

    # (1d) Slicing from upper end only, no downsampling
    tlim = [-1 - np.finfo(float).eps, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(imin, tlim[1] + 1, 1))

    # -------------------------------------------------------------------------
    # (2) Downsampling, trivial `t0_dig`
    # -------------------------------------------------------------------------
    # Vary `sig.Fs` and `sig._downsample` inversely such that
    # the `tlim` values can stay the same as above.
    sig._downsample = 2  # Note: must be an *integer*
    sig.Fs /= sig._downsample

    # (2a) Downsampling only
    tools.assert_equal(
        sig._getSlice(x, tlim=None, t0_dig=0.),
        slice(imin, imax, sig._downsample))

    # (2b) Slicing from both ends, no downsampling
    tlim = [1, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(tlim[0], tlim[1] + 1, sig._downsample))

    # (2c) Slicing from lower end only, no downsampling
    tlim = [1, 10 + np.finfo(float).eps]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(tlim[0], imax, sig._downsample))

    # (2d) Slicing from upper end only, no downsampling
    tlim = [-1 - np.finfo(float).eps, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=0.),
        slice(imin, tlim[1] + 1, sig._downsample))

    # -------------------------------------------------------------------------
    # (3) Downsampling, non-trivial `t0_dig`
    # -------------------------------------------------------------------------
    # Lower slicing indices *within* the bounds of the array should be
    # decremented by `np.floor(t0_dig)` relative to (2), and
    # upper slicing indices *within* the bounds of the array should be
    # decremented by `np.ceil(t0_dig)` (Note that this is equivalent to
    # (`1 + np.floor(t0_dig)`) relative to (2).
    t0_dig = 1.5

    # (3a) Downsampling only; `t0_dig` does not come into play
    # when `tlim` is None
    tools.assert_equal(
        sig._getSlice(x, tlim=None, t0_dig=t0_dig),
        slice(imin, imax, sig._downsample))

    # (3b) Slicing from both ends, no downsampling
    tlim = [2, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=t0_dig),
        slice(tlim[0] - np.floor(t0_dig),
              tlim[1] - np.floor(t0_dig),
              sig._downsample))

    # (3c) Slicing from lower end only, no downsampling
    tlim = [2, 10 + np.finfo(float).eps + t0_dig]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=t0_dig),
        slice(tlim[0] - np.floor(t0_dig), imax, sig._downsample))

    # (3d) Slicing from upper end only, no downsampling
    tlim = [-1 - np.finfo(float).eps + t0_dig, 8]
    tools.assert_equal(
        sig._getSlice(x, tlim=tlim, t0_dig=t0_dig),
        slice(imin, tlim[1] - np.floor(t0_dig), sig._downsample))

    return