Module: utils/pythonGenerator

Python Code Generator for AutoRA Workflows Generates executable Python code from workflow graph state.
Source:

Classes

CodeBuilder

Methods

(static) collectPipPackages(state) → {Array.<string>}

Collect the set of pip packages required by the workflow's components.
Parameters:
Name Type Description
state Object Editor state with `nodes` and `components`.
Source:
Returns:
Deduplicated list of pip package specifiers.
Type
Array.<string>

(static) flattenBlockNodes(blocks) → {Array.<Object>}

Flatten a (possibly nested) block tree into its components in execution order.
Parameters:
Name Type Description
blocks Array.<Block> Execution blocks from getExecutionOrder.
Source:
Returns:
All component nodes, in order.
Type
Array.<Object>

(static) generateImports(code, imports, optionsopt) → {void}

Emit the import block (standard + component + data imports) into a builder. `Variable` and numpy are only needed by the placeholder variables template, so they are skipped when the variables come from an experiment runner.
Parameters:
Name Type Attributes Description
code CodeBuilder Builder to append the imports to.
imports Map Map of import path to a Set of imported (possibly aliased) names.
options Object <optional>
Options object.
Properties
Name Type Attributes Default Description
usesPlaceholderVariables boolean <optional>
true Whether to also import `Variable` and numpy for the placeholder template.
usesEquationVariables boolean <optional>
false Whether to also import `IV`, `DV` and numpy for a synthesized equation runner X/y.
Source:
Returns:
Type
void

(static) generatePipInstalls(state) → {string}

Generate pip install commands for all required packages
Parameters:
Name Type Description
state Object Editor state with `nodes` and `components`.
Source:
Returns:
A `pip install ...` command, or a comment when none are required.
Type
string

(static) generatePythonCode(state) → {string}

Generate Python code from workflow state
Parameters:
Name Type Description
state Object Editor state with `nodes`, `connections` and `components`.
Source:
Returns:
A complete, runnable Python script as a string.
Type
string

(static) generateVariablesSetup(componentMeta, orderedNodes) → {string}

Build the variables-initialization block (indented for a function body). When the workflow contains a synthetic experiment runner, the variables are taken from the `runner` already built in that runner's component definition (see generateRunnerWrapper) rather than rebuilding it; otherwise fall back to the placeholder template (real runners such as firebase have no `.variables`).
Parameters:
Name Type Description
componentMeta Map Map of node id to component metadata.
orderedNodes Array.<Object> Nodes in execution order, used to find any runner.
Source:
Returns:
Indented Python source for the variables setup block.
Type
string

(static) generateWrapper(code, meta) → {void}

Dispatch wrapper generation based on the component's protocol type.
Parameters:
Name Type Description
code CodeBuilder Builder to append the wrapper to.
meta Object Component metadata; `protocolType` selects the wrapper style.
Source:
Returns:
Type
void

(static) getExecutionOrder(nodes, connections) → {Object}

Traverse the workflow graph and split it into a (possibly nested) tree of execution blocks, supporting any number of loops — including nested loops (each Filter node defines one loop). The graph is walked forward from the Start node. At each Filter the traversal follows the *exit* output (toward the end) and records the *loop-back* output, which points to an already-visited node marking where that loop's body began. Each filter therefore spans an interval over the ordered components (loop-back target → last component before the filter). Intervals that contain one another become nested loops, disjoint intervals become sibling loops, and components covered by no interval run once.
Parameters:
Name Type Description
nodes Array.<Object> Graph nodes, each with `id`, `type` and optional `filterParams`.
connections Array.<Object> Graph edges, each with `sourceId` and `targetId`.
Source:
Returns:
A tree of ordered execution blocks, where a `Block` is either `{type: 'once', nodes: Object[]}` (its nodes run a single time) or `{type: 'loop', maxCounter: number, children: Block[]}` (its child blocks run inside `for cycle_N in range(maxCounter)`).
Type
Object

(static) prepareWorkflow(state) → {Object}

Collect execution order, imports and per-component metadata from the workflow state. Shared by the Python file and Jupyter notebook generators.
Parameters:
Name Type Description
state Object Editor state with `nodes`, `connections` and `components`.
Source:
Returns:
`{ blocks, imports, componentMeta, derivesVariablesFromRunner, needsEquationVariables }`.
Type
Object

(static) toPythonName(name) → {string}

Generate a valid Python variable name from a component name. Parenthesized qualifiers like "(Synthetic, Economics)" are dropped.
Parameters:
Name Type Description
name string Human-readable component name.
Source:
Returns:
A lowercase, underscore-separated identifier.
Type
string

(inner) buildBlockTree(orderedNodes, lo, hi, intervals) → {Array.<Block>}

Fold a set of filter intervals over an ordered component list into a nested block tree spanning `[lo, hi]`. Intervals passed in are all within that range. Each range's *top-level* intervals (those not strictly contained in another) become loop blocks; their inner intervals recurse into child loops; and any component not covered by a top-level interval accumulates into a `once` block.
Parameters:
Name Type Description
orderedNodes Array.<Object> Components in execution order.
lo number First component index of this range (inclusive).
hi number Last component index of this range (inclusive).
intervals Array.<{start: number, end: number, maxCounter: number}> Filter intervals within `[lo, hi]`.
Source:
Returns:
Ordered child blocks covering `[lo, hi]`.
Type
Array.<Block>

