reno.components.Piecewise#

class reno.components.Piecewise(equations, conditions)#

Bases: EquationPart

A conditional allowing evaluation of two or more condition equations to determine which output equation to use.

Mathematically allows something like:

⎧ 1 if t < 4

f(t) = ⎨ 5 if 4 <= t < 7

⎩ 9 + t if 7 <= t

Parameters:
  • equations (list[EquationPart]) – The possible equation branches that selectively evaluate based on which condition is true. Must have same number of equations as conditions.

  • conditions (list[Callable]) – The boolean conditions to evaluate to determine which equation to output. These can all either be an equation based on EquationPart (see the boolean ops in ops.py), or a function that accepts a single parameter (t) as an input. Must have same number of conditions as equations.

Methods

__init__(equations, conditions)

clip(min, max)

equal(obj)

eval([t, save, force])

Evaluate condition equations/functions until a True is returned, and then evaluate and return the corresponding equation.

eval_condition(i, *args, **kwargs)

Recursively build nested np.where to handle all conditions.

find_refs_of_type(search_type[, already_checked])

Actually recursive as opposed to seek_refs, returns a list of all equation parts matching passed type.

get_shape()

For now this is returning an integer because we only allow a single additional dimension.

get_type()

Similar to shape, this gets computed recursively, used to automatically determine if the value needs to be initialized with a certain numpy type.

is_static()

Convenience shortcut for reno.utils.is_static() - True if this equation doesn't rely on any dynamic values (thus constant), False if it does.

latex(**kwargs)

Get a string representation for the cool left bracket with the equations and their corresponding conditions.

not_equal(obj)

parse(arg_strs, refs)

Parsing a piecewise has to be handled differently from normal operations because of the collection of associated arguments.

pt(**refs)

Get a pytensor graph representing this piece of an equation.

pt_condition(i, refs)

Get the pytensor equation starting at the ith condition (using ifelse).

pt_condition_str(i, refs)

Get the string for the pytensor code for the ith condition (using ifelse).

pt_str(**refs)

Construct a string containing relevant pytensor code for this piece of the equation.

seek_refs()

Immediate refs only, depth=1.

series_max()

series_min()

sum([axis])

Attributes

dtype

The type of each underlying value.

shape

The size of the data dimension, 1 by default.

timeseries

Get a timeseries view of the data (includes all historical data across all timesteps.)

__annotations__ = {}#
__module__ = 'reno.components'#
__repr__()#

Return repr(self).

eval(t=0, save=False, force=False, **kwargs)#

Evaluate condition equations/functions until a True is returned, and then evaluate and return the corresponding equation.

Parameters:
  • t (int)

  • save (bool)

  • force (bool)

Return type:

int | float | ndarray

eval_condition(i, *args, **kwargs)#

Recursively build nested np.where to handle all conditions. Note that this does evaluate all equations and all conditions, there are likely ways to improve efficiency/reduce unnecessary compute (e.g. this used to use np.piecewise, but adding data dimensions significantly increased complexity)

get_shape()#

For now this is returning an integer because we only allow a single additional dimension. Note that this shape _does not_ incoporate time or batch dimensions, only the “data” dimension if applicable. This should be overridden by subclasses, e.g. operations which would change the shape.

Return type:

int

get_type()#

Similar to shape, this gets computed recursively, used to automatically determine if the value needs to be initialized with a certain numpy type.

Return type:

type

latex(**kwargs)#

Get a string representation for the cool left bracket with the equations and their corresponding conditions. Conditions that aren’t EquationParts can’t really be converted meaningfully, so are represented simply with \lambda(t)

Return type:

str

static parse(arg_strs, refs)#

Parsing a piecewise has to be handled differently from normal operations because of the collection of associated arguments.

Parameters:
  • arg_strs (list[str])

  • refs (dict[str, Reference])

pt(**refs)#

Get a pytensor graph representing this piece of an equation.

Parameters:

refs (dict[str, TensorVariable])

Return type:

TensorVariable

pt_condition(i, refs)#

Get the pytensor equation starting at the ith condition (using ifelse). Recursively includes all >i conditions.

Return type:

TensorVariable

pt_condition_str(i, refs)#

Get the string for the pytensor code for the ith condition (using ifelse). Recursively includes all >i conditions.

Return type:

str

pt_str(**refs)#

Construct a string containing relevant pytensor code for this piece of the equation. This is useful for “compiling” into pymc code.

Parameters:

refs (dict[str, str])

Return type:

str