Additional util functions

class darts.utils.utils.ModelMode(value)[source]

Bases: Enum

An enumeration.

ADDITIVE = 'additive'
MULTIPLICATIVE = 'multiplicative'
NONE = None
class darts.utils.utils.SeasonalityMode(value)[source]

Bases: Enum

An enumeration.

ADDITIVE = 'additive'
MULTIPLICATIVE = 'multiplicative'
NONE = None
class darts.utils.utils.TrendMode(value)[source]

Bases: Enum

An enumeration.

EXPONENTIAL = 'exponential'
LINEAR = 'linear'
darts.utils.utils.drop_after_index(index, split_point)[source]

Drops everything after the provided time split_point (excluded) from the index.

Parameters
  • index (Union[RangeIndex, DatetimeIndex]) – The index to drop values from.

  • split_point (Union[int, Timestamp]) – The timestamp that indicates cut-off time.

Returns

A new index with values after split_point dropped.

Return type

Union[pd.RangeIndex, pd.DatetimeIndex]

darts.utils.utils.drop_before_index(index, split_point)[source]

Drops everything before the provided time split_point (excluded) from the index.

Parameters
  • index (Union[RangeIndex, DatetimeIndex]) – The index to drop values from.

  • split_point (Union[int, Timestamp]) – The timestamp that indicates cut-off time.

Returns

A new index with values before split_point dropped.

Return type

Union[pd.RangeIndex, pd.DatetimeIndex]

darts.utils.utils.expand_arr(arr, ndim)[source]

Expands a np.ndarray to ndim dimensions (if not already satisfied).

darts.utils.utils.generate_index(start=None, end=None, length=None, freq=None, name=None)[source]

Returns an index with a given start point and length. Either a pandas DatetimeIndex with given frequency or a pandas RangeIndex. The index starts at

Parameters
  • start (Union[int, str, Timestamp, None]) – The start of the returned index. If a pandas Timestamp or a date string is passed, the index will be a pandas DatetimeIndex. If an integer is passed, the index will be a pandas RangeIndex index. Works only with either length or end.

  • end (Union[int, str, Timestamp, None]) – Optionally, the end of the returned index. Works only with either start or length. If start is set, end must be of same type as start. Else, it can be either a pandas Timestamp or an integer.

  • length (Optional[int, None]) – Optionally, the length of the returned index. Works only with either start or end.

  • freq (Union[str, int, DateOffset, None]) – The time difference between two adjacent entries in the returned index. In case start is a timestamp, a DateOffset alias is expected; see docs. By default, “D” (daily) is used. If start is an integer, freq will be interpreted as the step size in the underlying RangeIndex. The freq is optional for generating an integer index (if not specified, 1 is used).

  • name (Optional[str, None]) – Optionally, an index name.

Return type

Union[DatetimeIndex, RangeIndex]

darts.utils.utils.likelihood_component_names(components, parameter_names)[source]

Generates formatted likelihood parameter names for components and parameter names.

The order of the returned names is: [comp1_param_1, … comp1_param_n, …, comp_n_param_n].

Parameters
  • components (Union[Index, list[str]]) – A sequence of component names to add to the beginning of the returned names.

  • parameter_names (list[str]) – A sequence of likelihood parameter names to add to the end of the returned names.

darts.utils.utils.n_steps_between(end, start, freq)[source]

Get the number of time steps with a given frequency freq between end and start. Works for both integers and time stamps.

  • if end, start, freq are all integers, we can simple divide the difference by the frequency.

  • if freq is a pandas Dateoffset with non-ambiguous timedelate (e.g. “d”, “h”, …, and not “ME”, “YE”, …),

    we can simply divide by the frequency

  • otherwise, we take the period difference between the two time stamps.

Parameters
  • end (Union[Timestamp, int]) – The end pandas Timestamp / integer.

  • start (Union[Timestamp, int]) – The start pandas Timestamp / integer.

  • freq (Union[DateOffset, int, str]) – The frequency / step size.

Returns

The number of steps/periods between end and start with a given frequency freq.

Return type

int

Examples

>>> n_steps_between(start=pd.Timestamp("2000-01-01"), end=pd.Timestamp("2000-03-01"), freq="ME")
2
>>> n_steps_between(start=0, end=2, freq=1)
2
>>> n_steps_between(start=0, end=2, freq=2)
1
darts.utils.utils.quantile_interval_names(q_interval, component=None)[source]

Generates formatted quantile interval names, optionally added to a component name.

Parameters
  • q_interval (Union[tuple[float, float], Sequence[tuple[float, float]]]) – A tuple or multiple tuples with the (lower bound, upper bound) of the quantile intervals.

  • component (Optional[str, None]) – Optionally, a component name to add to the beginning of the quantile names.

darts.utils.utils.quantile_names(q, component=None)[source]

Generates formatted quantile names, optionally added to a component name.

Parameters
  • q (Union[float, list[float]]) – A float or list of floats with the quantiles to generate the names for.

  • component (Optional[str, None]) – Optionally, a component name to add to the beginning of the quantile names.

darts.utils.utils.random_method(decorated)[source]

Decorator usable on any method within a class that will provide a random context.

The decorator will store a _random_instance property on the object in order to persist successive calls to the RNG.

This is the equivalent to darts.utils.torch.random_method but for non-torch models.

Parameters

decorated (Callable[…, ~T]) – A method to be run in an isolated torch random context.

Return type

Callable[…, ~T]

darts.utils.utils.sample_from_quantiles(vals, quantiles, num_samples)[source]

Generates num_samples samples from quantile predictions using linear interpolation. The generated samples should have quantile values close to the quantile predictions. For the lowest and highest quantiles, the lowest and highest quantile predictions are repeated.

Parameters
  • vals (ndarray) – A numpy array of quantile predictions/values. Either an array with two dimensions (n times, n components * n quantiles), or with three dimensions (n times, n components, n quantiles). In the two-dimensional case, the order is first by ascending column, then by ascending quantile value (comp_0_q_0, comp_0_q_1, … comp_n_q_m)

  • quantiles (ndarray) – A numpy array of quantiles.

  • num_samples (int) – The number of samples to generate.

darts.utils.utils.slice_index(index, start, end)[source]

Returns a new Index with the same type as the input index, containing the values between start and end included. If start and end are not in the index, the closest values are used instead. The start and end values can be either integers (in which case they are interpreted as indices), or pd.Timestamps (in which case they are interpreted as actual timestamps).

Parameters
  • index (Union[RangeIndex, DatetimeIndex]) – The index to slice.

  • start (Union[int, Timestamp]) – The start of the returned index.

  • end (Union[int, Timestamp]) – The end of the returned index.

Returns

A new index with the same type as the input index, but with only the values between start and end included.

Return type

Union[pd.RangeIndex, pd.DatetimeIndex]