(inner) buildFactoryParamString(params, sympifyNamesopt, excludeopt) → {string}

Build a factory/constructor parameter string, wrapping any params named in `sympifyNames` (declared `"sympify": true` in the component JSON) in `sympify(...)` so their string value is parsed into a SymPy expression. A sympify param left blank is treated as unset and omitted (so the runner's default applies) rather than emitting `sympify("")`, which would raise.
Parameters:
Name Type Attributes Default Description
params Object Map of parameter name to value.
sympifyNames Array.<string> <optional>
[] Names of params to wrap in `sympify(...)`.
exclude Array.<string> <optional>
[] Parameter names to omit.
Source:
Returns:
Comma-separated `name=value` keyword arguments (nulls skipped).
Type
string

(inner) buildParamString(params, excludeopt) → {string}

Build parameter string from params object
Parameters:
Name Type Attributes Default Description
params Object Map of parameter name to value.
exclude Array.<string> <optional>
[] Parameter names to omit.
Source:
Returns:
Comma-separated `name=value` keyword arguments (nulls skipped).
Type
string

(inner) buildXYRunnerCall(meta, baseSpacesopt) → {string}

Build the multi-line ` = (...)` call for a runner that takes X/y (IV/DV) Variable objects. The IV/DV literals are emitted verbatim from the component JSON's IV/DV parameter definitions (the node value, or the declared default); a TODO comment prompts the user to adjust the names and ranges. Emitted with a 4-space base indent for use inside a function body.
Parameters:
Name Type Attributes Default Description
meta Object Runner metadata with `pythonName`, `params`, `runParamNames`, `xyParams`, `varName`.
baseSpaces number <optional>
4 Leading indentation for the first line.
Source:
Returns:
The indented, newline-joined call.
Type
string

(inner) dataTypeName(dataType) → {string|null}

Find the variable name of a data type spec, descending through list wrappers
Parameters:
Name Type Description
dataType Object Data type spec with a `name` or a nested `variable`.
Source:
Returns:
The resolved data type name, or null if none.
Type
string | null

(inner) formatPythonValue(value) → {string}

Format a JavaScript value as Python literal
Parameters:
Name Type Description
value * Value to format (null/undefined, boolean, string, array or number).
Source:
Returns:
The Python literal representation.
Type
string

(inner) generateExperimentalistWrapper(code, meta) → {void}

Generate wrapper function for an experimentalist component (pooler/sampler)
Parameters:
Name Type Description
code CodeBuilder Builder to append the wrapper to.
meta Object Component metadata with `pythonName`, `params`, `varName`, `nodeName`, `inputDataType` and `outputDataType`.
Source:
Returns:
Type
void

(inner) generateRunnerWrapper(code, meta) → {void}

Generate wrapper function for an experiment runner component
Parameters:
Name Type Description
code CodeBuilder Builder to append the wrapper to.
meta Object Component metadata with `pythonName`, `params`, `varName`, `nodeName`, `runParamNames`, `importPath` and `usesFirebaseCredentials`.
Source:
Returns:
Type
void

(inner) generateTheoristWrapper(code, meta) → {void}

Generate wrapper function for a theorist component
Parameters:
Name Type Description
code CodeBuilder Builder to append the wrapper to.
meta Object Component metadata with `pythonName`, `params`, `varName`, `nodeName` and `runParamNames` (non-constructor params to exclude from instantiation).
Source:
Returns:
Type
void

(inner) isBlankString(value) → {boolean}

Whether a param's value is a blank (empty/whitespace-only) string. Such a value counts as "unset" — the user cleared the input — so the param is omitted and the runner's own default applies rather than emitting an invalid literal (e.g. `sympify("")`, which raises at runtime).
Parameters:
Name Type Description
value * The param value.
Source:
Returns:
Type
boolean

(inner) isSyntheticRunner(meta) → {boolean}

Whether a runner component is a synthetic experiment runner. Synthetic runners (under `autora.experiment_runner.synthetic.*`) return an object that exposes `.variables` and `.run(conditions)`. Real data-collection runners (e.g. firebase) return a plain callable that takes conditions directly and has no `.variables`.
Parameters:
Name Type Description
meta Object Component metadata with `protocolType` and `importPath`.
Source:
Returns:
True for synthetic experiment runners.
Type
boolean

(inner) needsXYVariables(meta) → {boolean}

Whether a runner takes X/y (IV/DV) Variable objects on its factory call. These are declared as parameters with datatype "IV"/"DV" in the component's JSON and collected into `meta.xyParams` by prepareWorkflow.
Parameters:
Name Type Description
meta Object Component metadata.
Source:
Returns:
Type
boolean

(inner) runnerVarName(meta) → {string}

Module-level variable name holding a synthetic runner's built object. Derived from the (already-unique) wrapper `varName` so identical wrappers share it (keeping wrapper-dedup intact) while two distinct synthetic runners get distinct names and never overwrite one another's global `runner`.
Parameters:
Name Type Description
meta Object Component metadata with an assigned `varName`.
Source:
Returns:
A unique Python identifier for this runner's object.
Type
string

Type Definitions

Block

One execution block produced by getExecutionOrder: a `once` block runs its nodes a single time, while a `loop` block runs its child blocks inside `for cycle_N in range(maxCounter)` (nested loops become nested block trees).
Type:
  • Object | Object
Source: