-
Notifications
You must be signed in to change notification settings - Fork 1
/
designR.py
303 lines (233 loc) · 8.59 KB
/
designR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
__author__ = "Georgi Tancev"
__copyright__ = "© Georgi Tancev"
from itertools import combinations
import pandas as pd
import numpy as np
import altair as alt
import streamlit as st
from pyDOE2.doe_factorial import *
from pyDOE2.doe_composite import *
from pyDOE2.doe_box_behnken import *
from pyDOE2.doe_plackett_burman import *
from PIL import Image
import base64
from io import BytesIO
import xlsxwriter
image = Image.open("logo.png")
design_image = Image.open("fullfact.png")
def compute_table(M, low, high):
"""
Transforms orthogonal design to real values.
Inputs:
M, orthogonal design
low, values of lowest levels
high, values of highest levels
Output:
Matrix with real values.
"""
M_s = (M-np.min(M, axis=0))/(np.max(M, axis=0)-np.min(M, axis=0))
low = np.asarray(low)
high = np.asarray(high)
delta = high-low
return np.add(np.multiply(M_s, delta), low)
def to_excel(DataFrame):
"""
Creates excel file.
Inputs:
pd.DataFrame
Outputs:
Excel-file
"""
output = BytesIO()
writer = pd.ExcelWriter(output, engine="xlsxwriter")
DataFrame.to_excel(writer, sheet_name="Sheet1")
writer.save()
processed_data = output.getvalue()
return processed_data
def factorial_design(n_factors):
"""
Create two-level full factorial design.
Inputs:
n_factors, number of factors
Outputs:
design matrix
"""
return ff2n(n_factors)
def fractorial_design(n_factors):
"""
Create two-level fractional factorial design.
Inputs:
n_factors, number of factors
Outputs:
design matrix
"""
alphabet = "A B C D E F G H I J K L M N O P Q R S T"
length = int(np.ceil((n_factors+1)/2))
letters = alphabet[:2*length:2]
words = []
for k in reversed(range(1, length+1)):
for word in combinations(letters, k):
words.append("".join(word[:]))
generator = alphabet[:2*length]+" ".join(words[:(n_factors-length)])
gens = st.sidebar.text_input("""design generators
(separated by spaces)""",
value=generator)
n_generators = len((gens[:-1] if gens[-1] == " " else gens).split(" "))
try:
assert n_generators == n_factors
except AssertionError:
st.error("Number of generators does not match number of factors.")
st.stop()
return fracfact(gens)
def composite_design(n_factors):
"""
Create central composite design.
Inputs:
n_factors, number of factors
Outputs:
design matrix
"""
alpha = st.sidebar.selectbox("symmetry",
options=["orthogonal",
"rotatable"])
face = st.sidebar.selectbox("geometry",
options=["circumscribed",
"inscribed",
"faced"])
n_c1 = st.sidebar.number_input("""number of center points in
the factorial block""", min_value=4)
n_c2 = st.sidebar.number_input("""number of center points in
the star block""", min_value=4)
return ccdesign(n_factors, (n_c1, n_c2), alpha, face)
def box_behnken_design(n_factors):
"""
Create Box-Behnken design.
Inputs:
n_factors, number of factors
Outputs:
design matrix
"""
n_c1 = st.sidebar.number_input("""number of center points""",
min_value=4)
return bbdesign(n_factors, n_c1)
def plackett_burman_design(n_factors):
"""
Create Plackett-Burman design.
Inputs:
n_factors, number of factors
Outputs:
design matrix
"""
return pbdesign(n_factors)
def main():
st.set_page_config(page_title="design-R",
page_icon="🎨",
layout="centered",
initial_sidebar_state="auto")
# st.sidebar.markdown(f'<a href="https://github.com/gtancev">© Georgi Tancev</a>',
# unsafe_allow_html=True)
st.sidebar.subheader("Optional: Rescaling and sorting.")
rescaled = st.sidebar.checkbox("Rescale levels.", value=False)
sorted = st.sidebar.checkbox("Sort experiments.", value=True)
st.sidebar.subheader("Choose design class.")
design = st.sidebar.selectbox("design class",
options=["full factorial",
"fractional factorial",
"Plackett-Burman",
"central composite",
"Box-Behnken"],
index=2)
st.image(image, use_column_width=True,
caption="""design-R is a web application for
the design of (laboratory) experiments.""")
with st.expander(label="Read instructions."):
st.write("""
1. Set number of factors.
2. Choose design class (and customize it).
3. Optional: Set labels, minimum, and maximum values of factors.
4. Inspect design for correctness.
5. Download design protocol.
""")
st.subheader("Set number of factors.")
n_factors = st.slider("number of factors",
min_value=2,
max_value=10,
value=7, step=1)
st.subheader("Optional: Set labels and levels.")
if not rescaled:
st.info("""
If rescale option is not selected, min./max. levels
will not apply.
""")
col1, col2, col3 = st.columns(3)
labels = np.empty(n_factors, dtype=np.object_)
low = np.empty(n_factors)
high = np.empty(n_factors)
for i in range(1, n_factors+1):
with col1:
labels[i-1] = col1.text_input("label of factor "+str(i),
value="x"+str(i))
with col2:
low[i-1] = col2.number_input("min. level of factor "+str(i),
value=-1)
with col3:
high[i-1] = col3.number_input("max. level of factor "+str(i),
value=1)
if design == "full factorial":
M = factorial_design(n_factors)
elif design == "fractional factorial":
try:
M = fractorial_design(n_factors)
except IndexError:
st.error("""One or more higher order design generators
do not match base designs.""")
st.stop()
elif design == "Plackett-Burman":
M = plackett_burman_design(n_factors)
elif design == "central composite":
M = composite_design(n_factors)
else:
try:
M = box_behnken_design(n_factors)
except AssertionError:
st.error("""Number of factors must be at least three.""")
st.stop()
if rescaled:
M = compute_table(M, low, high)
if sorted:
for k in range(0, n_factors):
M = M[np.argsort(M[:, k], axis=0, kind="stable"), :]
table = pd.DataFrame(data=M,
index=np.arange(1, M.shape[0]+1),
columns=labels)
table.index.name = "Experiment"
st.subheader("Inspect experimental design.")
st.write("The number of experiments to be performed is",
table.shape[0], ".")
if sorted:
st.info("""
It is good practice to perform
experiments in a random order.
""")
st.dataframe(table)
st.download_button("Download design.",
to_excel(table),
"design.xlsx")
with st.sidebar.expander(label="Read more about design of experiments."):
st.markdown("""
The theory of experimental design proposes optimal
configurations of $k$ factors (i.e.,
independent variables $x_1$,
$x_2$, $...$, $x_k$) in order to asses their effect on
a dependent variable $y$ while minimizing
the number of experiments
that have to be performed. Nevertheless,
limiting the amount
of available data reduces the number of
parameters that can be included in a model,
thereby constraining model complexity.
""")
st.image(design_image, use_column_width=True,
caption="Full factorial design for three factors.")
return
main()