The Const class#

Aliases#

halerium.core.operator.Const
class Const(value, shape=None, dtype=None)#

Operator representing constant (i.e. non-random) values.

Operator representing constant (i.e. non-random) values.

Parameters:
  • value – The value of the constant expression.

  • shape – The shape of the expression. Should only be set if intended shape is different from the shape inferred from the value argument.

  • dtype – The data type of the values. The default is None in which case the data type is determined from the data type of the value: All boolean values yield ‘bool’, whereas integer or floating-point values yield ‘float’. Thus, dtype=’int’ must be explicitly specified to get a Const operator with dtype=’int’, even if the data is all integers.

accept(visitor)#
depends_on(operators, stop_operators=None, stop_criterion=None, include_links=True)#

Check if self depends on operators.

Check whether the operator depends on another operator. The chain of dependencies will be interrupted by the operators in stop_operators or the stop_criterion.

Parameters:
  • operators (Iterable, OperatorBase,) – The operators for which the possible influence on self is to be checked.

  • stop_operators (iterable, optional) – The operators which interrupt the dependency chain. The default is None.

  • stop_criterion (callable, optional) – A callable that accepts an operator as an argument and returns True for stop and False otherwise. The default is None which means don’t stop.

  • include_links (bool, optional) – Whether a linked variable if viewed as dependent on the linked source.

Returns:

result – Whether self depends on operators

Return type:

bool

Examples

>>> import halerium
>>> a = halerium.core.operator.Const(0.)
>>> b = a + 1
>>> c = b + 1
>>> c.depends_on(a)
True
>>> c.depends_on(a, stop_operators=[b])
False
evaluate(cache=None)#

Evaluate operator.

Evaluate the operator and return a numerical value for it. This involves evaluating all the operator’s operands, all operands of those operands, and so on. When evaluating a variable, a value from its distribution is drawn.

Values computed for operands are stored in a cache for reuse should such a value be required again during the evaluation (e.g. when an operator appears more than once in the operand tree). That cache can be provided with values for operators by the user to be used during evaluation.

Parameters:

cache (dict, optional) – The cache used to store values of intermediate results for later reuse in case the corresponding expression is encountered again during evaluation. The cache can be used to provide values for operators that might be encountered during evaluation.

Returns:

The result of evaluating the operator. A number or numpy ndarray.

Return type:

result

Examples

>>> import halerium.core as hal
>>>
>>> x = hal.Variable('x', shape=(), mean=0, variance=1)
>>> y = hal.exp(x) + 1
>>> z = hal.log(y - 1)
>>> z.evaluate({x: 3})
3.0
get_containing_class(scopetor_cls)#

Get the closest parent that is of a specific type.

Parameters:

scopetor_cls (scopetor subclass) – The scopetor subclass of which the next containing instance is to be found.

Raises:

ValueError – If no containing scopetor of type scopetor_cls is found, an error is raised.

Returns:

scope – The containing scopetor of type scopetor_cls.

Return type:

scopetor_cls

Examples

>>> from halerium.core import Graph, Entity
>>> g = Graph("g")
>>> with g:
>>>     e = Entity("e")
>>>     with e:
>>>         Entity("m")
>>> g.e.m.get_containing_class(Graph) is g
True
>>> assert g.e.m.get_containing_class(Entity) is g.e
True
get_immediate_dependencies(follow_links)#

Get immediate dependencies.

Get the immediate dependencies of the operator. - If follow_links, for operators (variables) that are targets of a link,

return the source of that link.

  • For variables that are not targets of a link, return their distribution’s operator parameters.

  • For operators with operands (N-ary operators) return the operands.

Parameters:

follow_links (bool) – Whether to follow links. Otherwise ignore them.

Returns:

dependencies – The set of operators that self directly depends on.

Return type:

Set

get_operator_dependencies(stop_operators=None, stop_criterion=None, include_links=True)#

Get operator dependencies.

Get the operator chain that leads to the operator. The chain of dependencies will be interrupted by the operators in stop_operators or the stop_criterion.

Parameters:
  • stop_operators (iterable, optional) – A list, tuple or set of operators which interrupt the dependency chain. The default is None.

  • stop_criterion (callable, optional) – A callable that accepts an operator as an argument and returns True for stop and False otherwise. The default is None which means don’t stop.

  • include_links (bool, optional) – Whether a linked variable is viewed as dependent on the linked source.

Returns:

dependencies – A nested dictionary with the dependecies. Keys are operators, values are again dicts. The key-operators depend on the key-operators of the next dict and so on.

Return type:

dict

Examples

>>> import halerium
>>> a = halerium.core.operator.Const(0.)
>>> b = a + 1
>>> c = b + 1
>>> c.get_operator_dependencies()
{b: {a: {}}
>>> c.depends_on(a, stop_operators=[b])
{b: {}}
property global_name#

The global name of the scopee.

The global name is a chain of all parent names of this scopee separated by ‘/’ characters.

is_child_of(scopetor, include_self=True)#

Whether is child of scopetor.

This method checks whether the scopee is a (grand-) child of a given scopetor.

Parameters:
  • scopetor (Scopetor) – The (possible) parent scopetor

  • include_self (bool) – If True, a.is_child_of(a) would return True. If False, a.is_child_of(a) would return False.

Returns:

Whether self is a (grand-)child of scopetor or not.

Return type:

bool

is_circular(stop_criterion=None)#
property name#

The name of the scopee.

select_from_operator_dependencies(selection_criterion, pre_stop_operators=None, pre_stop_criterion=None, post_stop_operators=None, post_stop_criterion=None, include_links=True, selected=None)#

Select operators from operator dependency tree.

Scans the operator dependency tree for operators satisfying the selection criterion and collects these operators into a set.

Parameters:
  • selection_criterion (callable) – The selection criterion for inclusion. A callable that accepts an operator as an argument and returns True if the argument should be included.

  • pre_stop_operators (collections.abc.Collection[OperatorBase]) – A list, tuple or set of operators which interrupt the dependency chain before selection. The default is None.

  • pre_stop_criterion (callable, None) – The stop criterion before selection. A callable that accepts an operator as an argument and returns True when to stop following further dependencies. The default is None which means don’t stop.

  • post_stop_operators (collections.abc.Collection[OperatorBase]) – A list, tuple or set of operators which interrupt the dependency chain after selection. The default is None.

  • post_stop_criterion (callable, None) – The stop criterion after selection. A callable that accepts an operator as an argument and returns True when to stop following further dependencies. The default is None which means don’t stop.

  • include_links (bool) – Whether a linked variable is viewed as dependent on the linked source. The default is True.

  • selected (Set, None) – The set to which the selected operators are added.

Returns:

selected – The set to with the selected operators are added.

Return type:

Set