oasislmf.pytools.gulmc.manager

Module Contents

Functions

gen_empty_vuln_cdf_lookup(list_size)

Generate structures needed to store and retrieve vulnerability cdf in the cache.

run(run_dir, ignore_file_type, sample_size, ...[, ...])

Execute the main gulmc worklow.

compute_event_losses(event_id, coverages, ...)

Compute losses for an event.

get_vuln_cdf(vuln_i, haz_int_bin_id, ...)

Compute the cdf of a vulnerability function and store it in cache or, if it is already cached, retrieve it.

process_areaperils_in_footprint(event_footprint, ...)

Process all the areaperils in the footprint, filtering and retaining only those who have associated vulnerability functions,

reconstruct_coverages(event_id, areaperil_ids, ...)

Register each item to its coverage, with the location of the corresponding hazard intensity cdf

Attributes

oasislmf.pytools.gulmc.manager.logger[source]
oasislmf.pytools.gulmc.manager.VULN_LOOKUP_KEY_TYPE[source]
oasislmf.pytools.gulmc.manager.VULN_LOOKUP_VALUE_TYPE[source]
oasislmf.pytools.gulmc.manager.gen_empty_vuln_cdf_lookup(list_size)[source]

Generate structures needed to store and retrieve vulnerability cdf in the cache.

Args:

list_size (int): maximum number of cdfs to be stored in the cache.

Returns:
cached_vuln_cdf_lookup (Dict[VULN_LOOKUP_KEY_TYPE, VULN_LOOKUP_VALUE_TYPE]): dict to store

the map between vuln_id and intensity bin id and the location of the cdf in the cache.

cached_vuln_cdf_lookup_keys (List[VULN_LOOKUP_VALUE_TYPE]): list of lookup keys.

oasislmf.pytools.gulmc.manager.run(run_dir, ignore_file_type, sample_size, loss_threshold, alloc_rule, debug, random_generator, peril_filter=[], file_in=None, file_out=None, data_server=None, ignore_correlation=False, ignore_haz_correlation=False, effective_damageability=False, max_cached_vuln_cdf_size_MB=200, model_df_engine='oasis_data_manager.df_reader.reader.OasisPandasReader', **kwargs)[source]

Execute the main gulmc worklow.

Args:

run_dir (str): the directory of where the process is running ignore_file_type set(str): file extension to ignore when loading sample_size (int): number of random samples to draw. loss_threshold (float): threshold above which losses are printed to the output stream. alloc_rule (int): back-allocation rule. debug (int): for each random sample, print to the output stream the random loss (if 0), the random value used to draw

the hazard intensity sample (if 1), the random value used to draw the damage sample (if 2). Defaults to 0.

random_generator (int): random generator function id. peril_filter (list[int], optional): list of perils to include in the computation (if None, all perils will be included). Defaults to []. file_in (str, optional): filename of input stream. Defaults to None. file_out (str, optional): filename of output stream. Defaults to None. data_server (bool, optional): if True, run the data server. Defaults to None. ignore_correlation (bool, optional): if True, do not compute correlated random samples. Defaults to False. effective_damageability (bool, optional): if True, it uses effective damageability to draw damage samples instead of

using the full monte carlo approach (i.e., to draw hazard intensity first, then damage).

max_cached_vuln_cdf_size_MB (int, optional): size in MB of the in-memory cache to store and reuse vulnerability cdf. Defaults to 200. model_df_engine: (str) The engine to use when loading model dataframes

Raises:

ValueError: if alloc_rule is not 0, 1, 2, or 3. ValueError: if alloc_rule is 1, 2, or 3 when debug is 1 or 2.

Returns:

int: 0 if no errors occurred.

oasislmf.pytools.gulmc.manager.compute_event_losses(event_id, coverages, coverage_ids, items_event_data, items, last_processed_coverage_ids_idx, sample_size, haz_cdf, haz_cdf_ptr, areaperil_to_eff_vuln_cdf, eff_vuln_cdf, vuln_array, damage_bins, Ndamage_bins_max, cached_vuln_cdf_lookup, cached_vuln_cdf_lookup_keys, next_cached_vuln_cdf, cached_vuln_cdfs, agg_vuln_to_vuln_id, agg_vuln_to_vuln_idxs, vuln_dict, areaperil_vuln_idx_to_weight, loss_threshold, losses, vuln_cdf_empty, weighted_vuln_cdf_empty, alloc_rule, do_correlation, do_haz_correlation, haz_rndms_base, vuln_rndms_base, vuln_adj_dict, haz_eps_ij, eps_ij, norm_inv_parameters, norm_inv_cdf, norm_cdf, z_unif, effective_damageability, debug, max_bytes_per_item, byte_mv, cursor)[source]

Compute losses for an event.

Args:

