niacin.timeseries package

Submodules

niacin.timeseries.freq module

Frequency domain transforms

niacin.timeseries.freq.add_discrete_phase_shifts(x: numpy.ndarray, p: float = 0.01, m: float = 0.1)[source]

Shift each frequency component with probability p by distance m*len(x).

Compute real FFT on sequence. Then, for each frequency entry, with probability p, swap it with an entry that is ±m*len(x)//2 steps away. Swapped entries are tagged so that they are not moved twice. The 0th component is ignored.

Parameters:
  • x – sequence
  • p – per-frequency probability of shifting
  • m – magnitude of shift
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_discrete_phase_shifts(x, 1.0, 0.1)
|
+1.03587-|      \     -|     \      \|     \      |
|/|     | |    /|     ||     /|    / |    | |    /|
|||     | |    ||     ||    | |    | |    | |    | |
|| |   |  |    | |   | |    |  |   | |    | |    | |
|  |   |  |   |  |   |  |   |  |   | |    | |    | |
|  |   |  |   |  |   |  |   |  |  |   |   | |   |  |
+---|--|--|---|--|---|--|---|--|--|---|--|---|--|--|---+--
+0  |  |   | |   |   |  |  |   |  |   |  |   |  |   +99
|   | |    | |   |  |   |  |   |  |   |  |   |  |   |  *
|   | |    | |    | |   |  |    | |   | |    |  |   | |
|   | |    | |    | |    | |    | |    ||    |  |    ||
|    \|    | |    \ |    | |    \|     ||     \|     |/
-1.01053    /      /|     /      |     -|      |     -/
|
niacin.timeseries.freq.add_high_frequency_noise(x: numpy.ndarray, p: float = 0.01, m: float = 0.1)[source]

Add gaussian noise to single highest frequency component with probability p and magnitude N(0,1) * m * max(fft(x))

Parameters:
  • x – sequence
  • p – per-sequence probability of noise
  • m – magnitude of noise
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_high_frequency_noise(x, 1.0, 1.0)
|
+2.11065              ||||             || ||
| ||||||             ||||||           |||||||
|||||||||           ||||||||         |||||||||
||||||||| |       ||||||||||        |||||||||||        *
||||||||||||     ||||||||||||      |||||||||||||      |
|||||||||||||  ||||||||||||||| |  |||||||||||||||  | ||
+||||||||||||||||||||||||||||||||||||||||||||||||||||||+--
+0| |  |||||||||||||||  | |||||||||||||||  |||||||||+99
||      |||||||||||||      ||||||||||||     |||||||||||
|        |||||||||||       ||||||||| |       ||||||||||
|         |||||||||         ||||||||           ||||||||
|          |||||||           ||||||             ||||||
-2.11065    || ||             ||||               ||||
|
niacin.timeseries.freq.add_random_frequency_noise(x: numpy.ndarray, p: float = 0.01, m: float = 0.1)[source]

Add gaussian noise to each frequency with probability p and magnitude N(0,1) * m * max(fft(x))

Parameters:
  • x – sequence
  • p – per-frequency probability of noise
  • m – magnitude of noise
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_random_frequency_noise(x, 0.5, 0.1)
|
+1.59613                                \|
|  -\/\                 |               ||
|\| / \              -|||             \/||\/|
|/|    |             |/ |   \         |/  -/-\
|      \         |   |  | ||\|        |      |        |
+-------|-|-|--|-|-\\|--\|||-|-------|-------|-\------|+--
+0      ||| |  | |/ /|   | | | |     |        /|    +99*
|       ||| |  || /        | -/|     /         |      |
|        |\ |\|                |\\-\|          | |   |
|          / \|                //|| |           || \\|
|             |                  ||             / || |
|                                 |               |
-1.90176                                          |
|
niacin.timeseries.freq.remove_random_frequency(x: numpy.ndarray, p: float = 0.01, m=None)[source]

Remove each frequency component with probability p.

Parameters:
  • x – sequence
  • p – per-frequency probability of removal
  • m – ignored
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.reverse(x, 1.0)
|
+1
|
|
|
|
|
+------------------------------------------------------+--
+0                                                  +99
|
|
|
|
-1

niacin.timeseries.time module

Time domain transforms

niacin.timeseries.time.add_slope_trend(x: numpy.ndarray, p: float = 0.01, m: float = 0.1) → numpy.ndarray[source]

Add linear trend, with probability p, and magnitude m*std(x).

The probability refers to the entire trend – either it is added, or the original array is left alone. The direction of the trend is chosen randomly with probability=0.5.

Parameters:
  • x – sequence
  • p – per-sequence probability of applying trend
  • m – magnitude of trend
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_slope_trend(x, 1.0, 1.0)
|
+1.52476                                --\\
|                     --\              /   \
|   --\              // \\            //    \
|  // \\             /    \           /      \
| /    \            /     \          /       \         *
| /     \           /      \         /        \       /
|/      \          /       \\       /          \      /
|/       \        /         \      /           \     /
+---------\-------/----------\-----/------------\----/-+--
+0        \      /           \    /             \\ /+99
|          \    //            \\ //              --/
|           \   /              --/
-0.82       \--/
|
niacin.timeseries.time.add_spike(x: numpy.ndarray, p: float = 0.01, m: float = 1.0) → numpy.ndarray[source]

At each array entry, add a spike with probability p and magnitude m*std(x).

The direction of the spike (up or down) is determined randomly with probability=0.5.

Parameters:
  • x – sequence
  • p – per-entry probability of adding spike
  • m – magnitude of spike
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_spike(x, 0.1, 1.0)
|
+1.61319              |
| |                  ||
||| -|\              |--\               |-\
||// |\\             |  |            | /|  \
||   | \\           /   | \          |/ |   |
||   |  \           /   || \         |/ |   |\
+--------\\-------||-----|-\\--------|------|\|--------+--
+0        \      /||        \   |  //       | |     +99
|          \     / |         | ||  /          |\\    /
|           \  //            | ||//           ||\| ///
|            --/             |||-/             | |-/
|                            \||                 |
-1.64856                      ||                 |
|
niacin.timeseries.time.add_step_trend(x: numpy.ndarray, p: float = 0.01, m: float = 0.1) → numpy.ndarray[source]

Add a stepwise trend, where each entry in the timeseries has p probability of a stepwise change of magnitude m*std(x).

The direction of the stepwise trend is chosen with probability=0.5 for the entire trend (e.g. if the first step is upward, then every subsequent step is also upward).

