Skip to content

Commit c777ff0

Browse files
authored
Composition: set most_recent_context in execute (#3195)
- other Components generally set in their execute methods - setting only in Composition.run fails to set when a Composition is nested, because only execute gets called - must still be set in Composition.run in case Composition is empty
1 parent 05b8dfb commit c777ff0

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

psyneulink/core/compositions/composition.py

+2
Original file line numberDiff line numberDiff line change
@@ -12689,6 +12689,8 @@ def execute(
1268912689

1269012690
execution_scheduler.get_clock(context)._increment_time(TimeScale.TRIAL)
1269112691

12692+
self.most_recent_context = context
12693+
1269212694
# Extract result here
1269312695
if execution_mode.is_compiled():
1269412696
assert execution_mode & pnlvm.ExecutionMode._PerNode

psyneulink/library/compositions/autodiffcomposition.py

+1
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ def execute(self,
15251525

15261526
scheduler.get_clock(context)._increment_time(TimeScale.TRIAL)
15271527

1528+
self.most_recent_context = context
15281529
return all_output_values
15291530

15301531
# Call Composition execute in Python mode

tests/composition/test_composition.py

+42
Original file line numberDiff line numberDiff line change
@@ -8397,6 +8397,48 @@ def test_rebuild_scheduler_after_remove_node(self):
83978397
assert comp.scheduler.execution_list[comp.default_execution_id] == [{A}, {A}, {C}]
83988398
assert set(comp.scheduler._user_specified_conds.conditions.keys()) == {C}
83998399

8400+
# empty pathway bypasses call to .execute, but most_recent_context
8401+
# should still be set
8402+
@pytest.mark.parametrize(
8403+
'inner_pathway',
8404+
[
8405+
pytest.param([ProcessingMechanism()], id='1_mech'),
8406+
pytest.param([], id='empty'),
8407+
]
8408+
)
8409+
def test_most_recent_context_nested(self, inner_pathway):
8410+
inner = pnl.Composition(name='inner', pathways=inner_pathway)
8411+
outer = pnl.Composition(name='outer')
8412+
outer.add_node(inner)
8413+
8414+
assert outer.most_recent_context.execution_id is None
8415+
assert inner.most_recent_context.execution_id is None
8416+
8417+
inner.run()
8418+
assert outer.most_recent_context.execution_id is None
8419+
assert inner.most_recent_context.execution_id == inner.name
8420+
8421+
if len(inner_pathway) == 0:
8422+
with pytest.raises(
8423+
pnl.MechanismError,
8424+
match=r'Number of inputs \(1\) to inner Output_CIM does not match its number of input_ports \(0\)'
8425+
) as e:
8426+
outer.run()
8427+
if e:
8428+
pytest.xfail(
8429+
reason='running outer comp with empty inner comp fails validation'
8430+
)
8431+
else:
8432+
outer.run()
8433+
assert outer.most_recent_context.execution_id == outer.name
8434+
assert inner.most_recent_context.execution_id == outer.name
8435+
8436+
# COMMAND_LINE to bypass check for input passed to nested comp
8437+
c = pnl.Context(source=pnl.ContextFlags.COMMAND_LINE)
8438+
inner.run(context=c)
8439+
assert outer.most_recent_context.execution_id == outer.name
8440+
assert inner.most_recent_context.execution_id == c.execution_id
8441+
84008442

84018443
class TestInputSpecsDocumentationExamples:
84028444

0 commit comments

Comments
 (0)