easyvvuq.sampling.base

  1from easyvvuq.base_element import BaseElement
  2import logging
  3
  4__copyright__ = """
  5
  6    Copyright 2018 Robin A. Richardson, David W. Wright
  7
  8    This file is part of EasyVVUQ
  9
 10    EasyVVUQ is free software: you can redistribute it and/or modify
 11    it under the terms of the Lesser GNU General Public License as published by
 12    the Free Software Foundation, either version 3 of the License, or
 13    (at your option) any later version.
 14
 15    EasyVVUQ is distributed in the hope that it will be useful,
 16    but WITHOUT ANY WARRANTY; without even the implied warranty of
 17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18    Lesser GNU General Public License for more details.
 19
 20    You should have received a copy of the Lesser GNU General Public License
 21    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 22
 23"""
 24__license__ = "LGPL"
 25
 26
 27# Dict to store all registered samplers (any class which extends
 28# BaseSamplingElement is automatically registered as a sampler)
 29AVAILABLE_SAMPLERS = {}
 30
 31
 32class BaseSamplingElement(BaseElement):
 33    """Baseclass for all EasyVVUQ sampling elements.
 34
 35    Attributes
 36    ----------
 37    sampler_name : str
 38        Name of the particular sampler.
 39
 40    """
 41
 42    iteration = 0
 43
 44    def __init_subclass__(cls, sampler_name, **kwargs):
 45        """
 46        Catch any new samplers (all samplers must inherit from
 47        BaseSamplingElement) and add them to the dict of available samplers.
 48
 49        Parameters
 50        ----------
 51        sampler_name : str
 52            Name of the particular sampler.
 53        """
 54
 55        super().__init_subclass__(**kwargs)
 56
 57        cls.sampler_name = sampler_name
 58
 59        # Register new sampler
 60        AVAILABLE_SAMPLERS[sampler_name] = cls
 61
 62    def element_category(self):
 63        return "sampling"
 64
 65    def element_name(self):
 66        return self.sampler_name
 67
 68    def is_finite(self):
 69        raise NotImplementedError
 70
 71    def n_samples(self):
 72        raise NotImplementedError
 73
 74    def __iter__(self):
 75        """
 76        This method allows the sampler to be used as an iterator.
 77        The campaign object's draw_samples() method uses samplers
 78        in that manner.
 79        """
 80        return self
 81
 82    def __next__(self):
 83        """
 84        This must be implemented by any sampler class.
 85        It should return the next run in the sequence.
 86        In the case of a finite sampler, when there are
 87        no more runs remaining, __next__() should
 88        raise a StopIteration exception
 89        """
 90        raise NotImplementedError
 91
 92    @property
 93    def analysis_class(self):
 94        raise NotImplementedError
 95
 96    @property
 97    def sampler_id(self):
 98        try:
 99            return self._sampler_id
100        except AttributeError:
101            raise RuntimeError('this sampler does not have an id assigned, run set_sampler first')
102
103    @sampler_id.setter
104    def sampler_id(self, val):
105        self._sampler_id = val
106
107
108class Vary:
109    def __init__(self, vary_dict):
110        if vary_dict is None:
111            msg = ("'vary_dict' cannot be None. RandomSampler must be passed a "
112                   "dict of the names of the parameters you want to vary, "
113                   "and their corresponding distributions.")
114            logging.error(msg)
115            raise Exception(msg)
116        if not isinstance(vary_dict, dict):
117            msg = ("'vary_dict' must be a dictionary of the names of the "
118                   "parameters you want to vary, and their corresponding "
119                   "distributions.")
120            logging.error(msg)
121            raise Exception(msg)
122        if len(vary_dict) == 0:
123            msg = "'vary_dict' cannot be empty."
124            logging.error(msg)
125            raise Exception(msg)
126
127        self.vary_dict = vary_dict
128
129    def get_items(self):
130        return self.vary_dict.items()
131
132    def get_values(self):
133        return self.vary_dict.values()
134
135    def get_keys(self):
136        return self.vary_dict.keys()
137
138    def __str__(self):
139        return self.vary_dict.__str__()
AVAILABLE_SAMPLERS = {'random_sampler': <class 'easyvvuq.sampling.random.RandomSampler'>, 'sc_sampler': <class 'easyvvuq.sampling.stochastic_collocation.SCSampler'>, 'ssc_sampler': <class 'easyvvuq.sampling.simplex_stochastic_collocation.SSCSampler'>, 'PCE_sampler': <class 'easyvvuq.sampling.pce.PCESampler'>, 'FD_sampler': <class 'easyvvuq.sampling.fd.FDSampler'>, 'QMC_sampler': <class 'easyvvuq.sampling.qmc.QMCSampler'>, 'mcmc_sampler': <class 'easyvvuq.sampling.mcmc.MCMCSampler'>, 'basic_sweep': <class 'easyvvuq.sampling.sweep.BasicSweep'>, 'multisampler': <class 'easyvvuq.sampling.sampler_of_samplers.MultiSampler'>, 'lhc_sampler': <class 'easyvvuq.sampling.quasirandom.LHCSampler'>, 'halton_sampler': <class 'easyvvuq.sampling.quasirandom.HaltonSampler'>, 'empty': <class 'easyvvuq.sampling.empty.EmptySampler'>, 'replica_sampler': <class 'easyvvuq.sampling.replica_sampler.ReplicaSampler'>, 'MC_sampler': <class 'easyvvuq.sampling.mc_sampler.MCSampler'>, 'csv_sampler': <class 'easyvvuq.sampling.dataframe_sampler.DataFrameSampler'>, 'grid_sampler': <class 'easyvvuq.sampling.grid_sampler.Grid_Sampler'>}
class BaseSamplingElement(easyvvuq.base_element.BaseElement):
 33class BaseSamplingElement(BaseElement):
 34    """Baseclass for all EasyVVUQ sampling elements.
 35
 36    Attributes
 37    ----------
 38    sampler_name : str
 39        Name of the particular sampler.
 40
 41    """
 42
 43    iteration = 0
 44
 45    def __init_subclass__(cls, sampler_name, **kwargs):
 46        """
 47        Catch any new samplers (all samplers must inherit from
 48        BaseSamplingElement) and add them to the dict of available samplers.
 49
 50        Parameters
 51        ----------
 52        sampler_name : str
 53            Name of the particular sampler.
 54        """
 55
 56        super().__init_subclass__(**kwargs)
 57
 58        cls.sampler_name = sampler_name
 59
 60        # Register new sampler
 61        AVAILABLE_SAMPLERS[sampler_name] = cls
 62
 63    def element_category(self):
 64        return "sampling"
 65
 66    def element_name(self):
 67        return self.sampler_name
 68
 69    def is_finite(self):
 70        raise NotImplementedError
 71
 72    def n_samples(self):
 73        raise NotImplementedError
 74
 75    def __iter__(self):
 76        """
 77        This method allows the sampler to be used as an iterator.
 78        The campaign object's draw_samples() method uses samplers
 79        in that manner.
 80        """
 81        return self
 82
 83    def __next__(self):
 84        """
 85        This must be implemented by any sampler class.
 86        It should return the next run in the sequence.
 87        In the case of a finite sampler, when there are
 88        no more runs remaining, __next__() should
 89        raise a StopIteration exception
 90        """
 91        raise NotImplementedError
 92
 93    @property
 94    def analysis_class(self):
 95        raise NotImplementedError
 96
 97    @property
 98    def sampler_id(self):
 99        try:
