cana.boolean_node module

Boolean Node

Main class for Boolean node objects.

Naming for inputs, function / BooleanFunction, and related terms follows the glossary in cana.nodes_builder.

class cana.boolean_node.BooleanNode(id=0, name='x', k=1, inputs=None, state=False, outputs=[0, 1], constant=False, network=None, verbose=False, *args, **kwargs)[source]

Bases: object

One variable in a Boolean network hub.

Stores wiring (inputs, arity k) and the update rule on function (BooleanFunction). See the glossary in cana.nodes_builder.

property outputs
classmethod from_bit_lut(lut=None, *args, **kwargs)[source]

Instantiate a Boolean Node from a dense LUT (sequence or BitLut).

Parameters:

lut – Outputs of length \(2^k\), or a BitLut. (Also accepted as the legacy keyword outputs=.)

Returns:

the instantiated object. Subclasses receive an instance of the subclass.

Return type:

(BooleanNode)

Raises:

ValueError – if a sequence length is not a power of two.

Example

>>> BooleanNode.from_bit_lut([0, 0, 0, 1], name="AND")
classmethod from_positional_expression(expression, *args, **kwargs)[source]

Instantiate from a positional python expression (x0…x{k-1}).

Parameters:
  • expression (str) – Positional IR stored on BooleanFunction.

  • k (int) – Number of inputs (required keyword-only).

Example

>>> BooleanNode.from_positional_expression(
...     "x0 and x1", k=2, name="AND"
... )
classmethod from_one_symbol_schemata(schemata, *args, **kwargs)[source]

Instantiate from a one-symbol (wildcard) cover.

Parameters:
  • schemata – Polarity → set of length-k schemas over {0,1,2}.

  • k (int, optional) – Keyword-only; inferred from schema length when omitted.

Example

>>> BooleanNode.from_one_symbol_schemata({1: {"11"}}, k=2, name="AND")
classmethod from_two_symbol_schemata(schemata, *args, **kwargs)[source]

Instantiate from a two-symbol schemata pair (ts0, ts1).

Parameters:
  • schemata – (list, list) two-symbol structure.

  • k (int, optional) – Keyword-only; inferred from schema length when omitted.

input_names()[source]

Return input names in LUT / expression bit order.

Requires this node to be attached to a BooleanNetwork.

named_expression(style='python', warn=True)[source]

Return a named boolean expression for this node’s update rule.

Uses boolean_function_to_named_expression(): stored positional expression, else two-symbol, else one-symbol ON cover, else dense LUT DNF. Soft-warns when verification flags are not True for the chosen non-LUT source (never blocks).

Parameters:
  • style (str) – "python" or "symbolic".

  • warn (bool) – If False, suppress verification soft-warnings.

fill_function_representation(representation)[source]

Fill a missing BooleanFunction representation slot from another.

Parameters:

representation (str) – "bit_lut", "one_symbol_schemata", "two_symbol_schemata", or "positional_expression".

Returns:

This node’s function (mutated in place).

Return type:

(BooleanFunction)

set_constant(constant=True, state=None)[source]

Sets whether the node is to be treated as a contant

Parameters:
  • constant (Boolean) – Whether to set or unset the node as a constant.

  • state (str; optional) – The state value to which to set the node. Either ‘0’ or ‘1’; default to current state value.

constant_step(input_state)[source]

Treat the node as a constant variable, always returning its state.

dynamic_step(input_state)[source]

Update and return this node’s state for a specific input state.

Parameters:

input_state (str) – an input to the node (e.g.: ‘0111’ -> 7).

Returns:

the new state ('0' or '1').

Return type:

(str)

input_mask(binstate)[source]

Returns the mask applied to the binary state binstate

Parameters:

binstate (str) – the binary state

Returns:

the masked state

Return type:

output (str)

look_up_table()[source]

Returns the Look Up Table (LUT)

Returns:

the LUT

Return type:

(pandas.DataFrame)

Examples

>>> AND = BooleanNode.from_bit_lut([0,0,0,1])
>>> AND.look_up_table()
schemata_look_up_table(type='pi', pi_symbol='#', ts_symbol_list=['̊', '̯', '̃', '̰', '̆', '̮'])[source]

Returns the simplified schemata Look Up Table (LUT)

