API Documentation

iai_powerflow_sdk.base_class

Base Class Module for iai_powerflow_sdk > View Source
"""
Base Class Module for iai_powerflow_sdk
"""
from abc import ABC, abstractmethod
from typing import Any, Dict, Type, Callable, Tuple

import torch
from torch.nn import Module
from torch.utils.data import Dataset
from ignite.metrics import Loss

from iai_powerflow_sdk.exception import IaiBaseModuleException, IaiBaseDatasetException, IaiBaseMLTaskException


def find_sub_class(root_class: Type, class_name: str) -> Callable:
    """
    Initialize an instance based on the root class and a class name

    Args:
        root_class (Type): the root class of the class to be initialized
        class_name (str): name of the class to be initialized

    Returns:
        Callable: an instance of the specified class

    Raises:
        Exception: If the base class cannot be found.
    """
    if root_class is None or class_name is None:
        raise Exception("root_class and class_name cannot be None.")

    subclasses = root_class.__subclasses__()
    while len(subclasses) > 0:
        subclass = subclasses.pop()
        if subclass.__name__ == class_name:
            return subclass
        subclasses.extend(subclass.__subclasses__())
    raise Exception("Can not find {} from subclasses of {}.".format(class_name, root_class.__name__))


class IaiBaseModule(ABC, Module):
    @abstractmethod
    def __init__(self) -> None:
        """
        Super class of all models in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters
        """
        super().__init__()
        pass

    @abstractmethod
    def forward(self) -> torch.Tensor:
        """
        The forward function which is responsible for computations and returns the outputs of model.
        Returns:
            y_predict (Tensor): it should be a tensor of dimensions [N, C, l1, ...] where N is the batch size and
                C is the the number of classes and l1..n are the other dimensions, if your output consist of multiple
                dimensions.
        """
        pass

    @classmethod
    def _create_from_config(cls, model_name: str, configs: Dict) -> Module:
        """
        The factory method which is used to instantiate custom modules based on json config in the UI.
        Args:
            model_name (str): model name
            configs (Dict): json config coming from UI.
        Returns:
            Module: A module instance ready to use in the PowerFlow pipeline.
        Raises:
            IaiBaseModelException: If configs are not a dictionary
        """
        if not isinstance(configs, dict):
            raise IaiBaseModuleException("Model config should be a dictionary.")

        return find_sub_class(IaiBaseModule, model_name)(**configs)


class IaiBaseDataset(ABC, Dataset):
    @abstractmethod
    def __init__(self, path: str) -> None:
        """
        Super class of all Datasets in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters.
        Args:
            path (str): this parameter is required in any dataset and refers to the source of data
            that can be a folder or file and gets set in the CLI.
        """
        self.path = path
        pass

    @abstractmethod
    def __getitem__(self, item: int) -> Tuple[torch.Tensor]:
        """
            The function which is responsible for producing each data point tensor.
            Returns:
                It should be a tuple of tensors, and the last tuple index should be the data point label.
                PowerFlow will feed the rest of the tuple as input to the model.
        """

    @abstractmethod
    def __len__(self) -> int:
        pass

    @classmethod
    def _create_from_config(cls, path: str, name: str, configs: Dict) -> Dataset:
        """
        The factory method which is used to instantiate custom dataset based on json config in the UI.
        Args:
            path (str): path to the data root, it can be a file or folder depending on the data type
            and it will be set on client call.
            name (str): model name
            configs (Dict): json config coming from UI.
        Returns:
            Dataset: a Dataset instance ready to use in the PowerFlow pipeline.
        Raises:
            IaiBaseDatasetException: Raised if configs are not a dictionary
        """
        if not isinstance(configs, dict):
            raise IaiBaseDatasetException("Dataset config should be a dictionary.")

        return find_sub_class(IaiBaseDataset, name)(path=path, **configs)
def find_sub_class(root_class: Type, class_name: str) -> Callable > View Source
def find_sub_class(root_class: Type, class_name: str) -> Callable:
    """
    Initialize an instance based on the root class and a class name

    Args:
        root_class (Type): the root class of the class to be initialized
        class_name (str): name of the class to be initialized

    Returns:
        Callable: an instance of the specified class

    Raises:
        Exception: If the base class cannot be found.
    """
    if root_class is None or class_name is None:
        raise Exception("root_class and class_name cannot be None.")

    subclasses = root_class.__subclasses__()
    while len(subclasses) > 0:
        subclass = subclasses.pop()
        if subclass.__name__ == class_name:
            return subclass
        subclasses.extend(subclass.__subclasses__())
    raise Exception("Can not find {} from subclasses of {}.".format(class_name, root_class.__name__))

Initialize an instance based on the root class and a class name

Args

  • root_class (Type): the root class of the class to be initialized

  • class_name (str): name of the class to be initialized

Returns

  • Callable: an instance of the specified class

Raises

  • Exception: If the base class cannot be found.

IaiBaseModule