Parameters:
  • x – sequence
  • p – per-entry probability of step change
  • m – magnitude of step change
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_step_trend(x, 0.05, 0.5)
|
+1 /--\              /--\
| /    \\           //   |
|/      \          //     \
+--------\\-------/-------\\---------------------------+--
+0        \\     /         \\         \ --\         +99
|          \\   //          \\        //   \\
|           \--/             |       /      \\
|                             \   \//        \
|                              --///          \\
|                                              \\
|                                               \\
|                                                 -\ \/*
-3.02032                                           ////
|
niacin.timeseries.time.add_warp(x: numpy.ndarray, p: float = 0.01, m: float = 0.1, interp_method: str = 'linear') → numpy.ndarray[source]

Warp the distances between points in a timeseries.

With probability p, upsample a timeseries by a scale of m*len(x). Then, randomly len(x) items from upsampled timeseries, where choice is uniform.

Parameters:
  • x – sequence
  • p – per-sequence probability of warping
  • m – magnitude of warp
  • interp_method – scipy interp1d kind: can be ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. More details at scipy.interpolate.interp1d.
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.add_warp(x, 1.0, 0.1)
|
+1 -\                ---\                 -\\
| /  \|             |   --\              /  -\
|-/   |             |     \             /     |
||    |            |      \\            |     |
||    -\          -|       |            |     |
||      |        /         |            |     |
+-------|--------/---------|-----------/-------\------=+--
+0      |       /           \         /        |    +99
|        \     -/            \\       /        |     /
|        \    |                \     /          |    /
|         \   |                 \    /          |  //
|          -  |                 \   |           |-/
-1          -/                   -/-            --
|
niacin.timeseries.time.crop_and_stretch(x: numpy.ndarray, p: float = 0.01, m: float = 0.1, interp_method: str = 'linear') → numpy.ndarray[source]

Crop a sequence and stretch remaining entries to be the original size.

With probability p, crop x such that it has m*len(x) entries remaining. Then, stretch it back to size len(x) using given interpolation method (default method is linear).

Parameters:
  • x – sequence
  • p – per-sequence probability of cropping
  • m – magnitude of crop (larger m is fewer entries from x)
  • interp_method – scipy interp1d kind: can be ‘linear’, ‘nearest’, ‘nearest-up’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘previous’, or ‘next’. More details at scipy.interpolate.interp1d.
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.crop_and_stretch(x, 1.0, 0.5)
|
+1---\                               -----\
|     \\                           ///     \\
|      \\                         //        \\
|       \\                       /           \\
|         \                     /              \
|          \                   //               \
+-----------\-----------------//-----------------\-----+--
+0           \               /                   \\ +99
|            \\             /                     \\
|              \           //                      \\
|               \         //                         \
|                \\     ///                           \*
-1                \----//
|
niacin.timeseries.time.flip(x: numpy.ndarray, p: float = 0.5, m=None) → numpy.ndarray[source]

Flip sequence around origin with probability p.

Parameters:
  • x – sequence
  • p – per-sequence probability of flipping
  • m – ignored
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.flip(x, 1.0)
|
+1           --\               /-\               /-\
|           /   \             //  \             //  \
|          /    \            /    \             /    \
|          /     \           /     \           /     \
|         /      \          /      \           /      \
|         /       \         /       \         /       \
+--------/--------\--------/---------\-------/---------+--
+0       /         \       /         \       /      +99
|\      /           \      /          \      /
| \     /           \     /           \     /
| \    /             \    /            \    /
|  \  //             \  //             \   /
-1  -//               -//               --/
|
niacin.timeseries.time.reverse(x: numpy.ndarray, p: float = 0.5, m=None) → numpy.ndarray[source]

Reverse order of sequence with probability p

Parameters:
  • x – sequence
  • p – per-sequence probability of reversal
  • m – ignored
Returns:

enriched sequence

Examples

>>> x = np.sin(np.linspace(0, 6*np.pi, 100))
|
+1  -\\               -\\               --\
|  /  \\             /  \\             /   \
| /    \             /    \            /    \
| /     \           /     \           /     \
|/      \           /      \          /      \
|/       \         /       \         /       \
+--------\--------/--------\---------/-------\---------+--
+0        \       /         \       /         \     +99
|         \      /          \      /           \      /
|          \     /           \     /           \     /
|          \    /            \    /             \    /
|           \   /             \\  /             \\  /
-1           --/               \-/               \-/
|
>>> ts.reverse(x, 1.0)
|
+1           --\               /-\               /-\
|           /   \             //  \             //  \
|          /    \            /    \             /    \
|          /     \           /     \           /     \
|         /      \          /      \           /      \
|         /       \         /       \         /       \
+--------/--------\--------/---------\-------/---------+--
+0       /         \       /         \       /      +99
|\      /           \      /          \      /
| \     /           \     /           \     /
| \    /             \    /            \    /
|  \  //             \  //             \   /
-1  -//               -//               --/

Module contents

Functions for enriching timeseries data.

Includes transformations which operate in the time domain, and ones which operate in the frequency domain. Organization of this module and basic set of functionality are inspired by Wen et al.’s review, “Timeseries data Augmentation for Deep Learning” (arXiv:2002.12478).

Importable functions include:

Time domain

  • add_slope_trend
  • add_spike
  • add_step_trend
  • add_warp
  • crop_and_stretch
  • flip
  • reverse

Frequency domain

  • add_discrete_phase_shift
  • add_high_frequency_noise
  • add_random_frequency_noise
  • remove_random_frequency