easyvvuq.sampling.random
1from .base import BaseSamplingElement, Vary 2 3__copyright__ = """ 4 Copyright 2018 Robin A. Richardson, David W. Wright 5 This file is part of EasyVVUQ 6 EasyVVUQ is free software: you can redistribute it and/or modify 7 it under the terms of the Lesser GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 EasyVVUQ is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 Lesser GNU General Public License for more details. 14 You should have received a copy of the Lesser GNU General Public License 15 along with this program. If not, see <https://www.gnu.org/licenses/>. 16""" 17__license__ = "LGPL" 18 19 20class RandomSampler(BaseSamplingElement, sampler_name="random_sampler"): 21 22 def __init__(self, vary=None, count=0, max_num=0, analysis_class=None): 23 """ 24 Expects dict of var names, and their corresponding distributions 25 """ 26 self.vary = Vary(vary) 27 self.count = count 28 self.max_num = max_num 29 if analysis_class is not None: 30 self.analysis_class_ = analysis_class 31 32 def element_version(self): 33 return "0.1" 34 35 def is_finite(self): 36 if self.max_num > 0: 37 return True 38 return False 39 40 @property 41 def analysis_class(self): 42 """Return a corresponding analysis class. 43 """ 44 if self.analysis_class_ is not None: 45 return self.analysis_class_ 46 else: 47 raise NotImplementedError 48 49 def n_samples(self): 50 """Returns the number of samples in this sampler. 51 Returns 52 ------- 53 if the user specifies maximum number of samples than return that, otherwise - error 54 """ 55 if self.is_finite(): 56 return self.max_num 57 else: 58 raise RuntimeError("You can't get the number of samples in an infinite sampler") 59 60 def __next__(self): 61 62 if self.is_finite(): 63 if self.count >= self.max_num: 64 raise StopIteration 65 66 run_dict = {} 67 for param_name, dist in self.vary.get_items(): 68 run_dict[param_name] = dist.sample(1)[0] 69 70 self.count += 1 71 return run_dict
21class RandomSampler(BaseSamplingElement, sampler_name="random_sampler"): 22 23 def __init__(self, vary=None, count=0, max_num=0, analysis_class=None): 24 """ 25 Expects dict of var names, and their corresponding distributions 26 """ 27 self.vary = Vary(vary) 28 self.count = count 29 self.max_num = max_num 30 if analysis_class is not None: 31 self.analysis_class_ = analysis_class 32 33 def element_version(self): 34 return "0.1" 35 36 def is_finite(self): 37 if self.max_num > 0: 38 return True 39 return False 40 41 @property 42 def analysis_class(self): 43 """Return a corresponding analysis class. 44 """ 45 if self.analysis_class_ is not None: 46 return self.analysis_class_ 47 else: 48 raise NotImplementedError 49 50 def n_samples(self): 51 """Returns the number of samples in this sampler. 52 Returns 53 ------- 54 if the user specifies maximum number of samples than return that, otherwise - error 55 """ 56 if self.is_finite(): 57 return self.max_num 58 else: 59 raise RuntimeError("You can't get the number of samples in an infinite sampler") 60 61 def __next__(self): 62 63 if self.is_finite(): 64 if self.count >= self.max_num: 65 raise StopIteration 66 67 run_dict = {} 68 for param_name, dist in self.vary.get_items(): 69 run_dict[param_name] = dist.sample(1)[0] 70 71 self.count += 1 72 return run_dict
Baseclass for all EasyVVUQ sampling elements.
Attributes
- sampler_name (str): Name of the particular sampler.
RandomSampler(vary=None, count=0, max_num=0, analysis_class=None)
23 def __init__(self, vary=None, count=0, max_num=0, analysis_class=None): 24 """ 25 Expects dict of var names, and their corresponding distributions 26 """ 27 self.vary = Vary(vary) 28 self.count = count 29 self.max_num = max_num 30 if analysis_class is not None: 31 self.analysis_class_ = analysis_class
Expects dict of var names, and their corresponding distributions
analysis_class
41 @property 42 def analysis_class(self): 43 """Return a corresponding analysis class. 44 """ 45 if self.analysis_class_ is not None: 46 return self.analysis_class_ 47 else: 48 raise NotImplementedError
Return a corresponding analysis class.
def
n_samples(self):
50 def n_samples(self): 51 """Returns the number of samples in this sampler. 52 Returns 53 ------- 54 if the user specifies maximum number of samples than return that, otherwise - error 55 """ 56 if self.is_finite(): 57 return self.max_num 58 else: 59 raise RuntimeError("You can't get the number of samples in an infinite sampler")
Returns the number of samples in this sampler.
Returns
- if the user specifies maximum number of samples than return that, otherwise - error