Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
calculate_python_expression [2026/07/22 03:26] hermann |
calculate_python_expression [2026/07/22 18:15] (current) hermann |
||
|---|---|---|---|
| Line 57: | Line 57: | ||
| | [[Extract Struct Tuple]] | A tuple value | | | [[Extract Struct Tuple]] | A tuple value | | ||
| - | Each functor takes two inputs: the ''Struct'' returned by ''CalculatePythonExpression'', and the name of the entry to extract as a string constant. For example, to retrieve a numeric output named ''patchCount'' and a table output named ''filteredPatches'': | + | Each functor takes two inputs: the ''Struct'' returned by ''CalculatePythonExpression'', and the name of the entry to extract as a string constant. For example, to retrieve a numeric output named ''patchCount'' and a table output named ''filteredPatches'' — the ''String $"(...)"'' wrapper around the Python code is explained in [[#writing_the_expression_in_ego_script|Writing the expression in EGO Script]] below: |
| <code> | <code> | ||
| Line 71: | Line 71: | ||
| ==== Utilities ==== | ==== Utilities ==== | ||
| - | === dinamica.package() === | + | === Installing packages === |
| - | Installs (if needed via pip) and imports the requested module. | + | Packages can be installed in two ways: calling ''dinamica.package(...)'' from within the expression, or listing package names on the //packages// input port, one per line. The two differ in timing: ''dinamica.package(...)'' runs **during** the expression, so it can be called conditionally; the //packages// port always runs **before** the expression executes. Because ''dinamica.package(...)'' runs together with the script, it may fail if a package is already loaded in an incompatible version. |
| + | |||
| + | ''dinamica.package(packageName, installPath=None, loadPath=None)'' installs (if needed via pip) and imports the requested module. | ||
| ^ Parameter ^ Type ^ Default ^ Description ^ | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| Line 79: | Line 81: | ||
| | ''installPath'' | str | ''packageName'' | What is passed to ''pip install''. Can be a plain name, a version-pinned requirement, a wheel filename or URL, a ''git+'' URL, or a name followed by extra pip flags. | | | ''installPath'' | str | ''packageName'' | What is passed to ''pip install''. Can be a plain name, a version-pinned requirement, a wheel filename or URL, a ''git+'' URL, or a name followed by extra pip flags. | | ||
| | ''loadPath'' | str | ''packageName'' | The name used to ''import'' the module in Python. | | | ''loadPath'' | str | ''packageName'' | The name used to ''import'' the module in Python. | | ||
| - | |||
| - | === dinamica.prepareTable() === | ||
| - | |||
| - | Converts a list of lists into a table ready to be assigned to an output. The first inner list must be the header row. | ||
| - | |||
| - | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | ''inputTable'' | list of lists | — | The table data. First inner list is the header row; subsequent inner lists are data rows. | | ||
| - | | ''numKeys'' | int | — | Number of key columns. Key column names will have ''*'' appended in the output. | | ||
| - | |||
| - | === dinamica.prepareLookupTable() === | ||
| - | |||
| - | Converts a list of lists into a lookup table ready to be assigned to an output. The first inner list must be the header row. | ||
| - | |||
| - | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | ''lut'' | list of lists | — | The lookup table data. First inner list is the header row; subsequent inner lists are data rows. | | ||
| - | |||
| - | === dinamica.toTable() === | ||
| - | |||
| - | Converts several Python data shapes into a valid Dinamica table for output. | ||
| - | |||
| - | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| - | | ''inputTable'' | list of lists; dict of lists; list of tuples; flat list; ''pandas.DataFrame''; ''numpy.array'' | — | The data to convert. A flat list produces a lookup table with sequential keys. A ''numpy.array'' must have its header as the first row. | | ||
| - | | ''numKeys'' | int | — | Number of key columns. Key column names will have ''*'' appended in the output. Ignored for a flat list, which always produces a lookup table with sequential keys. | | ||
| - | |||
| - | === Installing packages === | ||
| - | |||
| - | Packages can be installed in two ways: | ||
| - | |||
| - | * Calling ''dinamica.package(...)'' from within the expression. | ||
| - | * Listing package names on the //packages// input port, one per line. | ||
| - | |||
| - | These two approaches differ in timing: ''dinamica.package(...)'' runs **during** the expression, so it can be called conditionally; the //packages// port always runs **before** the expression executes. Because ''dinamica.package(...)'' runs together with the script, it may fail if a package is already loaded in an incompatible version. | ||
| - | |||
| - | ''dinamica.package(packageName, installPath=None, loadPath=None)'': | ||
| - | |||
| - | * ''packageName'' identifies the package and is the default for both following parameters when omitted. | ||
| - | * ''installPath'' is passed to ''pip install''. It can be a version-pinned requirement, a wheel filename or URL, a ''git+'' URL, or a name plus extra pip flags. | ||
| - | * ''loadPath'' is the name used to ''import'' the module. It defaults to ''packageName''. | ||
| Simply install and load ''numpy'': | Simply install and load ''numpy'': | ||
| Line 146: | Line 110: | ||
| The //packages// port takes one package identifier per line, in any form accepted by ''pip''. It only installs — unlike ''dinamica.package()'', it does not also import the module, and it does not support specifying a separate install name and import name. Every listed package is installed before the expression runs, but the expression must still import each one itself, using its actual importable name (which may differ from the name given to ''pip''): | The //packages// port takes one package identifier per line, in any form accepted by ''pip''. It only installs — unlike ''dinamica.package()'', it does not also import the module, and it does not support specifying a separate install name and import name. Every listed package is installed before the expression runs, but the expression must still import each one itself, using its actual importable name (which may differ from the name given to ''pip''): | ||
| + | |||
| + | The //packages// port: | ||
| <code> | <code> | ||
| numpy | numpy | ||
| Line 152: | Line 118: | ||
| </code> | </code> | ||
| + | The expression: | ||
| <code> | <code> | ||
| import numpy | import numpy | ||
| Line 157: | Line 124: | ||
| import torchvision | import torchvision | ||
| </code> | </code> | ||
| + | |||
| + | === dinamica.prepareTable() === | ||
| + | |||
| + | Converts a list of lists into a table ready to be assigned to an output. The first inner list must be the header row. As with any table (see [[ego_script#constants|Constants]]), each column's type is inferred from its own values: a column of ''int''/''float'' values produces a Real column, a column of ''str'' values produces a String column. | ||
| + | |||
| + | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| + | | ''inputTable'' | list of lists | — | The table data. First inner list is the header row; subsequent inner lists are data rows. | | ||
| + | | ''numKeys'' | int | — | Number of key columns. Key column names will have ''*'' appended in the output. | | ||
| + | |||
| + | === dinamica.prepareLookupTable() === | ||
| + | |||
| + | Converts a list of lists into a lookup table ready to be assigned to an output. The first inner list must be the header row. Lookup tables are always Real-typed on both key and value sides — there is no String option — so every value in ''lut'' must be numeric. | ||
| + | |||
| + | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| + | | ''lut'' | list of lists | — | The lookup table data. First inner list is the header row; subsequent inner lists are data rows. | | ||
| + | |||
| + | === dinamica.toTable() === | ||
| + | |||
| + | Converts several Python data shapes into a valid Dinamica table for output. Column types are inferred the same way as ''dinamica.prepareTable()'' — except for a flat list, which always produces a Real-typed lookup table with sequential keys, matching the Real-only rule for lookup tables. | ||
| + | |||
| + | ^ Parameter ^ Type ^ Default ^ Description ^ | ||
| + | | ''inputTable'' | list of lists; dict of lists; list of tuples; flat list; ''pandas.DataFrame''; ''numpy.array'' | — | The data to convert. A flat list produces a lookup table with sequential keys. A ''numpy.array'' must have its header as the first row. | | ||
| + | | ''numKeys'' | int | — | Number of key columns. Key column names will have ''*'' appended in the output. Ignored for a flat list, which always produces a lookup table with sequential keys. | | ||
| ==== Examples ==== | ==== Examples ==== | ||
| Line 287: | Line 277: | ||
| CalculatePythonExpression | CalculatePythonExpression | ||
| - | |||
| - | |||