easyvvuq.sampling.replica_sampler

Replica Sampler

Summary

Primarily intended for sampling the same paramater values but with different random seed. Other uses may be possible. It takes a finite sampler and produces an infinite sampler from it. This infinite sampler loops through the parameters produced by the finite sampler and at each cycle adds a unique id number for that cycle to the parameter dictionary.

 1"""Replica Sampler
 2
 3Summary
 4-------
 5
 6Primarily intended for sampling the same paramater values but with
 7different random seed. Other uses may be possible. It takes a finite
 8sampler and produces an infinite sampler from it. This infinite
 9sampler loops through the parameters produced by the finite sampler
10and at each cycle adds a unique id number for that cycle to the
11parameter dictionary.
12"""
13
14from easyvvuq.sampling import BaseSamplingElement
15from itertools import cycle
16
17
18class ReplicaSampler(BaseSamplingElement, sampler_name='replica_sampler'):
19    """Replica Sampler
20
21    Parameters
22    ----------
23    sampler : an instance of a class derived from  BaseSamplingElement
24        a finite sampler to loop over
25
26    replica_col : string
27        a parameter name for the replica id
28    seed_col : string
29        a parameter name for the input parameter that specifies the RNG seed
30    replicas : int
31        number of replicas, if zero will result in an infinite sampler
32    """
33
34    def __init__(self, sampler, replica_col='ensemble_id', seed_col=None, replicas=0):
35        if not sampler.is_finite():
36            raise RuntimeError("Replica sampler only works with finite samplers")
37        self.sampler = sampler
38        self.replica_col = replica_col
39        self.replicas = replicas
40        self.sampler.n_replicas = replicas
41        self.reset()
42
43    def reset(self):
44        self.history = []
45        for sample in self.sampler:
46            self.history.append(sample)
47        self.size = len(self.history)
48        self.cycle = cycle(self.history)
49        self.counter = 0
50        if isinstance(self.sampler.n_samples, int):
51            self.total_counter = self.replicas * self.sampler.n_samples
52        else:
53            self.total_counter = self.replicas * self.sampler.n_samples()
54
55    def is_finite(self):
56        if self.replicas == 0:
57            return False
58        else:
59            return True
60
61    def n_samples(self):
62        if self.replicas == 0:
63            raise RuntimeError("You can't get the number of samples in an infinite sampler")
64        else:
65            return self.replicas * self.sampler.n_samples()
66
67    def __next__(self):
68        if self.replicas != 0:
69            self.total_counter -= 1
70            if self.total_counter < 0:
71                raise StopIteration
72        params = dict(next(self.cycle))
73        params[self.replica_col] = self.counter
74        self.counter = (self.counter + 1) % self.size
75        return params
76
77    def update(self, result, invalid):
78        self.reset()
79        return self.sampler.update(result, invalid)
80
81    @property
82    def iteration(self):
83        return self.sampler.iteration
84
85    @property
86    def analysis_class(self):
87        return self.sampler.analysis_class
88
89    @property
90    def inputs(self):
91        return self.sampler.inputs
92
93    @property
94    def qoi(self):
95        return self.sampler.qoi
class ReplicaSampler(easyvvuq.sampling.base.BaseSamplingElement):
19class ReplicaSampler(BaseSamplingElement, sampler_name='replica_sampler'):
20    """Replica Sampler
21
22    Parameters
23    ----------
24    sampler : an instance of a class derived from  BaseSamplingElement
25        a finite sampler to loop over
26
27    replica_col : string
28        a parameter name for the replica id
29    seed_col : string
30        a parameter name for the input parameter that specifies the RNG seed
31    replicas : int
32        number of replicas, if zero will result in an infinite sampler
33    """
34
35    def __init__(self, sampler, replica_col='ensemble_id', seed_col=None, replicas=0):
36        if not sampler.is_finite():
37            raise RuntimeError("Replica sampler only works with finite samplers")
38        self.sampler = sampler
39        self.replica_col = replica_col
40        self.replicas = replicas
41        self.sampler.n_replicas = replicas
42        self.reset()
43
44    def reset(self):
45        self.history = []
46        for sample in self.sampler:
47            self.history.append(sample)
48        self.size = len(self.history)
49        self.cycle = cycle(self.history)
50        self.counter = 0
51        if isinstance(self.sampler.n_samples, int):
52            self.total_counter = self.replicas * self.sampler.n_samples
53        else:
54            self.total_counter = self.replicas * self.sampler.n_samples()
55
56    def is_finite(self):
57        if self.replicas == 0:
58            return False
59        else:
60            return True
61
62    def n_samples(self):
63        if self.replicas == 0:
64            raise RuntimeError("You can't get the number of samples in an infinite sampler")
65        else:
66            return self.replicas * self.sampler.n_samples()
67
68    def __next__(self):
69        if self.replicas != 0:
70            self.total_counter -= 1
71            if self.total_counter < 0:
72                raise StopIteration
73        params = dict(next(self.cycle))
74        params[self.replica_col] = self.counter
75        self.counter = (self.counter + 1) % self.size
76        return params
77
78    def update(self, result, invalid):
79        self.reset()
80        return self.sampler.update(result, invalid)
81
82    @property
83    def iteration(self):
84        return self.sampler.iteration
85
86    @property
87    def analysis_class(self):
88        return self.sampler.analysis_class
89
90    @property
91    def inputs(self):
92        return self.sampler.inputs
93
94    @property
95    def qoi(self):
96        return self.sampler.qoi

Replica Sampler

Parameters
  • sampler (an instance of a class derived from BaseSamplingElement): a finite sampler to loop over
  • replica_col (string): a parameter name for the replica id
  • seed_col (string): a parameter name for the input parameter that specifies the RNG seed
  • replicas (int): number of replicas, if zero will result in an infinite sampler
ReplicaSampler(sampler, replica_col='ensemble_id', seed_col=None, replicas=0)
35    def __init__(self, sampler, replica_col='ensemble_id', seed_col=None, replicas=0):
36        if not sampler.is_finite():
37            raise RuntimeError("Replica sampler only works with finite samplers")
38        self.sampler = sampler
39        self.replica_col = replica_col
40        self.replicas = replicas
41        self.sampler.n_replicas = replicas
42        self.reset()
sampler
replica_col
replicas
def reset(self):
44    def reset(self):
45        self.history = []
46        for sample in self.sampler:
47            self.history.append(sample)
48        self.size = len(self.history)
49        self.cycle = cycle(self.history)
50        self.counter = 0
51        if isinstance(self.sampler.n_samples, int):
52            self.total_counter = self.replicas * self.sampler.n_samples
53        else:
54            self.total_counter = self.replicas * self.sampler.n_samples()
def is_finite(self):
56    def is_finite(self):
57        if self.replicas == 0:
58            return False
59        else:
60            return True
def n_samples(self):
62    def n_samples(self):
63        if self.replicas == 0:
64            raise RuntimeError("You can't get the number of samples in an infinite sampler")
65        else:
66            return self.replicas * self.sampler.n_samples()
def update(self, result, invalid):
78    def update(self, result, invalid):
79        self.reset()
80        return self.sampler.update(result, invalid)
iteration
82    @property
83    def iteration(self):
84        return self.sampler.iteration

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

>>> int('0b100', base=0)
4
analysis_class
86    @property
87    def analysis_class(self):
88        return self.sampler.analysis_class
inputs
90    @property
91    def inputs(self):
92        return self.sampler.inputs
qoi
94    @property
95    def qoi(self):
96        return self.sampler.qoi
sampler_name = 'replica_sampler'