Skip to content

Commit

Permalink
ExpDesign: Add LHS space-filling algorithm
Browse files Browse the repository at this point in the history
Also add parameters for random.

Example:

--exp-design lhs(42,100)

Will sample 100 points using maxmin design, and the seed 42

Very usefull to quickly get the influence of the factors
  • Loading branch information
tbarbette committed Sep 6, 2024
1 parent b91b7a7 commit 1fcc342
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions npf/expdesign/fullexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, vlist, overriden):
newList = []
l = v.makeValues()

# For every of those new values we duplicate all existing runs
for nvalue in l:
for ovalue in self.expanded:
z = ovalue.copy()
Expand Down
11 changes: 7 additions & 4 deletions npf/expdesign/randomexp.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from random import shuffle

import random
from npf.expdesign.fullexp import FullVariableExpander


class RandomVariableExpander(FullVariableExpander):
"""Same as BruteVariableExpander but shuffle the series to test"""

def __init__(self, vlist, overriden):
def __init__(self, vlist, overriden, seed, n_iter):
super().__init__(vlist, overriden)
shuffle(self.expanded)

random.seed(seed)
random.shuffle(self.expanded)
if n_iter > 0:
self.expanded = self.expanded[:n_iter]
51 changes: 43 additions & 8 deletions npf/sections/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from collections import OrderedDict
import re
import time
from typing import List, Set
from npf.expdesign.fullexp import FullVariableExpander
from npf.expdesign.lhsexp import LHSVariableExpander
from npf.expdesign.randomexp import RandomVariableExpander
from npf.expdesign.zltexp import ZLTVariableExpander
from npf.repository import Repository
Expand Down Expand Up @@ -200,19 +202,52 @@ def replace_all(self, value):
values.append(SectionVariable.replace_variables(v, value))
return values

def expand(self, results={}, method="full", overriden=set()):
if method == "shuffle" or method == "rand" or method == "random":
return RandomVariableExpander(self.vlist)
def expand(self, results=None, method="full", overriden=set()):
"""Expands variables based on the specified method.
This method determines how to expand the variables in the list based on the provided method.
It can return different types of variable expanders depending on the method specified, including
random shuffling or specific ZLT expansions.
Args:
results (dict, optional): A dictionary of previous results for iterative methods. Defaults to an empty dictionary.
method (str, optional): The method to use for expansion. Can be "full", "shuffle", "rand", "random", or variations of "zl". Defaults to "full".
overriden (set, optional): A set of overridden variables. Defaults to an empty set.
Returns:
VariableExpander: An instance of the appropriate variable expander based on the method specified.
"""

if results is None:
results = {}
a = method.find("(")
params = []

if a > 0:
params = method[a + 1:-1].split(",")
if method == "shuffle" or method.startswith("rand"):
if len(params) >= 1:
seed = int(params[0])
else:
print("WARNING: Using a time-based seed. Please set the seed with --exp-design random(42)")
seed = time.time()
return RandomVariableExpander(self.vlist, overriden, seed = seed, n_iter = int(params[1]) if len(params) >= 2 else -1)
elif method.startswith("lhs"):
if len(params) >= 1:
seed = int(params[0])
else:
print("WARNING: Using a time-based seed. Please set the seed with --exp-design lhs(42)")
seed = time.time()
return LHSVariableExpander(self.vlist, overriden, seed = seed, n_iter = int(params[1]) if len(params) >= 2 else -1)
elif "zl" in method.lower():
if method.lower().startswith("allzl"):
params = method[7:-1].split(",")
all = True
all_lower = True
perc = method.lower()[5] == 'p'
else:
params = method[4:-1].split(",")
all = False
all_lower = False
perc = method.lower()[2] == 'p'
return ZLTVariableExpander(self.vlist, overriden=overriden, results=results, input=params[0], output=params[1], margin=1.01 if len(params) == 2 else float(params[2]), all=all, perc=perc)
return ZLTVariableExpander(self.vlist, overriden=overriden, results=results, input=params[0], output=params[1], margin=1.01 if len(params) == 2 else float(params[2]), all=all_lower, perc=perc)

else:
return FullVariableExpander(self.vlist, overriden)
Expand Down
1 change: 1 addition & 0 deletions npf/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ def printline(n):
else:
interaction_matrix.loc[factors[0],result_type] = interaction_pvalues[interaction]

print("")
print("P-value of ANOVA (low p-value indicates a probable interaction):")
print(interaction_matrix.fillna(""))
ax = sn.heatmap(interaction_matrix, cmap="viridis", fmt=".2f", annot=True)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
'jinja2',
'spellwise',
'seaborn',
'statsmodels'
'statsmodels',
'scikit-optimize'
]

setuptools.setup(
Expand Down

0 comments on commit 1fcc342

Please sign in to comment.