easyvvuq.sampling.sampler_of_samplers
1from .base import BaseSamplingElement 2import logging 3import itertools 4import functools 5 6__copyright__ = """ 7 8 Copyright 2018 Robin A. Richardson, David W. Wright 9 10 This file is part of EasyVVUQ 11 12 EasyVVUQ is free software: you can redistribute it and/or modify 13 it under the terms of the Lesser GNU General Public License as published by 14 the Free Software Foundation, either version 3 of the License, or 15 (at your option) any later version. 16 17 EasyVVUQ is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 Lesser GNU General Public License for more details. 21 22 You should have received a copy of the Lesser GNU General Public License 23 along with this program. If not, see <https://www.gnu.org/licenses/>. 24 25""" 26__license__ = "LGPL" 27 28logger = logging.getLogger(__name__) 29 30 31class MultiSampler(BaseSamplingElement, sampler_name="multisampler"): 32 33 def __init__(self, *samplers, count=0): 34 """ 35 Expects one or more samplers 36 """ 37 38 if len(samplers) < 1: 39 raise RuntimeError("You need to supply at least one sampler to the MultiSampler") 40 41 self.samplers = samplers 42 43 # Multisampler is finite only if all samplers in it are finite 44 for sampler in self.samplers: 45 if sampler.is_finite() is False: 46 msg = "Multisampler must be composed of finite samplers" 47 logger.critical(msg) 48 raise RuntimeError(msg) 49 50 # Combine all the iterables/generators into one 51 self.multi_iterator = itertools.product(*self.samplers) 52 53 self.count = 0 54 for i in range(count): 55 try: 56 self.__next__() 57 except StopIteration: 58 logger.warning("Multisampler constructed, but has no samples left to draw.") 59 60 def is_finite(self): 61 return True 62 63 def n_samples(self): 64 """Returns the number of samples in this sampler. 65 66 Returns 67 ------- 68 a product of the sizes of samplers passed to MultiSampler 69 """ 70 return functools.reduce( 71 lambda x, y: x * y, [sampler.n_samples() for sampler in self.samplers], 1) 72 73 def __next__(self): 74 # Will raise StopIteration when there are none left 75 multisampler_run = self.multi_iterator.__next__() 76 77 run_dict = {} 78 for contribution in multisampler_run: 79 run_dict = {**run_dict, **contribution} 80 81 self.count += 1 82 return run_dict
logger =
<Logger easyvvuq.sampling.sampler_of_samplers (DEBUG)>
32class MultiSampler(BaseSamplingElement, sampler_name="multisampler"): 33 34 def __init__(self, *samplers, count=0): 35 """ 36 Expects one or more samplers 37 """ 38 39 if len(samplers) < 1: 40 raise RuntimeError("You need to supply at least one sampler to the MultiSampler") 41 42 self.samplers = samplers 43 44 # Multisampler is finite only if all samplers in it are finite 45 for sampler in self.samplers: 46 if sampler.is_finite() is False: 47 msg = "Multisampler must be composed of finite samplers" 48 logger.critical(msg) 49 raise RuntimeError(msg) 50 51 # Combine all the iterables/generators into one 52 self.multi_iterator = itertools.product(*self.samplers) 53 54 self.count = 0 55 for i in range(count): 56 try: 57 self.__next__() 58 except StopIteration: 59 logger.warning("Multisampler constructed, but has no samples left to draw.") 60 61 def is_finite(self): 62 return True 63 64 def n_samples(self): 65 """Returns the number of samples in this sampler. 66 67 Returns 68 ------- 69 a product of the sizes of samplers passed to MultiSampler 70 """ 71 return functools.reduce( 72 lambda x, y: x * y, [sampler.n_samples() for sampler in self.samplers], 1) 73 74 def __next__(self): 75 # Will raise StopIteration when there are none left 76 multisampler_run = self.multi_iterator.__next__() 77 78 run_dict = {} 79 for contribution in multisampler_run: 80 run_dict = {**run_dict, **contribution} 81 82 self.count += 1 83 return run_dict
Baseclass for all EasyVVUQ sampling elements.
Attributes
- sampler_name (str): Name of the particular sampler.
MultiSampler(*samplers, count=0)
34 def __init__(self, *samplers, count=0): 35 """ 36 Expects one or more samplers 37 """ 38 39 if len(samplers) < 1: 40 raise RuntimeError("You need to supply at least one sampler to the MultiSampler") 41 42 self.samplers = samplers 43 44 # Multisampler is finite only if all samplers in it are finite 45 for sampler in self.samplers: 46 if sampler.is_finite() is False: 47 msg = "Multisampler must be composed of finite samplers" 48 logger.critical(msg) 49 raise RuntimeError(msg) 50 51 # Combine all the iterables/generators into one 52 self.multi_iterator = itertools.product(*self.samplers) 53 54 self.count = 0 55 for i in range(count): 56 try: 57 self.__next__() 58 except StopIteration: 59 logger.warning("Multisampler constructed, but has no samples left to draw.")
Expects one or more samplers
def
n_samples(self):
64 def n_samples(self): 65 """Returns the number of samples in this sampler. 66 67 Returns 68 ------- 69 a product of the sizes of samplers passed to MultiSampler 70 """ 71 return functools.reduce( 72 lambda x, y: x * y, [sampler.n_samples() for sampler in self.samplers], 1)
Returns the number of samples in this sampler.
Returns
- a product of the sizes of samplers passed to MultiSampler