Skip to content

Commit d279ef6

Browse files
author
Alan Christie
committed
feat: Add set_variables_from_options_for_step nto decoder
1 parent 9ff82f0 commit d279ef6

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

tests/test_decoder.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
_SIMPLE_PYTHON_MOLPROPS_WORKFLOW: Dict[str, Any] = yaml.safe_load(workflow_file)
3131
assert _SIMPLE_PYTHON_MOLPROPS_WORKFLOW
3232

33+
_SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW_FILE: str = os.path.join(
34+
os.path.dirname(__file__),
35+
"workflow-definitions",
36+
"simple-python-molprops-with-options.yaml",
37+
)
38+
with open(
39+
_SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW_FILE, "r", encoding="utf8"
40+
) as workflow_file:
41+
_SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW: Dict[str, Any] = yaml.safe_load(
42+
workflow_file
43+
)
44+
assert _SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW
45+
3346
_DUPLICATE_WORKFLOW_VARIABLE_NAMES_WORKFLOW_FILE: str = os.path.join(
3447
os.path.dirname(__file__),
3548
"workflow-definitions",
@@ -206,3 +219,41 @@ def test_get_workflow_variables_for_duplicate_variables():
206219
assert len(names) == 2
207220
assert names[0] == "x"
208221
assert names[1] == "x"
222+
223+
224+
def test_get_required_variable_names_for_simnple_python_molprops_with_options():
225+
# Arrange
226+
227+
# Act
228+
rqd_variables = decoder.get_required_variable_names(
229+
_SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW
230+
)
231+
232+
# Assert
233+
assert len(rqd_variables) == 2
234+
assert "candidateMolecules" in rqd_variables
235+
assert "rdkitPropertyValue" in rqd_variables
236+
237+
238+
@pytest.mark.skip(reason="The decoder does not currently handle options processing")
239+
def test_set_variables_from_options_for_step_for_simnple_python_molprops_with_options():
240+
# Arrange
241+
variables = {
242+
"rdkitPropertyName": "propertyName",
243+
"rdkitPropertyValue": "propertyValue",
244+
}
245+
246+
# Act
247+
new_variables, error = decoder.set_variables_from_options_for_step(
248+
_SIMPLE_PYTHON_MOLPROPS_WITH_OPTIONS_WORKFLOW,
249+
variables,
250+
"step1",
251+
)
252+
253+
# Assert
254+
assert error is None
255+
assert len(new_variables) == 4
256+
assert "name" in new_variables
257+
assert "value" in new_variables
258+
assert new_variables["name"] == "propertyName"
259+
assert new_variables["value"] == "propertyValue"

tests/test_workflow_engine_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def test_workflow_engine_simple_python_molprops(basic_engine):
314314

315315

316316
@pytest.mark.skip(reason="The engine does not currently create the required variables")
317-
def test_workflow_engine_simple_python_molprops_with_option(basic_engine):
317+
def test_workflow_engine_simple_python_molprops_with_options(basic_engine):
318318
# Arrange
319319
da, md = basic_engine
320320
# Make sure files that should be generated by the test

workflow/decoder.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,42 @@ def get_variable_names(definition: dict[str, Any]) -> list[str]:
8282
return wf_variable_names
8383

8484

85+
def set_variables_from_options_for_step(
86+
definition: dict[str, Any], variables: dict[str, Any], step_name: str
87+
) -> tuple[dict[str, Any], str | None]:
88+
"""Given a Workflow definition, an existing map of variables and values,
89+
and a step name this function returns a new set of variables by adding
90+
variables and values that are required for the step that have been defined in the
91+
workflow's variables->options block.
92+
93+
As an example, the following option, which is used if the step name is 'step1',
94+
expects 'rdkitPropertyName' to exist in the current set of variables,
95+
and should be copied into the new set of variables using the key 'propertyName'
96+
and value that is the same as the one provided in the original 'rdkitPropertyName': -
97+
98+
name: rdkitPropertyName
99+
default: propertyName
100+
as:
101+
- option: propertyName
102+
step: step1
103+
104+
And ... in the above example ... if the input variables map
105+
is {"rdkitPropertyName": "rings"} then the output map would be
106+
{"rdkitPropertyName": "rings", "propertyName": "rings"}
107+
108+
The function returns a new variable map, with and an optional error string on error.
109+
"""
110+
111+
assert isinstance(definition, dict)
112+
assert isinstance(variables, dict)
113+
assert step_name
114+
115+
new_variables: dict[str, Any] = variables.copy()
116+
117+
# Success...
118+
return new_variables, None
119+
120+
85121
def get_required_variable_names(definition: dict[str, Any]) -> list[str]:
86122
"""Given a Workflow definition this function returns all the names of the
87123
variables that are required to be defined when it is RUN - i.e.

0 commit comments

Comments
 (0)