Пример #1
0
    def test_get_displacement_rate_and_affected_with_qsetting(self):
        """Test for get_displacement_rate and is_affected with QSettings."""
        # Create custom qsettings
        qsettings = QSettings(INASAFE_TEST)
        qsettings.clear()
        # Save the default profile to qsettings
        default_profile = generate_default_profile()
        set_setting('population_preference', default_profile, qsettings)

        # Check the default one first
        default_profile = generate_default_profile()
        class_key = flood_hazard_classes['classes'][0]['key']
        value = is_affected(hazard_flood['key'], flood_hazard_classes['key'],
                            class_key, qsettings)
        expected_value = default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['affected']
        self.assertEqual(value, expected_value)

        value = get_displacement_rate(hazard_flood['key'],
                                      flood_hazard_classes['key'], class_key,
                                      qsettings)
        expected_value = default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['displacement_rate']
        self.assertEqual(value, expected_value)

        # Change displacement rate
        new_displacement_rate = 1
        default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['displacement_rate'] = 1
        set_setting('population_preference', default_profile, qsettings)
        value = get_displacement_rate(hazard_flood['key'],
                                      flood_hazard_classes['key'], class_key,
                                      qsettings)
        self.assertEqual(value, new_displacement_rate)

        # Change affected value
        new_affected = False
        default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['affected'] = new_affected
        set_setting('population_preference', default_profile, qsettings)
        value = is_affected(hazard_flood['key'], flood_hazard_classes['key'],
                            class_key, qsettings)
        self.assertEqual(value, new_affected)
        value = get_displacement_rate(hazard_flood['key'],
                                      flood_hazard_classes['key'], class_key,
                                      qsettings)
        # Should be 0 since it's not affected
        self.assertEqual(value, 0)
Пример #2
0
    def test_get_displacement_rate_and_affected(self):
        """Test for get_displacement_rate and is_affected"""
        # Random key
        value = is_affected('foo', 'bar', 'boom')
        self.assertEqual(value, not_exposed_class['key'])
        value = get_displacement_rate('foo', 'bar', 'boom')
        self.assertEqual(value, 0)

        # From default values
        default_profile = generate_default_profile()
        class_key = flood_hazard_classes['classes'][0]['key']
        value = is_affected(hazard_flood['key'], flood_hazard_classes['key'],
                            class_key)
        expected_value = default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['affected']
        self.assertEqual(value, expected_value)

        value = get_displacement_rate(hazard_flood['key'],
                                      flood_hazard_classes['key'], class_key)
        expected_value = default_profile[hazard_flood['key']][
            flood_hazard_classes['key']][class_key]['displacement_rate']
        self.assertEqual(value, expected_value)
Пример #3
0
    def test_get_displacement_rate_and_affected(self):
        """Test for get_displacement_rate and is_affected"""
        # Random key
        value = is_affected('foo', 'bar', 'boom')
        self.assertEqual(value, not_exposed_class['key'])
        value = get_displacement_rate('foo', 'bar', 'boom')
        self.assertEqual(value, 0)

        # From default values
        default_profile = generate_default_profile()
        class_key = flood_hazard_classes['classes'][0]['key']
        value = is_affected(
            hazard_flood['key'], flood_hazard_classes['key'], class_key)
        expected_value = default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'affected']
        self.assertEqual(value, expected_value)

        value = get_displacement_rate(
            hazard_flood['key'], flood_hazard_classes['key'], class_key)
        expected_value = default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'displacement_rate']
        self.assertEqual(value, expected_value)
