aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/core/data_analysis.py
blob: 6adb41e30b2bd14a2572304c369ae910da838ac1 (plain)
1
2
3
4
5
6
7
8
9
10
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
66
67
68
69
70
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Module for data analysis."""
from abc import ABC
from abc import abstractmethod
from dataclasses import dataclass
from typing import List

from mlia.core.common import DataItem
from mlia.core.mixins import ContextMixin


class DataAnalyzer(ABC):
    """Base class for the data analysis.

    Purpose of this class is to extract valuable data out of
    collected data which could be used for advice generation.

    This process consists of two steps:
      - analyze every item of the collected data
      - get analyzed data
    """

    @abstractmethod
    def analyze_data(self, data_item: DataItem) -> None:
        """Analyze data.

        :param data_item: item of the collected data
        """

    @abstractmethod
    def get_analyzed_data(self) -> List[DataItem]:
        """Get analyzed data."""


class ContextAwareDataAnalyzer(DataAnalyzer, ContextMixin):
    """Context aware data analyzer.

    This class makes easier access to the Context object. Context object could
    be automatically injected during workflow configuration.
    """


@dataclass
class Fact:
    """Base class for the facts.

    Fact represents some piece of knowledge about collected
    data.
    """


class FactExtractor(ContextAwareDataAnalyzer):
    """Data analyzer based on extracting facts.

    Utility class that makes fact extraction easier.
    Class maintains list of the extracted facts.
    """

    def __init__(self) -> None:
        """Init fact extractor."""
        self.facts: List[Fact] = []

    def get_analyzed_data(self) -> List[DataItem]:
        """Return list of the collected facts."""
        return self.facts

    def add_fact(self, fact: Fact) -> None:
        """Add fact."""
        self.facts.append(fact)