Skip to content

Workflow step

workflow_step

A wrapping of a BaseTool in a GEMSEO Discipline to enable running workflows of tools.

Classes

Input dataclass

Input(name: str = '', option_key: str = '')

Input(name: 'str' = '', option_key: 'str' = '')

Output dataclass

Output(name: str = '', attr_name: str = '')

Output(name: 'str' = '', attr_name: 'str' = '')

WorkflowStep

WorkflowStep(
    name: str,
    tool_name: str,
    tool_settings: BaseSettings,
    tool_constructor_options: dict | None = None,
    inputs: Iterable[Input] = (),
    outputs: Iterable[Output] = (),
)

Bases: Discipline

The base class for disciplines.

The :meth:.execute method is used to do compute output data from input data. The :meth:.linearize method can be used to compute the Jacobian of the differentiable outputs with respect to differentiated inputs. The :attr:.jac stores this Jacobian. This method can evaluate the true derivatives (default) if the :meth:_compute_jacobian method is implemented or approximated the derivatives, depending on the :attr:.linearization_mode (one of :attr:.LinearizationMode).

Initialize self. See help(type(self)) for accurate signature.

Parameters:

  • name (str) –

    The name of the discipline. If empty, use the name of the class.

  • tool_name (str) –

    The description is missing.

  • tool_settings (BaseSettings) –

    The description is missing.

  • tool_constructor_options (dict | None, default: None ) –

    The description is missing.

  • inputs (Iterable[Input], default: () ) –

    The description is missing.

  • outputs (Iterable[Output], default: () ) –

    The description is missing.

Source code in src\vimseo\workflow\workflow_step.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
def __init__(
    self,
    name: str,
    tool_name: str,
    tool_settings: BaseSettings,
    tool_constructor_options: dict | None = None,
    inputs: Iterable[Input] = (),
    outputs: Iterable[Output] = (),
):
    super().__init__(name)
    self._tool_name = tool_name
    self._tool_constructor_options = tool_constructor_options or {}
    tool = ToolsFactory().create(tool_name, **self._tool_constructor_options)
    tool.update_options(**tool_settings.model_dump())
    self._tool = tool
    self._inputs = inputs
    self._outputs = outputs
    self._tool_settings = tool_settings

    for i in self._inputs:
        data = self._tool._options[i.option_key]
        if data is None:
            self.input_grammar.update_from_types({i.name: None})
        else:
            self.input_grammar.update_from_data({i.name: data})

    for output in self._outputs:
        output_data = (
            self._tool.result
            if output.attr_name == "result"
            else getattr(self._tool.result, output.attr_name)
        )
        if output_data is None:
            self.output_grammar.update_from_types({output.name: None})
        else:
            self.output_grammar.update_from_data({output.name: output_data})

Functions:

deserialize_pydantic_settings

deserialize_pydantic_settings(
    input_settings: Mapping[str, Any],
)

Reconstruct the fields of a BaseSettings as Pydantic model instance if they are of this type.

Only the case of nested Pydantic model fields in dictionaries is handled:

class MySettings(BaseSettings):
    pydantic_field: MyPydanticField = MyPydanticField
    nested_pydantic_field: Mapping[str, MyPydanticField] = {}
    double_nested_pydantic_field: Mapping[str, Mapping[str, MyPydanticField]] = {}

The case of lists of Pydantic fields is not handled.

Source code in src\vimseo\workflow\workflow_step.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def deserialize_pydantic_settings(input_settings: Mapping[str, Any]):
    """Reconstruct the fields of a ``BaseSettings`` as Pydantic model instance if they
    are of this type.

    Only the case of nested Pydantic model fields in dictionaries is handled:

    ```
    class MySettings(BaseSettings):
        pydantic_field: MyPydanticField = MyPydanticField
        nested_pydantic_field: Mapping[str, MyPydanticField] = {}
        double_nested_pydantic_field: Mapping[str, Mapping[str, MyPydanticField]] = {}
    ```

    The case of lists of Pydantic fields is not handled.
    """
    settings = deepcopy(input_settings)
    for name, value in settings.items():
        if isinstance(value, dict):
            if "__pydantic_model__" in value:
                create_pydantic_model(name, value, settings)
            else:
                for name_, value_ in value.items():
                    if isinstance(value_, dict):
                        if "__pydantic_model__" in value_:
                            create_pydantic_model(name_, value_, settings[name])
                        else:
                            for name__, value__ in value_.items():
                                if (
                                    isinstance(value__, dict)
                                    and "__pydantic_model__" in value__
                                ):
                                    create_pydantic_model(
                                        name__,
                                        value__,
                                        settings[name][name_],
                                    )
    return settings

serialize_pydantic_settings

serialize_pydantic_settings(
    input_settings: BaseSettings,
    attr_names: Iterable[str] = (),
)

Serialize (JSON compatible) a BaseSettings Pydantic model defining a tool settings.

It handles the case where a field of BaseSettings is a Pydantic model. This field is serialized as a dictionary obtained with my_field.model_dump(), and the item {'pydantic_model': field_module.field_class_name} is added to this dictionary. It allows to be able to reconstruct the field as a Pydantic model instance later.

Source code in src\vimseo\workflow\workflow_step.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def serialize_pydantic_settings(
    input_settings: BaseSettings, attr_names: Iterable[str] = ()
):
    """Serialize (JSON compatible) a ``BaseSettings`` Pydantic model defining a tool
    settings.

    It handles the case where a field of ``BaseSettings`` is a Pydantic model.
    This field is serialized as a dictionary obtained with ``my_field.model_dump()``,
    and the item {'__pydantic_model__': ``field_module.field_class_name``} is added to
    this dictionary.
    It allows to be able to reconstruct the field as a Pydantic model instance later.
    """
    settings = {}
    default_settings = input_settings.model_dump()
    attr_names = default_settings.keys() if len(attr_names) == 0 else attr_names
    for attr in attr_names:
        # Handle the case where a settings field is a Pydantic model:
        if isinstance(input_settings.model_fields[attr].default, BaseModel):
            settings[attr] = get_pydantic_model_item(
                input_settings.model_fields[attr].default
            )
            settings[attr].update(
                input_settings.model_fields[attr].default.model_dump()
            )
        else:
            # Otherwise use the settings default value
            settings[attr] = default_settings[attr]
    return settings