Пример #4
0
def post_processor_affected_function(
        exposure=None, hazard=None, classification=None, hazard_class=None):
    """Private function used in the affected postprocessor.

    It returns a boolean if it's affected or not, or not exposed.

    :param exposure: The exposure to use.
    :type exposure: str

    :param hazard: The hazard to use.
    :type hazard: str

    :param classification: The hazard classification to use.
    :type classification: str

    :param hazard_class: The hazard class of the feature.
    :type hazard_class: str

    :return: If this hazard class is affected or not. It can be `not exposed`.
        The not exposed value returned is the key defined in
        `hazard_classification.py` at the top of the file.
    :rtype: bool,'not exposed'
    """
    if exposure == exposure_population['key']:
        affected = is_affected(
            hazard, classification, hazard_class)
    else:
        classes = None
        for hazard in hazard_classes_all:
            if hazard['key'] == classification:
                classes = hazard['classes']
                break

        for the_class in classes:
            if the_class['key'] == hazard_class:
                affected = the_class['affected']
                break
        else:
            affected = not_exposed_class['key']

    return affected
Пример #5
0
def notes_assumptions_extractor(impact_report, component_metadata):
    """Extracting notes and assumptions of the exposure layer

    :param impact_report: the impact report that acts as a proxy to fetch
        all the data that extractor needed
    :type impact_report: safe.report.impact_report.ImpactReport

    :param component_metadata: the component metadata. Used to obtain
        information about the component we want to render
    :type component_metadata: safe.report.report_metadata.
        ReportComponentsMetadata

    :return: context for rendering phase
    :rtype: dict

    .. versionadded:: 4.0
    """
    context = {}
    provenance = impact_report.impact_function.provenance
    extra_args = component_metadata.extra_args
    hazard_keywords = provenance['hazard_keywords']
    exposure_keywords = provenance['exposure_keywords']
    exposure_type = definition(exposure_keywords['exposure'])

    analysis_note_dict = resolve_from_dictionary(extra_args, 'analysis_notes')
    context['items'] = [analysis_note_dict]

    context['component_key'] = component_metadata.key
    context['header'] = resolve_from_dictionary(extra_args, 'header')
    context['items'] += provenance['notes']

    # Get hazard classification
    hazard_classification = definition(
        active_classification(hazard_keywords, exposure_keywords['exposure']))

    # Check hazard affected class
    affected_classes = []
    for hazard_class in hazard_classification['classes']:
        if exposure_keywords['exposure'] == exposure_population['key']:
            # Taking from profile
            is_affected_class = is_affected(
                hazard=hazard_keywords['hazard'],
                classification=hazard_classification['key'],
                hazard_class=hazard_class['key'],
            )
            if is_affected_class:
                affected_classes.append(hazard_class)
        else:
            if hazard_class.get('affected', False):
                affected_classes.append(hazard_class)

    if affected_classes:
        affected_note_dict = resolve_from_dictionary(
            extra_args, 'affected_note_format')

        # generate hazard classes
        hazard_classes = ', '.join([
            c['name'] for c in affected_classes
        ])

        for index, affected_note in enumerate(affected_note_dict['item_list']):
            affected_note_dict['item_list'][index] = (
                affected_note.format(hazard_classes=hazard_classes)
            )

        context['items'].append(affected_note_dict)

    # Check hazard have displacement rate
    for hazard_class in hazard_classification['classes']:
        if hazard_class.get('displacement_rate', 0) > 0:
            have_displacement_rate = True
            break
    else:
        have_displacement_rate = False

    # Only show displacement note if analysis about population exposure
    if have_displacement_rate and exposure_type == exposure_population:
        # add notes for displacement rate used
        displacement_note_dict = resolve_from_dictionary(
            extra_args, 'displacement_rates_note_format')

        # generate rate description
        displacement_rates_note_format = resolve_from_dictionary(
            extra_args, 'hazard_displacement_rates_note_format')
        displacement_rates_note = []
        for hazard_class in hazard_classification['classes']:
            the_hazard_class = deepcopy(hazard_class)
            the_hazard_class['displacement_rate'] = get_displacement_rate(
                hazard=hazard_keywords['hazard'],
                classification=hazard_classification['key'],
                hazard_class=the_hazard_class['key']
            )
            displacement_rates_note.append(
                displacement_rates_note_format.format(**the_hazard_class))

        rate_description = ', '.join(displacement_rates_note)

        for index, displacement_note in enumerate(
                displacement_note_dict['item_list']):
            displacement_note_dict['item_list'][index] = (
                displacement_note.format(rate_description=rate_description)
            )

        context['items'].append(displacement_note_dict)

    # Check hazard have displacement rate
    have_fatality_rate = False
    for hazard_class in hazard_classification['classes']:
        if hazard_class.get('fatality_rate', None) is not None and \
                hazard_class.get('fatality_rate', 0) > 0:
            have_fatality_rate = True
            break

    if have_fatality_rate and exposure_type == exposure_population:
        # add notes for fatality rate used
        fatality_note_dict = resolve_from_dictionary(
            extra_args, 'fatality_rates_note_format')

        # generate rate description
        fatality_rates_note_format = resolve_from_dictionary(
            extra_args, 'hazard_fatality_rates_note_format')
        fatality_rates_note = []
        for hazard_class in hazard_classification['classes']:
            # we make a copy here because we don't want to
            # change the real value.
            copy_of_hazard_class = dict(hazard_class)
            if copy_of_hazard_class['fatality_rate'] is None or \
                    copy_of_hazard_class['fatality_rate'] <= 0:
                copy_of_hazard_class['fatality_rate'] = 0
            else:
                # we want to show the rate as a scientific notation
                copy_of_hazard_class['fatality_rate'] = (
                    html_scientific_notation_rate(
                        copy_of_hazard_class['fatality_rate']))

            fatality_rates_note.append(
                fatality_rates_note_format.format(**copy_of_hazard_class))

        rate_description = ', '.join(fatality_rates_note)

        for index, fatality_note in enumerate(fatality_note_dict['item_list']):
            fatality_note_dict['item_list'][index] = (
                fatality_note.format(rate_description=rate_description)
            )

        context['items'].append(fatality_note_dict)

    return context
