Skip to content

Commit 0f8da2b

Browse files
author
tommyod
committed
use permutations on background variables too
1 parent 177e479 commit 0f8da2b

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/semeio/fmudesign/create_design.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def generate(self, inputdict: Mapping[str, Any]) -> None:
121121

122122
# If background values used - read or generate
123123
if "background" in inputdict:
124-
self.add_background(inputdict["background"], max_reals, rng)
124+
self.add_background(
125+
inputdict["background"],
126+
max_reals,
127+
rng,
128+
correlation_iterations=inputdict.get("correlation_iterations", 0),
129+
)
125130

126131
sensitivity: SensitivityType
127132

@@ -348,6 +353,7 @@ def add_background(
348353
back_dict: Mapping[str, Any] | None,
349354
max_values: int,
350355
rng: np.random.Generator,
356+
correlation_iterations: int = 0,
351357
) -> None:
352358
"""Adding background as specified in dictionary.
353359
Either from external file or from distributions in background
@@ -357,13 +363,21 @@ def add_background(
357363
back_dict (OrderedDict): how to generate background values
358364
max_values (int): number of background values to generate
359365
rng (numpy.random.Generator): Random number generator instance
366+
correlation_iterations (int): Number of permutations performed
367+
on samples after Iman-Conover in an attempt to match observed
368+
correlation to desired correlation as well as possible.
360369
"""
361370
if back_dict is None:
362371
self.backgroundvalues = None
363372
elif "extern" in back_dict:
364373
self.backgroundvalues = _parameters_from_extern(back_dict["extern"])
365374
elif "parameters" in back_dict:
366-
self._add_dist_background(back_dict, max_values, rng)
375+
self._add_dist_background(
376+
back_dict,
377+
max_values,
378+
rng,
379+
correlation_iterations=correlation_iterations,
380+
)
367381

368382
def background_to_excel(
369383
self, filename: str, backgroundsheet: str = "Background"
@@ -442,7 +456,11 @@ def _fill_with_defaultvalues(self) -> None:
442456
raise LookupError(f"No defaultvalues given for parameter {key} ")
443457

444458
def _add_dist_background(
445-
self, back_dict: Mapping[str, Any], numreal: int, rng: np.random.Generator
459+
self,
460+
back_dict: Mapping[str, Any],
461+
numreal: int,
462+
rng: np.random.Generator,
463+
correlation_iterations: int,
446464
) -> None:
447465
"""Drawing background values from distributions
448466
specified in dictionary
@@ -462,7 +480,7 @@ def _add_dist_background(
462480
seedvalues=None,
463481
corrdict=back_dict["correlations"],
464482
rng=rng,
465-
correlation_iterations=0,
483+
correlation_iterations=correlation_iterations,
466484
)
467485

468486
mc_backgroundvalues = mc_background.sensvalues

tests/fmudesign/test_designmatrix.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ def test_endpoint(tmpdir, monkeypatch):
8686
Generating sensitivity : contacts
8787
Added sensitivity : contacts
8888
Generating sensitivity : multz
89-
Wrote 10 samples from 'MULTZ_ILE' Distribution("loguniform", a=0.0001, b=1.0)
89+
Wrote 10 samples from 'MULTZ_ILE'
9090
Added sensitivity : multz
9191
Generating sensitivity : sens6
92-
Wrote 10 samples from 'PARAM5' Distribution("truncnorm", a=-2.0, b=2.0, loc=3.0, scale=1.0)
93-
Wrote 10 samples from 'PARAM6' Distribution("uniform", loc=0.0, scale=1.0)
94-
Wrote 10 samples from 'PARAM7' Distribution("triang", loc=1.0, scale=4.0, c=0.5)
95-
Wrote 10 samples from 'FAULT_SEAL' Distribution("uniform")
92+
Wrote 10 samples from 'PARAM5'
93+
Wrote 10 samples from 'PARAM6'
94+
Wrote 10 samples from 'PARAM7'
95+
Wrote 10 samples from 'FAULT_SEAL'
9696
Added sensitivity : sens6
9797
Generating sensitivity : sens7
9898
Sampling parameters in 'corr1': ['PARAM9', 'PARAM10', 'PARAM11', 'PARAM12']
9999
100-
Warning: Correlation matrix 'corr1' is not consistent
100+
Warning: Correlation matrix 'corr1' is invalid
101101
Requirements:
102102
- Ones on the diagonal
103103
- Positive semi-definite matrix
@@ -117,10 +117,10 @@ def test_endpoint(tmpdir, monkeypatch):
117117
| PARAM10 | 0.74 | 1.00 | | |
118118
| PARAM11 | 0.11 | 0.74 | 1.00 | |
119119
| PARAM12 | 0.00 | 0.00 | 0.00 | 1.00 |
120-
Wrote 30 samples from 'PARAM9' Distribution("lognorm", s=1.0, scale=Exp(Constant(0.0)))
121-
Wrote 30 samples from 'PARAM10' Distribution("uniform", loc=0.0, scale=1.0)
122-
Wrote 30 samples from 'PARAM11' Distribution("triang", loc=1.0, scale=4.0, c=0.5)
123-
Wrote 30 samples from 'PARAM12' Distribution("loguniform", a=1.0, b=10.0)
120+
Wrote 30 samples from 'PARAM9'
121+
Wrote 30 samples from 'PARAM10'
122+
Wrote 30 samples from 'PARAM11'
123+
Wrote 30 samples from 'PARAM12'
124124
Added sensitivity : sens7
125125
Generating sensitivity : sens8
126126
Added sensitivity : sens8
@@ -129,7 +129,8 @@ def test_endpoint(tmpdir, monkeypatch):
129129
Provided number of background values (11) is smaller than number of realisations for sensitivity ('sens7', 'p10_p90') and parameter PARAM15. Will be filled with default values.
130130
Provided number of background values (11) is smaller than number of realisations for sensitivity ('sens7', 'p10_p90') and parameter PARAM16. Will be filled with default values.
131131
A total of 91 realizations were generated
132-
Designmatrix written to generateddesignmatrix.xlsx"""
132+
Designmatrix written to generateddesignmatrix.xlsx
133+
"""
133134

134135
assert result.stdout.split() == expected_output.split()
135136
assert Path("generateddesignmatrix.xlsx").exists # Default output file

0 commit comments

Comments
 (0)