Parameters:
  • type (string) – The type of schemata to return, either Prime Implicants pi or Two-Symbol ts. Defaults to ‘pi’.

  • pi_symbol (str) – The Prime Implicant don’t care symbol. Default is #.

  • ts_symbol_list (list) – A list containing Two Symbol permutable symbols, one per permutation group of a schema (combining marks: ring above, inverted breve below, tilde, tilde below, breve, breve below). A ValueError is raised if a schema has more groups than symbols.

Returns:

the schemata LUT

Return type:

(pandas.DataFrame or Latex)

Examples

>>> AND = BooleanNode.from_bit_lut([0,0,0,1])
>>> AND.schemata_look_up_table(type='pi')

Note

See the full list of combining characters to use other symbols as the permutation symbol.

See also

look_up_table()

pi_coverage()[source]

Return the \(F'\) (Prime Implicants) binary state coverage.

Lazily fills _pi_coverage (refuses oversized k).

sampled_pi_coverage(*, exact_k_max=14, n_samples=None, rng=None, reuse=True)[source]

Sample a partial pi-coverage for high-k measure estimates.

Caches the result on _sampled_pi_coverage. Pass reuse=False to force a fresh sample.

ts_coverage()[source]

Return the \(F''\) (Two-Symbol schematas) binary state coverage.

Lazily fills _ts_coverage (refuses oversized k).

canalizing_map(polarity=None, builder='legacy', output=None)[source]

Computes the node Canalizing Map (CM).

Parameters:
Returns:

a directed graph representation of the CM.

Return type:

CM (networkx.DiGraph)

See also

boolean_network.dynamics_canalization_map() for the DCM and drawing.draw_canalizing_map_graphviz() for plotting.

input_redundancy(operator=<function mean>, norm=True, *, exact_k_max=14, n_samples=None, rng=None, return_mode=False)[source]

The Input Redundancy \(k_{r}\) is the mean number of unnecessary inputs (or #) in the Prime Implicants Look Up Table (LUT). Since there may be more than one redescription schema for each input entry, the input redundancy is bounded by an upper and lower limit.

\[k_{r}(x) = \frac{ \sum_{f_{\alpha} \in F} \Phi_{\theta:f_{\alpha} \in \Theta_{\theta}} (n^{\#}_{\theta} ) }{ |F| }\]

where \(\Phi\) is a function (\(min\) or \(max\)) and \(F\) is the node LUT.

Parameters:
  • operator (function) – The operator to use while computing input redundancy for the node. Defaults to statistics.mean, but min or max can be also considered.

  • norm (bool) –

    Normalized between [0,1]. Use this value when comparing nodes with different input sizes. (Defaults to “True”.)

    \(k^{*}_r(x) = \frac{ k_r(x) }{ k(x) }\).

  • exact_k_max (int) – For k <= exact_k_max, compute exactly from full pi_coverage. Above that, sample minterms. Defaults to 14. Pass exact_k_max=k (or larger) to force the exact path for k > 14. Pass exact_k_max=-1 to force sampling without a warning.

  • n_samples (int or None) – Sample size when k > exact_k_max. Defaults to 2**exact_k_max.

  • rng – Optional numpy.random.Generator for the sampling path.

  • return_mode (bool) – If True, return (value, \"exact\"|\"sampled\").

Returns:

The \(k_r\) value. or (float, str) when return_mode is True.

Return type:

(float)

Note

The complete mathematical description can be found in [MPR13]. For large k, the last sampled coverage is cached on _sampled_pi_coverage for bootstrap CIs via cana.function_properties.redundancy.bootstrap_input_redundancy(). Automatic sampling (k > exact_k_max with exact_k_max >= 0) emits a UserWarning.

edge_redundancy(bound='mean', *, exact_k_max=14, n_samples=None, rng=None, return_mode=False)[source]

The Edge Redundancy \(r_{i}\) is the mean number of unnecessary inputs (or #) in the Prime Implicants Look Up Table (LUT) for that input. Since there may be more than one redescription schema for each input entry, the input redundancy is bounded by an upper and lower limit.

\[r_i(x_i) = \frac{ \sum_{f_{\alpha} \in F} \Phi_{\theta:f_{\alpha} \in \Theta_{\theta}} (X^{\#}_{\theta_i} ) }{ |F| }\]

where \(\Phi\) is a function (\(min\) or \(max\)) and \(F\) is the node LUT.

Parameters:
  • bound (string) – The bound to which compute input redundancy. Mode “input” accepts: [“lower”, “mean”, “ave”, upper”, “tuple”]. Defaults to “mean”.

  • exact_k_max (int) – For k <= exact_k_max, compute exactly from full pi_coverage. Above that, sample minterms. Defaults to 14. Pass exact_k_max=k (or larger) to force the exact path for k > 14. Pass exact_k_max=-1 to force sampling without a warning.

  • n_samples (int or None) – Sample size when k > exact_k_max. Defaults to 2**exact_k_max.

  • rng – Optional numpy.random.Generator for the sampling path.

  • return_mode (bool) – If True, return (value, \"exact\"|\"sampled\").

Returns:

The list of \(r_i\) for inputs. or (list, str) when return_mode is True.

Return type:

(list)

Note

The complete mathematical description can be found in :cite:Gates:2020`. For large k, the last sampled coverage is cached on _sampled_pi_coverage for bootstrap CIs via cana.function_properties.redundancy.bootstrap_edge_redundancy(). Automatic sampling (k > exact_k_max with exact_k_max >= 0) emits a UserWarning.

effective_connectivity(operator=<function mean>, norm=True)[source]

The Effective Connectiviy is the mean number of input nodes needed to determine the transition of the node.

\[k_e(x) = k(x) - k_r(x)\]
Parameters:
  • operator (function) – The operator to use while computing input redundancy for the node. Defaults to statistics.mean, but min or max can be also considered.

  • norm (bool) –

    Normalized between [0,1]. Use this value when comparing nodes with different input sizes. (Defaults to “True”.)

    \(k^{*}_e(x) = \frac{ k_e(x) }{ k(x) }\).

Returns:

The \(k_e\) value.

Return type:

(float)

edge_effectiveness(bound='mean', *, exact_k_max=14, n_samples=None, rng=None, return_mode=False)[source]

The Edge Effectiveness is the mean number of an input’s states needed to determine the transition of the node.

\[e_i(x_i) = 1 - r_i(x_i)\]
Parameters:
Returns:

The list of \(e_r\) values. or (list, str) when return_mode is True.

Return type:

(list)

symKernel_numDots(ts, sameSymbol=False)[source]

compute the number of variables involved in a symmetry group.

Parameters:
  • ts ([str, [[]], [[]]]) – two symbol schema

  • sameSymbol (bool) – whether or not to consider same-symbol symmetry

Returns:

(int)

input_symmetry(aggOp='mean', kernel='numDots', sameSymbol=False)[source]

compute the input symmetry (k_s) of the boolean node, with variations via the specified functions.

Parameters:
  • aggOp – the function aggregating over all two-symbol schemata that redescribe a LUT entry

  • kernel – the function to compute on a given two-symbol schema

Returns:

the mean over all LUT entries of the aggOp applied to the kernel of all two-symbol schemata that redescribe the LUT entry

Return type:

(float)

input_symmetry_mean()[source]
compute the input symmetry (k_s) of the boolean node.

Specifically, computes it using the avg operator for the summand. Refactoring of input_symmetry for speed.

Returns:

(float)

distinct_symmetry()[source]

Compute the distinct permutation symmetry of the node LUT.

For each LUT entry, this computes the fraction of distinct input permutations that preserve the same output, excluding the identity permutation from both numerator and denominator.

Returns:

(float)

raw_symmetry()[source]

Compute the raw symmetry of the node LUT.

LUT rows are grouped by input Hamming weight. For each row, this computes the fraction of rows in the same weight group that have the same output, then averages across all LUT rows.

Returns:

(float)

activities_from_bit_lut()[source]

Activities from the dense LUT via cana.function_properties.sensitivity.activities_from_bit_lut().

Returns:

(list of floats)

sensitivity_from_bit_lut(norm=False)[source]

Average sensitivity from the dense LUT via cana.function_properties.sensitivity.sensitivity_from_bit_lut().

Parameters:

norm (bool) – whether or not to normalize by the number of inputs (k)

Returns:

(float)

activities_from_schemata(*, exact_k_max=14, n_samples=None, rng=None)[source]

Activities as upper edge effectiveness from one-symbol schemata.

See cana.function_properties.sensitivity.activities_from_schemata(). Prefer this when prime implicants are available, or when k exceeds the dense-LUT limit.

Parameters:
Returns:

(list of floats)

sensitivity_from_schemata(norm=False, *, exact_k_max=14, n_samples=None, rng=None)[source]

Average sensitivity from one-symbol schemata: sum of activities_from_schemata().

Parameters:
Returns:

(float)

activities(*, exact_k_max=14, n_samples=None, rng=None)[source]

Activities of each incoming edge.

Prefers activities_from_schemata() when full or sampled pi_coverage is already cached; otherwise uses activities_from_bit_lut() when k <= exact_k_max; otherwise activities_from_schemata(). Sampling kwargs apply only on the schemata path.

Returns:

(list of floats)

sensitivity(norm=False, *, exact_k_max=14, n_samples=None, rng=None)[source]

Average sensitivity: mean number of single-input flips that change the output.

Prefers sensitivity_from_schemata() when full or sampled pi_coverage is already cached; otherwise uses sensitivity_from_bit_lut() when k <= exact_k_max; otherwise sensitivity_from_schemata(). The two formulations are bit-exactly equal on the full state space. Sampling kwargs apply only on the schemata path.

Parameters:
  • norm (bool) – whether or not to normalize by the number of inputs (k)

  • exact_k_max – Forwarded to the schemata path when used.

  • n_samples – Forwarded to the schemata path when used.

  • rng – Forwarded to the schemata path when used.

Returns:

(float)

c_sensitivity(c, mode='default', max_k=0)[source]

Node c-sensitivity. c-sensitivity is defined as: the mean probability that changing exactly c variables in input variables would change output value. There is another mode “forceK”, which will be used to calculate Derrida value. In that mode, it would assume the number of input variables is specified as max_k this methods is equivalent to Derrida value in [KKL17], only move a normalization coefficient from expression of Derrida value to c-sensitivity to simplify it

Parameters:
  • c (int) – the c in the definition of c-senstivity above

  • mode (string) – either “default” or “forceK”

  • max_k (int) – you must specify max_k when you set mode as ‘forceK’

Returns:

(float)

See also

derrida_curve()

bias_from_bit_lut()[source]

Bias from the dense LUT via cana.function_properties.bias.bias_from_bit_lut().

Returns:

(float)

bias_from_schemata(*, exact_k_max=14, n_samples=None, rng=None, return_mode=False)[source]

Bias as ON-fraction from one-symbol schemata.

See cana.function_properties.bias.bias_from_schemata(). Prefer this when prime implicants are available, or when k exceeds the dense-LUT limit.

Parameters:
Returns:

(float) or (float, str) when return_mode is True.

bias(*, exact_k_max=14, n_samples=None, rng=None, return_mode=False)[source]

The node bias. The sum of the boolean outputs divided by the number of entries (\(2^k\)) in the LUT.

\[bias(x) = \frac{ \sum_{f_{\alpha}\in F} s_{\alpha} }{ |F| }\]

Prefers bias_from_schemata() when full or sampled pi_coverage is already cached; otherwise uses bias_from_bit_lut() when k <= exact_k_max; otherwise bias_from_schemata(). Sampling kwargs apply only on the schemata path.

Parameters:
  • exact_k_max – Forwarded to the schemata path when used.

  • n_samples – Forwarded to the schemata path when used.

  • rng – Forwarded to the schemata path when used.

  • return_mode (bool) – If True, return (value, \"exact\"|\"sampled\"). On the LUT path the mode is always \"exact\".

Returns:

(float) or (float, str) when return_mode is True.

input_signs()[source]

Return per-input interaction signs as frozenset values.

Each entry is one of SIGN_POSITIVE, SIGN_NEGATIVE, SIGN_BINATE, or SIGN_IRRELEVANT.

Prefers a fast syntactic NNF scan when a stored positional expression is available and syntactically unate (no LUT). Otherwise uses semantic (truth-table) signs via input_signs().

Returns:

The list of input signs (list[frozenset]).

Return type:

(list)