Пример #6
0
    def test_get_displacement_rate_and_affected_with_qsetting(self):
        """Test for get_displacement_rate and is_affected with QSettings."""
        # Create custom qsettings
        qsettings = QSettings(INASAFE_TEST)
        qsettings.clear()
        # Save the default profile to qsettings
        default_profile = generate_default_profile()
        set_setting('population_preference', default_profile, qsettings)

        # Check the default one first
        default_profile = generate_default_profile()
        class_key = flood_hazard_classes['classes'][0]['key']
        value = is_affected(
            hazard_flood['key'],
            flood_hazard_classes['key'],
            class_key,
            qsettings)
        expected_value = default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'affected']
        self.assertEqual(value, expected_value)

        value = get_displacement_rate(
            hazard_flood['key'],
            flood_hazard_classes['key'],
            class_key,
            qsettings)
        expected_value = default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'displacement_rate']
        self.assertEqual(value, expected_value)

        # Change displacement rate
        new_displacement_rate = 1
        default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'displacement_rate'] = 1
        set_setting('population_preference', default_profile, qsettings)
        value = get_displacement_rate(
            hazard_flood['key'],
            flood_hazard_classes['key'],
            class_key,
            qsettings)
        self.assertEqual(value, new_displacement_rate)

        # Change affected value
        new_affected = False
        default_profile[
            hazard_flood['key']][flood_hazard_classes['key']][class_key][
            'affected'] = new_affected
        set_setting('population_preference', default_profile, qsettings)
        value = is_affected(
            hazard_flood['key'],
            flood_hazard_classes['key'],
            class_key,
            qsettings)
        self.assertEqual(value, new_affected)
        value = get_displacement_rate(
            hazard_flood['key'],
            flood_hazard_classes['key'],
            class_key,
            qsettings)
        # Should be 0 since it's not affected
        self.assertEqual(value, 0)