Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add WorkflowTopology class and workflow_to_workflow_topology operator #1902

Merged
merged 24 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
feded14
feat: add WorkflowTopology class and workflow_to_workflow_topology op…
Matteo-Baussart-ANSYS Nov 15, 2024
4ff8c4e
fix: updated Workflow.get_topology() to use derived classes
Matteo-Baussart-ANSYS Nov 21, 2024
f8467b6
feat: add tests for WorkflowTopology
Matteo-Baussart-ANSYS Nov 26, 2024
2078cd0
fix: add version conditions on new tests
Matteo-Baussart-ANSYS Nov 26, 2024
46c79f8
fix: !! (I forgot one)
Matteo-Baussart-ANSYS Nov 26, 2024
9880d99
feat: add test for WorkflowTopology.__str__()
Matteo-Baussart-ANSYS Nov 27, 2024
87aacb8
fix: remove leftover code
Matteo-Baussart-ANSYS Nov 27, 2024
4dfcee5
fix: add docstrings and typehinting
Matteo-Baussart-ANSYS Nov 29, 2024
2bc8fcd
feat: add titles and descriptions
Matteo-Baussart-ANSYS Nov 29, 2024
0e59baf
fix: add missing title and description for OperatorConnection
Matteo-Baussart-ANSYS Nov 29, 2024
09abcd7
fix: fix circular dependence
Matteo-Baussart-ANSYS Nov 29, 2024
5962788
feat: add get_output with CustomContainerBase and derived class
Matteo-Baussart-ANSYS Dec 2, 2024
f9ff71e
fix: simplify import of WorkflowTopology
Matteo-Baussart-ANSYS Dec 2, 2024
529909b
feat: add tests for derived class outputs
Matteo-Baussart-ANSYS Dec 2, 2024
0d8d7fc
fix: fix broken derived classes (mesh_info)
Matteo-Baussart-ANSYS Dec 2, 2024
2ba8475
fix: fix docstring
Matteo-Baussart-ANSYS Dec 2, 2024
da60366
fix: fix docstring escaping
Matteo-Baussart-ANSYS Dec 2, 2024
0b7522d
fix: remove extra import
Matteo-Baussart-ANSYS Dec 2, 2024
71f35ae
fix: add docstring/typehints; add exception in `Workflow.get_topology()`
Matteo-Baussart-ANSYS Dec 6, 2024
6052034
fix(test): revert some failing changes
Matteo-Baussart-ANSYS Dec 6, 2024
5a026b3
fix(test): revert some failing changes (bis)
Matteo-Baussart-ANSYS Dec 6, 2024
c047105
fix(test): revert some failing changes (ter)
Matteo-Baussart-ANSYS Dec 6, 2024
9e28ee6
fix: change version check
Matteo-Baussart-ANSYS Dec 6, 2024
1399247
feat: add ability to record derived types
Matteo-Baussart-ANSYS Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
CustomTypeFieldsCollection:type = _CollectionFactory(CustomTypeField)
GenericDataContainersCollection:type = _CollectionFactory(GenericDataContainer)
StringFieldsCollection:type = _CollectionFactory(StringField)
OperatorsCollection: type = _CollectionFactory(Operator)
AnyCollection:type = _Collection

# for matplotlib
Expand Down
46 changes: 46 additions & 0 deletions src/ansys/dpf/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import re
import sys
from enum import Enum
from typing import Dict

from ansys.dpf.core.misc import module_exists
from ansys.dpf.gate.common import locations, ProgressBarBase # noqa: F401
Expand Down Expand Up @@ -430,6 +431,51 @@ def type_to_special_dpf_constructors():
return _type_to_special_dpf_constructors


_derived_class_name_to_type = None


def derived_class_name_to_type() -> Dict[str, type]:
"""
Returns a mapping of derived class names to their corresponding Python classes.
Returns
-------
dict[str, type]
A dictionary mapping derived class names (str) to their corresponding
Python class objects.
"""
global _derived_class_name_to_type
if _derived_class_name_to_type is None:
from ansys.dpf.core.workflow_topology import WorkflowTopology

_derived_class_name_to_type = {"WorkflowTopology": WorkflowTopology}
return _derived_class_name_to_type


def record_derived_class(class_name: str, py_class: type, overwrite: bool = False):
"""
Records a new derived class in the mapping of class names to their corresponding Python classes.
This function updates the global dictionary that maps derived class names (str) to their corresponding
Python class objects (type). If the provided class name already exists in the dictionary, it will either
overwrite the existing mapping or leave it unchanged based on the `overwrite` flag.
Parameters
----------
class_name : str
The name of the derived class to be recorded.
py_class : type
The Python class type corresponding to the derived class.
overwrite : bool, optional
A flag indicating whether to overwrite an existing entry for the `class_name`.
If `True`, the entry will be overwritten. If `False` (default), the entry will
not be overwritten if it already exists.
"""
recorded_classes = derived_class_name_to_type()
if overwrite or class_name not in recorded_classes:
recorded_classes[class_name] = py_class


def create_dpf_instance(type, internal_obj, server):
spe_constructors = type_to_special_dpf_constructors()
if type in spe_constructors:
Expand Down
54 changes: 54 additions & 0 deletions src/ansys/dpf/core/custom_container_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (C) 2020 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
CustomContainerBase
===================
This module contains the `CustomContainerBase` class, which serves as a base
for creating wrappers around `GenericDataContainer` objects.
These wrappers provide an interface for accessing and managing data in
generic containers, enabling more intuitive usage and the addition of custom
behaviors tailored to specific use cases.
"""

from ansys.dpf.core.generic_data_container import GenericDataContainer


class CustomContainerBase:
"""
Base class for custom container wrappers.
This class provides a common interface for managing an underlying
`GenericDataContainer` object.
"""

def __init__(self, container: GenericDataContainer) -> None:
"""
Initialize the base container with a `GenericDataContainer`.
Parameters
----------
container : GenericDataContainer
The underlying data container to be wrapped by this class.
"""
self._container = container

Check warning on line 54 in src/ansys/dpf/core/custom_container_base.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/custom_container_base.py#L54

Added line #L54 was not covered by tests
Matteo-Baussart-ANSYS marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 14 additions & 2 deletions src/ansys/dpf/core/dpf_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ def _type_to_output_method(self):
mesh_info,
collection_base,
any,
custom_container_base,
)

out = [
Expand Down Expand Up @@ -481,6 +482,15 @@ def _type_to_output_method(self):
self._api.operator_getoutput_as_any,
lambda obj, type: any.Any(server=self._server, any_dpf=obj).cast(type),
),
(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Matteo-Baussart-ANSYS can a Workflow also potentially take in or output a WorkflowTopology object?
Can an operator take a WorkflowTopology as input?

Copy link
Collaborator Author

@Matteo-Baussart-ANSYS Matteo-Baussart-ANSYS Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WorkflowTopology can be the output for an Operator and from a Workflow. Following syntaxes are tested:

# For operator outputs
workflow_to_workflow_topology_op.outputs.workflow_topology()
workflow_to_workflow_topology_op.get_output(0, WorkflowTopology)
# For workflow outputs
workflow_topology = dpf_workflow_wrapper.get_output("output", WorkflowTopology)

As for inputs, there is currently no operator requiring a CustomContainerBase as input.

custom_container_base.CustomContainerBase,
self._api.operator_getoutput_generic_data_container,
lambda obj, type: type(
container=generic_data_container.GenericDataContainer(
generic_data_container=obj, server=self._server
)
),
),
]
if hasattr(self._api, "operator_getoutput_generic_data_container"):
out.append(
Expand Down Expand Up @@ -726,8 +736,10 @@ def default_config(name, server=None):

def __del__(self):
try:
if self._internal_obj is not None:
self._deleter_func[0](self._deleter_func[1](self))
if hasattr(self, "_deleter_func"):
obj = self._deleter_func[1](self)
if obj is not None:
self._deleter_func[0](obj)
except:
warnings.warn(traceback.format_exc())

Expand Down
40 changes: 40 additions & 0 deletions src/ansys/dpf/core/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import inspect
import sys
from typing import Any, Optional


def _sort_supported_kwargs(bound_method, **kwargs):
Expand All @@ -48,3 +49,42 @@
warnings.warn(txt)
# Return the accepted arguments
return kwargs_in


def indent(text: Any, subsequent_indent: str = "", initial_indent: Optional[str] = None) -> str:
"""Indents each line of a given text.

Parameters
----------
text : Any
The input text to be indented. If it is not already a string, it will be converted to one.
subsequent_indent : str, optional
The string to prefix all lines of the text after the first line. Default is an empty string.
initial_indent : Optional[str], optional
The string to prefix the first line of the text. If not provided, `subsequent_indent` will be used.

Returns
-------
str
The indented text with specified prefixes applied to each line.

