processors.py 3.37 KB
Newer Older
1
class SequentialProcessor(object):
2
    """A helper class which takes several processors and applies them one by
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
3
    one sequentially.
4

5
6
7
8
    Attributes
    ----------
    processors : list
        A list of processors to apply.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

    Examples
    --------
    You can use this class to apply a chain of processes on your data. For
    example:

    >>> import numpy as np
    >>> from functools import  partial
    >>> from bob.bio.base.utils.processors import SequentialProcessor
    >>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
    >>> seq_processor = SequentialProcessor(
    ...     [np.cast['float64'], lambda x: x / 2, partial(np.mean, axis=1)])
    >>> seq_processor(raw_data)
    array([ 1.,  1.])
    >>> np.all(seq_processor(raw_data) == \
    ...        np.mean(np.cast['float64'](raw_data) / 2, axis=1))
    True
26
    """
27

28
29
30
    def __init__(self, processors, **kwargs):
        super(SequentialProcessor, self).__init__()
        self.processors = processors
31

32
33
34
    def __call__(self, data, **kwargs):
        """Applies the processors on the data sequentially. The output of the
        first one goes as input to the next one.
35

36
37
38
39
40
41
        Parameters
        ----------
        data : object
            The data that needs to be processed.
        **kwargs
            Any kwargs are passed to the processors.
42

43
44
45
46
47
48
49
50
        Returns
        -------
        object
            The processed data.
        """
        for processor in self.processors:
            data = processor(data, **kwargs)
        return data
51
52
53


class ParallelProcessor(object):
54
    """A helper class which takes several processors and applies them on each
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
55
    processor separately and yields their outputs one by one.
56

57
58
59
60
    Attributes
    ----------
    processors : list
        A list of processors to apply.
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    Examples
    --------
    You can use this class to apply several processes on your data and get all
    the results back. For example:

    >>> import numpy as np
    >>> from functools import  partial
    >>> from bob.bio.base.utils.processors import ParallelProcessor
    >>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
    >>> parallel_processor = ParallelProcessor(
    ...     [np.cast['float64'], lambda x: x / 2.0])
    >>> list(parallel_processor(raw_data))
    [array([[ 1.,  2.,  3.],
           [ 1.,  2.,  3.]]),
     array([[ 0.5,  1. ,  1.5],
           [ 0.5,  1. ,  1.5]])]

    The data may be further processed using a :any:`SequentialProcessor`:

    >>> from bob.bio.base.utils.processors import SequentialProcessor
    >>> total_processor = SequentialProcessor(
    ...     [parallel_processor, list, partial(np.concatenate, axis=1)])
    >>> total_processor(raw_data)
    array([[ 1. ,  2. ,  3. ,  0.5,  1. ,  1.5],
           [ 1. ,  2. ,  3. ,  0.5,  1. ,  1.5]])
87
    """
88

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
89
    def __init__(self, processors, **kwargs):
90
91
        super(ParallelProcessor, self).__init__()
        self.processors = processors
92

93
    def __call__(self, data, **kwargs):
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
94
95
        """Applies the processors on the data independently and outputs a
        generator of their outputs.
96

97
98
99
100
101
102
        Parameters
        ----------
        data : object
            The data that needs to be processed.
        **kwargs
            Any kwargs are passed to the processors.
103

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
104
105
        Yields
        ------
106
        object
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
107
            The processed data from processors one by one.
108
109
        """
        for processor in self.processors:
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
110
            yield processor(data, **kwargs)