Skip to content

Commit b6f55f3

Browse files
authored
Merge pull request #804 from hpcflow/calibration-workflow
Hypercube Sampling Bounds
2 parents 8013ac3 + 3e728cf commit b6f55f3

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

hpcflow/sdk/core/parameters.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from typing_extensions import override, TypeIs
1515

1616
import numpy as np
17-
from scipy.stats.qmc import LatinHypercube
17+
from scipy.stats.qmc import LatinHypercube, scale
1818
from valida import Schema as ValidaSchema # type: ignore
1919

2020
from hpcflow.sdk.typing import hydrate
@@ -1584,6 +1584,7 @@ def _values_from_latin_hypercube(
15841584
paths: Sequence[str],
15851585
num_samples: int,
15861586
*,
1587+
bounds: dict[str, Sequence[float]] | None = None,
15871588
scramble: bool = True,
15881589
strength: int = 1,
15891590
optimization: Literal["random-cd", "lloyd"] | None = None,
@@ -1598,20 +1599,32 @@ def _values_from_latin_hypercube(
15981599
optimization=optimization,
15991600
rng=rng,
16001601
)
1602+
1603+
bounds = bounds or {}
1604+
1605+
parameter_ranges = np.array([bounds.get(path, [0, 1]) for path in paths]).T
1606+
1607+
lower_bound = parameter_ranges[0]
1608+
upper_bound = parameter_ranges[1]
1609+
16011610
try:
16021611
sampler = LatinHypercube(**kwargs)
16031612
except TypeError:
16041613
# `rng` was previously (<1.15.0) `seed`:
16051614
kwargs["seed"] = kwargs.pop("rng")
16061615
sampler = LatinHypercube(**kwargs)
1607-
return sampler.random(n=num_samples).T
1616+
1617+
samples = scale(sampler.random(n=num_samples), lower_bound, upper_bound).T
1618+
1619+
return samples
16081620

16091621
@classmethod
16101622
def from_latin_hypercube(
16111623
cls,
16121624
paths: Sequence[str],
16131625
num_samples: int,
16141626
*,
1627+
bounds: dict[str, Sequence[float]] | None = None,
16151628
scramble: bool = True,
16161629
strength: int = 1,
16171630
optimization: Literal["random-cd", "lloyd"] | None = None,
@@ -1629,6 +1642,7 @@ def from_latin_hypercube(
16291642
"strength": strength,
16301643
"optimization": optimization,
16311644
"rng": rng,
1645+
"bounds": bounds,
16321646
}
16331647
values = cls._values_from_latin_hypercube(**kwargs)
16341648
assert values is not None

hpcflow/tests/unit/test_multi_path_sequences.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ def test_MPS_latin_hypercube_sequence_values():
170170
assert np.array_equal(np.asarray(seq_2.values), mps_values[1])
171171

172172

173+
def test_MPS_latin_hypercube_sequence_bounds():
174+
175+
bounds = {"inputs.a": [16789.2, 17812.5]}
176+
177+
mps = hf.MultiPathSequence.from_latin_hypercube(
178+
paths=["inputs.a", "inputs.b"],
179+
num_samples=10,
180+
bounds=bounds,
181+
)
182+
183+
vals_arr = np.array(mps.values)
184+
assert vals_arr.shape == (2, 10)
185+
186+
vals_0 = vals_arr[0]
187+
bounds_0 = list(bounds.values())[0]
188+
189+
vals_1 = vals_arr[1]
190+
bounds_1 = [0, 1]
191+
192+
assert np.logical_and(vals_0 > bounds_0[0], vals_0 < bounds_0[1]).all()
193+
assert np.logical_and(vals_1 > bounds_1[0], vals_1 < bounds_1[1]).all()
194+
195+
173196
def test_MPS_move_from_sequences_list():
174197
wft_yaml = dedent(
175198
"""\

0 commit comments

Comments
 (0)