-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimbatch.py
More file actions
executable file
·136 lines (116 loc) · 3.96 KB
/
simbatch.py
File metadata and controls
executable file
·136 lines (116 loc) · 3.96 KB
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
#!/usr/bin/env python3
import sys
import subprocess
import getopt
working_dir=""
input_file=""
output_file=""
simulation_steps="200"
isforml=False
try:
opts, args = getopt.getopt(sys.argv[1:], "w:i:o:s:m", ["working_dir=","input_file=","output_file=", "simulation_steps=","ml"])
except getopt.GetoptError:
sys.exit(2)
for o, a in opts:
if o in ("-w", "--working_dir"):
working_dir = a
elif o in ("-i", "--input_file"):
input_file = a
elif o in ("-o", "--output_file"):
output_file = a
elif o in ("-s", "--simulation_steps"):
simulation_steps = a
elif o in ("-m", "--ml"):
isforml = True
if working_dir == "":
working_dir="working_dir"
if input_file == "":
input_file="simbatch_input.csv"
if output_file == "":
output_file=working_dir+"/simbatch_output.csv"
last_step = str(int(simulation_steps) - 1)
# Load the BondMachine inputs
command="bondmachine -bondmachine-file "+working_dir+"/bondmachine.json -list-inputs"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
inputs={}
if p.returncode==0:
while True:
o = p.stdout.readline().decode()
if o == '' and p.poll() != None:
break
result=o.split()
if len(result)==2:
inputs[result[0]]=result[1]
# Load the BondMachine outputs
command="bondmachine -bondmachine-file "+working_dir+"/bondmachine.json -list-outputs"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
outputs={}
if p.returncode==0:
while True:
o = p.stdout.readline().decode()
if o == '' and p.poll() != None:
break
result=o.split()
if len(result)==2:
outputs[result[0]]=result[1]
# print (inputs)
# print (outputs)
# Open the input file
input_file_handle=open(input_file, "r")
output_file_handle=open(output_file, "w")
if isforml:
for i in range(len(outputs)):
output_file_handle.write("probability_"+str(i)+",")
output_file_handle.write("classification\n")
# Read every line of the input file
for line in input_file_handle:
line=line.strip()
inputs_values=line.split(",")
if len(inputs_values)==0:
continue
elif len(inputs_values)==len(inputs):
print ("Running simulation with inputs: "+line)
# Remove the simbox file
command="rm -f "+working_dir+"/simboxtemp.json"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
# Create the simbox file
for i in range(len(inputs_values)):
input_name=inputs[str(i)]
input_value=inputs_values[i]
command="simbox -simbox-file "+working_dir+"/simboxtemp.json -add \"absolute:0:set:"+input_name+":"+input_value+"\""
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
if p.returncode!=0:
print ("Error setting input "+input_name+" to "+input_value)
sys.exit(2)
for output_name in outputs:
command="simbox -simbox-file "+working_dir+"/simboxtemp.json -add \"absolute:"+last_step+":get:"+outputs[output_name]+":float32\""
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
if p.returncode!=0:
print ("Error getting output "+output_name)
sys.exit(2)
# Run the simulation
command="bondmachine -bondmachine-file "+working_dir+"/bondmachine.json -simbox-file "+working_dir+"/simboxtemp.json -sim -sim-interactions "+simulation_steps
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
p.wait()
if p.returncode!=0:
print ("Error running simulation")
sys.exit(2)
outline=p.stdout.read().decode()
if isforml:
import numpy as np
vals=np.asarray(outline.split(','))
index=np.argmax(vals)
output_file_handle.write(outline)
output_file_handle.write(str(index)+"\n")
else:
outline=outline.strip(',')
output_file_handle.write(outline+"\n")
else:
print("Error: The input file has an invalid number of columns")
input_file_handle.close()
output_file_handle.close()