1+ """
2+ This tutorial shows how to run one iteration of Bayesian calibration for a linear regression model.
3+ """
4+ import os
5+ from math import floor , log
6+ from grainlearning import BayesianCalibration
7+ from grainlearning .dynamic_systems import IODynamicSystem , DynamicSystem
8+
9+ PATH = os .path .abspath (os .path .dirname (__file__ ))
10+ executable = f'python { PATH } /linear_model.py'
11+
12+
13+ def run_sim (calib ):
14+ """
15+ Run the external executable and passes the parameter sample to generate the output file.
16+ """
17+ system = calib .system
18+ # keep the naming convention consistent between iterations
19+ mag = floor (log (system .num_samples , 10 )) + 1
20+ # check the software name and version
21+ print ("*** Running external software... ***\n " )
22+ # loop over and pass parameter samples to the executable
23+ for i , params in enumerate (system .param_data ):
24+ description = 'Iter' + str (system .curr_iter ) + '_Sample' + str (i ).zfill (mag )
25+ linear (executable , params , system .sim_name , description )
26+
27+
28+ def linear (executable , params , sim_name , description ):
29+ print (" " .join ([executable , "%.8e %.8e" % tuple (params ), sim_name , description ]))
30+ os .system (' ' .join ([executable , "%.8e %.8e" % tuple (params ), sim_name , description ]))
31+
32+
33+ calibration = BayesianCalibration .from_dict (
34+ {
35+ "num_iter" : 3 ,
36+ "callback" : run_sim ,
37+ "system" : {
38+ "system_type" : IODynamicSystem ,
39+ "param_min" : [0.001 , 0.001 ],
40+ "param_max" : [1 , 10 ],
41+ "param_names" : ['a' , 'b' ],
42+ "num_samples" : 20 ,
43+ "obs_data_file" : PATH + '/linear_obs.dat' ,
44+ "obs_names" : ['f' ],
45+ "ctrl_name" : 'u' ,
46+ "sim_name" : 'linear' ,
47+ "sim_data_dir" : PATH + '/sim_data/' ,
48+ "sim_data_file_ext" : '.txt' ,
49+ "sigma_tol" : 0.01 ,
50+ },
51+ "calibration" : {
52+ "inference" : {"ess_target" : 0.3 },
53+ "sampling" : {
54+ "max_num_components" : 1 ,
55+ "covariance_type" : "full" ,
56+ },
57+ },
58+ "save_fig" : - 1 ,
59+ }
60+ )
61+
62+ # Run one iteration of Bayesian calibration
63+ calibration .run ()
64+
65+ # Continue Bayesian calibration from a previous iteration until the total number of iterations or tolerance is reached
66+ curr_iter = 0
67+ calibration_continued = BayesianCalibration .from_dict (
68+ {
69+ "curr_iter" : curr_iter ,
70+ "num_iter" : 3 ,
71+ "callback" : run_sim ,
72+ "system" : {
73+ "system_type" : IODynamicSystem ,
74+ "param_min" : [0.001 , 0.001 ],
75+ "param_max" : [1 , 10 ],
76+ "obs_data_file" : PATH + '/linear_obs.dat' ,
77+ "obs_names" : ['f' ],
78+ "ctrl_name" : 'u' ,
79+ "sim_name" : 'linear' ,
80+ "sim_data_dir" : PATH + '/sim_data/' ,
81+ "sim_data_file_ext" : ".txt" ,
82+ "param_names" : ['a' , 'b' ],
83+ },
84+ "calibration" : {
85+ "inference" : {"ess_target" : 0.3 },
86+ "sampling" : {
87+ "max_num_components" : 1 ,
88+ "covariance_type" : "full" ,
89+ },
90+ },
91+ "save_fig" : 0 ,
92+ }
93+ )
94+
95+ calibration_continued .load_and_run_one_iteration ()
96+ calibration_continued .run ()
0 commit comments