TimeseriesUtilities

DOI version

Documentation for TimeseriesUtilities.

Most of the utilities operate on the time dimension by default, but you can specify other dimensions using the dim or query parameter.

TimeseriesUtilities.TimeseriesUtilitiesModule
TimeseriesUtilities

A collection of utilities to simplify common time series analysis.

From data cleaning to arithmetic operations (e.g. linear algebra) to common time series operations (e.g. resampling, filtering).

Data Cleaning

Query

(Windowed) Statistics

Algebra

Time-Domain Operations

Time-Frequency Domain Operations

source

Installation

using Pkg
Pkg.add("TimeseriesUtilities")

API Reference

TimeseriesUtilities.TimeseriesUtilitiesModule
TimeseriesUtilities

A collection of utilities to simplify common time series analysis.

From data cleaning to arithmetic operations (e.g. linear algebra) to common time series operations (e.g. resampling, filtering).

Data Cleaning

Query

(Windowed) Statistics

Algebra

Time-Domain Operations

Time-Frequency Domain Operations

source
TimeseriesUtilities.DiffQType
DiffQ(v, t; dim=1)

Difference quotient of v with respect to t.

Date/DateTime differences are converted to seconds before division.

source
TimeseriesUtilities.WindowedViewType
WindowedView(data, coords, windows; dim=1)
WindowedView(data, coords, before, after; dim=1)

AbstractVector of views into data, one per time window. wv[i] returns the slice of data along dimension dim whose coords fall within the i-th window.

windows is any indexable source of (t_start, t_stop) pairs (e.g. TimeRanges). The before/after form builds a symmetric window around each point in coords. Requires coords to be sorted.

source
TimeseriesUtilities.cadenceMethod
cadence(x; tol=2, rtol=exp10(-tol-1), check=true)

Robust sampling cadence of times(x) via IQR-trimmed modal clustering on positive diffs, refined by the median of values near the modal center. With check=true, warns on gaps or non-integer-multiple intervals.

source
TimeseriesUtilities.find_outliersMethod
find_outliers(A, [method, window]; dim = nothing, kw...)

Find outliers in data A along the specified dim dimension.

Returns a Boolean array whose elements are true when an outlier is detected in the corresponding element of A.

The default method is :median (other option is :mean), which uses the median absolute deviation (MAD) to detect outliers. When the length of A is greater than 256, it uses a moving window of size 16.

See also: find_outliers_median, find_outliers_mean, isoutlier - MATLAB

source
TimeseriesUtilities.pspectrumFunction
pspectrum(A, times; dim=ndims(A), nfft=256, noverlap=div(nfft, 2), window=DSP.hamming)
pspectrum(A; dim=nothing, freqdim=:frequency, kwargs...)

Compute a time-frequency power spectrum with DSP.spectrogram.

For containers that implement the common axis API, times are inferred from the selected axis and the result is rebuilt with frequency and spectrogram time axes followed by the remaining axes.

source
TimeseriesUtilities.replace_outliers!Method
replace_outliers!(A, method, [find_method, window]; kwargs...)
replace_outliers!(A, method, outliers; kwargs...)

Replaces outliers in A with values determined by the specified method.

Outliers can be detected using find_outliers with optional find_method and window parameters or specified directly as a Boolean array outliers.

method can be one of the following:

  • :linear: Linear interpolation of neighboring, nonoutlier values
  • :previous: Previous nonoutlier value
  • :next: Next nonoutlier value
  • :nearest: Nearest nonoutlier value

See also: filloutliers - MATLAB

source
TimeseriesUtilities.smoothMethod
smooth(data, times, window; dim=ndims(data))
smooth(data, window; dim=ndims(data))

Smooths a time series by computing a moving average over a sliding window. Edge windows are truncated, so the output has the same size as the input.

Each window covers the half-open interval [coord - before, coord + after). A scalar window is interpreted as a coordinate span along the smoothed axis.

Arguments

  • dim=ndims(data): Dimension along which to perform smoothing
  • op=nanmean: Function used to aggregate each window
source
TimeseriesUtilities.tclipsMethod
tclips(xs...; trange=common_timerange(xs...))

Clip multiple arrays to a common time range trange.

If trange is not provided, automatically finds the common time range across all input arrays.

source
TimeseriesUtilities.tfilterFunction
tfilter(A, times, Wn1, Wn2=nothing; dim=ndims(A), designmethod=nothing)
tfilter(A, Wn1, Wn2=nothing; dim=nothing, designmethod=nothing)