event_id (int32): event id. coverages (numpy.array[oasis_float]): array with the coverage values for each coverage_id. coverage_ids (numpy.array[int]): array of unique coverage ids used in this event. items_data (numpy.array[items_data_type]): items-related data. items (np.ndarray): items table merged with correlation parameters. last_processed_coverage_ids_idx (int): index of the last coverage_id stored in coverage_ids that was fully processed

and printed to the output stream.

sample_size (int): number of random samples to draw. haz_cdf (np.array[oasis_float]): hazard intensity cdf. haz_cdf_ptr (np.array[int]): array with the indices where each cdf record starts in haz_cdf. areaperil_to_eff_vuln_cdf (dict[ITEM_MAP_KEY_TYPE_internal, int]): map between (areaperil_id, vuln_idx) and the location

where the effective damageability function is stored in eff_vuln_cdf.

eff_vuln_cdf (np.array[oasis_float]): effective damageability cdf. vuln_array (np.array[float]): damage pdf for different vulnerability functions, as a function of hazard intensity. damage_bins (List[Union[damagebindictionaryCsv, damagebindictionary]]): loaded data from the damage_bin_dict file. Ndamage_bins_max (int): maximum number of damage bins. cached_vuln_cdf_lookup (Dict[VULN_LOOKUP_KEY_TYPE, VULN_LOOKUP_VALUE_TYPE]): dict to store

the map between vuln_id and intensity bin id and the location of the cdf in the cache.

cached_vuln_cdf_lookup_keys (List[VULN_LOOKUP_VALUE_TYPE]): list of lookup keys. next_cached_vuln_cdf (int): index of the next free slot in the vuln cdf cache. cached_vuln_cdfs (np.array[oasis_float]): vulnerability cdf cache. agg_vuln_to_vuln_id (dict[int, list[int]]): map of aggregate vulnerability id to list of vulnerability ids. agg_vuln_to_vuln_idxs (dict[int, list[int]]): map between aggregate vulnerability id and the list of indices where the individual vulnerability_ids

that compose it are stored in vuln_array.

vuln_dict (Dict[int, int]): map between vulnerability_id and the index where the vulnerability function is stored in vuln_array. areaperil_vuln_idx_to_weight (dict[AGG_VULN_WEIGHTS_KEY_TYPE, AGG_VULN_WEIGHTS_VAL_TYPE]): map between the areaperil id and the index where the vulnerability function

is stored in vuln_array and the vulnerability weight.

loss_threshold (float): threshold above which losses are printed to the output stream. losses (numpy.array[oasis_float]): array (to be re-used) to store losses for each item. vuln_cdf_empty (numpy.array[oasis_float]): array (to be re-used) to store vulnerability cdf. weighted_vuln_cdf_empty (numpy.array[oasis_float]): array (to be re-used) to store the weighted vulnerability cdf. alloc_rule (int): back-allocation rule. do_correlation (bool): if True, compute correlated random samples of damage. do_haz_correlation (bool): if True, compute correlated random samples of hazard intensity. haz_rndms_base (numpy.array[float64]): 2d array of shape (number of seeds, sample_size) storing the random values

drawn for each seed for the hazard intensity sampling.

vuln_rndms_base (numpy.array[float64]): 2d array of shape (number of seeds, sample_size) storing the random values

drawn for each seed for the damage sampling.

vuln_adj_dict (dict[int, float]): map between vulnerability_id and the adjustment factor to be applied to the (random numbers extracted) vulnerability function. haz_eps_ij (np.array[float]): correlated random values of shape (number of seeds, sample_size) for hazard sampling. eps_ij (np.array[float]): correlated random values of shape (number of seeds, sample_size) for damage sampling. norm_inv_parameters (NormInversionParameters): parameters for the Normal (Gaussian) inversion functionality. norm_inv_cdf (np.array[float]): inverse Gaussian cdf. norm_cdf (np.array[float]): Gaussian cdf. z_unif (np.array[float]): buffer to be re-used to store all the correlated random values. effective_damageability (bool): if True, it uses effective damageability to draw damage samples instead of

using the full monte carlo approach (i.e., to draw hazard intensity first, then damage).

debug (int): for each random sample, print to the output stream the random loss (if 0),

the random value used to draw the hazard intensity sample (if 1), the random value used to draw the damage sample (if 2).

max_bytes_per_item (int): maximum bytes to be written in the output stream for an item. byte_mv (numpy.array): byte view of where the output is buffered. cursor (int): index of byte_mv where to start writing.

Returns:

cursor (int): index of byte_mv where to data has been written. last_processed_coverage_ids_idx (int): index of the last coverage_id stored in coverage_ids that was fully processed

and printed to the output stream.

next_cached_vuln_cdf (int): index of the next free slot in the vuln cdf cache.

