Skip to content

method

Method

Bases: Entity

Abstraction of owl:class ds:AtomicMethod.

❗ Important for contributors: See Section "Naming conventions" in README.md of "classes.tasks" package before extending the code's functionality.

Source code in exe_kg_lib/classes/method.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Method(Entity):
    """
    Abstraction of owl:class ds:AtomicMethod.

    ❗ Important for contributors: See Section "Naming conventions" in README.md of "classes.tasks" package before extending the code's functionality.
    """

    def __init__(
        self,
        iri: str,
        parent_entity: Entity,
        module_chain: List[str] = None,
        params_dict: Dict[str, Union[str, int, float]] = None,
        inherited_params_dict: Dict[str, Union[str, int, float]] = None,
    ):
        super().__init__(iri, parent_entity)

        if module_chain is None:
            module_chain = []
        self.module_chain = module_chain  # e.g. ['sklearn','model_selection', 'StratifiedShuffleSplit'] Used for resolving the Python module that contains the method to be executed

        if params_dict is None:
            params_dict = {}
        self.params_dict = params_dict  # used for storing method parameters during KG execution

        if inherited_params_dict is None:
            inherited_params_dict = {}
        self.inherited_params_dict = {}  # used for storing inherited method parameters during KG execution

    def resolve_module(self, module_name_to_snakecase=False) -> Any:
        """
        Resolves and returns the Python module specified by the method module chain.

        Args:
            module_name_to_snakecase (bool, optional): Whether to convert the last module name to snake case.
                                                      Defaults to False.

        Returns:
            Any: The resolved module.

        Raises:
            NotImplementedError: If the method module chain is not defined for the task.
        """
        if not self.module_chain:
            raise NotImplementedError(f"Method module chain not defined for task {self.name}.")

        module_chain = self.module_chain
        if module_name_to_snakecase:
            module_chain = self.module_chain[:-1] + [camel_to_snake(self.module_chain[-1])]

        module_chain_parents = ".".join(module_chain[:-1])
        module_chain_child = module_chain[-1]
        module_container = importlib.import_module(module_chain_parents)
        module = getattr(module_container, module_chain_child)
        return module

resolve_module(module_name_to_snakecase=False)

Resolves and returns the Python module specified by the method module chain.

Parameters:

Name Type Description Default
module_name_to_snakecase bool

Whether to convert the last module name to snake case. Defaults to False.

False

Returns:

Name Type Description
Any Any

The resolved module.

Raises:

Type Description
NotImplementedError

If the method module chain is not defined for the task.

Source code in exe_kg_lib/classes/method.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def resolve_module(self, module_name_to_snakecase=False) -> Any:
    """
    Resolves and returns the Python module specified by the method module chain.

    Args:
        module_name_to_snakecase (bool, optional): Whether to convert the last module name to snake case.
                                                  Defaults to False.

    Returns:
        Any: The resolved module.

    Raises:
        NotImplementedError: If the method module chain is not defined for the task.
    """
    if not self.module_chain:
        raise NotImplementedError(f"Method module chain not defined for task {self.name}.")

    module_chain = self.module_chain
    if module_name_to_snakecase:
        module_chain = self.module_chain[:-1] + [camel_to_snake(self.module_chain[-1])]

    module_chain_parents = ".".join(module_chain[:-1])
    module_chain_child = module_chain[-1]
    module_container = importlib.import_module(module_chain_parents)
    module = getattr(module_container, module_chain_child)
    return module