def mi(asdr, cv, csmr, population, covariate_data, Demographics, settings): m = MeasurementInputsFromSettings(settings=settings) m.asdr = deepcopy(asdr) m.csmr = deepcopy(csmr) m.data = deepcopy(cv) m.covariate_data = [deepcopy(covariate_data)] m.population = deepcopy(population) m.demographics = Demographics m.configure_inputs_for_dismod(settings=settings, mortality_year_reduction=1) return m
def test_location_drill_start_end(ihme): these_settings = deepcopy(BASE_CASE) model_settings = these_settings["model"] tree = LocationDAG(these_settings['location_set_version_id'], these_settings['gbd_round_id']) region_ids = tree.parent_children(1) parent_test_loc = choice(region_ids) test_children = list(tree.parent_children(parent_test_loc)) num_test_children = randint(2, len(test_children)) children_test_locs = sample(test_children, num_test_children) num_descendants = 0 for child in children_test_locs: num_descendants += len(tree.descendants(child)) model_settings['drill_location_end'] = children_test_locs model_settings['drill_location_start'] = parent_test_loc these_settings['model'] = model_settings s = load_settings(these_settings) mi = MeasurementInputsFromSettings(settings=s) # demographics.location_id shoul be set to all descendants of each # location in drill_location_end, plus drill_location_end locations # themselves, plus the drill_location_start location assert len(mi.demographics.location_id) == (num_descendants + len(children_test_locs) + 1) assert len(mi.demographics.drill_locations) == (len(children_test_locs) + 1)
def main(): """ Grabs the inputs for a specific model version ID, sets up the folder structure, and pickles the inputs object plus writes the settings json for use later on. If you're doing a drill, then only get input data from locations that will be used for the drilling for parent-children. """ args = get_args() logging.basicConfig(level=LEVELS[args.loglevel]) LOG.info( f"Configuring inputs for model version ID {args.model_version_id}.") LOG.debug(f"Arguments: {args}.") context = Context(model_version_id=args.model_version_id, make=args.make, configure_application=args.configure) parameter_json = settings_json_from_model_version_id( model_version_id=args.model_version_id, conn_def=context.model_connection) settings = load_settings(settings_json=parameter_json) inputs = MeasurementInputsFromSettings(settings=settings) inputs.get_raw_inputs() inputs.configure_inputs_for_dismod(settings=settings) context.write_inputs(inputs=inputs, settings=parameter_json)
def configure_inputs(model_version_id: int, make: bool, configure: bool, midpoint: bool = False, test_dir: Optional[str] = None, json_file: Optional[str] = None) -> None: """ Grabs the inputs for a specific model version ID, sets up the folder structure, and pickles the inputs object plus writes the settings json for use later on. Also uploads CSMR to the database attached to the model version, if applicable. Optionally use a json file for settings instead of a model version ID's json file. Parameters ---------- model_version_id The model version ID to configure inputs for make Whether or not to make the directory structure for the model version ID configure Configure the application for the IHME cluster, otherwise will use the test_dir for the directory tree instead. test_dir A test directory to use rather than the directory specified by the model version context in the IHME file system. json_file An optional filepath pointing to a different json than is attached to the model_version_id. Will use this instead for settings. """ LOG.info(f"Configuring inputs for model version ID {model_version_id}.") context = Context(model_version_id=model_version_id, make=make, configure_application=configure, root_directory=test_dir) if json_file: LOG.info(f"Reading settings from file: {json_file}") with open(json_file, 'r') as json_file: parameter_json = json.load(json_file) else: parameter_json = settings_json_from_model_version_id( model_version_id=model_version_id, conn_def=context.model_connection) settings = load_settings(settings_json=parameter_json) inputs = MeasurementInputsFromSettings(settings=settings) inputs.get_raw_inputs() inputs.configure_inputs_for_dismod(settings=settings, midpoint=midpoint) if not inputs.csmr.raw.empty: LOG.info("Uploading CSMR to t3 table.") inputs.csmr.attach_to_model_version_in_db( model_version_id=model_version_id, conn_def=context.model_connection) context.write_inputs(inputs=inputs, settings=parameter_json)
def test_no_drill(ihme): these_settings = deepcopy(BASE_CASE) model_settings = these_settings["model"] tree = LocationDAG(these_settings['location_set_version_id'], these_settings['gbd_round_id']) num_descendants = len(tree.descendants(1)) model_settings.pop('drill_location_end') model_settings.pop('drill_location_start') these_settings['model'] = model_settings s = load_settings(these_settings) mi = MeasurementInputsFromSettings(settings=s) # since we haven't set either drill_location_start or # drill_location_end, demographics.location_id should be set # to the entire hierarchy assert len(mi.demographics.location_id) == num_descendants + 1 assert len(mi.demographics.drill_locations) == num_descendants + 1
def test_location_drill_start_only(ihme): these_settings = deepcopy(BASE_CASE) model_settings = these_settings["model"] tree = LocationDAG(these_settings['location_set_version_id'], these_settings['gbd_round_id']) region_ids = tree.parent_children(1) test_loc = choice(region_ids) num_descendants = len(tree.descendants(test_loc)) num_mr_locs = len(tree.parent_children(test_loc)) model_settings.pop("drill_location_end") model_settings['drill_location_start'] = test_loc these_settings["model"] = model_settings s = load_settings(these_settings) mi = MeasurementInputsFromSettings(settings=s) # with drill_location_end unset, demographics.location_id should # be set to all descendants of the test loc, plus the test loc itself assert len(mi.demographics.location_id) == num_descendants + 1 assert len(mi.demographics.drill_locations) == num_mr_locs