From e2a41e7cc2d40586bd1af1d5abe0d44fc02ac1d1 Mon Sep 17 00:00:00 2001 From: Amir MOHAMMADI <amir.mohammadi@idiap.ch> Date: Fri, 9 Mar 2018 16:34:15 +0100 Subject: [PATCH] Base stacked processors are moved to bob.extension --- bob/bio/base/extractor/stacks.py | 2 +- bob/bio/base/preprocessor/stacks.py | 2 +- bob/bio/base/test/test_stacks.py | 12 --- bob/bio/base/utils/__init__.py | 1 - bob/bio/base/utils/processors.py | 109 ---------------------------- doc/py_api.rst | 8 -- 6 files changed, 2 insertions(+), 132 deletions(-) delete mode 100644 bob/bio/base/utils/processors.py diff --git a/bob/bio/base/extractor/stacks.py b/bob/bio/base/extractor/stacks.py index 780dafdb..941b2895 100644 --- a/bob/bio/base/extractor/stacks.py +++ b/bob/bio/base/extractor/stacks.py @@ -1,4 +1,4 @@ -from ..utils.processors import SequentialProcessor, ParallelProcessor +from bob.extension.processors import SequentialProcessor, ParallelProcessor from .Extractor import Extractor from bob.io.base import HDF5File diff --git a/bob/bio/base/preprocessor/stacks.py b/bob/bio/base/preprocessor/stacks.py index 0ffc67ea..3fbda2cd 100644 --- a/bob/bio/base/preprocessor/stacks.py +++ b/bob/bio/base/preprocessor/stacks.py @@ -1,4 +1,4 @@ -from ..utils.processors import SequentialProcessor, ParallelProcessor +from bob.extension.processors import SequentialProcessor, ParallelProcessor from .Preprocessor import Preprocessor diff --git a/bob/bio/base/test/test_stacks.py b/bob/bio/base/test/test_stacks.py index a296a9a3..6b27123a 100644 --- a/bob/bio/base/test/test_stacks.py +++ b/bob/bio/base/test/test_stacks.py @@ -1,8 +1,6 @@ from functools import partial import numpy as np import tempfile -from bob.bio.base.utils.processors import ( - SequentialProcessor, ParallelProcessor) from bob.bio.base.preprocessor import ( SequentialPreprocessor, ParallelPreprocessor, CallablePreprocessor) from bob.bio.base.extractor import ( @@ -15,16 +13,6 @@ SEQ_DATA = PROCESSORS[1](PROCESSORS[0](DATA)) PAR_DATA = (PROCESSORS[0](DATA), PROCESSORS[1](DATA)) -def test_processors(): - proc = SequentialProcessor(PROCESSORS) - data = proc(DATA) - assert np.allclose(data, SEQ_DATA) - - proc = ParallelProcessor(PROCESSORS) - data = proc(DATA) - assert all(np.allclose(x1, x2) for x1, x2 in zip(data, PAR_DATA)) - - def test_preprocessors(): processors = [CallablePreprocessor(p, False) for p in PROCESSORS] proc = SequentialPreprocessor(processors) diff --git a/bob/bio/base/utils/__init__.py b/bob/bio/base/utils/__init__.py index 2cd7501a..75737bdc 100644 --- a/bob/bio/base/utils/__init__.py +++ b/bob/bio/base/utils/__init__.py @@ -6,7 +6,6 @@ from .resources import * from .io import * from .singleton import * -from . import processors import six import inspect import numpy diff --git a/bob/bio/base/utils/processors.py b/bob/bio/base/utils/processors.py deleted file mode 100644 index b01a953d..00000000 --- a/bob/bio/base/utils/processors.py +++ /dev/null @@ -1,109 +0,0 @@ -class SequentialProcessor(object): - """A helper class which takes several processors and applies them one by - 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): - super(SequentialProcessor, self).__init__(**kwargs) - self.processors = processors - - 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. - - Parameters - ---------- - data : object - The data that needs to be processed. - **kwargs - Any kwargs are passed to the processors. - - Returns - ------- - object - The processed data. - """ - for processor in self.processors: - data = processor(data, **kwargs) - return data - - -class ParallelProcessor(object): - """A helper class which takes several processors and applies them on each - processor separately and yields their outputs one by one. - - Attributes - ---------- - 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): - super(ParallelProcessor, self).__init__(**kwargs) - self.processors = processors - - def __call__(self, data, **kwargs): - """Applies the processors on the data independently and outputs a - generator of their outputs. - - Parameters - ---------- - data : object - The data that needs to be processed. - **kwargs - Any kwargs are passed to the processors. - - Yields - ------ - object - The processed data from processors one by one. - """ - for processor in self.processors: - yield processor(data, **kwargs) diff --git a/doc/py_api.rst b/doc/py_api.rst index 13daaa8f..b82661b4 100644 --- a/doc/py_api.rst +++ b/doc/py_api.rst @@ -42,14 +42,6 @@ Miscellaneous functions bob.bio.base.selected_indices -Generic classes ---------------- - -.. autosummary:: - bob.bio.base.utils.processors.SequentialProcessor - bob.bio.base.utils.processors.ParallelProcessor - - Tools to run recognition experiments ------------------------------------ -- GitLab