-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"""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. | ||
""" | ||
|
||
from easyvvuq.sampling import BaseSamplingElement | ||
from itertools import cycle | ||
|
||
|
||
class ReplicaSampler(BaseSamplingElement, sampler_name='replica_sampler'): | ||
"""Replica Sampler | ||
Parameters | ||
---------- | ||
sampler : an instance of a class derived from BaseSamplingElement | ||
a finite sampler to loop over | ||
ensemble_col : string | ||
a parameter name for the ensemble id | ||
""" | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from easyvvuq.sampling import ReplicaSampler | ||
from easyvvuq.sampling import BasicSweep | ||
from easyvvuq.sampling import EmptySampler | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def replica_sampler(): | ||
return ReplicaSampler(BasicSweep({'a': [1, 2], 'b': [3, 4]})) | ||
|
||
|
||
def test_infite_exception(): | ||
with pytest.raises(RuntimeError): | ||
ReplicaSampler(EmptySampler()) | ||
|
||
|
||
def test_is_finite(replica_sampler): | ||
assert(not replica_sampler.is_finite()) | ||
|
||
|
||
def test_element_version(replica_sampler): | ||
assert(replica_sampler.element_version() == '0.1') | ||
|
||
|
||
def test_n_samples(replica_sampler): | ||
with pytest.raises(RuntimeError): | ||
replica_sampler.n_samples() | ||
|
||
|
||
def test_replica_sampler(replica_sampler): | ||
for _ in range(18): | ||
params = next(replica_sampler) | ||
assert(params['a'] == 1) | ||
assert(params['b'] == 4) | ||
assert(params['ensemble'] == 4) |