def start_calibration(custom_crop, server, preprocessed_data=None): #read params to be calibrated params = [] with open('calibrator/calibratethese.csv') as paramscsv: dialect = csv.Sniffer().sniff(paramscsv.read(), delimiters=';,\t') paramscsv.seek(0) reader = csv.reader(paramscsv, dialect) next(reader, None) # skip the header for row in reader: p = {} p["name"] = row[0] p["array"] = row[1] p["low"] = float(row[2]) p["high"] = float(row[3]) p["stepsize"] = float(row[4]) p["optguess"] = float(row[5]) p["minbound"] = float(row[6]) p["maxbound"] = float(row[7]) if len(row) == 9 and row[8] != "": p["derive_function"] = make_lambda(row[8]) params.append(p) spot_setup = spotpy_setup_MONICA.spot_setup(params, custom_crop, server, preprocessed_data) rep = 100 sampler = spotpy.algorithms.sceua(spot_setup, dbname='SCEUA', dbformat='ram') #sampler.sample(rep, ngs=len(params)+1, kstop=2*len(params)+1, peps=0.0001, pcento=0.00001) sampler.sample(rep, ngs=len(params) + 1, kstop=2) best_params = sampler.status.params with open('optimizedparams.csv', 'wb') as outcsvfile: writer = csv.writer(outcsvfile) for i in range(len(best_params)): outrow = [] arr_pos = "" if params[i]["array"].upper() != "FALSE": arr_pos = params[i]["array"] outrow.append(params[i]["name"] + arr_pos) outrow.append(best_params[i]) writer.writerow(outrow) if len(params) > len(best_params): reminder = [] reminder.append( "Don't forget to calculate and set derived params!") writer.writerow(reminder) text = 'optimized parameters saved!' print(text)
next(reader, None) # skip the header for row in reader: p = {} p["name"] = row[0] p["array"] = row[1] p["low"] = row[2] p["high"] = row[3] p["stepsize"] = row[4] p["optguess"] = row[5] p["minbound"] = row[6] p["maxbound"] = row[7] if len(row) == 9 and row[8] != "": p["derive_function"] = make_lambda(row[8]) params.append(p) spot_setup = spotpy_setup_MONICA.spot_setup(params, exp_maps, obslist) rep = 10 results = [] sampler = spotpy.algorithms.sceua(spot_setup, dbname='SCEUA', dbformat='ram') sampler.sample(rep, ngs=3, kstop=10) #sampler.sample(rep, ngs=3, kstop=50, pcento=0.01, peps=0.05) results.append(sampler.getdata()) best = sampler.status.params with open('optimizedparams.csv', 'wb') as outcsvfile: writer = csv.writer(outcsvfile) for i in range(len(best)): outrow = [] arr_pos = ""
"runexps": [1] # 1: Askov, 3: Grignon, 4: Kursk, 5: Rothamsted, 6: Ultuna, 7: Versailles, None: all }, "muencheberg": { "map_file": "crop_sim_site_MAP_mue.csv", "observations": "observations_mue.csv", "params": "calibratethese.csv", "runexps": None #[13,25] #None: all } } run = "muencheberg" exp_maps = helper.read_exp_map(filename=datasets[run]["map_file"], exps=datasets[run]["runexps"]) observations = helper.read_obs(filename=datasets[run]["observations"], exps=datasets[run]["runexps"]) params = helper.read_params(datasets[run]["params"]) spot_setup = spotpy_setup_MONICA.spot_setup(params, exp_maps, observations, config, distrib="normal") sampler = spotpy.algorithms.demcz(spot_setup, dbname='DEMCz', dbformat='csv') sampler.sample(1000) best = sampler.status.params with open('optimizedparams.csv', 'wb') as outcsvfile: writer = csv.writer(outcsvfile) for i in range(len(best)): outrow=[] outrow.append(params[i]["name"]) outrow.append(best[i]) writer.writerow(outrow) text='optimized parameters saved!' print(text)
] #customize environment: for soil_p in soil_params: set_param(env["params"]["userSoilOrganicParameters"]["DEFAULT"], soil_p["name"], soil_p["value"]) for cm in env["cropRotations"][0]["cropRotation"]: for ws in cm["worksteps"]: if ws["type"] == "OrganicFertilization": for fert_p in fert_params: set_param(ws["parameters"], fert_p["name"], fert_p["value"]) #with open("dumped_" + env_file, "wb") as _: # _.write(json.dumps(env, indent=4)) spot_setup = spotpy_setup_MONICA.spot_setup(params, obslist, config, env) results = [] if config["cal-method"] == "SCE-UA": sampler = spotpy.algorithms.sceua(spot_setup, dbname='SCEUA', dbformat='ram') sampler.sample(repetitions=config["rep"], ngs=len(params) + 1, kstop=10, pcento=10, peps=10) elif config["cal-method"] == "MLE": sampler = spotpy.algorithms.mle(spot_setup, dbname='MLE_CMF', dbformat='ram')