Prev 
Next 
Index-> 
contents 
reference 
index 
search 
external 
 
 
Up-> 
dismod_at 
user_example 
user_continue_fit.py 
 
 
 
 
user_continue_fit.py 
 
This is dismod_at-20221105 documentation:  Here is a link to its
current documentation Continuing a Fit From Where it Left Off Option Table 
Source Code 
Option Table max_num_iter_fixed  = 5 warn_on_stderr  = false print_level_fixed  = 5 run this example Source Code 
# values used to simulate data =  0.01 
chi_true                  =  0.1 
n_data                    =  51 
# ------------------------------------------------------------------------ import import import =  'example/user/continue_fit.py' 
if . argv[ 0 ]  !=  test_program  or len ( sys. argv)  !=  1  : 
   usage  =  'python3 '  +  test_program +  '\n' 
   usage +=  'where python3 is the python 3 program on your system\n' 
   usage +=  'and working directory is the dismod_at distribution directory\n' 
   sys. exit ( usage) 
print ( test_program) 
# # import dismod_at =  os. getcwd ()  +  '/python' 
if (  os. path. isdir (  local_dir +  '/dismod_at'  )  )  : 
   sys. path. insert ( 0 ,  local_dir) 
import # # change into the build/example/user directory if not . path. exists ( 'build/example/user' )  : 
   os. makedirs ( 'build/example/user' ) 
os. chdir ( 'build/example/user' ) 
# ------------------------------------------------------------------------ # Note that the a, t values are not used for this example def example_db ( file_name)  : 
   # note that the a, t values are not used for this case def fun_iota ( a,  t)  : 
      return ( 'prior_iota' ,  None,  None) 
   def fun_chi ( a,  t)  : 
      return ( chi_true,  None,  None) 
   # ---------------------------------------------------------------------- # age table: =  [  0.0 ,  5.0 ,  15.0 ,  35.0 ,  50.0 ,  75.0 ,  90.0 ,  100.0  ] 
   # # time table: =  [  1990.0 ,  2000.0 ,  2010.0 ,  2200.0  ] 
   # # integrand table: =  [ 
       {  'name' : 'prevalence'  } 
   ] 
   # # node table: =  [  {  'name' : 'world' ,  'parent' : ''  }  ] 
   # # weight table: =  list () 
   # # covariate table: =  list () 
   # # mulcov table: =  list () 
   # # avgint table: empty =  list () 
   # # nslist_table: =  dict () 
   # ---------------------------------------------------------------------- # data table: =  list () 
   # values that are the same for all data rows =  { 
      'meas_value' :   0.0 ,              # not used (will be simulated) 'density' :      'gaussian' , 
      'weight' :       '' , 
      'hold_out' :      False, 
      'time_lower' :    2000 ., 
      'time_upper' :    2000 ., 
      'subgroup' :      'world' , 
   } 
   # values that change between rows: for in range (  n_data )  : 
      fraction =  data_id /  float ( n_data- 1 ) 
      age      =  age_list[ 0 ]  +  ( age_list[- 1 ]  -  age_list[ 0 ])* fraction
      row[ 'age_lower' ]  =  age
      row[ 'age_upper' ]  =  age
      row[ 'node' ]       =  'world' 
      row[ 'integrand' ]  =  'prevalence' 
      row[ 'meas_std' ]   =  0.01 
      data_table. append (  copy. copy ( row)  ) 
   # # ---------------------------------------------------------------------- # prior_table =  [ 
      {  # prior_iota 'name' :      'prior_iota' , 
         'density' :   'uniform' , 
         'lower' :     iota_true /  10 ., 
         'upper' :     iota_true *  10 ., 
         'mean' :      iota_true *  2.0 , 
      } 
   ] 
   # ---------------------------------------------------------------------- # smooth table =  'smooth_iota' 
   fun            =  fun_iota
   age_id         =  int (  len (  age_list )  /  2  ) 
   time_id        =  int (  len (  time_list )  /  2  ) 
   smooth_table =  [ 
      {   'name' : name, 
         'age_id' :[ age_id], 
         'time_id' :[ time_id], 
         'fun' : fun
      } 
   ] 
   name           =  'smooth_chi' 
   fun            =  fun_chi
   age_id         =  int (  len (  age_list )  /  2  ) 
   time_id        =  int (  len (  time_list )  /  2  ) 
   smooth_table . append ( 
      {   'name' : name, 
         'age_id' :[ age_id], 
         'time_id' :[ time_id], 
         'fun' : fun
      } 
   ) 
   # ---------------------------------------------------------------------- # rate table: =  [ 
      {   'name' :           'iota' , 
         'parent_smooth' :  'smooth_iota' , 
      },{   'name' :           'chi' , 
         'parent_smooth' :  'smooth_chi' , 
      } 
   ] 
   # ---------------------------------------------------------------------- # option_table =  [ 
      {  'name' : 'rate_case' ,               'value' : 'iota_pos_rho_zero'    }, 
      {  'name' : 'parent_node_name' ,        'value' : 'world'                }, 
      {  'name' : 'ode_step_size' ,           'value' : '10.0'                 }, 
      {  'name' : 'random_seed' ,             'value' : '0'                    }, 
      {  'name' : 'warn_on_stderr' ,          'value' : 'false'                }, 
      {  'name' : 'quasi_fixed' ,             'value' : 'true'                 }, 
      {  'name' : 'max_num_iter_fixed' ,      'value' : '5'                    }, 
      {  'name' : 'print_level_fixed' ,       'value' : '0'                    }, 
      {  'name' : 'tolerance_fixed' ,         'value' : '1e-7'                 }, 
      {  'name' : 'max_num_iter_random' ,     'value' : '50'                   }, 
      {  'name' : 'print_level_random' ,      'value' : '0'                    }, 
      {  'name' : 'tolerance_random' ,        'value' : '1e-10'                } 
   ] 
   # ---------------------------------------------------------------------- # subgroup_table =  [  {  'subgroup' : 'world' ,  'group' : 'world'  }  ] 
   # ---------------------------------------------------------------------- # create database . create_database ( 
      file_name, 
      age_list, 
      time_list, 
      integrand_table, 
      node_table, 
      subgroup_table, 
      weight_table, 
      covariate_table, 
      avgint_table, 
      data_table, 
      prior_table, 
      smooth_table, 
      nslist_table, 
      rate_table, 
      mulcov_table, 
      option_table
   ) 
   # ---------------------------------------------------------------------- return # =========================================================================== # Run the init command to create the var table =  'example.db' 
