OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 2 # |
| 3 # Use of this source code is governed by a BSD-style license |
| 4 # that can be found in the LICENSE file in the root of the source |
| 5 # tree. An additional intellectual property rights grant can be found |
| 6 # in the file PATENTS. All contributing project authors may |
| 7 # be found in the AUTHORS file in the root of the source tree. |
| 8 |
| 9 import logging |
| 10 import os |
| 11 |
| 12 from . import data_access |
| 13 from . import eval_scores |
| 14 from . import noise_generation |
| 15 |
| 16 class ApmModuleSimulator(object): |
| 17 |
| 18 _NOISE_GENERATOR_CLASSES = noise_generation.NoiseGenerator.REGISTERED_CLASSES |
| 19 _EVAL_SCORE_WORKER_CLASSES = eval_scores.EvaluationScore.REGISTERED_CLASSES |
| 20 |
| 21 def __init__(self): |
| 22 # TODO(alessio): instance when implementation is ready. |
| 23 self._audioproc_wrapper = None |
| 24 self._evaluator = None |
| 25 |
| 26 self._base_output_path = None |
| 27 self._noise_generators = None |
| 28 self._evaluation_score_workers = None |
| 29 self._config_filepaths = None |
| 30 self._input_filepaths = None |
| 31 |
| 32 def run(self, config_filepaths, input_filepaths, noise_generator_names, |
| 33 eval_score_names, output_dir): |
| 34 """ |
| 35 Initializes paths and required instances, then runs all the simulations. |
| 36 """ |
| 37 self._base_output_path = os.path.abspath(output_dir) |
| 38 |
| 39 # Instance noise generators. |
| 40 self._noise_generators = [ |
| 41 self._NOISE_GENERATOR_CLASSES[name]() for name in noise_generator_names] |
| 42 |
| 43 # Instance evaluation score workers. |
| 44 self._evaluation_score_workers = [ |
| 45 self._EVAL_SCORE_WORKER_CLASSES[name]() for name in eval_score_names] |
| 46 |
| 47 # Set APM configuration file paths. |
| 48 self._config_filepaths = self._get_paths_collection(config_filepaths) |
| 49 |
| 50 # Set probing signal file paths. |
| 51 self._input_filepaths = self._get_paths_collection(input_filepaths) |
| 52 |
| 53 self._simulate_all() |
| 54 |
| 55 def _simulate_all(self): |
| 56 """ |
| 57 Iterates over the combinations of APM configurations, probing signals, and |
| 58 noise generators. |
| 59 """ |
| 60 # Try different APM config files. |
| 61 for config_name in self._config_filepaths: |
| 62 config_filepath = self._config_filepaths[config_name] |
| 63 |
| 64 # Try different probing signal files. |
| 65 for input_name in self._input_filepaths: |
| 66 input_filepath = self._input_filepaths[input_name] |
| 67 |
| 68 # Try different noise generators. |
| 69 for noise_generator in self._noise_generators: |
| 70 logging.info('config: <%s>, input: <%s>, noise: <%s>', |
| 71 config_name, input_name, noise_generator.NAME) |
| 72 |
| 73 # Output path for the input-noise pairs. It is used to cache the noisy |
| 74 # copies of the probing signals (shared across some simulations). |
| 75 input_noise_cache_path = os.path.join( |
| 76 self._base_output_path, |
| 77 '_cache', |
| 78 'input_{}-noise_{}'.format(input_name, noise_generator.NAME)) |
| 79 data_access.make_directory(input_noise_cache_path) |
| 80 logging.debug('input-noise cache path: <%s>', input_noise_cache_path) |
| 81 |
| 82 # Full output path. |
| 83 output_path = os.path.join( |
| 84 self._base_output_path, |
| 85 'cfg-{}'.format(config_name), |
| 86 'input-{}'.format(input_name), |
| 87 'noise-{}'.format(noise_generator.NAME)) |
| 88 data_access.make_directory(output_path) |
| 89 logging.debug('output path: <%s>', output_path) |
| 90 |
| 91 self._simulate(noise_generator, input_filepath, |
| 92 input_noise_cache_path, output_path, config_filepath) |
| 93 |
| 94 def _simulate(self, noise_generator, input_filepath, input_noise_cache_path, |
| 95 output_path, config_filepath): |
| 96 """ |
| 97 Simulates a given combination of APM configurations, probing signals, and |
| 98 noise generators. It iterates over the noise generator internal |
| 99 configurations. |
| 100 """ |
| 101 # TODO(alessio): implement. |
| 102 pass |
| 103 |
| 104 @classmethod |
| 105 def _get_paths_collection(cls, filepaths): |
| 106 """ |
| 107 Given a list of file paths, makes a collection with one pair for each item |
| 108 in the list where the key is the file name without extension and the value |
| 109 is the path. |
| 110 """ |
| 111 filepaths_collection = {} |
| 112 for filepath in filepaths: |
| 113 name = os.path.splitext(os.path.split(filepath)[1])[0] |
| 114 filepaths_collection[name] = os.path.abspath(filepath) |
| 115 return filepaths_collection |
OLD | NEW |