Functions used to transform TSTensors (Data Augmentation).
dsid = 'NATOPS'
X, y, splits = get_UCR_data(dsid, return_split=False)
tfms = [None, Categorize()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, y, tfms=tfms, splits=splits, batch_tfms=batch_tfms, bs=128)
xb, yb = next(iter(dls.train))

class TSIdentity[source]

TSIdentity(magnitude=None, **kwargs) :: RandTransform

Applies the identity tfm to a TSTensor batch

test_eq(TSIdentity()(xb, split_idx=0).shape, xb.shape)

class TSShuffle_HLs[source]

TSShuffle_HLs(magnitude=1.0, ex=None, **kwargs) :: RandTransform

Randomly shuffles HIs/LOs of an OHLC TSTensor batch

test_eq(TSShuffle_HLs()(xb, split_idx=0).shape, xb.shape)

class TSShuffleSteps[source]

TSShuffleSteps(magnitude=1.0, ex=None, **kwargs) :: RandTransform

Randomly shuffles consecutive sequence datapoints in batch

t = TSTensor(torch.arange(11).float())
tt_ = []
for _ in range(1000): 
    tt = TSShuffleSteps()(t, split_idx=0)
    test_eq(len(set(tt.tolist())), len(t))
    test_ne(tt, t)
    tt_.extend([t for i,t in enumerate(tt) if t!=i])
x, y = np.unique(tt_, return_counts=True) # This is to visualize distribution which should be equal for all and half for first and last items
plt.bar(x, y);

class TSMagAddNoise[source]

TSMagAddNoise(magnitude=1, ex=None, **kwargs) :: RandTransform

Applies additive noise on the y-axis for each step of a TSTensor batch

class TSMagMulNoise[source]

TSMagMulNoise(magnitude=1, ex=None, **kwargs) :: RandTransform

Applies multiplicative noise on the y-axis for each step of a TSTensor batch

test_eq(TSMagAddNoise()(xb, split_idx=0).shape, xb.shape)
test_eq(TSMagMulNoise()(xb, split_idx=0).shape, xb.shape)
test_ne(TSMagAddNoise()(xb, split_idx=0), xb)
test_ne(TSMagMulNoise()(xb, split_idx=0), xb)

random_curve_generator[source]

random_curve_generator(o, magnitude=0.1, order=4, noise=None)

random_cum_curve_generator[source]

random_cum_curve_generator(o, magnitude=0.1, order=4, noise=None)

random_cum_noise_generator[source]

random_cum_noise_generator(o, magnitude=0.1, noise=None)

random_cum_linear_generator[source]

random_cum_linear_generator(o, magnitude=0.1)

class TSTimeNoise[source]

TSTimeNoise(magnitude=0.1, ex=None, **kwargs) :: RandTransform

Applies noise to each step in the x-axis of a TSTensor batch based on smooth random curve

test_eq(TSTimeNoise()(xb, split_idx=0).shape, xb.shape)
test_ne(TSTimeNoise()(xb, split_idx=0), xb)

class TSMagWarp[source]

TSMagWarp(magnitude=0.02, ord=4, ex=None, **kwargs) :: RandTransform

Applies warping to the y-axis of a TSTensor batch based on a smooth random curve

test_eq(TSMagWarp()(xb, split_idx=0).shape, xb.shape)
test_ne(TSMagWarp()(xb, split_idx=0), xb)

class TSTimeWarp[source]

TSTimeWarp(magnitude=0.02, ord=4, ex=None, **kwargs) :: RandTransform

Applies time warping to the x-axis of a TSTensor batch based on a smooth random curve

test_eq(TSTimeWarp()(xb, split_idx=0).shape, xb.shape)
test_ne(TSTimeWarp()(xb, split_idx=0), xb)

class TSWindowWarp[source]

TSWindowWarp(magnitude=0.1, ex=None, **kwargs) :: RandTransform

Applies window slicing to the x-axis of a TSTensor batch based on a random linear curve based on https://halshs.archives-ouvertes.fr/halshs-01357973/document

test_eq(TSWindowWarp()(xb, split_idx=0).shape, xb.shape)

class TSMagScale[source]

TSMagScale(magnitude=0.5, ex=None, **kwargs) :: RandTransform

Applies scaling to the y-axis of a TSTensor batch based on a scalar

class TSMagScalePerVar[source]

TSMagScalePerVar(magnitude=0.5, ex=None, **kwargs) :: RandTransform

Applies per_var scaling to the y-axis of a TSTensor batch based on a scalar

test_eq(TSMagScale()(xb, split_idx=0).shape, xb.shape)
test_eq(TSMagScalePerVar()(xb, split_idx=0).shape, xb.shape)
test_ne(TSMagScale()(xb, split_idx=0), xb)
test_ne(TSMagScalePerVar()(xb, split_idx=0), xb)

