-
Notifications
You must be signed in to change notification settings - Fork 0
/
msslhs.py
297 lines (232 loc) · 8.79 KB
/
msslhs.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
# Script for generating standard uniform/standard normal Latin-Hypercube samples
# Parameter space is mapped to an array index space using integer truncation
# Once array space is filled, a random value is generated within each cell
# Array space is filled using strata index exclusion
'''
Copyright (c) 2017 Justin M. Hughes
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
'''
import numpy as np
import matplotlib.pyplot as plt
import random,sys,os
from copy import deepcopy
import scipy.stats as st
def getIndex(val,h,minVal=0.0,maxVal=1.0):
minIndex = int(float(minVal/h))
valIndex = int(float(val/h))-minIndex
return valIndex
def indexToEdge(dim,lEdge,h,minVal=0.0,maxVal=1.0):
rEdge = deepcopy(lEdge)
for i in range(0,len(lEdge)):
for j in range(0,dim):
lEdge[i][j] = getFloatVal(lEdge[i][j],h)
rEdge[i][j] = lEdge[i][j]+h
return lEdge,rEdge
def getFloatVal(index,h,minVal=0.0,maxVal=1.0):
return float(index*h+minVal)
def initializeSpace(dim,numStrata,minVal=0.0,maxVal=1.0,debug='False'):
dimList = []
for i in range(0,dim):
dimList.append(numStrata)
h = float((maxVal-minVal)/(numStrata))
return h,getIndex(maxVal,h)
def buildList(dim,numStrata):
eligibleIndices = []
iList = []
for i in range(0,numStrata):
iList.append(i)
for i in range(0,dim):
eligibleIndices.append(iList)
return eligibleIndices
def getLimitedDraw(dim,eligibleIndices,history):
# set eligible indices for each dimension
indices = deepcopy(eligibleIndices)
for ent in history:
for i in range(0,dim):
indices[i] = [x for x in indices[i] if x != ent[i]]
point = []
for i in range(0,dim):
point.append(random.choice(indices[i]))
return point, indices
def getDraw(dim,eligibleIndices,maxIndex,history):
eIndex = deepcopy(eligibleIndices)
#print(len(eIndex[0]))
point = [0]*dim
draws = 0
rejected = 0
invalid = []
while True:
if history == []:
# Initial random draw
point = [np.random.randint(0,maxIndex) for x in point]
history.append(point)
#print(len(history))
break
elif history != []:
# Limited draw by limiting sample-able indices
point,eIndex = getLimitedDraw(dim,eIndex,history)
history.append(point)
#print(len(history))
break
'''
#iterate over each index, compare to history
for ent in history:
for i in range(0,dim):
if point[i] == ent[i]:
invalid.append("True")
else:
invalid.append("False")
if "True" in invalid:
draws += 1
rejected += 1
else:
draws += 1
history.append(point)
break
'''
return list(point),history,eIndex
def convertToRandomCDF(dim,history,h):
#Get bin left edge
leftEdge = deepcopy(history)
rightEdge = deepcopy(history)
#iterate over leftEdge, convert ints to floats
leftEdge, rightEdge = indexToEdge(dim,leftEdge,h)
randCDFVal = deepcopy(leftEdge)
for i in range(0,len(leftEdge)):
for j in range(0,dim):
randCDFVal[i][j] = float(np.random.uniform(leftEdge[i][j],0.999999*rightEdge[i][j],1))
return randCDFVal
def CDFtoNorm(CDF):
dim = len(CDF[0])
CDF = deepcopy(CDF)
for i in range(0,len(CDF)):
for j in range(0,dim):
CDF[i][j] = st.norm.ppf(CDF[i][j])
return CDF
def wtf(data,filename):
f = open(filename,'w')
for ent in data:
for thing in ent:
f.write(str(thing)+',')
f.write('\n')
f.close()
def sample(dim,numSamples,ratio):
numStrata = numSamples
# Try getting different random points in the array space
# to satisfy nearest-neighbor constraint
# In 3 tries, get another LH sample and go again
sampleNum = 0
while True:
history = []
h,maxIndex = initializeSpace(dim,numStrata)
eIndices = buildList(dim,numStrata)
# Maximum radius within a single cell
minRadius = h*np.sqrt(dim)
for i in range(0,numSamples):
point,history,eIndices = getDraw(dim,eIndices,maxIndex,history)
sampleNum += 1
print("Sampling Latin-Hypercube array space (%s)" %(sampleNum))
tries = 0
while True:
randUniform = convertToRandomCDF(dim,history,h)
randStandardNorm = CDFtoNorm(randUniform)
tries += 1
wtf(randUniform,'utemp.csv')
wtf(randStandardNorm,'ntemp.csv')
cols = range(0,dim)
randUniform = np.genfromtxt('utemp.csv',delimiter=',',usecols=cols)
randStandardNorm = np.genfromtxt('ntemp.csv',delimiter=',',usecols=cols)
uninnd = nnd(randUniform)
normnnd = nnd(randStandardNorm)
sampleMin = np.nanmin(uninnd)
sampleMinNorm = np.nanmin(normnnd)
print(sampleMin,sampleMinNorm,ratio*minRadius)
if tries == 3:
break
if sampleMin > ratio*minRadius and sampleMinNorm > ratio*minRadius:
break
if sampleMin > ratio*minRadius and sampleMinNorm > ratio*minRadius:
break
os.remove(os.getcwd()+'/utemp.csv')
os.remove(os.getcwd()+'/ntemp.csv')
return randUniform,randStandardNorm
def nnd(a):
# For each sample, get the nearest neighbor w.r.t. each variable
b = np.zeros((a.shape[0],a.shape[0]),dtype=float)
for i in range(0,b.shape[0]):
for j in range(0,b.shape[0]):
b[i,j] = radius(a[j,:] - a[i,:])
if i == j:
b[i,j] = 10e3
return b
def radius(v):
a = np.nansum(v*v)
return np.sqrt(a)
def help():
# Displays help in terminal
print("\n\tPython script for generating Latin Hypercube samples")
print("\nUsage:\n\t\tpython lhs.py dimensions samples cellRatio\n")
print("\tdimensions: number of dimensions in hypercube")
print("\tsamples: number of samples per dimension")
print("\tcellRatio: ratio of the cell maximum radius for")
print("\t nearest-neighbor limit (default=1.0)")
print("\t Higher ratios force more a space-filling")
print("\t sample, higher chance of no solution")
print("\n\tOutputs: Uniform.csv, StandardNormal.csv")
print("\n\tConvert uniform values to variable values")
print("\t\tvar = range*value + min")
print("\n\tConvert standard normal values to variable values")
print("\t\tvar = stdev*value + mean")
sys.exit("\nHelp called, exiting...")
if __name__ == "__main__":
try:
if str(sys.argv[1]) == '-help' or str(sys.argv[1]) == '-h':
help()
except IndexError:
help()
try:
dim = int(sys.argv[1])
except:
dim = 2
try:
numSamples = int(sys.argv[2])
except:
numSamples = 10
try:
minRatio = float(sys.argv[3])
except:
minRatio = 1.0
# Get standard normal and standard uniform samples
uni,std = sample(dim,numSamples,minRatio)
np.savetxt('StandardUniform.csv',uni,delimiter=',')
np.savetxt('StandardNormal.csv',std,delimiter=',')
if dim == 2:
h = 1.0/numSamples
for i in range(1,numSamples):
plt.plot((0,1),(i*h,i*h),'k--',linewidth=1)
plt.plot((i*h,i*h),(0,1),'k--',linewidth=1)
plt.plot(uni[:,0],uni[:,1],'ro')
plt.axes().set_aspect('equal')
plt.xlabel('X',fontsize=18)
plt.ylabel('Y',fontsize=18)
plt.xlim(0,1)
plt.ylim(0,1)
plt.savefig("LHS_2D_example")
plt.clf()