Examples
--------
>>> text = "Hello\\nWorld"
>>> print(indent(text, subsequent_indent=" ", initial_indent="--> "))
--> Hello
World
"""
if initial_indent is None:
initial_indent = subsequent_indent

Check warning on line 79 in src/ansys/dpf/core/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/helpers/utils.py#L78-L79

Added lines #L78 - L79 were not covered by tests

if not isinstance(text, str):
text = str(text)

Check warning on line 82 in src/ansys/dpf/core/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/helpers/utils.py#L81-L82

Added lines #L81 - L82 were not covered by tests

lines = text.rstrip().splitlines()
indented_lines = [

Check warning on line 85 in src/ansys/dpf/core/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/helpers/utils.py#L84-L85

Added lines #L84 - L85 were not covered by tests
f"{initial_indent if index == 0 else subsequent_indent}{line}"
for (index, line) in enumerate(lines)
]

return "\n".join(indented_lines)

Check warning on line 90 in src/ansys/dpf/core/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/helpers/utils.py#L90

Added line #L90 was not covered by tests
25 changes: 16 additions & 9 deletions src/ansys/dpf/core/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,24 @@
elif type_output == "int32":
type_output = types.int

output = self._operator.get_output(self._pin, type_output)

type_output_derive_class = self._spec.name_derived_class
if type_output_derive_class == "":
return output

from ansys.dpf.core.common import derived_class_name_to_type

derived_type = derived_class_name_to_type().get(type_output_derive_class)
if derived_type is not None:
return derived_type(output)

Check warning on line 94 in src/ansys/dpf/core/outputs.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/outputs.py#L94

Added line #L94 was not covered by tests

if type_output_derive_class != "":
out_type = [
type_tuple
for type_tuple in self._operator._type_to_output_method
if type_output_derive_class in type_tuple
]
return out_type[0][0](self._operator.get_output(self._pin, type_output))
else:
return self._operator.get_output(self._pin, type_output)
derived_types = [
type_tuple
for type_tuple in self._operator._type_to_output_method
if type_output_derive_class in type_tuple
]
return derived_types[0][0](output)

def __call__(self):
return self.get_data()
Expand Down
30 changes: 30 additions & 0 deletions src/ansys/dpf/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
collection_base,
streams_container,
)
from ansys.dpf.core.custom_container_base import CustomContainerBase

out = [
(streams_container.StreamsContainer, self._api.work_flow_getoutput_streams),
Expand Down Expand Up @@ -421,6 +422,15 @@
self._api.work_flow_getoutput_as_any,
lambda obj, type: any.Any(server=self._server, any_dpf=obj).cast(type),
),
(
CustomContainerBase,
self._api.work_flow_getoutput_generic_data_container,
lambda obj, type: type(
container=generic_data_container.GenericDataContainer(
generic_data_container=obj, server=self._server
)
),
),
]
if hasattr(self._api, "work_flow_connect_generic_data_container"):
out.append(
Expand Down Expand Up @@ -953,6 +963,26 @@
"""Saves the workflow to a GraphViz file."""
return self._api.work_flow_export_graphviz(self, str(path))

@version_requires("10.0")
def get_topology(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Matteo-Baussart-ANSYS same, missing a docstring and typehinting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added docstring and typehinting

"""Get the topology of the workflow.

Returns
-------
workflow_topology : workflow_topology.WorkflowTopology

Notes
-----
Available from 10.0 server version.
"""
workflow_to_workflow_topology_op = dpf_operator.Operator(

Check warning on line 978 in src/ansys/dpf/core/workflow.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/workflow.py#L978

Added line #L978 was not covered by tests
"workflow_to_workflow_topology", server=self._server
)
workflow_to_workflow_topology_op.inputs.workflow.connect(self)
workflow_topology = workflow_to_workflow_topology_op.outputs.workflow_topology()

Check warning on line 982 in src/ansys/dpf/core/workflow.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/workflow.py#L981-L982

Added lines #L981 - L982 were not covered by tests

return workflow_topology

Check warning on line 984 in src/ansys/dpf/core/workflow.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/workflow.py#L984

Added line #L984 was not covered by tests

def __del__(self):
try:
if hasattr(self, "_internal_obj"):
Expand Down
26 changes: 26 additions & 0 deletions src/ansys/dpf/core/workflow_topology/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (C) 2020 - 2024 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from .workflow_topology import WorkflowTopology
from .operator_connection import OperatorConnection
from .data_connection import DataConnection
from .exposed_pin import ExposedPin
Loading
Loading