Skip to content

Ensemble

EnsembleBase

Bases: ABC

Base class for ensemble methods. Ensemble methods are used to combine the annotations of multiple annotators into a single annotation. The ensemble method is called with a list of annotations, where each annotation is a list of probabilities for each label. The ensemble method should return a single annotation, which is a list of probabilities for each label.

Source code in src/ensemble/ensemble.py
 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
class EnsembleBase(ABC):
    """
    Base class for ensemble methods. Ensemble methods are used to combine the annotations of multiple annotators into
    a single annotation. The ensemble method is called with a list of annotations, where each annotation is a list of
    probabilities for each label. The ensemble method should return a single annotation, which is a list of probabilities
    for each label.
    """

    def __init__(self):
        pass

    def __call__(self, annotations: List[Union[np.array, Annotation]], *args, **kwargs) \
            -> Tuple[Union[List | np.array], int]:
        """
        Making the ensemble method callable allows to also define functions as ensemble methods instead of classes. This
        is useful for ensemble methods that do not have any state.
        :param annotations:
        :param args:
        :param kwargs:
        :return:
        """
        distributions, unannotated = self.run(annotations)
        return self.normalize(distributions), unannotated

    def run(self, annotations: List[Annotation]) -> Tuple[Union[List | np.array], int]:
        """
        Run the ensemble method. This method should be implemented by subclasses.
        The ensemble method is called with a list of annotations, where each annotation is a list of probabilities for
        each label. The ensemble method should return a single annotation, which is a list of probabilities for each
        label. The ensemble method should also return a boolean indicating whether the ensemble method was able to
        produce a valid annotation. If the ensemble method was not able to produce a valid annotation, the ensemble
        method should return the first annotation in the list of annotations.
        :param annotations:
        :return:
        """
        pass

    @staticmethod
    def normalize(annotations: np.array) -> np.array:
        """
        Normalize the annotations. This method is used to bring the ensemble result into probability vectors.
        :param annotations:
        :return:
        """
        return np.array(annotations) / np.linalg.norm(annotations)

__call__(annotations, *args, **kwargs)

Making the ensemble method callable allows to also define functions as ensemble methods instead of classes. This is useful for ensemble methods that do not have any state.

Parameters:

Name Type Description Default
annotations List[Union[array, Annotation]]
required
args
()
kwargs
{}

Returns:

Type Description
Tuple[Union[List | array], int]
Source code in src/ensemble/ensemble.py
20
21
22
23
24
25
26
27
28
29
30
31
def __call__(self, annotations: List[Union[np.array, Annotation]], *args, **kwargs) \
        -> Tuple[Union[List | np.array], int]:
    """
    Making the ensemble method callable allows to also define functions as ensemble methods instead of classes. This
    is useful for ensemble methods that do not have any state.
    :param annotations:
    :param args:
    :param kwargs:
    :return:
    """
    distributions, unannotated = self.run(annotations)
    return self.normalize(distributions), unannotated

normalize(annotations) staticmethod

Normalize the annotations. This method is used to bring the ensemble result into probability vectors.

Parameters:

Name Type Description Default
annotations array
required

Returns:

Type Description
array
Source code in src/ensemble/ensemble.py
46
47
48
49
50
51
52
53
@staticmethod
def normalize(annotations: np.array) -> np.array:
    """
    Normalize the annotations. This method is used to bring the ensemble result into probability vectors.
    :param annotations:
    :return:
    """
    return np.array(annotations) / np.linalg.norm(annotations)

run(annotations)

Run the ensemble method. This method should be implemented by subclasses. The ensemble method is called with a list of annotations, where each annotation is a list of probabilities for each label. The ensemble method should return a single annotation, which is a list of probabilities for each label. The ensemble method should also return a boolean indicating whether the ensemble method was able to produce a valid annotation. If the ensemble method was not able to produce a valid annotation, the ensemble method should return the first annotation in the list of annotations.

Parameters:

Name Type Description Default
annotations List[Annotation]
required

Returns:

Type Description
Tuple[Union[List | array], int]
Source code in src/ensemble/ensemble.py
33
34
35
36
37
38
39
40
41
42
43
44
def run(self, annotations: List[Annotation]) -> Tuple[Union[List | np.array], int]:
    """
    Run the ensemble method. This method should be implemented by subclasses.
    The ensemble method is called with a list of annotations, where each annotation is a list of probabilities for
    each label. The ensemble method should return a single annotation, which is a list of probabilities for each
    label. The ensemble method should also return a boolean indicating whether the ensemble method was able to
    produce a valid annotation. If the ensemble method was not able to produce a valid annotation, the ensemble
    method should return the first annotation in the list of annotations.
    :param annotations:
    :return:
    """
    pass