Example #1
0
def building_type_postprocessor():
    """Get building-type parameter for postprocessing.

    :return: Selectors to activate building breakdown postprocessor.
    :rtype: list
    """
    building_type = GroupParameter()
    building_type.name = tr('Building type')
    building_type.enable_parameter = True
    building_type.description = tr(
        'Check this option if you want to enable a building impact report '
        'broken down by building type for each aggregation area.')

    unit_threshold = Unit()
    unit_threshold.name = tr('features')
    unit_threshold.plural = tr('features')
    unit_threshold.abbreviation = tr('features')
    unit_threshold.description = tr('Number of features.')

    threshold = IntegerParameter()
    threshold.name = 'Threshold'
    threshold.minimum_allowed_value = 1
    # We shouldn't set a maximum, but there is a bug (#2468).
    threshold.maximum_allowed_value = 9999
    threshold.value = 25
    threshold.unit = unit_threshold
    threshold.allowed_units = [unit_threshold]
    threshold.help_text = tr('Threshold')
    threshold.description = tr(
        'The threshold is used to consolidate small building usage groups '
        'which are within this threshold.')

    building_type.value = [threshold]

    return [building_type]
    def test_all(self):
        """Basic test of all properties."""
        parameter = IntegerParameter()
        parameter.is_required = True
        parameter.minimum_allowed_value = 1
        parameter.maximum_allowed_value = 5
        parameter.value = 3

        self.assertEqual(3, parameter.value)

        with self.assertRaises(TypeError):
            parameter.value = 'Test'

        with self.assertRaises(TypeError):
            parameter.value = 1.5

        with self.assertRaises(ValueOutOfBounds):
            parameter.value = 10

        with self.assertRaises(ValueOutOfBounds):
            parameter.value = -1
    def test_all(self):
        """Basic test of all properties."""
        parameter = IntegerParameter()
        parameter.is_required = True
        parameter.minimum_allowed_value = 1
        parameter.maximum_allowed_value = 5
        parameter.value = 3

        self.assertEqual(3, parameter.value)

        with self.assertRaises(TypeError):
            parameter.value = 'Test'

        with self.assertRaises(TypeError):
            parameter.value = 1.5

        with self.assertRaises(ValueOutOfBounds):
            parameter.value = 10

        with self.assertRaises(ValueOutOfBounds):
            parameter.value = -1
Example #4
0
    def default_field_to_parameter(self, default_field):
        """Obtain parameter from default field.

        :param default_field: A default field definition.
        :type default_field: dict

        :returns: A parameter object.
        :rtype: FloatParameter, IntegerParameter
        """
        if default_field.get('type') == QVariant.Double:
            parameter = FloatParameter()
        elif default_field.get('type') in qvariant_whole_numbers:
            parameter = IntegerParameter()
        else:
            return
        default_value = default_field.get('default_value')
        if not default_value:
            message = ('InaSAFE default field %s does not have default value' %
                       default_field.get('name'))
            LOGGER.exception(message)
            return

        parameter.guid = default_field.get('key')
        parameter.name = default_value.get('name')
        parameter.is_required = True
        parameter.precision = default_field.get('precision')
        parameter.minimum_allowed_value = default_value.get('min_value', 0)
        parameter.maximum_allowed_value = default_value.get(
            'max_value', 100000000)
        parameter.help_text = default_value.get('help_text')
        parameter.description = default_value.get('description')

        # Check if user ask to restore to the most default value.
        if self.is_restore_default:
            parameter._value = default_value.get('default_value')
        else:
            # Current value
            qsetting_default_value = get_inasafe_default_value_qsetting(
                self.settings, GLOBAL, default_field['key'])

            # To avoid python error
            if qsetting_default_value > parameter.maximum_allowed_value:
                qsetting_default_value = parameter.maximum_allowed_value
            if qsetting_default_value < parameter.minimum_allowed_value:
                qsetting_default_value = parameter.minimum_allowed_value

            parameter.value = qsetting_default_value
        return parameter
Example #5
0
def building_type_postprocessor():
    """Get building-type parameter for postprocessing.

    :return: Selectors to activate building breakdown postprocessor.
    :rtype: list
    """
    building_type = GroupParameter()
    building_type.name = tr('Building type')
    building_type.enable_parameter = True
    building_type.description = tr(
        'Check this option if you want to enable a building impact report '
        'broken down by building type for each aggregation area.'
    )

    unit_threshold = Unit()
    unit_threshold.name = tr('features')
    unit_threshold.plural = tr('features')
    unit_threshold.abbreviation = tr('features')
    unit_threshold.description = tr(
        'Number of features.'
    )

    threshold = IntegerParameter()
    threshold.name = 'Threshold'
    threshold.minimum_allowed_value = 1
    # We shouldn't set a maximum, but there is a bug (#2468).
    threshold.maximum_allowed_value = 9999
    threshold.value = 25
    threshold.unit = unit_threshold
    threshold.allowed_units = [unit_threshold]
    threshold.help_text = tr('Threshold')
    threshold.description = tr(
        'The threshold is used to consolidate small building usage groups '
        'which are within this threshold.'
    )

    building_type.value = [threshold]

    return [building_type]
    def test_set_minimum_allowed_value(self):
        """Test setter for minimum allowed value."""
        parameter = IntegerParameter()

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 'One thousand'

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 1.5

        parameter.minimum_allowed_value = 1

        # Also check that it raises an error if it exceeds max
        parameter.maximum_allowed_value = 10
        with self.assertRaises(InvalidMinimumError):
            parameter.minimum_allowed_value = 15

        # Also check that when we set a value it falls within [min, max]
        parameter.value = 5
    def test_set_maximum_allowed_value(self):
        """Test setter for maximum allowed value."""
        parameter = IntegerParameter()

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 'One million'

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 100.0

        parameter.maximum_allowed_value = 1000

        # Also check that it raises an error if it precedes min
        parameter.minimum_allowed_value = 10

        with self.assertRaises(InvalidMaximumError):
            parameter.maximum_allowed_value = 1

        # Also check that when we set a value it falls within [min, max]
        parameter.value = 17
    def test_set_minimum_allowed_value(self):
        """Test setter for minimum allowed value."""
        parameter = IntegerParameter()

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 'One thousand'

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 1.5

        parameter.minimum_allowed_value = 1

        # Also check that it raises an error if it exceeds max
        parameter.maximum_allowed_value = 10
        with self.assertRaises(InvalidMinimumError):
            parameter.minimum_allowed_value = 15

        # Also check that when we set a value it falls within [min, max]
        parameter.value = 5
    def test_set_maximum_allowed_value(self):
        """Test setter for maximum allowed value."""
        parameter = IntegerParameter()

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 'One million'

        with self.assertRaises(TypeError):
            parameter.maximum_allowed_value = 100.0

        parameter.maximum_allowed_value = 1000

        # Also check that it raises an error if it precedes min
        parameter.minimum_allowed_value = 10

        with self.assertRaises(InvalidMaximumError):
            parameter.maximum_allowed_value = 1

        # Also check that when we set a value it falls within [min, max]
        parameter.value = 17
    def test_init(self):

        unit_feet = Unit('130790')
        unit_feet.load_dictionary(unit_feet_depth)

        unit_metres = Unit('900713')
        unit_metres.load_dictionary(unit_metres_depth)

        integer_parameter = IntegerParameter()
        integer_parameter.name = 'Paper'
        integer_parameter.is_required = True
        integer_parameter.minimum_allowed_value = 1
        integer_parameter.maximum_allowed_value = 5
        integer_parameter.help_text = 'Number of paper'
        integer_parameter.description = (
            'A <b>test _description</b> that is very long so that you need '
            'to read it for one minute and you will be tired after read this '
            'description. You are the best user so far. Even better if you '
            'read this description loudly so that all of your friends will be '
            'able to hear you')
        integer_parameter.unit = unit_feet
        integer_parameter.allowed_units = [unit_feet]
        integer_parameter.value = 3

        widget = IntegerParameterWidget(integer_parameter)

        expected_value = integer_parameter.name
        real_value = widget._label.text()
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = integer_parameter.value
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(1.5)

        expected_value = 1
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(1.55555)

        expected_value = 1
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(7)

        expected_value = 5
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = 'QLabel'
        real_value = widget._unit_widget.__class__.__name__
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = 'feet'
        real_value = widget._unit_widget.text()
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)
    def test_init(self):

        unit_feet = Unit('130790')
        unit_feet.load_dictionary(unit_feet_depth)

        unit_metres = Unit('900713')
        unit_metres.load_dictionary(unit_metres_depth)

        integer_parameter = IntegerParameter()
        integer_parameter.name = 'Paper'
        integer_parameter.is_required = True
        integer_parameter.minimum_allowed_value = 1
        integer_parameter.maximum_allowed_value = 5
        integer_parameter.help_text = 'Number of paper'
        integer_parameter.description = (
            'A <b>test _description</b> that is very long so that you need '
            'to read it for one minute and you will be tired after read this '
            'description. You are the best user so far. Even better if you '
            'read this description loudly so that all of your friends will be '
            'able to hear you')
        integer_parameter.unit = unit_feet
        integer_parameter.allowed_units = [unit_feet]
        integer_parameter.value = 3

        widget = IntegerParameterWidget(integer_parameter)

        expected_value = integer_parameter.name
        real_value = widget._label.text()
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = integer_parameter.value
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(1.5)

        expected_value = 1
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(1.55555)

        expected_value = 1
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget._input.setValue(7)

        expected_value = 5
        real_value = widget.get_parameter().value
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = 'QLabel'
        real_value = widget._unit_widget.__class__.__name__
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = 'feet'
        real_value = widget._unit_widget.text()
        message = 'Expected %s get %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)
