3.2.2. core module
This module defines some core classes used elsewhere in the package. These include classes to represent yield curves and dynamcic programming state trees.
- class core.Curve(label=None, id=None, is_volume=False, points=None, type='a', is_special=False, period_length=10, xmin=0, xmax=1000, epsilon=0.01, simplify=True)[source]
Bases:
objectDescribes change in state over time (between treatments).
- Parameters:
label (str) – A label for the curve.
id (str) – An ID for the curve.
is_volume (bool) – Flag indicating whether the curve tracks volume. Defaults to
False.points (list) – A list of (x,y) pairs defining the curve.
type (str) – A string indicating the type of curve. Defaults to
'a'.is_special (bool) – Flag indicating whether the curve is special. Defaults to
False. Special curves are immune to simplification.period_length (float) – The length of the period. Defaults to
ws3.common.PERIOD_LENGTH_DEFAULT.xmin (float) – The minimum age. Defaults to
ws3.common.MIN_AGE_DEFAULT.xmax (float) – The maximum age. Defaults to
ws3.common.MAX_AGE_DEFAULT.epsilon (float) – The tolerance for simplifying the curve. Defaults to
ws3.common.CURVE_EPSILON_DEFAULT.simplify (bool) – Flag indicating whether to simplify the curve. Defaults to
True.
- add_points(points, simplify=True, compile_y=False)[source]
Adds points to the curve and optionally simplifies point geometry.
- Parameters:
points (list of tuples) – The points to add to the curve.
simplify (bool) – Flag indicating whether to simplify the curve after adding points. Defaults to
True.compile_y (bool) – Flag indicating whether to compile the y-component after adding points. Defaults to
False.
- cai()[source]
Calculates a current annual increment (CAI) curve.
- Returns:
A curve representing the current annual increment.
- Return type:
ws3.core.Curve
- lookup(y, from_right=False, roundx=False)[source]
Looks up the x-coordinate corresponding to the given y-coordinate.
- Parameters:
y (float) – The y-coordinate to look up.
from_right (bool) – Flag indicating whether to search from the right. Defaults to
False.roundx (bool) – Flag indicating whether to round the x-coordinate to the nearest integer. Defaults to
False.
- Return float:
The x-coordinate corresponding to the given y-coordinate.
- mai()[source]
Calculates a mean annual increment (MAI) curve.
- Returns:
A curve representing the mean annual increment.
- Return type:
ws3.core.Curve
- range(lo=None, hi=None, as_bounds=False, left_range=True)[source]
Returns a Curve representing the range within the specified bounds.
- Parameters:
lo (float) – The lower bound of the range. Defaults to None.
hi (float) – The upper bound of the range. Defaults to None.
as_bounds (bool) – Flag indicating whether to return the range as a tuple of bounds. Defaults to
False.left_range (bool) – Flag indicating whether to look up the upper bound from the left (default) or from the right (widest possible range).
- Returns:
Returns either curve representing the range within the specified bounds, or a tuple representing lower- and upper-bound values (if
as_boundsset toTrue).- Return type:
ws3.core.Curveor tuple
- simplify(points=None, autotune=True, compile_y=False, verbose=False)[source]
Simplifies the curve by removing redundant points.
- Parameters:
points (list of tuples) – The points to simplify. Defaults to None.
autotune (bool) – Flag indicating whether to automatically tune the simplification process. Defaults to True.
compile_y (bool) – Flag indicating whether to compile the y-component. Defaults to False.
verbose (bool) – Flag indicating whether to print verbose output. Defaults to False.
- y(compile_y=False)[source]
Returns the y-values of the curve stored in
self._y(will first compile them ifcompile_yis set toTrueandself._yis empty), else will interpolate a list of y values for each x value inself.x.- Parameters:
compile_y (bool) – Flag indicating whether to compile the y-component of the curve. Defaults to
False.- Return list:
A list of y values.
- class core.Interpolator(points)[source]
Bases:
objectInterpolates x and y values from sparse curve point list.
Used by the
ws3.core.Curveclass to interpolate between real data points.- Parameters:
points (list) – A list of (x,y) coordinate pairs.
- lookup(y, from_right=False)[source]
Looks up the x-coordinate corresponding to the given y-coordinate.
- Parameters:
y (float) – The y-coordinate to look up.
from_right (bool) – Flag indicating whether to search from the right. Defaults to False.
- Return int:
The x-coordinate corresponding to the given y-coordinate.
- class core.Node(nid, data=None, parent=None)[source]
Bases:
objectA node class representing a state in a dynamic programming state tree.
The constructor for a node class.
- Parameters:
nid – The unique ID of this node
data – The data stored in this node
parent – The parent of this node
- add_child(child)[source]
The function adds a child node to the current object.
:param
ws3.core.Nodechild: The child node to be added.
- children()[source]
The function gets the list of child nodes of the current object.
- Returns:
List of child nodes.
- Return type:
list of
ws3.core.Nodeobjects.
- data(key=None)[source]
The function gets the data associated with the current object. If a specific key is provided, return the corresponding value. If no key is provided, return the entire data dictionary.
- Parameters:
key – The key to retrieve a specific value (default is None).
- Returns:
The data associated with the
keyif a key is specified (or the entire data dictionary if a key is not specified).
- is_leaf()[source]
Checks if the current object is a leaf node (i.e., node has no children).
- Returns:
Trueif the object is a leaf node,Falseotherwise.- Return type:
bool
- class core.Tree(period=1)[source]
Bases:
objectRepresents a tree object.
- add_node(data, parent=None)[source]
Adds a new node to the tree.
- Parameters:
data – The data associated with the new node.
parent – The parent node to which the new node will be attached.
- Returns:
The newly created node.
- Return type:
ws3.core.Node
- children(nid)[source]
The function gets the child nodes of the node with the specified ID.
- Parameters:
nid – The ID of the node for which to retrieve children.
- Returns:
List of child nodes.
- Return type:
list of
ws3.core.Nodeobjects.
- grow(data)[source]
Expands the current path by adding a new child node. The new node is added as a child of the last node in the current path. The current path used by the optimization problem formulation functions to iterate over all possible states (in a depth-first-search pattern).
- Parameters:
data – The data associated with the new node.
- Returns:
The newly created node.
- Return type:
ws3.core.Node
- leaves()[source]
Returns all leaf nodes.
- Returns:
A list of all leaf nodes.
- Return type:
list of
ws3.core.Nodeobjects
- node(nid)[source]
Returns a node with the specified ID.
- Parameters:
nid – The unique identifier of the node to be retrieved.
- Returns:
The node object corresponding to the specified ID.
- Return type:
ws3.core.Node
- nodes()[source]
Returns all nodes in the tree. :returns: List of all nodes in the tree. :rtype: list of
ws3.core.Nodeobjects.
- path(leaf=None)[source]
Retrieves the path from the root to a specific leaf node or to the current path.
- Parameters:
leaf – The leaf node for which the path is to be retrieved. Default is
None(which returns the current path).- Returns:
a path
- Return type:
tuple of
ws3.core.Nodeobjects