echo.utils module

echo.utils.TimeExpandableType = float | list[float] | list[list[float]] | numpy.ndarray

TimeExpandableType

Variables with type TimeExpandableType should only contain numbers (int or float). They should be either a single number e.g 37.3, a list containing a single number e.g. [5], a 1-dimensional list of numbers e.g. [1.1, 2.2, 3.3, 4.4, …] or a 2-dimensional list of number e.g. [[1,2,3,4, …], [5, 6, 7, 8, …]].

class echo.utils.TimeSeriesData(value: float | list[float] | list[list[float]] | ndarray, num_time_intervals: int, num_expansion_intervals: int)

Bases: object

A dataclass to hold a compressed way of describing time series data.

The TimeSeriesData object describes a potentially compressed form of time-series data by allowing you to store only a minimal set of values.

value has type TimeExpandableType. It should only contain numbers (int or float). It should be either a single number e.g 37.3, a list containing a single number e.g. [5], a 1-dimensional list of numbers e.g. [1.1, 2.2, 3.3, 4.4, …] or a 2-dimensional list of number e.g. [[1,2,3,4, …], [5, 6, 7, 8, …]].

For 1-dimensional and 2-dimensional lists, it is important to match the number of elements in the lists to the num_time_intervals and num_expansion_intervals. 1-dimensional lists:

len(value) == num_time_intervals * num_expansion_intervals

2-dimensional lists:

len(value) == num_expansion_intervals AND the sublists in value should have len(sublist) == num_time_intervals

For example, if we have 4 time-intervals and 2 expansion-intervals, then the following would be valid: value = [[1, 2, 3, 4], [5, 6, 7, 8]] (2-dimensional list). Remember: the first index into a 2-dimensional list is the expansion interval and the second index is the time interval.

or we could flatten the above list into a 1-dimensional list: value = [1, 2, 3, 4, 5, 6, 7, 8] since len(value) == 4 * 2. The first four value (1, 2, 3, 4) would apply to the first expansion period and the next four values (5, 6, 7, 8) would apply to the second expansion period.

Note

TimeSeriesData performs no validation. In other words it is possible to create invalid TimeSeriesData objects. For example, TimeSeriesData(value=[1,2,3], num_time_intervals=5, num_expansion_intervals=1) would be invalid because the num_time_intervals (5) is greater than the number of elements in the value (only 3).

TimeSeriesData objects serve as input to the expand_as_array and expand_as_dict functions. These functions serve to expand a TimeSeriesData description into a full set of time series data.

value

time series data. See above for more information on valid values.

Type:

TimeExpandableType

num_time_intervals

the number of time intervals for the time series data.

Type:

int

num_expansion_intervals

the number of expansion intervals for the time series data. Minimum value is 1, which can be thought as having no expansion intervals.

Type:

int

num_expansion_intervals: int
num_time_intervals: int
value: float | list[float] | list[list[float]] | ndarray
exception echo.utils.UnexpandableTimeSeriesDataError

Bases: Exception

echo.utils.clamp(n: float, smallest: float, largest: float) → float
echo.utils.create_input_output_pts_from_coefficients(temp_coef: list[float], input_coef: list[float], temperature_array: list[float], xpts: int, time_periods: int) → tuple[dict, dict]

Generates a set of output (y) points based on two coefficient arrays, one that applies to the input variable and one that applies to temperature, and non decreasing xpts. :param temp_coef: list of coefficients, right to left in increasing order :param input_coef: list of coefficients, right to left in increasing order :param temperature_array: list of temperature data for time intervals T :param xpts: number of x points we want to do our piecewise evaluation over :param time_periods: number of optimisation time periods

Returns:

a dict of lists, where keys are the index set, defining the set of domain breakpoints for

the piecewise linear function.

y: a dict of lists, where keys are the index set, defining the set of domain breakpoints for

the piecewise linear function.

Return type:

x

echo.utils.create_named_constraint_with_rule(model: EchoConcreteModel, con_name: str, rule: Expression) → None

Util function for creating pyomo constraints from a rule.

echo.utils.do_multivariate_regression(x: list[list[float]] | ndarray, y: list[float] | list[list[float]] | ndarray) → tuple[tuple[ndarray, float], float]

Performs multivariate regression on provided data.

Implementation of sklean.linear_model.LinearRegression.

Parameters:
  • x – Test samples. Array-like of shape (n_samples, n_features).

  • y – array-like of shape (n_samples,) or (n_samples, n_outputs). True values for x.

Returns:

coefficient array R^2

echo.utils.domain_from_flow(flow: Flows) → RangeSet
echo.utils.expand_as_array(data: TimeSeriesData) → NDArray

Expands a TimeSeriesData object into a numpy array.

The TimeSeriesData object described (potentially) compressed form of time-series data by only stor

Example

>>> t = TimeSeriesData(value=[1,2,3,4],num_time_intervals=2,num_expansion_intervals=2)
>>> expand_as_array()
array([[1, 2],
    [3, 4]])

Example

