-
Notifications
You must be signed in to change notification settings - Fork 2
/
latinHypercube.py
executable file
·92 lines (69 loc) · 3.04 KB
/
latinHypercube.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
from shared import *
import numpy,os,sys,csv,subprocess,time
def unitCube(numberOfNode):
"function to return the nodes contained within a unit latin hypercube with given numbers of nodes"
return samplePlan(3,numberOfNode)
def allUnitCubes():
"function to calculate all unit cube latin hypercubes"
for i in ALL_RESULTS:
unitCube(i)
def samplePlan(numberDimension,numberOfNode):
"function to load or compute latin hypercubes of given number of nodes and dimensions"
numberDimension = int(numberDimension)
numberOfNode = int(numberOfNode)
matlabUpdate = "update_data.m"
#if less than two nodes return None
if (numberOfNode < 2):
return None
def updateMatlab(numberDimension,numberOfNode):
"""function to run the required matlab script that updates all required data
assumes that the function to update matlab is stored in a folder called matlab of current directory
"""
#define matlab update file
fileContents = ""
fileContents+="output = bestlh({},{},1,1);\n".format(numberOfNode,numberDimension)
fileContents+="csvwrite('latin_hypercube_{}_{}.csv',output);".format(numberDimension,numberOfNode)
#write update file for matlab to call
updatePath = "matlab/update_data.m"
with open(updatePath,"w") as openFile:
openFile.write(fileContents)
#define command to call matlab in current working directory and run update file
currentDirectory = os.getcwd()
matlabCommand = 'cd {}, run {}, exit'.format(currentDirectory,updatePath)
#run command with no windows or spash screen
subprocess.check_call(['matlab', '-nosplash', '-noFigureWindows', '-r', matlabCommand])
def loadMatlab(filePath):
"function to load the data created by MATLAB"
#for all rows in the output file
nodes = []
with open(filePath,"r") as csvFile:
csvReader = csv.reader(csvFile,dialect="excel")
for row in csvReader:
nodes.append([float(item) for item in row if len(item)>0])
#return numpy array of node coordinates
return [numpy.array(node) for node in nodes]
filePath = "matlab/latin_hypercube_{}_{}.csv".format(numberDimension,numberOfNode)
try:
#attempt to open the required hypercube
open(filePath)
except IOError:
#if file not found update matlab and wait for a second
updateMatlab(numberDimension,numberOfNode)
time.sleep(1)
return loadMatlab(filePath)
def sampleSpace(lengths,numberOfNode):
"function to return a latin hypercube scaled to the required sample size and with the given number of points"
#define number of dimensions from length of lengths
dimensions = len(lengths)
#obtain the required number of nodes of given dimensions
nodes = samplePlan(dimensions,numberOfNode)
#create numpy array of lengths for easy dot product multiplication
lengths = numpy.array(lengths)
#preallocate memory for scaled nodes
scaledNodes = numpy.zeros([numberOfNode,dimensions])
#for all nodes populate scaled nodes
for i,node in enumerate(nodes):
scaledNodes[i] = node*lengths
return scaledNodes
if (__name__ == "__main__"):
allUnitCubes()