nd2py.core.symbols package#
Submodules#
nd2py.core.symbols.empty module#
- class nd2py.core.symbols.empty.Empty(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#
Bases:
Symbol- n_operands = 0#
- __init__(nettype: Literal['node', 'edge', 'scalar'] | None = None)[source]#
Initialize a Symbol node.
This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.
- Parameters:
*operands – Child operands of this symbol. The number of operands must match
n_operandsof the concrete subclass. Non-symbol scalar values are automatically wrapped asNumbersymbols.nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as
"node","edge", or"scalar", or a set of allowed nettypes. If provided, it is propagated through the tree byinfer_nettype().
- map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
nd2py.core.symbols.functions module#
nd2py.core.symbols.number module#
- class nd2py.core.symbols.number.Number(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar', fitable=None)[source]#
Bases:
Symbol- n_operands = 0#
- __init__(value, nettype: Literal['node', 'edge', 'scalar'] = 'scalar', fitable=None)[source]#
Initialize a Symbol node.
This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.
- Parameters:
*operands – Child operands of this symbol. The number of operands must match
n_operandsof the concrete subclass. Non-symbol scalar values are automatically wrapped asNumbersymbols.nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as
"node","edge", or"scalar", or a set of allowed nettypes. If provided, it is propagated through the tree byinfer_nettype().
- map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
- get_nettype_range() Set[Literal['node', 'edge', 'scalar']][source]#
获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。
- property nettype_range: Set[Literal['node', 'edge', 'scalar']]#
获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。
nd2py.core.symbols.operands module#
- class nd2py.core.symbols.operands.Add(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Sub(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Mul(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Div(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Pow(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Max(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Min(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Identity(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sin(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Cos(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Tan(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sec(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Csc(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Cot(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Arcsin(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Arccos(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Arctan(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Log(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.LogAbs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Exp(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Abs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Neg(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Inv(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sqrt(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.SqrtAbs(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Pow2(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Pow3(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sinh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Cosh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Tanh(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Coth(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sech(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Csch(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Sigmoid(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- class nd2py.core.symbols.operands.Regular(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 2#
- class nd2py.core.symbols.operands.Sour(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
- class nd2py.core.symbols.operands.Targ(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Sour
- class nd2py.core.symbols.operands.Aggr(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
- class nd2py.core.symbols.operands.Rgga(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Aggr
- class nd2py.core.symbols.operands.Readout(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
Symbol- n_operands = 1#
- classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
nd2py.core.symbols.symbol module#
- class nd2py.core.symbols.symbol.Symbol(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Bases:
NetTypeMixin,TreeMixin,SymbolAPIMixin- n_operands = None#
- __init__(*operands, nettype: Literal['node', 'edge', 'scalar'] | Set[Literal['node', 'edge', 'scalar']] | None = None)[source]#
Initialize a Symbol node.
This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.
- Parameters:
*operands – Child operands of this symbol. The number of operands must match
n_operandsof the concrete subclass. Non-symbol scalar values are automatically wrapped asNumbersymbols.nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as
"node","edge", or"scalar", or a set of allowed nettypes. If provided, it is propagated through the tree byinfer_nettype().
- copy()[source]#
Return a deep copy of this symbol.
The copied symbol has the same tree structure and values as the original but does not share
parentlinks, so it can be safely inserted into a different expression tree.- Returns:
A deep copy of the current symbol.
- Return type:
- get_numbers(fitable_only: bool = False, float_only: bool = False, scalar_only: bool = False) List[Number][source]#
Collect all
Numbernodes contained in this symbol.Traverses the expression tree in preorder and returns all numeric nodes that satisfy the given filters.
- Parameters:
fitable_only (bool, optional) – If True, return only numbers marked as fitable (trainable) parameters. Defaults to False.
float_only (bool, optional) – If True, exclude integer-like values (for example exponents that should remain fixed). Defaults to False.
scalar_only (bool, optional) – If True, only consider scalar numbers (nettype
"scalar"). Defaults to False.
- Returns:
List of numeric symbol nodes that match the filters.
- Return type:
List[Number]
- get_parameters(fitable_only: bool = False, float_only: bool = False) List[float][source]#
Return numeric parameter values contained in this symbol.
This is a convenience wrapper over
get_numbers()that extracts the underlying scalar values fromNumbernodes.- Parameters:
fitable_only (bool, optional) – If True, return only parameters associated with fitable numbers. Defaults to False.
float_only (bool, optional) – If True, exclude integer-like parameters. Defaults to False.
- Returns:
Flat list of parameter values in traversal order.
- Return type:
List[float]
- set_parameters(params: List[float], fitable_only: bool = False, float_only: bool = False)[source]#
Assign new numeric parameter values to this symbol.
The values in
paramsare consumed in the same order as produced byget_parameters()with the same filter options.- Parameters:
params (List[float]) – New parameter values to assign.
fitable_only (bool, optional) – If True, only update fitable parameters and leave others unchanged. Defaults to False.
float_only (bool, optional) – If True, only update non-integer parameters. Defaults to False.
- Raises:
ValueError – If the length of
paramsdoes not match the number of parameters selected by the filters.
- classmethod map_nettype(*children_nettypes: Literal['node', 'edge', 'scalar']) Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
nd2py.core.symbols.variable module#
- class nd2py.core.symbols.variable.Variable(name, nettype: Literal['node', 'edge', 'scalar'] = 'scalar')[source]#
Bases:
Symbol- n_operands = 0#
- __init__(name, nettype: Literal['node', 'edge', 'scalar'] = 'scalar')[source]#
Initialize a Symbol node.
This constructor sets the nettype, sanitizes and attaches child operands, and then triggers a nettype inference pass on the whole expression tree.
- Parameters:
*operands – Child operands of this symbol. The number of operands must match
n_operandsof the concrete subclass. Non-symbol scalar values are automatically wrapped asNumbersymbols.nettype (Optional[NetType | Set[NetType]]) – Nettype constraint for this symbol, such as
"node","edge", or"scalar", or a set of allowed nettypes. If provided, it is propagated through the tree byinfer_nettype().
- map_nettype() Literal['node', 'edge', 'scalar'] | None[source]#
Default nettype mapping rule for symbol subclasses.
The default behavior enforces that
"node"and"edge"nettypes cannot be mixed. If only scalars are present, the result is"scalar"; otherwise it follows the presence of"node"or"edge".- Parameters:
*children_nettypes (NetType) – Nettypes of the child operands.
- Returns:
Inferred nettype for the parent symbol, or
Noneif the combination is invalid or cannot be determined.- Return type:
Optional[NetType]
- Raises:
ValueError – If the number of child nettypes does not match
cls.n_operands.
- get_nettype_range() Set[Literal['node', 'edge', 'scalar']][source]#
获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。
- property nettype_range: Set[Literal['node', 'edge', 'scalar']]#
获取此节点可能产生的所有 nettype 值域,并在首次调用时缓存到类属性中。