Bandpass filter A between Wn1 and Wn2. The upper cutoff defaults to the Nyquist frequency.

References

  • https://docs.juliadsp.org/stable/filters/
  • https://www.mathworks.com/help/signal/ref/filtfilt.html
  • https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.filtfilt.html
source
TimeseriesUtilities.time_gridMethod
time_grid(x, dt)

Create a time grid from the minimum to maximum time in x with the step size dt.

Examples

# Create hourly time grid
time_grid(x, Hour(1))
time_grid(x, 1u"hr")

# Create 1-s intervals
time_grid(x, Second(1))
time_grid(x, 1u"second")
time_grid(x, 1u"Hz")
source
TimeseriesUtilities.timerangeMethod
timerange(x)
timerange(x1, xs...)

Get the time range of time series data x.

For a single argument, returns a tuple (tmin, tmax) containing the minimum and maximum times. For multiple arguments, returns the common time range (intersection) across all arrays - equivalent to common_timerange(x1, xs...).

Examples

# Single time series
times = [1, 2, 3, 4, 5]
timerange(times)  # (1, 5)

# Multiple time series - find common range
x1_times = [1, 2, 3, 4]
x2_times = [2, 3, 4, 5]
timerange(x1_times, x2_times)  # (2, 4)

See also: common_timerange, tminimum, tmaximum

source
TimeseriesUtilities.tinterpMethod
tinterp(A, old_times, new_times; interp=LinearInterpolation)

Interpolate time series A at new time points new_times.

The interp constructor must accept (u, t; kws...) and return a callable object. Its syntax is compatible with DataInterpolations.jl.

Examples

# Interpolate at a single time point
tinterp(time_series, DateTime("2023-01-01T12:00:00"))

# Interpolate at multiple time points using cubic spline interpolation
new_times = DateTime("2023-01-01"):Hour(1):DateTime("2023-01-02")
tinterp(time_series, new_times; interp = CubicSpline)
source
TimeseriesUtilities.tmask!Method
tmask!(A, t0, t1; dim=nothing)
tmask!(A, its; dim=nothing)

Mask all data values within the specified time range(s) (t0, t1) / its with NaN.

source
TimeseriesUtilities.tmeanMethod
tmean(x, [dt]; dim=nothing)

Calculate the arithmetic mean of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tmedianMethod
tmedian(x, [dt]; dim=nothing)

Calculate the median of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tselectMethod
tselect(A, t; dim=nothing)
tselect(A, t, δt; dim=nothing)

Select the value of A at time nearest to t.

With δt, restricts the search to [t-δt, t+δt] and returns missing if that window is empty.

source
TimeseriesUtilities.tsemMethod
tsem(x, [dt]; dim=nothing)

Calculate the standard error of the mean of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tsplitMethod
tsplit(t0, t1, n::Int)
tsplit(t0, t1, dt)
tsplit(t0, t1, dtType::Type{<:Period})

Split the range from t0 to t1 into n parts, dt-sized parts, or by period type (e.g., Month, Day).

source
TimeseriesUtilities.tstdMethod
tstd(x, [dt]; dim=nothing)

Calculate the standard deviation of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tsumMethod
tsum(x, [dt]; dim=nothing)

Calculate the sum of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tsyncMethod
tsync(A, Bs...)

Synchronize multiple time series to have the same time points.

This function aligns time series Bs... to match time points of A by:

  1. Finding common time range between all time series
  2. Extracting subset of A within common range
  3. Interpolating each series in Bs... to match the time points of the subset of A

Examples

A_sync, B_sync, C_sync = tsync(A, B, C)

See also: tinterp, common_timerange

source
TimeseriesUtilities.tvarMethod
tvar(x, [dt]; dim=nothing)

Calculate the variance of x along dimension dim, optionally grouped by dt.

dim accepts integer index, dimension type/instance, or nothing (defaults to the time dimension).

source
TimeseriesUtilities.tviewMethod
tview(A, t0, t1; dim=nothing)
tview(f, t0, t1)

View A in time range [t0, t1] along dimension dim.

When f is callable, returns f(t0, t1).

source
TimeseriesUtilities.window_bf_sizesMethod
window_bf_sizes(window)

Converts a window specification to backward and forward window sizes.

When window is a positive integer scalar, the window is centered about the current element and contains window-1 neighboring elements. If window is even, then the window is centered about the current and previous elements.

source