Skip to content

Commit

Permalink
Merge 2976c9f into 854be47
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitfold committed Jun 16, 2020
2 parents 854be47 + 2976c9f commit 92506b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions easyvvuq/sampling/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .sampler_of_samplers import MultiSampler
from .quasirandom import LHCSampler
from .empty import EmptySampler
from .replica_sampler import ReplicaSampler

__copyright__ = """
Expand Down
39 changes: 39 additions & 0 deletions easyvvuq/sampling/replica_sampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from easyvvuq.sampling import BaseSamplingElement
from itertools import cycle


class ReplicaSampler(BaseSamplingElement, sampler_name='replica_sampler'):
def __init__(self, sampler, ensemble_col='ensemble'):
if not sampler.is_finite():
raise RuntimeError("Replica sampler only works with finite samplers")
self.sampler = sampler
self.ensemble_col = ensemble_col
self.history = []
for sample in sampler:
self.history.append(sample)
self.size = len(self.history)
self.cycle = cycle(self.history)
self.ensemble = 0
self.counter = 0

def is_finite(self):
return False

def element_version(self):
return '0.1'

def n_samples(self):
raise RuntimeError("You can't get the number of samples in an infinite sampler")

def __next__(self):
params = dict(next(self.cycle))
if self.counter < self.size - 1:
self.counter += 1
else:
self.counter = 0
self.ensemble += 1
params[self.ensemble_col] = self.ensemble
return params

def is_restartable(self):
return False

0 comments on commit 92506b5

Please sign in to comment.