easyvvuq.sampling.sweep

 1from .base import BaseSamplingElement
 2import itertools
 3import logging
 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
31def wrap_iterable(var_name, iterable):
32    for val in iterable:
33        yield (var_name, val)
34
35
36class BasicSweep(BaseSamplingElement, sampler_name="basic_sweep"):
37
38    def __init__(self, sweep=None, count=0):
39        """
40            Expects dict of var names, and their corresponding lists of values to cycle through
41        """
42        self.sweep = sweep
43
44        gens = []
45        for var_name, iterable in self.sweep.items():
46            gens.append(wrap_iterable(var_name, iterable))
47
48        # Combine all the iterables/generators into one
49        self.sweep_iterator = itertools.product(*gens)
50
51        self.count = 0
52        for i in range(count):
53            try:
54                self.__next__()
55            except StopIteration:
56                logger.warning("BasicSweep constructed, but has no samples left to draw.")
57
58    def is_finite(self):
59        return True
60
61    def n_samples(self):
62        """Returns the number of samples in this sampler.
63
64        Returns
65        -------
66        a product of the lengths of lists passed to BasicSweep
67        """
68        return functools.reduce(
69            lambda x, y: x * y, [len(lst) for lst in [self.sweep[key] for key in self.sweep]], 1)
70
71    def __next__(self):
72        # Will raise StopIteration when there are none left
73        sweep_run = self.sweep_iterator.__next__()
74
75        run_dict = {}
76        for var_name, value in sweep_run:
77            run_dict[var_name] = value
78
79        self.count += 1
80        return run_dict
logger = <Logger easyvvuq.sampling.sweep (DEBUG)>
def wrap_iterable(var_name, iterable):
32def wrap_iterable(var_name, iterable):
33    for val in iterable:
34        yield (var_name, val)
class BasicSweep(easyvvuq.sampling.base.BaseSamplingElement):
37class BasicSweep(BaseSamplingElement, sampler_name="basic_sweep"):
38
39    def __init__(self, sweep=None, count=0):
40        """
41            Expects dict of var names, and their corresponding lists of values to cycle through
42        """
43        self.sweep = sweep
44
45        gens = []
46        for var_name, iterable in self.sweep.items():
47            gens.append(wrap_iterable(var_name, iterable))
48
49        # Combine all the iterables/generators into one
50        self.sweep_iterator = itertools.product(*gens)
51
52        self.count = 0
53        for i in range(count):
54            try:
55                self.__next__()
56            except StopIteration:
57                logger.warning("BasicSweep constructed, but has no samples left to draw.")
58
59    def is_finite(self):
60        return True
61
62    def n_samples(self):
63        """Returns the number of samples in this sampler.
64
65        Returns
66        -------
67        a product of the lengths of lists passed to BasicSweep
68        """
69        return functools.reduce(
70            lambda x, y: x * y, [len(lst) for lst in [self.sweep[key] for key in self.sweep]], 1)
71
72    def __next__(self):
73        # Will raise StopIteration when there are none left
74        sweep_run = self.sweep_iterator.__next__()
75
76        run_dict = {}
77        for var_name, value in sweep_run:
78            run_dict[var_name] = value
79
80        self.count += 1
81        return run_dict

Baseclass for all EasyVVUQ sampling elements.

Attributes
  • sampler_name (str): Name of the particular sampler.
BasicSweep(sweep=None, count=0)
39    def __init__(self, sweep=None, count=0):
40        """
41            Expects dict of var names, and their corresponding lists of values to cycle through
42        """
43        self.sweep = sweep
44
45        gens = []
46        for var_name, iterable in self.sweep.items():
47            gens.append(wrap_iterable(var_name, iterable))
48
49        # Combine all the iterables/generators into one
50        self.sweep_iterator = itertools.product(*gens)
51
52        self.count = 0
53        for i in range(count):
54            try:
55                self.__next__()
56            except StopIteration:
57                logger.warning("BasicSweep constructed, but has no samples left to draw.")

Expects dict of var names, and their corresponding lists of values to cycle through

sweep
sweep_iterator
count
def is_finite(self):
59    def is_finite(self):
60        return True
def n_samples(self):
62    def n_samples(self):
63        """Returns the number of samples in this sampler.
64
65        Returns
66        -------
67        a product of the lengths of lists passed to BasicSweep
68        """
69        return functools.reduce(
70            lambda x, y: x * y, [len(lst) for lst in [self.sweep[key] for key in self.sweep]], 1)

Returns the number of samples in this sampler.

Returns
  • a product of the lengths of lists passed to BasicSweep
sampler_name = 'basic_sweep'