Commit ed3e5d0f authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Add more documentation

parent 4698589e
Pipeline #13455 failed with stages
in 13 minutes and 48 seconds
......@@ -61,7 +61,32 @@ class MultipleExtractor(Extractor):
class SequentialExtractor(SequentialProcessor, MultipleExtractor):
__doc__ = SequentialProcessor.__doc__
"""A helper class which takes several extractors and applies them one by
one sequentially.
Attributes
----------
processors : list
A list of extractors to apply.
Examples
--------
You can use this class to apply a chain of extractors on your data. For
example:
>>> import numpy as np
>>> from functools import partial
>>> from bob.bio.base.extractor import SequentialExtractor, CallableExtractor
>>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
>>> seq_extractor = SequentialExtractor(
... [CallableExtractor(f) for f in
... [np.cast['float64'], lambda x: x / 2, partial(np.mean, axis=1)]])
>>> seq_extractor(raw_data)
array([ 1., 1.])
>>> np.all(seq_extractor(raw_data) == \
... np.mean(np.cast['float64'](raw_data) / 2, axis=1))
True
"""
def __init__(self, processors):
......@@ -93,7 +118,42 @@ class SequentialExtractor(SequentialProcessor, MultipleExtractor):
class ParallelExtractor(ParallelProcessor, MultipleExtractor):
__doc__ = ParallelProcessor.__doc__
"""A helper class which takes several extractors and applies them on
each processor separately and yields their outputs one by one.
Attributes
----------
processors : list
A list of extractors to apply.
Examples
--------
You can use this class to apply several extractors on your data and get
all the results back. For example:
>>> import numpy as np
>>> from functools import partial
>>> from bob.bio.base.extractor import ParallelExtractor, CallableExtractor
>>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
>>> parallel_extractor = ParallelExtractor(
... [CallableExtractor(f) for f in
... [np.cast['float64'], lambda x: x / 2.0]])
>>> list(parallel_extractor(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.extractor import SequentialExtractor
>>> total_extractor = SequentialExtractor(
... [parallel_extractor, CallableExtractor(list),
... CallableExtractor(partial(np.concatenate, axis=1))])
>>> total_extractor(raw_data)
array([[ 1. , 2. , 3. , 0.5, 1. , 1.5],
[ 1. , 2. , 3. , 0.5, 1. , 1.5]])
"""
def __init__(self, processors):
......@@ -135,6 +195,13 @@ class CallableExtractor(Extractor):
A callable object with the signature of
``write_feature(feature, feature_file)``. If not provided, the default
implementation handles numpy arrays.
Examples
--------
You can take any function like ``numpy.cast['float32']`` to cast your data
to float32 for example. This is useful when you want to stack several
extractors using the :any:`SequentialExtractor` and
:any:`ParallelExtractor` classes.
"""
def __init__(self, callable, write_feature=None, read_feature=None,
......
......@@ -3,22 +3,44 @@ from .Preprocessor import Preprocessor
class SequentialPreprocessor(SequentialProcessor, Preprocessor):
__doc__ = SequentialProcessor.__doc__
"""A helper class which takes several preprocessors and applies them one by
one sequentially.
def __init__(self, processors, **kwargs):
min_preprocessed_file_size = 1000
try:
min_preprocessed_file_size = min(
(p.min_preprocessed_file_size for p in processors))
except AttributeError:
pass
Attributes
----------
processors : list
A list of preprocessors to apply.
Examples
--------
You can use this class to apply a chain of preprocessors on your data. For
example:
>>> import numpy as np
>>> from functools import partial
>>> from bob.bio.base.preprocessor import SequentialPreprocessor, CallablePreprocessor
>>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
>>> seq_preprocessor = SequentialPreprocessor(
... [CallablePreprocessor(f, accepts_annotations=False) for f in
... [np.cast['float64'], lambda x: x / 2, partial(np.mean, axis=1)]])
>>> seq_preprocessor(raw_data)
array([ 1., 1.])
>>> np.all(seq_preprocessor(raw_data) == \
... np.mean(np.cast['float64'](raw_data) / 2, axis=1))
True
"""
def __init__(self, processors, read_original_data=None, **kwargs):
min_preprocessed_file_size = min(
(p.min_preprocessed_file_size for p in processors))
if read_original_data is None:
read_original_data = processors[0].read_original_data
SequentialProcessor.__init__(self, processors)
Preprocessor.__init__(
self, min_preprocessed_file_size=min_preprocessed_file_size,
**kwargs)
def __call__(self, data, annotations):
def __call__(self, data, annotations=None):
return super(SequentialPreprocessor, self).__call__(
data, annotations=annotations)
......@@ -30,7 +52,42 @@ class SequentialPreprocessor(SequentialProcessor, Preprocessor):
class ParallelPreprocessor(ParallelProcessor, Preprocessor):
__doc__ = ParallelProcessor.__doc__
"""A helper class which takes several preprocessors and applies them on
each processor separately and yields their outputs one by one.
Attributes
----------
processors : list
A list of preprocessors to apply.
Examples
--------
You can use this class to apply several preprocessors on your data and get
all the results back. For example:
>>> import numpy as np
>>> from functools import partial
>>> from bob.bio.base.preprocessor import ParallelPreprocessor, CallablePreprocessor
>>> raw_data = np.array([[1, 2, 3], [1, 2, 3]])
>>> parallel_preprocessor = ParallelPreprocessor(
... [CallablePreprocessor(f, accepts_annotations=False) for f in
... [np.cast['float64'], lambda x: x / 2.0]])
>>> list(parallel_preprocessor(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.preprocessor import SequentialPreprocessor
>>> total_preprocessor = SequentialPreprocessor(
... [parallel_preprocessor, CallablePreprocessor(list, False),
... CallablePreprocessor(partial(np.concatenate, axis=1), False)])
>>> total_preprocessor(raw_data)
array([[ 1. , 2. , 3. , 0.5, 1. , 1.5],
[ 1. , 2. , 3. , 0.5, 1. , 1.5]])
"""
def __init__(self, processors, **kwargs):
min_preprocessed_file_size = min(p.min_preprocessed_file_size for p in
......@@ -41,7 +98,7 @@ class ParallelPreprocessor(ParallelProcessor, Preprocessor):
self, min_preprocessed_file_size=min_preprocessed_file_size,
**kwargs)
def __call__(self, data, annotations):
def __call__(self, data, annotations=None):
return super(ParallelPreprocessor, self).__call__(
data, annotations=annotations)
......@@ -65,6 +122,13 @@ class CallablePreprocessor(Preprocessor):
A callable object with the signature of
``write_data(data, data_file)``. If not provided, the default
implementation handles numpy arrays.
Examples
--------
You can take any function like ``numpy.cast['float32']`` to cast your data
to float32 for example. This is useful when you want to stack several
preprocessors using the :any:`SequentialPreprocessor` and
:any:`ParallelPreprocessor` classes.
"""
def __init__(self, callable, accepts_annotations=True, write_data=None,
......@@ -79,7 +143,7 @@ class CallablePreprocessor(Preprocessor):
if read_data is not None:
self.read_data = read_data
def __call__(self, data, annotations):
def __call__(self, data, annotations=None):
if self.accepts_annotations:
return self.callable(data, annotations)
else:
......
class SequentialProcessor(object):
"""A helper class which takes several processors and applies them one by
one sequentially
one sequentially.
Attributes
----------
processors : list
A list of processors to apply.
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
"""
def __init__(self, processors, **kwargs):
......@@ -41,6 +58,32 @@ class ParallelProcessor(object):
----------
processors : list
A list of processors to apply.
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]])
"""
def __init__(self, processors, **kwargs):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment