TimeseriesBase
Welcome to the documentation for TimeseriesBase, a Julia package for working with time series data.
The TimeseriesBase
package provides a standardized collection of types designed for handling various types of time-series data. Defining consistent types for time series provides three key benefits:
- It simplifies the workspace and method signatures by aggregating much of the data that defines a time series into a single variable; thanks to the
DimensionalData.jl
package, one variable can hold the values of a time series, its time points, spatial coordinates, units, metadata, and more. - It facilitates generic functions that dispatch on the various types of time series; for instance, more efficient algorithms can be written for
RegularTimeseries
types than forIrregularTimeseries
types, but the same high-level functionality can be provided by the same generic function that dispatches these methods given the type of the input time series. - Most importantly, this intuitively aligns the structure of time-series data in code to mathematical conventions, which can vastly simplify the process of developing and interpreting programs. Many small complexities (Was this time series regularly sampled? What are the output frequencies of my Fourier transform? The units of my power spectrum?) are handled effortlessly, leaving room to focus on higher-level problems.
To achieve this, TimeseriesBase.jl defines a custom version of the DimensionalData.DimArray
and custom DimensionalData.Dimension
s:
x = Timeseries(1:10, rand(10))
x isa AbstractToolsArray # In most cases, an AbstractToolsArray behaves like a DimArray; see DimensionalData
x isa AbstractTimeseries # An AbstractTimeseries is an AbstractToolsArray...
lookup(x, 1) isa 𝑡 # ...where the first dimension is a custom TimeDim 𝑡
If a ToolsArray
or DimArray
has a 𝑡
as its first dimension, it will be rebuilt as a ToolsArray
(i.e. when using functions like eachcol
). There are a small number of other custom dimensions, all exported, that share this property and are subtypes of ToolsDimension
: e.g. 𝑥
, 𝑦
, 𝑧
, 𝑓
,Var
, Obs
. To define more of these ToolsDimension
s, use:
DimensionalData.@dim NewDim ToolsDim "NameOfNewDim"
Please note that functions operating on a ToolsArray
without a ToolsDimension
as the first or last dimension may NOT return a ToolsArray
, especially if they perform slicing and rebuilding. Be careful using the DimensionalData.Dim{:name}
syntax.
TimeseriesBase.ToolsArrays.ToolsDimension
— TypeToolsDimension
A union of all Dimension
types that fall within the scope of TimeseriesBase
. Analogous to DimensionalData.Dimension
for dispatch purposes.
See also
TimeseriesBase.ToolsArrays.AbstractToolsArray
— TypeA local type to avoid overloading and piracy issues with DimensionalData.jl
TimeseriesBase.ToolsArrays.ToolsArray
— MethodToolsArray(f::Function, dim::Dimension; [name])
Apply function f
across the values of the dimension dim
(using broadcast
), and return the result as a dimensional array with the given dimension. Optionally provide a name for the result.
TimeseriesBase.ToolsArrays.ToolsDim
— TypeToolsDim{T}
An abstract type for custom macro-defined dimensions in TimeseriesBase
. Analogous to DimensionalData.Dimension
for the purposes of DimensionalData.@dim
.
Examples
DimensionalData.@dim MyDim ToolsDim "My dimension" # Defines a new `ToolsDim <: ToolsDimension`
See also
TimeseriesBase.TimeSeries.AbstractTimeseries
— TypeAbstractTimeseries{T, N, B}
A type alias for an AbstractDimArray with a time index.
TimeseriesBase.TimeSeries.BinaryTimeseries
— TypeBinaryTimeseries
A type alias for a time series of bits.
TimeseriesBase.TimeSeries.IrregularIndex
— TypeIrregularIndex
A type alias for an irregularly sampled dimension, wrapping an AbstractVector
.
TimeseriesBase.TimeSeries.IrregularTimeIndex
— TypeIrregularTimeIndex
A type alias for a tuple of dimensions containing a TimeIndex
and any number of other dimensions.
TimeseriesBase.TimeSeries.IrregularTimeseries
— TypeIrregularTimeseries
A type alias for a potentially irregularly sampled time series.
TimeseriesBase.TimeSeries.MultidimensionalTimeseries
— TypeA multidimensional time series has a regular sampling over a dimension other than time; a one-dimensional time series can be thought of as a field over an even grid in 1 dimension that fluctuates over time.
TimeseriesBase.TimeSeries.MultivariateTimeseries
— TypeMultivariateTimeseries{T}
A type alias for a multivariate time series (A matrix, with a first Ti
dimension and an arbitrary second dimension).
TimeseriesBase.TimeSeries.RegularIndex
— TypeRegularIndex
A type alias for a regularly sampled dimension, wrapping an AbstractRange
.
TimeseriesBase.TimeSeries.RegularTimeIndex
— TypeRegularTimeIndex
A type alias for a tuple of dimensions containing a TimeIndex
and any number of other dimensions.
TimeseriesBase.TimeSeries.RegularTimeseries
— TypeRegularTimeseries{T, N, B}
A type alias for a regularly sampled time series.
TimeseriesBase.TimeSeries.SpikeTrain
— TypeSpikeTrain
A type alias for a spike-train time series, which contains spike times in the time dimension and true
for all values corresponding to a spike. The spike times can be retrieved with times(x)
.
TimeseriesBase.TimeSeries.TimeIndex
— TypeTimeIndex
A type alias for a tuple containing a time dimension and any number of other dimensions.
TimeseriesBase.TimeSeries.UnivariateTimeseries
— TypeUnivariateTimeseries{T}
A type alias for a time series with one variable (a vector with only a Ti
dimension).
TimeseriesBase.ToolsArrays.Var
— TypeVar
A DimensionalData.jl dimension representing the variables of a multivariate time series.
TimeseriesBase.TimeSeries.Timeseries
— MethodTimeseries(x, t, v)
Constructs a multivariate time series with time t, variable v, and data x.
Examples
julia> t = 1:100;
julia> v = [:a, :b, :c];
julia> x = rand(100, 3);
julia> mts = Timeseries(x, t, v)
julia> mts isa typeintersect(MultivariateTimeseries, RegularTimeseries)
TimeseriesBase.TimeSeries.Timeseries
— MethodTimeseries(x, t)
Constructs a univariate time series with time t
and data x
.
Examples
julia> using TimeseriesBase, Unitful;
julia> t = 1:100
julia> x = rand(100)
julia> ts = Timeseries(x, t)
julia> ts isa typeintersect(UnivariateTimeseries, RegularTimeseries)
TimeseriesBase.Spectra.AbstractSpectrum
— TypeAbstractSpectrum{T, N, B}
A type alias for an AbstractToolsArray
in which the first dimension is 𝑓
requency.
TimeseriesBase.Spectra.FreqIndex
— TypeFreqIndex
A type alias for a tuple of dimensions, where the first dimension is of type FrequencyDim
.
TimeseriesBase.Spectra.MultivariateSpectrum
— TypeMultivariateSpectrum{T} = AbstractSpectrum{T, 2} where T
A type alias for a multivariate spectrum.
TimeseriesBase.Spectra.RegularFreqIndex
— TypeRegularFreqIndex
A type alias for a tuple of dimensions, where the first dimension is a regularly sampled 𝑓
requency.
TimeseriesBase.Spectra.RegularSpectrum
— TypeRegularSpectrum{T, N, B}
A type alias for a spectrum with a regularly sampled frequency index.
TimeseriesBase.Spectra.UnivariateSpectrum
— TypeUnivariateSpectrum{T} = AbstractSpectrum{T, 1} where T
A type alias for a univariate spectrum.
TimeseriesBase.ToolsArrays.𝑓
— Type𝑓
A DimensionalData.jl dimension representing the frequency domain.
TimeseriesBase.Spectra.Spectrum
— MethodSpectrum(f, v, x)
Constructs a multivariate spectrum with frequencies f
, variables v
, and data x
.
TimeseriesBase.Spectra.Spectrum
— MethodSpectrum(f, x)
Constructs a univariate spectrum with frequencies f
and data x
.
IntervalSets.Interval
— MethodIntervalSets.Interval(x::AbstractTimeseries)
Returns an interval representing the range of the AbstractTimeseries
x
.
Examples
julia> using IntervalSets;
julia> t = 1:100;
julia> x = rand(100);
julia> ts = Timeseries(x, t);
julia> IntervalSets.Interval(ts) == (1..100)
Base.step
— Methodstep(x::RegularTimeseries)
Returns the step size (time increment) of a regularly sampled RegularTimeseries
.
Examples
julia> t = 1:100;
julia> x = rand(100);
julia> rts = Timeseries(x, t);
julia> step(rts) == 1
TimeseriesBase.Utils.align
— Methodalign(x::AbstractDimArray, ts, dt; dims = 1)
Align a DimArray
x
to each of a set of dimension values ts
, selecting a window given by dt
centered at each element of ts
. dt
can be a two-element vector/tuple, or an interval. The dims
argument specifies the dimension along which the alignment is performed. Each element of the resulting DimArray
is an aligned portion of the original x
.
TimeseriesBase.Utils.buffer
— Methodbuffer(x::RegularTimeseries, n::Integer, p::Integer; kwargs...)
Buffer a time series x
with a given window length and overlap between successive buffers.
Arguments
x
: The regular time series to be buffered.n
: The number of samples in each buffer.p
: The number of samples of overlap betweeen the buffers.0
indicates no overlap- +
2
indicates2
samples of overlap between successive buffers - -
2
indicates2
samples of gap between buffers
See also: window
, delayembed
, coarsegrain
TimeseriesBase.Utils.centralderiv!
— Methodcentralderiv!(x::RegularTimeseries; kwargs...)
Compute the central derivative of a regular time series x
, in-place. See centraldiff!
for available keyword arguments.
TimeseriesBase.Utils.centralderiv
— Methodcentralderiv(x::AbstractTimeseries)
Compute the central derivative of a time series x
. See centraldiff
for available keyword arguments. Also c.f. centralderiv!
.
TimeseriesBase.Utils.centraldiff!
— Methodcentraldiff!(x::RegularTimeseries; dims=𝑡, grad=-)
Compute the central difference of a regular time series x
, in-place. The first and last elements are set to the forward and backward difference, respectively. The dimension to perform differencing over can be specified as dims
, and the differencing function can be specified as grad
(defaulting to the euclidean distance, -
)
TimeseriesBase.Utils.centraldiff
— Methodcentraldiff(x::RegularTimeseries; dims=𝑡, grad=-)
Compute the central difference of a regular time series x
. The first and last elements are set to the forward and backward difference, respectively. The dimension to perform differencing over can be specified as dims
, and the differencing function can be specified as grad
(defaulting to the euclidean distance, -
) See centraldiff!
.
TimeseriesBase.Utils.coarsegrain
— Methodcoarsegrain(X::AbstractArray; dims = nothing, newdim=ndims(X)+1)
Coarse-grain an array by taking every second element over the given dimensions dims
and concatenating them in the dimension newdim
. dims
are coarse-grained in sequence, from last to first. If dims
is not specified, we iterate over all dimensions that are not newdim
. If the array has an odd number of slices in any dims
, the last slice is discarded. This is more flexibile than the conventional, mean-based definition of coarse graining: it can be used to generate coarse-grained distributions from an array. To recover this conventional mean-based coarse-graining:
C = coarsegrain(X)
mean(C, dims=ndims(C))
TimeseriesBase.Utils.delayembed
— Functiondelayembed(x::UnivariateRegular, n::Integer, τ::Integer, p::Integer=1; kwargs...)
Delay embed a univariate time series x
with a given dimension n
, delay τ
, and skip length of p
Arguments
x
: The regular time series to be delay embedded.n
: The embedding dimension, i.e., the number of samples in each embedded vector.τ
: The number of original sampling periods between each sample in the embedded vectors.p
: The number of samples to skip between each successive embedded vector.
TimeseriesBase.Utils.duration
— Methodduration(x::AbstractTimeseries)
Returns the duration of the AbstractTimeseries
x
.
Examples
julia> t = 1:100;
julia> x = rand(100);
julia> ts = Timeseries(x, t);
julia> TimeseriesBase.duration(ts) == 99
TimeseriesBase.Utils.rectifytime
— Methodrectifytime(X::AbstractTimeseries; tol = 6, zero = false)
Rectifies the time values of an IrregularTimeseries
. Checks if the time step of the input time series is approximately constant. If it is, the function rounds the time step and constructs a RegularTimeseries
with range time indices. If the time step is not approximately constant, a warning is issued and the rectification is skipped.
Arguments
X::IrregularTimeseries
: The input time series.tol::Int
: The number of significant figures for rounding the time step. Default is 6.zero::Bool
: Iftrue
, the rectified time values will start from zero. Default isfalse
.
TimeseriesBase.Utils.samplingperiod
— Methodsamplingperiod(x::RegularTimeseries)
Returns the sampling period (step size) of a regularly sampled RegularTimeseries
.
Examples
julia> t = 1:100;
julia> x = rand(100);
julia> rts = Timeseries(x, t);
julia> samplingperiod(rts) == 1
TimeseriesBase.Utils.samplingrate
— Methodsamplingrate(x::RegularTimeseries)
Returns the sampling rate (inverse of the step size) of a regularly sampled RegularTimeseries
.
Examples
julia> t = 1:100;
julia> x = rand(100);
julia> rts = Timeseries(x, t);
julia> samplingrate(rts) == 1
TimeseriesBase.Utils.stitch
— Methodstitch(x, args...)
Stitch multiple time series together by concatenating along the time dimension generating new contiguous time indices. The time series must be of the same type (UnivariateRegular
, MultivariateRegular
, or AbstractArray
), and the sampling period and dimensions of the data arrays must match. If the arguments are `MultivariateRegular, they must have the same dimensions (except for the time dimension).
Arguments
X
: The first time series.args...
: Additional time series.
Returns
- A new time series containing the concatenated data.
TimeseriesBase.Utils.times
— Methodtimes(x::AbstractTimeseries)
Returns the time indices of the AbstractTimeseries
x
.
Examples
julia> t = 1:100;
julia> x = rand(100);
julia> ts = Timeseries(x, t);
julia> times(ts) == t
TimeseriesBase.Utils.window
— Functionwindow(x::RegularTimeseries, n::Integer, p::Integer; kwargs...)
Window a time series x
with a given window length and step between successive windows.
Arguments
x
: The regular time series to be windows.n
: The number of samples in each window.p
: The number of samples to slide each successive window.
See also: buffer
, delayembed
, coarsegrain