100            return self._sampler_id
101        except AttributeError:
102            raise RuntimeError('this sampler does not have an id assigned, run set_sampler first')
103
104    @sampler_id.setter
105    def sampler_id(self, val):
106        self._sampler_id = val

Baseclass for all EasyVVUQ sampling elements.

Attributes
  • sampler_name (str): Name of the particular sampler.
iteration = 0
def element_category(self):
63    def element_category(self):
64        return "sampling"
def element_name(self):
66    def element_name(self):
67        return self.sampler_name
def is_finite(self):
69    def is_finite(self):
70        raise NotImplementedError
def n_samples(self):
72    def n_samples(self):
73        raise NotImplementedError
analysis_class
93    @property
94    def analysis_class(self):
95        raise NotImplementedError
sampler_id
 97    @property
 98    def sampler_id(self):
 99        try:
100            return self._sampler_id
101        except AttributeError:
102            raise RuntimeError('this sampler does not have an id assigned, run set_sampler first')
class Vary:
109class Vary:
110    def __init__(self, vary_dict):
111        if vary_dict is None:
112            msg = ("'vary_dict' cannot be None. RandomSampler must be passed a "
113                   "dict of the names of the parameters you want to vary, "
114                   "and their corresponding distributions.")
115            logging.error(msg)
116            raise Exception(msg)
117        if not isinstance(vary_dict, dict):
118            msg = ("'vary_dict' must be a dictionary of the names of the "
119                   "parameters you want to vary, and their corresponding "
120                   "distributions.")
121            logging.error(msg)
122            raise Exception(msg)
123        if len(vary_dict) == 0:
124            msg = "'vary_dict' cannot be empty."
125            logging.error(msg)
126            raise Exception(msg)
127
128        self.vary_dict = vary_dict
129
130    def get_items(self):
131        return self.vary_dict.items()
132
133    def get_values(self):
134        return self.vary_dict.values()
135
136    def get_keys(self):
137        return self.vary_dict.keys()
138
139    def __str__(self):
140        return self.vary_dict.__str__()
Vary(vary_dict)
110    def __init__(self, vary_dict):
111        if vary_dict is None:
112            msg = ("'vary_dict' cannot be None. RandomSampler must be passed a "
113                   "dict of the names of the parameters you want to vary, "
114                   "and their corresponding distributions.")
115            logging.error(msg)
116            raise Exception(msg)
117        if not isinstance(vary_dict, dict):
118            msg = ("'vary_dict' must be a dictionary of the names of the "
119                   "parameters you want to vary, and their corresponding "
120                   "distributions.")
121            logging.error(msg)
122            raise Exception(msg)
123        if len(vary_dict) == 0:
124            msg = "'vary_dict' cannot be empty."
125            logging.error(msg)
126            raise Exception(msg)
127
128        self.vary_dict = vary_dict
vary_dict
def get_items(self):
130    def get_items(self):
131        return self.vary_dict.items()
def get_values(self):
133    def get_values(self):
134        return self.vary_dict.values()
def get_keys(self):
136    def get_keys(self):
137        return self.vary_dict.keys()