-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment.py
152 lines (112 loc) · 4.79 KB
/
experiment.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
# we need change this so that constructor path is what tells the engine
# where it needs to go to get the constructor for the sim configs
from apparatus.simple_constructor import sim_configs_constructor
from apparatus.evaluator import evaluate_sim
from simulation import Simulation
# import runpy
import pandas as pd
class Experiment:
def __init__(self, exp_config = {"Name": "Name This Experiment", "Description":"Explain the purpose of this Experiment" ,"Constructor":"path/to/constructor", 'Trials':1 }):
self.config = exp_config
# use the iterator to append unique simulation configs to sim_configs
# self.sim_configs = [] #sim_config objects
self.simcount = 0
self.configcount = 0
self.simulations = []
# use the generate to append unique Simulation objects to simulations
# for sim_config in self.sim_configs:
# simId = self.size
# sim = Simulation(sim_config=sim_config)
# self.simulations.append({"Simulation Id": simId, "Simulation":sim, "Complete":False })
# self.size +=1
def set_config(self, exp_config):
self.config = exp_config
def construct_sim_configs(self):
# Placeholder/Reminder that we'll need
# to get the "dynamics" characterized by a
# runstep function or equivalent
# built from our PSUBs
#runpy.run_path(self.config["Constructor"])
sim_configs = sim_configs_constructor()
#running the iterator script must result in a list of
#valid sim_configs
return sim_configs
def generate(self):
sim_configs = self.construct_sim_configs()
monte_carlo_number = self.config['Trials']
for sim_config in sim_configs:
for n in range(monte_carlo_number):
#simId = self.simcount
#print(simId)
sim = Simulation(sim_config=sim_config)
self.simulations.append({"Simulation Id": self.simcount, "Config Id":self.configcount, "Simulation":sim, "Trial":n,"Complete":False })
self.simcount +=1
self.configcount +=1
def execute(self):
for simRecord in self.simulations:
if not(simRecord["Complete"]):
sim = simRecord["Simulation"]
sim.run()
simRecord["Complete"] = True
def evaluate(self):
for simRecord in self.simulations:
if simRecord["Complete"]:
sim = simRecord['Simulation']
evals = evaluate_sim(sim)
print("evals")
print(evals)
print("")
simRecord["Evaluations"] = {}
for k in evals.keys():
print(k)
simRecord["Evaluations"][k] = evals[k]
def get_records(self, as_df=True):
#Export!
records = []
for simRecord in self.simulations:
if simRecord["Complete"]:
records.append(simRecord)
if as_df:
return pd.DataFrame(records)
else:
return records
def export_results_df(self, dropIntermediates=True):
dataframes = []
for simRecord in self.simulations:
if simRecord["Complete"]:
dataframe = simRecord['Simulation'].results2df(dropIntermediates=dropIntermediates)
dataframe['Simulation Id'] = simRecord['Simulation Id']
dataframes.append(dataframe)
return pd.concat(dataframes, axis=0)
# def export_evals_df(self):
# evals = []
# for simRecord in self.simulations:
# if simRecord["Complete"]:
# #sim = simRecord['Simulation']
# eval = {}
# eval['Simulation Id'] = simRecord['Simulation Id']
# for k in simRecord["Evaluations"].keys():
# simRecord["Evaluations"][k] = evals[k]
# evals.append(eval)
# #print(evals)
# return pd.DataFrame(evals)
# def export_configs_df(self):
# configs = []
# for simRecord in self.simulations:
# if simRecord["Complete"]:
# simId = simRecord['Simulation Id']
# config = simRecord['Simulation'].config
# config['Simulation Id'] = simId
# #print(simRecord['Simulation Id'])
# configs.append(config)
# #print(configs)
# return pd.DataFrame(configs)
def run(self):
self.generate()
self.execute()
try:
self.config["Evaluator"]
self.evaluate()
except:
print(self.config)
return self.get_records(as_df=False)