def test_find_attributes_fails(self): config = {jobs.LOADCSVEXPOSURE: {'file_name': 'exposure.csv'}} with self.assertRaises(RuntimeError) as context: find_attributes(config, 'invalid_key') self.assertEqual( str(context.exception), 'Mandatory key not found in config file: invalid_key' )
def _wind_v4_reader(config: dict) -> list: """ From a wind configuration list build the job list. :param config_list: A list describing the simulation. :returns: A list of jobs to process over. """ job_insts = [] atts = find_attributes(config, LOADCSVEXPOSURE) add_job(job_insts, LOADCSVEXPOSURE, atts) file_list = find_attributes(config, [HAZARDRASTER, LOADWINDTCRM]) atts = { 'file_list': file_list, 'attribute_label': '0.2s gust at 10m height m/s' } add_job(job_insts, LOADRASTER, atts) vul_filename = os.path.join(misc.RESOURCE_DIR, find_attributes(config, VULNFILE)) add_job(job_insts, LOADXMLVULNERABILITY, {'file_name': vul_filename}) # The vulnerabilitySetID from the nrml file = 'domestic_flood_2012' # The column title in the exposure file = 'WIND_VULNERABILITY_FUNCTION_ID' vulnerability_set_id = find_attributes(config, VULNSET) atts = { 'vul_functions_in_exposure': { vulnerability_set_id: 'WIND_VULNERABILITY_FUNCTION_ID' } } add_job(job_insts, SIMPLELINKER, atts) atts = {'variability_method': {vulnerability_set_id: 'mean'}} add_job(job_insts, SELECTVULNFUNCTION, atts) add_job(job_insts, LOOKUP) atts_dict = find_attributes(config, CALCSTRUCTLOSS) if REP_VAL_NAME not in atts_dict: msg = '\nMandatory key not found in config file; %s\n' % REP_VAL_NAME raise RuntimeError(msg) attributes = { 'var1': 'structural', 'var2': atts_dict[REP_VAL_NAME], 'var_out': 'structural_loss' } add_job(job_insts, MDMULT, attributes) file_name = find_attributes(config, SAVE) add_job(job_insts, SAVEALL, {'file_name': file_name}) file_name = find_attributes(config, SAVE) base = os.path.splitext(file_name)[0] file_name = f"{base}.xml" add_job(job_insts, SAVEPROVENANCE, {'file_name': file_name}) return job_insts
def _earthquake_v1_reader(config: dict) -> list: """ Build a job list from earthquake configuration. :param config: A dict describing the simulation :returns: A list of jobs to process over """ LOGGER.info("Using earthquake_v1 template") job_insts = [] atts = find_attributes(config, LOADCSVEXPOSURE) add_job(job_insts, LOADCSVEXPOSURE, atts) atts = find_attributes(config, HAZARDRASTER) atts.setdefault('attribute_label', 'MMI') add_job(job_insts, LOADRASTER, atts) vul_filename = os.path.join(misc.RESOURCE_DIR, find_attributes(config, VULNFILE)) add_job(job_insts, LOADXMLVULNERABILITY, {'file_name': vul_filename}) # The column title in the exposure file = 'WIND_VULNERABILITY_FUNCTION_ID' vulnerability_set_id = find_attributes(config, VULNSET) atts = { 'vul_functions_in_exposure': { vulnerability_set_id: 'EQ_VULNERABILITY_FUNCTION_ID' } } add_job(job_insts, SIMPLELINKER, atts) atts = {'variability_method': {vulnerability_set_id: 'mean'}} add_job(job_insts, SELECTVULNFUNCTION, atts) if PERMUTATION in config: atts = find_attributes(config, PERMUTATION) add_job(job_insts, PERMUTATE_EXPOSURE, atts) else: add_job(job_insts, LOOKUP) if CALCSTRUCTLOSS in config: atts_dict = find_attributes(config, CALCSTRUCTLOSS) if REP_VAL_NAME not in atts_dict: msg = f"Mandatory key not found in config file; {REP_VAL_NAME}" raise RuntimeError(msg) attributes = { 'var1': 'structural', 'var2': atts_dict[REP_VAL_NAME], 'var_out': 'structural_loss' } add_job(job_insts, MDMULT, attributes) if AGGREGATION in config: attributes = find_attributes(config, AGGREGATION) add_job(job_insts, AGGREGATE_LOSS, attributes) file_name = find_attributes(config, SAVEAGG) add_job(job_insts, SAVEAGG, {'file_name': file_name}) if CATEGORISE in config: attributes = find_attributes(config, CATEGORISE) add_job(job_insts, CATEGORISE, attributes) file_name = find_attributes(config, SAVE) add_job(job_insts, SAVEALL, {'file_name': file_name}) if AGGREGATE in config: attributes = find_attributes(config, AGGREGATE) add_job(job_insts, AGGREGATE, attributes) if TABULATE in config: attributes = find_attributes(config, TABULATE) add_job(job_insts, TABULATE, attributes) # Eventually, this needs to be included in pipeline.Pipeline and # automatically added to the list of jobs file_name = find_attributes(config, SAVE) base = os.path.splitext(file_name)[0] file_name = f"{base}.xml" add_job(job_insts, SAVEPROVENANCE, {'file_name': file_name}) return job_insts
def test_find_attributes_by_lesser_preference(self): expected_attributes = {'file_name': 'exposure.csv'} config = {jobs.LOADCSVEXPOSURE: expected_attributes} attributes = find_attributes(config, [jobs.LOADRASTER, jobs.LOADCSVEXPOSURE]) self.assertEqual(attributes, expected_attributes)
def test_find_attributes_succeeds(self): expected_attributes = {'file_name': 'exposure.csv'} config = {jobs.LOADCSVEXPOSURE: expected_attributes} attributes = find_attributes(config, jobs.LOADCSVEXPOSURE) self.assertEqual(attributes, expected_attributes)
def _flood_fabric_v2_reader(config: dict) -> list: """ This function does two things; * From a flood fabric template v2 configuration dictionary build the job list. * Set up the attributes of the jobs and calc's specifically for a flood study. :param config_list: A list describing the simulation. :returns: A list of jobs to process over. """ job_insts = [] atts = find_attributes(config, LOADCSVEXPOSURE) add_job(job_insts, LOADCSVEXPOSURE, atts) file_list = find_attributes(config, HAZARDRASTER) atts = {'file_list': file_list, 'attribute_label': WATER_DEPTH} add_job(job_insts, LOADRASTER, atts) vul_filename = os.path.join(misc.RESOURCE_DIR, 'fabric_flood_avg_curve.xml') add_job(job_insts, LOADXMLVULNERABILITY, {'file_name': vul_filename}) floor_height_value = find_attributes(config, FLOOR_HEIGHT) atts = {'var': FLOOR_HEIGHT, 'value': floor_height_value} add_job(job_insts, CONSTANT, atts) add_job(job_insts, FLOOR_HEIGHT_CALC) # The vulnerabilitySetID from the nrml file = 'domestic_flood_2012' # The column title in the exposure file = 'WIND_VULNERABILITY_FUNCTION_ID' atts = { 'vul_functions_in_exposure': { 'structural_domestic_flood_2012': 'FABRIC_FLOOD_FUNCTION_ID' } } add_job(job_insts, SIMPLELINKER, atts) atts = {'variability_method': {'structural_domestic_flood_2012': 'mean'}} add_job(job_insts, SELECTVULNFUNCTION, atts) add_job(job_insts, LOOKUP) atts_dict = find_attributes(config, CALCSTRUCTLOSS) if REP_VAL_NAME not in atts_dict: msg = '\nMandatory key not found in config file; %s\n' % REP_VAL_NAME raise RuntimeError(msg) attributes = { 'var1': 'structural', 'var2': atts_dict[REP_VAL_NAME], 'var_out': 'structural_loss' } add_job(job_insts, MDMULT, attributes) file_name = find_attributes(config, SAVE) add_job(job_insts, SAVEALL, {'file_name': file_name}) file_name = find_attributes(config, SAVE) base = os.path.splitext(file_name)[0] file_name = f"{base}.xml" add_job(job_insts, SAVEPROVENANCE, {'file_name': file_name}) return job_insts
def _flood_contents_v2_reader(config: dict) -> list: # pylint: disable=R0915 """ This function does two things; * From a flood contents template v2 configuration dictionary build the job list. * Set up the attributes of the jobs and calc's specifically for a flood study. :param config_list: A list describing the simulation. :returns: A list of jobs to process over. """ job_insts = [] atts = find_attributes(config, LOADCSVEXPOSURE) add_job(job_insts, LOADCSVEXPOSURE, atts) file_list = find_attributes(config, HAZARDRASTER) atts = {'file_list': file_list, 'attribute_label': WATER_DEPTH} add_job(job_insts, LOADRASTER, atts) vul_filename = os.path.join(misc.RESOURCE_DIR, 'content_flood_avg_curve.xml') add_job(job_insts, LOADXMLVULNERABILITY, {'file_name': vul_filename}) floor_height_value = find_attributes(config, FLOOR_HEIGHT) atts = {'var': FLOOR_HEIGHT, 'value': floor_height_value} add_job(job_insts, CONSTANT, atts) add_job(job_insts, FLOOR_HEIGHT_CALC) # select save, nosave or expose atts = find_attributes(config, CONT_ACTIONS) probs = {} for key in CONT_MAP: if key not in atts: msg = '\nMandatory key not found in config file; %s\n' % key msg += 'Section; %s\n' % CONT_ACTIONS raise RuntimeError(msg) try: probs[CONT_MAP[key]] = atts[key] except TypeError: msg = "\nError: May be due to no spaces after ':' in YAML file\n" raise RuntimeError(msg) attributes = {'var': CONT_ACTION_COL, 'values': probs} add_job(job_insts, RANDOM_CONSTANT, attributes) # select insured or uninsured atts = find_attributes(config, INSURE_PROB) probs = {} for key in INSURE_MAP: if key not in atts: msg = '\nMandatory key not found in config file; %s\n' % key msg += 'Section; %s\n' % INSURE_PROB raise RuntimeError(msg) try: probs[INSURE_MAP[key]] = atts[key] except TypeError: msg = "\nError: May be due to no spaces after ':' in YAML file\n" raise RuntimeError(msg) attributes = {'var': CONT_INSURANCE_COL, 'values': probs} add_job(job_insts, RANDOM_CONSTANT, attributes) # combine columns to give constant_function_id attributes = { 'var1': 'BUILDING_TYPE', 'var2': CONT_INSURANCE_COL, 'var_out': CONT_TEMP } add_job(job_insts, ADD, attributes) attributes = { 'var1': CONT_TEMP, 'var2': CONT_ACTION_COL, 'var_out': 'CONTENTS_FLOOD_FUNCTION_ID' } add_job(job_insts, ADD, attributes) # The vulnerabilitySetID from the nrml file = 'domestic_flood_2012' # The column title in the exposure file = 'CONTENTS_FLOOD_FUNCTION_ID' atts = { 'vul_functions_in_exposure': { 'contents_domestic_flood_2012': 'CONTENTS_FLOOD_FUNCTION_ID' } } add_job(job_insts, SIMPLELINKER, atts) atts = {'variability_method': {'contents_domestic_flood_2012': 'mean'}} add_job(job_insts, SELECTVULNFUNCTION, atts) add_job(job_insts, LOOKUP) atts_dict = find_attributes(config, CALCCONTLOSS) if REP_VAL_NAME not in atts_dict: msg = '\nMandatory key not found in config file; %s\n' % REP_VAL_NAME raise RuntimeError(msg) attributes = { 'var1': 'contents', 'var2': atts_dict[REP_VAL_NAME], 'var_out': 'contents_loss' } add_job(job_insts, MDMULT, attributes) file_name = find_attributes(config, SAVE) add_job(job_insts, SAVEALL, {'file_name': file_name}) file_name = find_attributes(config, SAVE) base = os.path.splitext(file_name)[0] file_name = f"{base}.xml" add_job(job_insts, SAVEPROVENANCE, {'file_name': file_name}) return job_insts