Skip to content

Commit fac0b57

Browse files
authored
Merge pull request #358 from UCL-CCS/fix_MC_for_one_param
Fix MCSampler for 1D problems
2 parents 78ca8fb + 43c141b commit fac0b57

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

easyvvuq/sampling/mc_sampler.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ def saltelli(self, n_mc):
108108
# number of different sampling matrices
109109
step = self.n_params + 2
110110
# store M2 first, with entries separated by step places
111+
if M_2.ndim == 1:
112+
M_2 = M_2.reshape([-1, 1])
111113
self.xi_mc[0:self.max_num:step] = M_2
112114
# store M1 entries last
115+
if M_1.ndim == 1:
116+
M_1 = M_1.reshape([-1, 1])
113117
self.xi_mc[(step - 1):self.max_num:step] = M_1
114118
# store N_i entries between M2 and M1
115119
for i in range(self.n_params):

tests/test_sampling_mc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
import chaospy as cp
3+
from easyvvuq.sampling import MCSampler
4+
from easyvvuq.sampling.base import Vary
5+
6+
7+
def test_sampling():
8+
vary = {'a': cp.Uniform(-5, 0), 'b': cp.Uniform(2, 10)}
9+
sampler = MCSampler(vary, 100)
10+
assert(sampler.n_samples() == 400)
11+
for _ in range(sampler.n_samples()):
12+
sample = next(sampler)
13+
assert(sample['a'] >= -5 and sample['a'] <= 0)
14+
assert(sample['b'] >= 2 and sample['b'] <= 10)
15+
with pytest.raises(StopIteration):
16+
next(sampler)
17+
18+
19+
def test_sampling_1D():
20+
vary = {'a': cp.Uniform(-1, 1)}
21+
sampler = MCSampler(vary, 100)
22+
# This used to fail in the saltelli subroutine if there was only 1 input
23+
for _ in range(sampler.n_samples()):
24+
sample = next(sampler)

0 commit comments

Comments
 (0)