def init_model_settings(): """initialize Model Settings object (this controls settings that apply to all scenarios to be executed)""" model_settings = ModelSettings() #========================================================================================================== # add the config to model settings; set model starting time, and output directory based on run time #========================================================================================================== model_settings.set('start_year', START_YEAR) model_settings.set('model_path', MODEL_PATH) model_settings.set('local_cores', LOCAL_CORES) model_settings.set('model_init', utilfunc.get_epoch_time()) datetime = utilfunc.get_formatted_time() output_dir = datetime #str(input('Run name (default of formatted_time):')) or datetime model_settings.set('cdate', datetime) model_settings.set('out_dir', '%s/runs/results_%s' % (os.path.dirname(os.getcwd()), output_dir)) model_settings.set('git_hash', utilfunc.get_git_hash()) # --- check for scenarios listed in config --- input_scenarios = [s for s in glob.glob(os.path.join(os.pardir,'input_scenarios','*.xls*')) if not '~$' in s] if SCENARIOS == None: pass else: input_scenarios = [s for s in input_scenarios if s.split('/')[-1].split('.xls')[0] in SCENARIOS] model_settings.set('input_scenarios', input_scenarios) #========================================================================================================== # validate model settings and make the ouput directory #========================================================================================================== model_settings.validate() os.makedirs(model_settings.out_dir) return model_settings
def create_output_schema(pg_conn_string, role, suffix, scenario_list, source_schema='diffusion_template', include_data=False): """ Creates output schema that will be dropped into the database Parameters ---------- **pg_conn_string** : 'string' String to connect to pgAdmin database **role** : 'string' Owner of schema **suffix** : 'string' String to mark the time that model is kicked off. Added to end of schema to act as a unique indentifier **source_schema** : 'SQL schema' Schema to be used as template for the output schema **include_data** : 'bool' If True includes data from diffusion_shared schema. Default is False Returns ------- dest_schema : 'SQL schema' Output schema that will house the final results """ inputs = locals().copy() suffix = utilfunc.get_formatted_time() suffix_microsecond = datetime.now().strftime('%f') logger.info('Creating output schema based on {source_schema}'.format(**inputs)) con, cur = utilfunc.make_con(pg_conn_string, role="postgres") # check that the source schema exists sql = """SELECT count(*) FROM pg_catalog.pg_namespace WHERE nspname = '{source_schema}';""".format(**inputs) check = pd.read_sql(sql, con) if check['count'][0] != 1: msg = "Specified source_schema ({source_schema}) does not exist.".format(**inputs) raise ValueError(msg) scen_suffix = os.path.split(scenario_list[0])[1].split('_')[2].rstrip('.xlsm') dest_schema = 'diffusion_results_{}'.format(suffix+suffix_microsecond+'_'+scen_suffix) inputs['dest_schema'] = dest_schema sql = '''SELECT diffusion_shared.clone_schema('{source_schema}', '{dest_schema}', '{role}', {include_data});'''.format(**inputs) cur.execute(sql) con.commit() logger.info('\tOutput schema is: {}'.format(dest_schema)) return dest_schema
def init_model_settings(): # initialize Model Settings object (this controls settings that apply to # all scenarios to be executed) model_settings = ModelSettings() # add the config to model settings; set model starting time, output directory based on run time, etc. model_settings.add_config(config) model_settings.set('model_init', utilfunc.get_epoch_time()) model_settings.set('cdate', utilfunc.get_formatted_time()) model_settings.set('out_dir', datfunc.make_output_directory_path(model_settings.cdate)) model_settings.set('input_data_dir', '{}/input_data'.format(os.path.dirname(os.getcwd()))) model_settings.set('input_agent_dir', '{}/input_agents'.format(os.path.dirname(os.getcwd()))) model_settings.set('input_scenarios', datfunc.get_input_scenarios()) # validate all model settings model_settings.validate() return model_settings