def main(dirname): """ dirname gives the location of the experiment input files""" # Semibatch Vars to estimate in parmest theta_names = ['k1', 'k2', 'E1', 'E2'] # Semibatch data: list of dictionaries data = [] for exp_num in range(10): fname = os.path.join(dirname, 'exp' + str(exp_num + 1) + '.out') with open(fname, 'r') as infile: d = json.load(infile) data.append(d) pest = parmest.Estimator(generate_model, data, theta_names) scenmaker = sc.ScenarioCreator(pest, "ipopt") ofile = "delme_exp.csv" print("Make one scenario per experiment and write to {}".format(ofile)) experimentscens = sc.ScenarioSet("Experiments") scenmaker.ScenariosFromExperiments(experimentscens) ###experimentscens.write_csv(ofile) numtomake = 3 print("\nUse the bootstrap to make {} scenarios and print.".format( numtomake)) bootscens = sc.ScenarioSet("Bootstrap") scenmaker.ScenariosFromBoostrap(bootscens, numtomake) for s in bootscens.ScensIterator(): print("{}, {}".format(s.name, s.probability)) for n, v in s.ThetaVals.items(): print(" {}={}".format(n, v))
def test_semibatch_bootstrap(self): scenmaker = sc.ScenarioCreator(self.pest, "ipopt") bootscens = sc.ScenarioSet("Bootstrap") numtomake = 2 scenmaker.ScenariosFromBoostrap(bootscens, numtomake, seed=1134) tval = bootscens.ScenarioNumber(0).ThetaVals["k1"] self.assertAlmostEqual(tval, 20.64, places=1)
def test_no_csv_if_empty(self): # low level test of scenario sets # verify that nothing is written, but no errors with empty set emptyset = sc.ScenarioSet("empty") tfile = uuid.uuid4().hex+".csv" emptyset.write_csv(tfile) self.assertFalse(os.path.exists(tfile), "ScenarioSet wrote csv in spite of empty set")
def test_scen_from_exps(self): scenmaker = sc.ScenarioCreator(self.pest, "ipopt") experimentscens = sc.ScenarioSet("Experiments") scenmaker.ScenariosFromExperiments(experimentscens) experimentscens.write_csv("delme_exp_csv.csv") df = pd.read_csv("delme_exp_csv.csv") os.remove("delme_exp_csv.csv") # March '20: all reactor_design experiments have the same theta values! k1val = df.loc[5].at["k1"] self.assertAlmostEqual(k1val, 5.0/6.0, places=2) tval = experimentscens.ScenarioNumber(0).ThetaVals["k1"] self.assertAlmostEqual(tval, 5.0/6.0, places=2)