def taxonomy_full2short_test(self):
        pla = platform_get()
        from openquakeplatform.common.taxonomy import taxonomy_full2short

        failed = 0
        data_path = os.path.join(os.path.dirname(
            sys.modules[self.__module__].__file__), 'data')

        with open(os.path.join(data_path, 'taxonomies.txt')) as f:
            csv_rows = csv.reader(f, delimiter='|')
            for csv_row in csv_rows:
                taxonomy_type = csv_row[0]
                full = csv_row[1]
                short_exp = csv_row[2]

                # if taxonomy_type != gem_taxonomy => continue
                if taxonomy_type != '1':
                    continue
                short = taxonomy_full2short(full)
                if short != short_exp:
                    sys.stderr.write("IN: [%s] OUT: [%s] EXP: [%s]\n" % (
                        full, short, short_exp))
                    failed += 1
                # Uncomment to see tested taxonomies with correct conversion
                # else:
                #     sys.stderr.write("[%s] Ok\n" % short)

        self.assertEqual(failed, 0)
Example #2
0
def _stream_frag_cont_data(curr, first_obj, func_obj, is_first):
    if is_first:
        frmo = etree.SubElement(curr, "fragilityModel", format="continuous")
        desc = etree.SubElement(frmo, "description")
        # NOTE: We are currently using a prefix plus the function name, but in
        # the future we are going to have more than one function in the same
        # NRML file and this tag will contain a general description of the set
        # of curves
        desc.text = 'Fragility from GVD: %s' % func_obj.name

        limit_states = _limit_states_slugify(
            func_obj.fragility_func.limit_states_desc)

        func_obj._mangled_limit_states = limit_states

        lims = etree.SubElement(frmo, "limitStates")
        lims.text = limit_states
    else:
        limit_states = first_obj._mangled_limit_states
        frmo = curr

    func_distr_shape_id = \
        func_obj.fragility_func.func_distr_frag_cont.func_distr_shape
    func_distr_shape = dict(FUNC_DISTR_SHAPES_ALL)[func_distr_shape_id].lower()
    if func_distr_shape != "lognormal":
        raise FuncDistrShapeException(func_distr_shape)

    imt_idx = int(func_obj.fragility_func.predictor_var.intensity_measure_type)
    imts = dict((k, v) for k, v in INTENSITY_MEASURE_TYPES)
    imt = imts[str(imt_idx)]
    if imt_idx == IMT.SDT:
        raise IntensityMeasureTypeException(imt)
    if imt_idx == IMT.PGA:
        noDamageLimit = '0.05'
    elif imt_idx == IMT.PGV:
        noDamageLimit = '1'
    elif imt_idx in (IMT.PGD, IMT.SAT, IMT.IA, IMT.CAV):
        noDamageLimit = '0.01'
        if imt_idx == IMT.SAT:
            period = func_obj.fragility_func.predictor_var.period
            imt = imt.replace('T', str(period))
    elif imt_idx == IMT.RSD:
        noDamageLimit = '3'
    elif imt_idx == IMT.MMI:
        noDamageLimit = '4'
    else:
        raise IntensityMeasureTypeException(imt)

    ffs = etree.SubElement(
        frmo, "ffs", noDamageLimit=noDamageLimit, type=func_distr_shape)

    if func_obj.taxonomy_gem:
        tax = etree.SubElement(ffs, "taxonomy")
        if func_obj.taxonomy_type_id == 1:
            tax.text = taxonomy_full2short(func_obj.taxonomy_gem)
        else:
            tax.text = func_obj.taxonomy_gem
    else:
        ffs.append(etree.Comment('an empty taxonomy means "all taxonomies"'))
        tax = etree.SubElement(ffs, "taxonomy")

    iml_orig_type = int(
        func_obj.fragility_func.predictor_var.intensity_measure_type)
    iml_orig_unit = int(
        func_obj.fragility_func.predictor_var.intensity_measure_unit)

    if iml_orig_type == IMT.SDT:
        raise ValueError

    maxIML = func_obj.fragility_func.predictor_var.maximum_im
    minIML = func_obj.fragility_func.predictor_var.minimum_im

    (iml_k, imlUnit) = _normalize_measurement(iml_orig_type, iml_orig_unit)

    iml = etree.SubElement(  # NOTE: Never used
        ffs, "IML",
        IMT=imt,
        imlUnit=imlUnit,
        maxIML=str(maxIML * iml_k),
        minIML=str(minIML * iml_k))
    mean_arr = func_obj.fragility_func.func_distr_frag_cont.mean.split(';')
    stddev_arr = func_obj.fragility_func.func_distr_frag_cont.std_dev.split(
        ';')
    for i, ls in enumerate(limit_states.split(' ')):
        ffc = etree.SubElement(ffs, "ffc", ls=ls)
        params = etree.SubElement(ffc, "params")
        params.attrib['mean'] = mean_arr[i]
        params.attrib['stddev'] = stddev_arr[i]

    return frmo
