dims
codegen.dims
The axes an expression’s value carries, followed by name through the expression tree.
A result is keyed by its axis names wherever they are known, so a step that reshapes its input (sum_axis, indexing, welch) must say which axes it keeps rather than leave the container to guess from a shape. This module is that statement for the array vocabulary of :mod:tvbo.parse.expression: :func:expression_dims propagates a mapping of symbol names to axis names through one expression, and :func:resolve_axis lets a reduction name the axis it drops (sum_axis(x, node)) as well as number it. Both are backend-neutral; the printers in :mod:tvbo.codegen.code call them at codegen and the tvboptim observation layer uses them to declare a derived observation’s axes.
It is also the one reader of a declared shape: :func:shape_terms parses it, :func:resolve_shape sizes it, :func:shape_code writes it into generated code, :func:dynamics_sizes finds and checks the sizes a dynamics’ shapes name, and :func:per_node_dims types a per-node equation by the element axes those shapes give its operands.
Attributes
| Name | Description |
|---|---|
| AXIS_ARGUMENTS | For each primitive that takes an axis, the position of that argument. The axis is an integer literal or the name of one of its operand’s axes. |
| Dims | |
| FREQUENCY | The axis a spectrum lies along. |
| NODE | The axis every per-node array carries, which the shape term n_nodes sizes. |
| N_NODES | The shape term for the owning network’s node count. |
| REDUCTIONS | The one-argument array functions that a per-node equation reads as a reduction over the element axis. |
Functions
| Name | Description |
|---|---|
| axis_symbols | The names standing in an axis position anywhere in expr, which are axis names rather than free variables. |
| dynamics_sizes | The sizes dynamics declares: each of its parameters that one of its shapes names, with its integer value. |
| expression_dims | The axes expr’s value carries, given env, the axes of each symbol it reads (() for a scalar), or None where they cannot be followed. |
| per_node_dims | The element axes expr carries in a per-node equation, given env, each symbol’s element axes (() for a scalar per node). |
| resolve_axis | axis as an int: an integer literal, or the name of one of the operand’s operand_dims. |
| resolve_shape | The shape’s integer extents, None when none is declared. |
| shape_code | The declared shape written as :func:terms_code writes its terms. |
| shape_dims | The axis names a declared shape gives its array: node for n_nodes and element_<term> for an integer or a size. |
| shape_terms | The terms of a declared shape: None when none is declared, else its integer literals and names in order. |
| terms_code | Shape terms as the tuple a generated module writes, without spaces, each size replaced by its integer and n_nodes kept as the module’s name for the node count. |
axis_symbols
codegen.dims.axis_symbols(expr)The names standing in an axis position anywhere in expr, which are axis names rather than free variables.
dynamics_sizes
codegen.dims.dynamics_sizes(dynamics, experiment=None)The sizes dynamics declares: each of its parameters that one of its shapes names, with its integer value.
A shape here is a parameter’s or a state variable’s. A size fixes an array’s extent when the code is compiled, so it must be a literal integer that nothing changes: one that is drawn, sourced, produced or not an integer is refused, and so, given experiment, is one that a fit, an update rule, a continuation or a sweep acts on, as :func:tvbo.utils.parameters_acted_on names them. Each refusal names the size and the parameter or state whose shape reads it.
expression_dims
codegen.dims.expression_dims(expr, env)The axes expr’s value carries, given env, the axes of each symbol it reads (() for a scalar), or None where they cannot be followed.
Numbers are scalars; an elementwise operation broadcasts its operands by name (:func:_broadcast); indexing drops one leading axis per index; the array primitives follow :data:_RULES; a symbol or a function the table does not know is unknown, as is any operand that is.
per_node_dims
codegen.dims.per_node_dims(expr, env)The element axes expr carries in a per-node equation, given env, each symbol’s element axes (() for a scalar per node).
The node axis is the equation’s own batch axis, so it never appears here: a per-region readout of shape (n_nodes, n) reads as (element_n,) at its node. matmul contracts the left operand’s last axis with the right one’s first, which must be the same axis; sum, mean, max, min, std, var and prod of one argument reduce its element axis; + - * / and every other function combine operands of equal axes or a scalar. A mismatch, an index into an element and a symbol env does not know are refused, naming the operands.
resolve_axis
codegen.dims.resolve_axis(axis, operand_dims, signature, name='axis')axis as an int: an integer literal, or the name of one of the operand’s operand_dims.
An array axis is fixed when the code is compiled, so a traced or symbolic value is refused. A name is refused when the operand’s axes are not known at this point, naming the way out: give the position, or declare dims: on the source so the name can be followed.
resolve_shape
codegen.dims.resolve_shape(shape, *, owner, sizes=None, n_nodes=None)The shape’s integer extents, None when none is declared.
n_nodes takes n_nodes, a size its value in sizes. A term that is neither, or n_nodes where no node count is known, is refused, naming owner, the shape and the term.
shape_code
codegen.dims.shape_code(shape, sizes=None)The declared shape written as :func:terms_code writes its terms.
shape_dims
codegen.dims.shape_dims(shape)The axis names a declared shape gives its array: node for n_nodes and element_<term> for an integer or a size.
shape_terms
codegen.dims.shape_terms(shape)The terms of a declared shape: None when none is declared, else its integer literals and names in order.
A shape is written as a Python tuple, "(n_nodes, n)", "(2, 2)" or "()" for a scalar; a single parenthesised term without its comma, "(n_nodes)", is read as the one-term tuple. Anything else is refused, naming the shape.
terms_code
codegen.dims.terms_code(terms, sizes=None)Shape terms as the tuple a generated module writes, without spaces, each size replaced by its integer and n_nodes kept as the module’s name for the node count.