Skip to content

Conversation

liamhuber
Copy link
Member

@liamhuber liamhuber commented Oct 12, 2023

Composite instances can now replace one of their children with another node instance or class, as long as that node has no parent or connections and has compatible IO (this PR introduces a tool for coping connections from one node to the other)

Resolves a couple of the TODOs on #7.

from pyiron_workflow import Workflow

@Workflow.wrap_as.single_value_node()
def add_one(x):
    result = x + 1
    return result

@Workflow.wrap_as.macro_node()
def add_three_macro(macro):
    macro.one = add_one()
    macro.two = add_one(x=macro.one)
    macro.three = add_one(x=macro.two)
    
    macro.inputs_map = {"one__x": "x"}
    macro.outputs_map = {"three__result": "result"}

macro = add_three_macro()

replacement = add_three_macro()

print(macro(x=0).result)  # 1 + 1 + 1 = 3
>>> 3

macro.replace(macro.two, replacement)
# Alternate syntaxes support using labels and classes:
# macro.replace("two", replacement)
# macro.replace(macro.two, add_three_macro)
# macro.two.replace_with(replacement)
# macro.two.replace_with(add_three_macro)
# macro.two = add_three_macro  # Some real sugar!
print(macro(x=0).result)  # 1 + (1 + 1 + 1) + 1 = 5
>>> 5

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@liamhuber liamhuber changed the base branch from main to refactor_run_cycle October 12, 2023 23:06
@github-actions
Copy link

Binder 👈 Launch a binder notebook on branch pyiron/pyiron_workflow/allow_replacing_nodes

For readability and to make sure subtests don't depend on state that is modified in other (potentially failing) subtests.
To reflect the fact a class can now be passed
@liamhuber liamhuber added the format_black trigger the Black formatting bot label Oct 13, 2023
@codacy-production
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.58% (target: -1.00%) 92.59%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (d9aa842) 1580 1299 82.22%
Head commit (de0699a) 1627 (+47) 1347 (+48) 82.79% (+0.58%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#26) 54 50 92.59%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@liamhuber liamhuber merged commit 51f2750 into refactor_run_cycle Oct 13, 2023
@liamhuber liamhuber deleted the allow_replacing_nodes branch October 13, 2023 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
format_black trigger the Black formatting bot
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants