utils

exception OutOfInterval

Bases: Exception

An error raised when a value is outside of an acceptable timestamp interval.

get_interval_slice(timestamps, start, stop)

Gets a slice object representing the indices of timestamps within a specified interval.

Parameters:
  • timestamps (ndarray) – The timestamps to get the slice for.

  • start (int | float) – The start of the interval (inclusive).

  • stop (int | float) – The end of the interval (inclusive).

Return type:

slice

Returns:

A slice object representing the indices of timestamps within the interval.

greatest_common_denominator(values, return_inverse=True)

Finds the greatest common denominator (GCD) of a list of numbers and optionally returns its inverse.

Parameters:
  • values (Iterable[float]) – The values to find the GCD of.

  • return_inverse (bool, default: True) – Whether to return the inverse of the GCD.

Return type:

float

Returns:

The GCD of the values, or its inverse if return_inverse=True.

validate_keys(keys, keys_reference, allow_missing)

Validates that a set of keys conforms to a reference set, optionally allowing missing keys.

Parameters:
  • keys (Iterable[str]) – The set of keys to validate.

  • keys_reference (Iterable[str]) – The reference set of valid keys.

  • allow_missing (bool) – Whether to allow missing keys.

Return type:

bool

Returns:

If the keys pass validation.

Raises:

KeyError – If any keys are undefined or if missing keys are not allowed.

validate_timestamps(timestamps)

Validates that a sequence of timestamps contains no duplicates. Always returns True if the timestamps are valid, otherwise raises a ValueError.

Parameters:

timestamps (ndarray | int | float | str | integer | floating) – The timestamps to validate.

Return type:

bool

Returns:

If the timestamps are valid.

Raises:
validated_length(*values)

Returns the length of all iterable values if they are all the same length. Returns None if all values are scalars.

Parameters:

*values (ndarray | int | float | str | integer | floating) – An iterable of values to check the length of.

Return type:

int | None

Returns:

The length of all iterable values if they are all the same length. Returns None if all values are scalars.

Raises:

ValueError – If the values have unequal lengths or contain unsized iterables objects.

writeable(*arrays)

A context manager that temporarily makes numpy arrays writeable.

This context manager is useful when you need to modify the contents of a read-only array within a specific block of code. It ensures that the writeable flag is restored to its original state after the block, even if exceptions occur.

Parameters:

arrays (ndarray) – The arrays to make writeable.

Raises:

Exception – Any exception raised within the context is re-raised.

Return type:

Generator