class TSRandomResizedCrop[source]

TSRandomResizedCrop(magnitude=0.1, size=None, scale=None, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly amplifies a sequence focusing on a random section of the steps

test_eq(TSRandomResizedCrop(.5)(xb, split_idx=0).shape, xb.shape)
test_ne(TSRandomResizedCrop(size=.8, scale=(.5, 1))(xb, split_idx=0).shape, xb.shape)
test_ne(TSRandomResizedCrop(size=20, scale=(.5, 1))(xb, split_idx=0).shape, xb.shape)

class TSWindowSlicing[source]

TSWindowSlicing(magnitude=0.1, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly extracts an resize a ts slice based on https://halshs.archives-ouvertes.fr/halshs-01357973/document

test_eq(TSWindowSlicing()(xb, split_idx=0).shape, xb.shape)
test_ne(TSWindowSlicing()(xb, split_idx=0), xb)

class TSRandomZoomOut[source]

TSRandomZoomOut(magnitude=0.1, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly compresses a sequence on the x-axis

test_eq(TSRandomZoomOut(.5)(xb, split_idx=0).shape, xb.shape)

class TSRandomTimeScale[source]

TSRandomTimeScale(magnitude=0.1, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly amplifies/ compresses a sequence on the x-axis keeping the same length

test_eq(TSRandomTimeScale(.5)(xb, split_idx=0).shape, xb.shape)

class TSRandomTimeStep[source]

TSRandomTimeStep(magnitude=0.02, ex=None, mode='linear', **kwargs) :: RandTransform

Compresses a sequence on the x-axis by randomly selecting sequence steps and interpolating to previous size

test_eq(TSRandomTimeStep()(xb, split_idx=0).shape, xb.shape)

class TSBlur[source]

TSBlur(magnitude=1.0, ex=None, filt_len=None, **kwargs) :: RandTransform

Blurs a sequence applying a filter of type [1, 0, 1]

test_eq(TSBlur(filt_len=7)(xb, split_idx=0).shape, xb.shape)
test_ne(TSBlur()(xb, split_idx=0), xb)

class TSSmooth[source]

TSSmooth(magnitude=1.0, ex=None, filt_len=None, **kwargs) :: RandTransform

Smoothens a sequence applying a filter of type [1, 5, 1]

test_eq(TSSmooth(filt_len=7)(xb, split_idx=0).shape, xb.shape)
test_ne(TSSmooth()(xb, split_idx=0), xb)

maddest[source]

maddest(d, axis=None)

class TSFreqDenoise[source]

TSFreqDenoise(magnitude=0.1, ex=None, wavelet='db4', level=2, thr=None, thr_mode='hard', pad_mode='per', **kwargs) :: RandTransform

Denoises a sequence applying a wavelet decomposition method

test_eq(TSFreqDenoise()(xb, split_idx=0).shape, xb.shape)
test_ne(TSFreqDenoise()(xb, split_idx=0), xb)

class TSRandomFreqNoise[source]

TSRandomFreqNoise(magnitude=0.1, ex=None, wavelet='db4', level=2, mode='constant', **kwargs) :: RandTransform

Applys random noise using a wavelet decomposition method

test_eq(TSRandomFreqNoise()(xb, split_idx=0).shape, xb.shape)

class TSRandomResizedLookBack[source]

TSRandomResizedLookBack(magnitude=0.1, mode='linear', **kwargs) :: RandTransform

Selects a random number of sequence steps starting from the end and return an output of the same shape

for i in range(100): 
    o = TSRandomResizedLookBack()(xb, split_idx=0)
    test_eq(o.shape[-1], xb.shape[-1])

class TSRandomLookBackOut[source]

TSRandomLookBackOut(magnitude=0.1, **kwargs) :: RandTransform

Selects a random number of sequence steps starting from the end and set them to zero

for i in range(100): 
    o = TSRandomLookBackOut()(xb, split_idx=0)
    test_eq(o.shape[-1], xb.shape[-1])

class TSVarOut[source]

TSVarOut(magnitude=0.05, ex=None, **kwargs) :: RandTransform

Set the value of a random number of variables to zero

test_eq(TSVarOut()(xb, split_idx=0).shape, xb.shape)

class TSCutOut[source]

TSCutOut(magnitude=0.05, ex=None, **kwargs) :: RandTransform

Sets a random section of the sequence to zero

test_eq(TSCutOut()(xb, split_idx=0).shape, xb.shape)

class TSTimeStepOut[source]

TSTimeStepOut(magnitude=0.05, ex=None, **kwargs) :: RandTransform

Sets random sequence steps to zero

test_eq(TSTimeStepOut()(xb, split_idx=0).shape, xb.shape)

class TSRandomCropPad[source]

TSRandomCropPad(magnitude=0.05, ex=None, **kwargs) :: RandTransform

Crops a section of the sequence of a random length

test_eq(TSRandomCropPad()(xb, split_idx=0).shape, xb.shape)

class TSMaskOut[source]

TSMaskOut(magnitude=0.05, ex=None, **kwargs) :: RandTransform

Set a random number of steps to zero

test_eq(TSMaskOut()(xb, split_idx=0).shape, xb.shape)
test_ne(TSMaskOut()(xb, split_idx=0), xb)

class TSTranslateX[source]

TSTranslateX(magnitude=0.1, ex=None, **kwargs) :: RandTransform

Moves a selected sequence window a random number of steps

test_eq(TSTranslateX()(xb, split_idx=0).shape, xb.shape)

class TSRandomShift[source]

TSRandomShift(magnitude=0.02, ex=None, **kwargs) :: RandTransform

Shifts and splits a sequence

test_eq(TSRandomShift()(xb, split_idx=0).shape, xb.shape)

class TSHorizontalFlip[source]

TSHorizontalFlip(magnitude=1.0, ex=None, **kwargs) :: RandTransform

Flips the sequence along the x-axis

test_eq(TSHorizontalFlip()(xb, split_idx=0).shape, xb.shape)
test_ne(TSHorizontalFlip()(xb, split_idx=0), xb)

class TSRandomTrend[source]

TSRandomTrend(magnitude=0.1, ex=None, **kwargs) :: RandTransform

Randomly rotates the sequence along the z-axis

test_eq(TSRandomTrend()(xb, split_idx=0).shape, xb.shape)

class TSVerticalFlip[source]

TSVerticalFlip(magnitude=1.0, ex=None, **kwargs) :: RandTransform

Applies a negative value to the time sequence

test_eq(TSVerticalFlip()(xb, split_idx=0).shape, xb.shape)
test_ne(TSVerticalFlip()(xb, split_idx=0), xb)

class TSResize[source]

TSResize(magnitude=-0.5, size=None, ex=None, mode='linear', **kwargs) :: RandTransform

Resizes the sequence length of a time series

for sz in np.linspace(.2, 2, 10): test_eq(TSResize(sz)(xb, split_idx=0).shape[-1], int(round(xb.shape[-1]*(1+sz))))
test_ne(TSResize(1)(xb, split_idx=0), xb)

class TSRandomSize[source]

TSRandomSize(magnitude=0.1, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly resizes the sequence length of a time series

seq_len_ = []
for i in range(100): 
    o = TSRandomSize(.5)(xb, split_idx=0)
    seq_len_.append(o.shape[-1])
test_lt(min(seq_len_), xb.shape[-1])
test_gt(max(seq_len_), xb.shape[-1])

class TSRandomLowRes[source]

TSRandomLowRes(magnitude=0.5, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly resizes the sequence length of a time series to a lower resolution

class TSDownUpScale[source]

TSDownUpScale(magnitude=0.5, ex=None, mode='linear', **kwargs) :: RandTransform

Downscales a time series and upscales it again to previous sequence length

test_eq(TSDownUpScale()(xb, split_idx=0).shape, xb.shape)

class TSRandomDownUpScale[source]

TSRandomDownUpScale(magnitude=0.5, ex=None, mode='linear', **kwargs) :: RandTransform

Randomly downscales a time series and upscales it again to previous sequence length

test_eq(TSRandomDownUpScale()(xb, split_idx=0).shape, xb.shape)
test_ne(TSDownUpScale()(xb, split_idx=0), xb)
test_eq(TSDownUpScale()(xb, split_idx=1), xb)

class TSRandomConv[source]

TSRandomConv(magnitude=0.05, ex=None, ks=[1, 3, 5, 7], **kwargs) :: RandTransform

Applies a convolution with a random kernel and random weights with required_grad=False

for i in range(5): 
    o = TSRandomConv(magnitude=0.05, ex=None, ks=[1, 3, 5, 7])(xb, split_idx=0)
    test_eq(o.shape, xb.shape)

class RandAugment[source]

RandAugment(tfms:list, N:int=1, M:int=3, **kwargs) :: RandTransform

A transform that before_call its state at each __call__

test_ne(RandAugment(TSMagAddNoise, N=5, M=10)(xb, split_idx=0), xb)

class TestTfm[source]

TestTfm(tfm, magnitude=1.0, ex=None, **kwargs) :: RandTransform

Utility class to test the output of selected tfms during training

get_tfm_name[source]

get_tfm_name(tfm)

test_eq(get_tfm_name(partial(TSMagScale()))==get_tfm_name((partial(TSMagScale()), 0.1, .05))==get_tfm_name(TSMagScale())==get_tfm_name((TSMagScale(), 0.1, .05)), True)
all_TS_randaugs_names = [get_tfm_name(t) for t in all_TS_randaugs]