# class IaiBaseModule(abc.ABC, torch.nn.modules.module.Module) > View Source
class IaiBaseModule(ABC, Module):
    @abstractmethod
    def __init__(self) -> None:
        """
        Super class of all models in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters
        """
        super().__init__()
        pass

    @abstractmethod
    def forward(self) -> torch.Tensor:
        """
        The forward function which is responsible for computations and returns the outputs of model.
        Returns:
            y_predict (Tensor): it should be a tensor of dimensions [N, C, l1, ...] where N is the batch size and
                C is the the number of classes and l1..n are the other dimensions, if your output consist of multiple
                dimensions.
        """
        pass

    @classmethod
    def _create_from_config(cls, model_name: str, configs: Dict) -> Module:
        """
        The factory method which is used to instantiate custom modules based on json config in the UI.
        Args:
            model_name (str): model name
            configs (Dict): json config coming from UI.
        Returns:
            Module: A module instance ready to use in the PowerFlow pipeline.
        Raises:
            IaiBaseModelException: If configs are not a dictionary
        """
        if not isinstance(configs, dict):
            raise IaiBaseModuleException("Model config should be a dictionary.")

        return find_sub_class(IaiBaseModule, model_name)(**configs)

Helper class that provides a standard way to create an ABC using inheritance.

@abstractmethod IaiBaseModule()
@abstractmethod
    def __init__(self) -> None:
        """
        Super class of all models in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters
        """
        super().__init__()
        pass

Super class of all models in PowerFlow Note: ALL child class SHOULD implement the __init__ functions with proper parameters

@abstractmethoddef forward(self) -> torch.Tensor:
@abstractmethod
    def forward(self) -> torch.Tensor:
        """
        The forward function which is responsible for computations and returns the outputs of model.
        Returns:
            y_predict (Tensor): it should be a tensor of dimensions [N, C, l1, ...] where N is the batch size and
                C is the the number of classes and l1..n are the other dimensions, if your output consist of multiple
                dimensions.
        """
        pass

The forward function which is responsible for computations and returns the outputs of model.

Returns

y_predict (Tensor): it should be a tensor of dimensions [N, C, l1, ...] where N is the batch size and C is the the number of classes and l1..n are the other dimensions, if your output consist of multiple dimensions.

Inherited Members

  • torch.nn.modules.module.Module dump_patches, register_buffer, register_parameter, add_module, get_submodule, get_parameter, get_buffer, get_extra_state, set_extra_state, apply, cuda, xpu, cpu, type, float, double, half, bfloat16, to_empty, to, register_backward_hook, register_full_backward_hook, register_forward_pre_hook, register_forward_hook, T_destination, state_dict, load_state_dict, parameters, named_parameters, buffers, named_buffers, children, named_children, modules, named_modules, train, eval, requires_grad_, zero_grad, share_memory, extra_repr

IaiBaseDataset

# class IaiBaseDataset(typing.Generic[+T_co]) > View Source
class IaiBaseDataset(ABC, Dataset):
    @abstractmethod
    def __init__(self, path: str) -> None:
        """
        Super class of all Datasets in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters.
        Args:
            path (str): this parameter is required in any dataset and refers to the source of data
            that can be a folder or file and gets set in the CLI.
        """
        self.path = path
        pass

    @abstractmethod
    def __getitem__(self, item: int) -> Tuple[torch.Tensor]:
        """
            The function which is responsible for producing each data point tensor.
            Returns:
                It should be a tuple of tensors, and the last tuple index should be the data point label.
                PowerFlow will feed the rest of the tuple as input to the model.
        """

    @abstractmethod
    def __len__(self) -> int:
        pass

    @classmethod
    def _create_from_config(cls, path: str, name: str, configs: Dict) -> Dataset:
        """
        The factory method which is used to instantiate custom dataset based on json config in the UI.
        Args:
            path (str): path to the data root, it can be a file or folder depending on the data type
            and it will be set on client call.
            name (str): model name
            configs (Dict): json config coming from UI.
        Returns:
            Dataset: a Dataset instance ready to use in the PowerFlow pipeline.
        Raises:
            IaiBaseDatasetException: Raised if configs are not a dictionary
        """
        if not isinstance(configs, dict):
            raise IaiBaseDatasetException("Dataset config should be a dictionary.")

        return find_sub_class(IaiBaseDataset, name)(path=path, **configs)

Helper class that provides a standard way to create an ABC using inheritance.

# @abstractmethodIaiBaseDataset(path: str) > View Source
 @abstractmethod
    def __init__(self, path: str) -> None:
        """
        Super class of all Datasets in PowerFlow
        Note: ALL child class SHOULD implement the __init__ functions with proper parameters.
        Args:
            path (str): this parameter is required in any dataset and refers to the source of data
            that can be a folder or file and gets set in the CLI.
        """
        self.path = path
        pass

Super class of all Datasets in PowerFlow Note: ALL child class SHOULD implement the __init__ functions with proper parameters.

Args

  • path (str): this parameter is required in any dataset and refers to the source of data

  • that can be a folder or file and gets set in the CLI.

Inherited Members

torch.utils.data.dataset.Dataset functions, register_function, register_datapipe_as_function

Last updated