oasislmf.pytools.gulmc.manager.get_vuln_cdf(vuln_i, haz_int_bin_id, cached_vuln_cdf_lookup, cached_vuln_cdf_lookup_keys, vuln_array, vuln_cdf_empty, Ndamage_bins_max, cached_vuln_cdfs, next_cached_vuln_cdf)[source]

Compute the cdf of a vulnerability function and store it in cache or, if it is already cached, retrieve it.

Args:

vuln_i (int): index of the vuln_array matrix where the vulnerability pdf is stored. haz_int_bin_id (int): the selected hazard intensity bin id (starts from 1). cached_vuln_cdf_lookup (Dict[VULN_LOOKUP_KEY_TYPE, VULN_LOOKUP_VALUE_TYPE]): dict to store

the map between vuln_id and intensity bin id and the location of the cdf in the cache.

cached_vuln_cdf_lookup_keys (List[VULN_LOOKUP_VALUE_TYPE]): list of lookup keys. vuln_array (np.array[float]): damage pdf for different vulnerability functions, as a function of hazard intensity. vuln_cdf_empty (numpy.array[oasis_float]): array (to be re-used) to store vulnerability cdf. Ndamage_bins_max (int): maximum number of damage bins. cached_vuln_cdfs (np.array[oasis_float]): vulnerability cdf cache. next_cached_vuln_cdf (int): index of the next free slot in the vuln cdf cache.

Returns:

vuln_cdf (np.array[oasis_float]): the desired vulnerability cdf. Ndamage_bins (int): number of bins of vuln_cdf. next_cached_vuln_cdf (int): updated index of the next free slot in the vuln cdf cache.

oasislmf.pytools.gulmc.manager.process_areaperils_in_footprint(event_footprint, vuln_array, areaperil_to_vulns_idx_dict, areaperil_to_vulns_idx_array, areaperil_to_vulns)[source]

Process all the areaperils in the footprint, filtering and retaining only those who have associated vulnerability functions, computing the hazard intensity cdf for each of those areaperil_id.

Args:

event_footprint (np.array[Event or EventCSV]): footprint, made of one or more event entries. vuln_array (np.array[float]): damage pdf for different vulnerability functions, as a function of hazard intensity. areaperil_to_vulns_idx_dict (dict[int, int]): areaperil to vulnerability index dictionary. areaperil_to_vulns_idx_array (List[IndexType]]): areaperil ID to vulnerability index array. areaperil_to_vulns (np.array ): vuln indexes for each area perils

Returns:

areaperil_ids (List[int]): list of all areaperil_ids present in the footprint. Nhaz_cdf_this_event (int): number of hazard cdf stored for this event. If zero, it means no items have losses in such event. areaperil_to_haz_cdf (dict[int, int]): map between the areaperil_id and the hazard cdf index. haz_cdf (np.array[oasis_float]): hazard intensity cdf. haz_cdf_ptr (np.array[int]): array with the indices where each cdf record starts in haz_cdf. eff_vuln_cdf (np.array[oasis_float]): effective damageability cdf. areaperil_to_eff_vuln_cdf (dict[ITEM_MAP_KEY_TYPE_internal, int]): map between (areaperil_id, vuln_idx) and the location

where the effective damageability function is stored in eff_vuln_cdf.

oasislmf.pytools.gulmc.manager.reconstruct_coverages(event_id, areaperil_ids, areaperil_ids_map, areaperil_to_haz_cdf, item_map, items, coverages, compute, haz_seeds, vuln_seeds)[source]

Register each item to its coverage, with the location of the corresponding hazard intensity cdf in the footprint, compute the random seeds for the hazard intensity and vulnerability samples.

Args:

event_id (int32): event id. areaperil_ids (List[int]): list of all areaperil_ids present in the footprint. areaperil_ids_map (Dict[int, Dict[int, int]]) dict storing the mapping between each

areaperil_id and all the vulnerability ids associated with it.

areaperil_to_haz_cdf (dict[int, int]): map between the areaperil_id and the hazard cdf index. item_map (Dict[ITEM_MAP_KEY_TYPE, ITEM_MAP_VALUE_TYPE]): dict storing

the mapping between areaperil_id, vulnerability_id to item.

items (np.ndarray): items table merged with correlation parameters. coverages (numpy.array[oasis_float]): array with the coverage values for each coverage_id. compute (numpy.array[int]): list of coverage ids to be computed. haz_seeds (numpy.array[int]): the random seeds to draw the hazard intensity samples. vuln_seeds (numpy.array[int]): the random seeds to draw the damage samples.

Returns:

compute_i (int): index of the last coverage id stored in compute. items_data (numpy.array[items_MC_data_type]): item-related data. rng_index (int): number of unique random seeds for damage sampling computed so far. hazard_rng_index (int): number of unique random seeds for hazard intensity sampling computed so far.

oasislmf.pytools.gulmc.manager.test_dir[source]