Example #12
0
def low_threshold():
    """Generate low threshold parameter

    :return: list of IntegerParameter
    :rtype: list[IntegerParameter]
    """
    field = IntegerParameter()
    field.is_required = True
    field.name = 'Low Threshold'
    field.value = 6
    field.minimum_allowed_value = 1
    field.maximum_allowed_value = 10
    unit_mmi = parameter_unit_mmi()
    field.unit = unit_mmi
    field.allowed_units = [unit_mmi]
    field.help_text = tr('Low Hazard class threshold.')
    field.description = tr(
        'The threshold of hazard categorized as Low Hazard class in MMI '
        'scale.')
    return field
Example #13
0
    def restore_default_values_page(self):
        """Setup UI for default values setting."""
        # Clear parameters so it doesn't add parameters when
        # restore from changes.
        if self.default_value_parameters:
            self.default_value_parameters = []

        unordered_parameters = []

        default_fields = all_default_fields()
        for default_field in default_fields:
            if default_field.get('type') == QVariant.Double:
                parameter = FloatParameter()
            elif default_field.get('type') in qvariant_whole_numbers:
                parameter = IntegerParameter()
            else:
                continue
            default_value = default_field.get('default_value')
            if not default_value:
                message = (
                    'InaSAFE default field %s does not have default value'
                    % default_field.get('name'))
                LOGGER.exception(message)
                continue

            parameter.guid = default_field.get('key')
            parameter.name = default_value.get('name')
            parameter.is_required = True
            parameter.precision = default_field.get('precision')
            parameter.minimum_allowed_value = default_value.get(
                'min_value', 0)
            parameter.maximum_allowed_value = default_value.get(
                'max_value', 100000000)
            parameter.help_text = default_value.get('help_text')
            parameter.description = default_value.get('description')

            # Check if user ask to restore to the most default value.
            if self.is_restore_default:
                parameter._value = default_value.get('default_value')
            else:
                # Current value
                qsetting_default_value = get_inasafe_default_value_qsetting(
                    self.settings, GLOBAL, default_field['key'])

                # To avoid python error
                if qsetting_default_value > parameter.maximum_allowed_value:
                    qsetting_default_value = parameter.maximum_allowed_value
                if qsetting_default_value < parameter.minimum_allowed_value:
                    qsetting_default_value = parameter.minimum_allowed_value

                parameter.value = qsetting_default_value

            unordered_parameters.append(parameter)

        preferred_order = [
            youth_ratio_field,
            adult_ratio_field,
            elderly_ratio_field
        ]

        for order in preferred_order:
            parameter_index = [
                p.guid for p in unordered_parameters].index(order['key'])
            if parameter_index > -1:
                self.default_value_parameters.append(
                    unordered_parameters[parameter_index])
                unordered_parameters.pop(parameter_index)

        self.default_value_parameters.extend(unordered_parameters)

        description_text = tr(
            'In this options you can change the global default values for '
            'these variables.')
        self.default_value_parameter_container = ParameterContainer(
            self.default_value_parameters, description_text=description_text)
        self.default_value_parameter_container.setup_ui()
        self.default_values_layout.addWidget(
            self.default_value_parameter_container)