example_db ( file_name) 
# =  '../../devel/dismod_at' 
dismod_at. system_command_prc ([  program,  file_name,  'init'  ]) 
# ----------------------------------------------------------------------- # read database =  False
connection    =  dismod_at. create_connection ( file_name,  new) 
var_table     =  dismod_at. get_table_dict ( connection,  'var' ) 
rate_table    =  dismod_at. get_table_dict ( connection,  'rate' ) 
# ----------------------------------------------------------------------- # truth table: =  'truth_var' 
col_name     =  [  'truth_var_value'  ] 
col_type     =  [  'real'  ] 
row_list     =  list () 
var_id2true  =  list () 
for in range (  len ( var_table)  )  : 
   var_info        =  var_table[ var_id] 
   truth_var_value =  None
   var_type =  var_info[ 'var_type' ] 
   assert ==  'rate' 
   rate_id   =  var_info[ 'rate_id' ] 
   rate_name =  rate_table[ rate_id][ 'rate_name' ] 
   if ==  'iota'  : 
      value =  iota_true
   elif ==  'chi'  : 
      value =  chi_true
   else : 
      assert . append (  [  value ]  ) 
dismod_at. create_table ( connection,  tbl_name,  col_name,  col_type,  row_list) 
connection. close () 
# ----------------------------------------------------------------------- # Simulate one data set, start at prior mean fit, start at fit results, fit . system_command_prc ([  program,  file_name,  'simulate' ,  '1'  ]) 
dismod_at. system_command_prc ([  program,  file_name,  'fit' ,  'both' ,  '0'  ]) 
dismod_at. system_command_prc ( 
   [  program,  file_name,  'set' ,  'start_var' ,  'fit_var'  ] 
) 
dismod_at. system_command_prc ([  program,  file_name,  'fit' ,  'both' ,  '0'  ]) 
# ----------------------------------------------------------------------- # check fit results =  False
connection    =  dismod_at. create_connection ( file_name,  new) 
fit_var_table =  dismod_at. get_table_dict ( connection,  'fit_var' ) 
log_table     =  dismod_at. get_table_dict ( connection,  'log'  ) 
connection. close () 
# # check that we got one warning =  0 
for in : 
   if [ 'message_type' ]  ==  'warning'  : 
      warning_count +=  1 
assert in [  1 ,  2 ] 
# =  0.0 
for in range (  len ( var_table)  )  : 
   fit_value =  fit_var_table[ var_id][ 'fit_var_value' ] 
   var_row   =  var_table[ var_id] 
   rate_id   =  var_row[ 'rate_id' ] 
   rate_name =  rate_table[ rate_id][ 'rate_name' ] 
   if ==  'iota'  : 
      ok =  abs (  fit_value /  iota_true -  1.0  )  <  . 05 
      if not : 
         print (  "iota relative error = " ,  fit_value /  iota_true -  1.0 ) 
         assert abs (  fit_value /  iota_true -  1.0  )  <  . 05 
   else : 
      assert ==  chi_true
# ----------------------------------------------------------------------------- print ( 'continue_fit.py: OK' ) 
# -----------------------------------------------------------------------------