Example #3
0
def _stream_frag_discr_data(curr, first_obj, func_obj, is_first):
    if is_first:
        frmo = etree.SubElement(curr, "fragilityModel", format="discrete")

        desc = etree.SubElement(frmo, "description")
        # NOTE: We are currently using a prefix plus the function name, but in
        # the future we are going to have more than one function in the same
        # NRML file and this tag will contain a general description of the set
        # of curves
        desc.text = 'Fragility from GVD: %s' % func_obj.name

        limit_states = _limit_states_slugify(
            func_obj.fragility_func.limit_states_desc)

        func_obj._mangled_limit_states = limit_states

        lims = etree.SubElement(frmo, "limitStates")
        lims.text = limit_states
    else:
        limit_states = first_obj._mangled_limit_states
        frmo = curr

    imt_idx = int(func_obj.fragility_func.predictor_var.intensity_measure_type)
    imts = dict((k, v) for k, v in INTENSITY_MEASURE_TYPES)
    imt = imts[str(imt_idx)]
    if imt_idx == IMT.SDT:
        raise IntensityMeasureTypeException(imt)
    if imt_idx == IMT.PGA:
        noDamageLimit = '0.05'
    elif imt_idx == IMT.PGV:
        noDamageLimit = '1'
    elif imt_idx in (IMT.PGD, IMT.SAT, IMT.IA, IMT.CAV):
        noDamageLimit = '0.01'
        if imt_idx == IMT.SAT:
            period = func_obj.fragility_func.predictor_var.period
            imt = imt.replace('T', str(period))
    elif imt_idx == IMT.RSD:
        noDamageLimit = '3'
    elif imt_idx == IMT.MMI:
        noDamageLimit = '4'
    else:
        raise IntensityMeasureTypeException(imt)
    ffs = etree.SubElement(
        frmo, "ffs", noDamageLimit=noDamageLimit)

    if func_obj.taxonomy_gem:
        tax = etree.SubElement(ffs, "taxonomy")
        if func_obj.taxonomy_type_id == 1:
            tax.text = taxonomy_full2short(func_obj.taxonomy_gem)
        else:
            tax.text = func_obj.taxonomy_gem
    else:
        ffs.append(etree.Comment('an empty taxonomy means "all taxonomies"'))
        tax = etree.SubElement(ffs, "taxonomy")

    iml_orig_type = int(
        func_obj.fragility_func.predictor_var.intensity_measure_type)
    iml_orig_unit = int(
        func_obj.fragility_func.predictor_var.intensity_measure_unit)

    if iml_orig_type == IMT.SDT:
        raise ValueError

    # NOTE: Those two fields are going to be set as mandatory
    maxIML = func_obj.fragility_func.predictor_var.maximum_im
    minIML = func_obj.fragility_func.predictor_var.minimum_im

    (iml_k, imlUnit) = _normalize_measurement(iml_orig_type, iml_orig_unit)

    iml = etree.SubElement(  # NOTE: Never used
        ffs, "IML",
        IMT=imt,
        imlUnit=imlUnit,
        maxIML=str(maxIML * iml_k),
        minIML=str(minIML * iml_k)
        ).text = (func_obj.fragility_func.func_distr_frag_discr.
                  predictor_var_im_val.replace(';', ' '))

    for i, ls in enumerate(limit_states.split(' ')):
        ffd = etree.SubElement(ffs, "ffd", ls=ls)
        poEs = etree.SubElement(ffd, "poEs")
        lspe = (func_obj.fragility_func.func_distr_frag_discr.
                limit_state_prob_exceed)
        lspe_i = lspe.split('\n')[i]
        lspe_i_spaces = lspe_i.replace(';', ' ')
        poEs.text = lspe_i_spaces

    return frmo