|
| 1 | +# ActivitySim |
| 2 | +# See full license in LICENSE.txt. |
| 3 | + |
| 4 | +from __future__ import (absolute_import, division, print_function, ) |
| 5 | +from future.standard_library import install_aliases |
| 6 | +install_aliases() # noqa: E402 |
| 7 | + |
| 8 | +import logging |
| 9 | +import argparse |
| 10 | + |
| 11 | +from activitysim.core import inject |
| 12 | +from activitysim.core import tracing |
| 13 | +from activitysim.core import config |
| 14 | +from activitysim.core import pipeline |
| 15 | +from activitysim.core import mp_tasks |
| 16 | +from activitysim.core import chunk |
| 17 | + |
| 18 | +logger = logging.getLogger('activitysim') |
| 19 | + |
| 20 | + |
| 21 | +def cleanup_output_files(): |
| 22 | + |
| 23 | + active_log_files = \ |
| 24 | + [h.baseFilename for h in logger.root.handlers if isinstance( |
| 25 | + h, logging.FileHandler)] |
| 26 | + tracing.delete_output_files('log', ignore=active_log_files) |
| 27 | + |
| 28 | + tracing.delete_output_files('h5') |
| 29 | + tracing.delete_output_files('csv') |
| 30 | + tracing.delete_output_files('txt') |
| 31 | + tracing.delete_output_files('yaml') |
| 32 | + tracing.delete_output_files('prof') |
| 33 | + |
| 34 | + |
| 35 | +def run(run_list, injectables=None): |
| 36 | + |
| 37 | + # Create a new skims.omx file from BEAM (http://beam.lbl.gov/) skims |
| 38 | + # if skims do not already exist in the input data directory |
| 39 | + if config.setting('create_skims_from_beam'): |
| 40 | + pipeline.run(models=['create_skims_from_beam']) |
| 41 | + pipeline.close_pipeline() |
| 42 | + |
| 43 | + # Create persons, households, and land use .csv files from UrbanSim |
| 44 | + # data if these files do not already exist in the input data directory |
| 45 | + if config.setting('create_inputs_from_usim_data'): |
| 46 | + pipeline.run(models=['create_inputs_from_usim_data']) |
| 47 | + pipeline.close_pipeline() |
| 48 | + |
| 49 | + if run_list['multiprocess']: |
| 50 | + logger.info("run multiprocess simulation") |
| 51 | + mp_tasks.run_multiprocess(run_list, injectables) |
| 52 | + else: |
| 53 | + logger.info("run single process simulation") |
| 54 | + pipeline.run( |
| 55 | + models=run_list['models'], resume_after=run_list['resume_after']) |
| 56 | + pipeline.close_pipeline() |
| 57 | + chunk.log_write_hwm() |
| 58 | + |
| 59 | + |
| 60 | +def log_settings(injectables): |
| 61 | + |
| 62 | + settings = [ |
| 63 | + 'households_sample_size', |
| 64 | + 'chunk_size', |
| 65 | + 'multiprocess', |
| 66 | + 'num_processes', |
| 67 | + 'resume_after', |
| 68 | + ] |
| 69 | + |
| 70 | + for k in settings: |
| 71 | + logger.info("setting %s: %s" % (k, config.setting(k))) |
| 72 | + |
| 73 | + for k in injectables: |
| 74 | + logger.info("injectable %s: %s" % (k, inject.get_injectable(k))) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + |
| 79 | + parser = argparse.ArgumentParser() |
| 80 | + |
| 81 | + parser.add_argument( |
| 82 | + "-b", "--bucket_name", action="store", help="s3 bucket name") |
| 83 | + parser.add_argument( |
| 84 | + "-y", "--year", action="store", type=int, help="data year") |
| 85 | + parser.add_argument("-s", "--scenario", action="store", help="scenario") |
| 86 | + parser.add_argument( |
| 87 | + "-u", "--skims_url", action="store", help="url of skims .csv") |
| 88 | + parser.add_argument( |
| 89 | + "-x", "--path_to_remote_data", action="store", |
| 90 | + help="url of urbansim .h5 model data") |
| 91 | + parser.add_argument( |
| 92 | + "-w", "--write_to_s3", action="store_true", help="write output to s3?") |
| 93 | + |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + if args.skims_url: |
| 97 | + inject.add_injectable('beam_skims_url', args.skims_url) |
| 98 | + |
| 99 | + if args.bucket_name: |
| 100 | + inject.add_injectable('bucket_name', args.bucket_name) |
| 101 | + |
| 102 | + if args.scenario: |
| 103 | + inject.add_injectable('scenario', args.scenario) |
| 104 | + |
| 105 | + if args.year: |
| 106 | + inject.add_injectable('year', args.year) |
| 107 | + |
| 108 | + if args.path_to_remote_data: |
| 109 | + inject.add_injectable( |
| 110 | + 'remote_data_full_path', args.path_to_remote_data) |
| 111 | + |
| 112 | + if args.write_to_s3: |
| 113 | + inject.add_injectable('s3_output', True) |
| 114 | + |
| 115 | + inject.add_injectable('data_dir', 'data') |
| 116 | + inject.add_injectable('configs_dir', ['configs', 'configs/configs']) |
| 117 | + |
| 118 | + injectables = config.handle_standard_args(parser) |
| 119 | + |
| 120 | + config.filter_warnings() |
| 121 | + tracing.config_logger() |
| 122 | + |
| 123 | + log_settings(injectables) |
| 124 | + |
| 125 | + t0 = tracing.print_elapsed_time() |
| 126 | + |
| 127 | + # cleanup if not resuming |
| 128 | + if not config.setting('resume_after', False): |
| 129 | + cleanup_output_files() |
| 130 | + print(injectables) |
| 131 | + |
| 132 | + run_list = mp_tasks.get_run_list() |
| 133 | + |
| 134 | + if run_list['multiprocess']: |
| 135 | + |
| 136 | + # do this after config.handle_standard_args, |
| 137 | + # as command line args may override injectables |
| 138 | + injectables = list( |
| 139 | + set(injectables) | set(['data_dir', 'configs_dir', 'output_dir'])) |
| 140 | + injectables = {k: inject.get_injectable(k) for k in injectables} |
| 141 | + else: |
| 142 | + injectables = None |
| 143 | + |
| 144 | + run(run_list, injectables) |
| 145 | + |
| 146 | + # pipeline will be closed if multiprocessing |
| 147 | + # if you want access to tables, BE SURE TO OPEN |
| 148 | + # WITH '_' or all tables will be reinitialized |
| 149 | + # pipeline.open_pipeline('_') |
| 150 | + |
| 151 | + t0 = tracing.print_elapsed_time("everything", t0) |
0 commit comments