>>> t = TimeSeriesData(value=89.2,num_time_intervals=3,num_expansion_intervals=2)
>>> expand_as_array(t)
array([[89.2, 89.2, 89.2],
    [89.2, 89.2, 89.2]])

Example

>>> t = TimeSeriesData(value=[12.1,12.2],num_time_intervals=2,num_expansion_intervals=4)
>>> expand_as_array(t)
array([[12.1, 12.2],
    [12.1, 12.2],
    [12.1, 12.2],
    [12.1, 12.2]])
Parameters:

data (TimeSeriesData) – A TimeSeriesData object.

Returns:

An 2-dimensional array of time series data. The first index is the expansion interval and the second index is the time period.

Raises:
echo.utils.expand_as_dict(data: TimeSeriesData) → dict[tuple[int, int], float]

Converts a TimeSeriesData object to a dictionary of time-series values keyed by the expansion and time intervals.

Calls expand_as_array internally to produce enough values for the time and expansion intervals. See expand_as_array for an explanation of how this works.

Example

>>> expand_as_dict(TimeSeriesData(value=[[1,2],[3,4]],num_time_intervals=2,num_expansion_intervals=2))
{(0, 0): 1, (0, 1): 2, (1, 0): 3, (1, 1): 4}
Parameters:

data (TimeSeriesData) – A TimeSeriesData object.

Returns:

(expansion interval, time interval)

Return type:

dictionary of time series values keyed by a tuple of the form

Raises:
echo.utils.fix_port_variable(model: EchoConcreteModel, var_name: str, new_values: ndarray, expansion_periods: int = 1) → None

Updates existing pyomo variable to have fixed values. :param model: pyomo concrete model :param var_name: pyomo variable name (str) :param new_values: array or list of new values :param expansion_periods: number of expansion periods (int)

echo.utils.generate_array_constraint(constraint: float | list[float] | ndarray, time_periods: int, expansion_periods: int) → dict
Parameters:

constraint – float or array

Returns:

a formatted dict to be used in constraining a variable

Return type:

d

echo.utils.generate_dict_with_pyomo_keys_from_array(array: list[float] | set[float] | ndarray, time_periods: int, expansion_periods: int = 1) → dict[tuple[int, int], float]

Generates a dict suitable for initializing a pyomo var or param.

The dict keys are a tuple (expansion period, time period).

echo.utils.generate_input_output_values_from_polynomial_coeff_array(coeff_array: list[float], pts: list[float]) → tuple[list[float], list[float]]

Generates x and y values for the polynomial function with coefficients defined by the coeff array.

Parameters:
  • array (coeff) – list of polynomial function coefficients

  • pts – list of x values for which we want to evaluate the function described by the coeff array

Returns:

The pts array returned. y: List of values at values of x for polynomial coefficients coeff_array.

Return type:

x

echo.utils.generate_piecewise_input_output_arrays(coeff_array: list[float], pts: list[float]) → tuple[list[float], list[float]]

Generates x and y values for the polynomial function with coefficients defined by the coeff array

Parameters:
  • array (coeff) – list of polynomial function coefficients, indexed by time

  • pts – list of x values for which we want to evaluate the function described by the coeff array

echo.utils.orjson_dumps(v: dict, *, default: Callable | None) → str
echo.utils.populate_values_across_time_and_expansion_indices(values: float | list[float] | set[float] | ndarray, time_periods: int, expansion_periods: int) → dict[tuple[int, int], float]

Takes some input (values) - could be array, or int. Adds a time_period and expansion period key.

Example

values=10 time_periods=4 expansion_periods=2

populate_values_across_time_and_expansion_indices(values, time_periods, expansion_periods) {(0, 0): 10, (0, 1): 10, (0, 2): 10, (0, 3): 10, (1, 0): 10, (1, 1): 10, (1, 2): 10, (1, 3): 10}

echo.utils.set_float_var_bounds(model: EchoConcreteModel, var_name: str, ub: float | None, lb: float | None) → None

Updates the bounds on a pyomo variable. Only floats can be used as bounds. :param model: pyomo concrete model :param var_name: variable name (str) corresponding to a variable in the model :param ub: upper bound value, or None :param lb: lower bound value, or None

Returns:

None

echo.utils.set_var_bounds_from_dict(model: EchoConcreteModel, var_name: str, ub: dict | None, lb: dict | None) → None

Updates the bounds on a pyomo variable using an array of floats. :param var: pyomo variable :param ub: dict of floats, where dict keys match variable index sets, or None :param lb: dict of floats, where dict keys match variable index sets, or None

Returns:

None

echo.utils.tile_array_over_expansion_periods(array: list[float] | set[float] | ndarray, expansion_periods: int) → ndarray

Constructs an array by repeating ‘array’ input x times where x = num of expansion periods

echo.utils.to_initial_values(profile: DataFrame | None, key: str, time_periods: int, expansion_periods: int, scaling: int = 1) → dict[tuple[int, int], float]
echo.utils.to_initial_values_for_array(values: ndarray, time_periods: int | None = None, expansion_periods: int = 1, scaling: int = 1) → dict[tuple[int, int], float]