예제 #1
0
    def set_widgets(self):
        """Set widgets on the Params tab"""

        # TODO Put the params to metadata! Now we need to import the IF class.
        # Notes: Why don't we store impact_function to class attribute?
        impact_function_id = self.parent.\
            step_fc_function.selected_function()['id']
        impact_function = self.impact_function_manager.get(impact_function_id)
        if not impact_function:
            return
        if_params = None
        if hasattr(impact_function, 'parameters'):
            if_params = impact_function.parameters

        text = self.tr(
            'Please set impact functions parameters.<br/>Parameters for '
            'impact function "%s" that can be modified are:' %
            impact_function_id)
        self.lblSelectIFParameters.setText(text)

        self.parameter_dialog = FunctionOptionsDialog(self)
        self.parameter_dialog.set_dialog_info(impact_function_id)
        self.parameter_dialog.build_form(if_params)

        if self.twParams:
            self.twParams.hide()

        self.twParams = self.parameter_dialog.tabWidget
        self.layoutIFParams.addWidget(self.twParams)
    def test_parse_input(self):
        function_input = {
            'thresholds': lambda: [1.0],
            'postprocessors': {
                'Gender': {'on': lambda: True},
                'Age': {
                    'on': lambda: True,
                    'params': {
                        'youth_ratio': lambda: 0.263,
                        'elderly_ratio': lambda: 0.078,
                        'adult_ratio': lambda: 0.659}}}}

        dialog = FunctionOptionsDialog()
        result = dialog.parse_input(function_input)
        print result
        expected = OrderedDict([
            ('thresholds', [1.0]),
            ('postprocessors', OrderedDict([
                ('Gender', OrderedDict([('on', True)])),
                ('Age', OrderedDict([
                    ('on', True),
                    ('params', OrderedDict([
                        ('elderly_ratio', 0.078),
                        ('youth_ratio', 0.263),
                        ('adult_ratio', 0.659)]))]))]))])
        # noinspection PyPep8Naming
        self.maxDiff = None
        self.assertDictEqual(result, expected)
    def test_build_form_minimum_needs(self):
        """Test that we can build a form by passing it params.
        """
        dialog = FunctionOptionsDialog()

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [1.0]  # default value

        parameters = {
            'thresholds': threshold,
            'postprocessors': OrderedDict([
                ('Gender', default_gender_postprocessor()),
                ('Age', age_postprocessor()),
                ('MinimumNeeds', minimum_needs_selector()),
                ])
        }

        dialog.build_form(parameters)

        assert dialog.tabWidget.count() == 2

        children = dialog.tabWidget.findChildren(QLineEdit)
        assert len(children) == 4
예제 #4
0
    def test_build_form_minimum_needs(self):
        """Test that we can build a form by passing it params.
        """
        dialog = FunctionOptionsDialog()

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [1.0]  # default value

        parameters = {
            'thresholds':
            threshold,
            'postprocessors':
            OrderedDict([
                ('Gender', default_gender_postprocessor()),
                ('Age', age_postprocessor()),
                ('MinimumNeeds', minimum_needs_selector()),
            ])
        }

        dialog.build_form(parameters)

        assert dialog.tabWidget.count() == 2

        children = dialog.tabWidget.findChildren(QLineEdit)
        assert len(children) == 4
예제 #5
0
class StepFcParams(WizardStep, FORM_CLASS):
    """Function Centric Wizard Step: Parameters"""
    def __init__(self, parent=None):
        """Constructor for the tab.

        :param parent: parent - widget to use as parent (Wizad Dialog).
        :type parent: QWidget

        """
        WizardStep.__init__(self, parent)
        self.parameter_dialog = None
        self.twParams = None

    def is_ready_to_next_step(self):
        """Check if the step is complete. If so, there is
            no reason to block the Next button.

        :returns: True if new step may be enabled.
        :rtype: bool
        """
        return True

    def get_next_step(self):
        """Find the proper step when user clicks the Next button.

        :returns: The step to be switched to
        :rtype: WizardStep instance or None
        """
        new_step = self.parent.step_fc_summary
        return new_step

    def set_widgets(self):
        """Set widgets on the Params tab"""

        # TODO Put the params to metadata! Now we need to import the IF class.
        # Notes: Why don't we store impact_function to class attribute?
        impact_function_id = self.parent.\
            step_fc_function.selected_function()['id']
        impact_function = self.impact_function_manager.get(impact_function_id)
        if not impact_function:
            return
        if_params = None
        if hasattr(impact_function, 'parameters'):
            if_params = impact_function.parameters

        text = self.tr(
            'Please set impact functions parameters.<br/>Parameters for '
            'impact function "%s" that can be modified are:' %
            impact_function_id)
        self.lblSelectIFParameters.setText(text)

        self.parameter_dialog = FunctionOptionsDialog(self)
        self.parameter_dialog.set_dialog_info(impact_function_id)
        self.parameter_dialog.build_form(if_params)

        if self.twParams:
            self.twParams.hide()

        self.twParams = self.parameter_dialog.tabWidget
        self.layoutIFParams.addWidget(self.twParams)
예제 #6
0
    def test_build_form(self):
        """Test that we can build a form by passing params.
        """

        dialog = FunctionOptionsDialog()

        # Define rice for minimum needs
        rice = ResourceParameter()
        rice.value = 2.8
        rice.frequency = 'weekly'
        rice.minimum_allowed_value = 1.4
        rice.maximum_allowed_value = 5.6
        rice.name = 'Rice'
        rice.unit.abbreviation = 'kg'
        rice.unit.name = 'kilogram'
        rice.unit.plural = 'kilograms'

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [1.0]  # default value

        parameter = {
            'thresholds':
            threshold,
            'postprocessors':
            OrderedDict([
                ('Gender', default_gender_postprocessor()),
                ('Age', age_postprocessor()),
                ('MinimumNeeds', minimum_needs_selector()),
            ]),
            'minimum needs': [rice]
        }

        dialog.build_form(parameter)

        message = 'There should be %s tabwidget but got %s' % (
            3, dialog.tabWidget.count())
        self.assertEqual(dialog.tabWidget.count(), 3, message)

        children = dialog.tabWidget.findChildren(QLineEdit)
        message = 'There should be %s QLineEdit but got %s' % (5,
                                                               len(children))
        self.assertEqual(len(children), 5, message)
    def test_build_form(self):
        """Test that we can build a form by passing params.
        """

        dialog = FunctionOptionsDialog()

        # Define rice for minimum needs
        rice = ResourceParameter()
        rice.value = 2.8
        rice.frequency = 'weekly'
        rice.minimum_allowed_value = 1.4
        rice.maximum_allowed_value = 5.6
        rice.name = 'Rice'
        rice.unit.abbreviation = 'kg'
        rice.unit.name = 'kilogram'
        rice.unit.plural = 'kilograms'

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [1.0]  # default value

        parameter = {
            'thresholds': threshold,
            'postprocessors': OrderedDict([
                ('Gender', default_gender_postprocessor()),
                ('Age', age_postprocessor()),
                ('MinimumNeeds', minimum_needs_selector()),
                ]),
            'minimum needs': [rice]
        }

        dialog.build_form(parameter)

        message = 'There should be %s tabwidget but got %s' % (
            3, dialog.tabWidget.count())
        self.assertEqual(dialog.tabWidget.count(), 3, message)

        children = dialog.tabWidget.findChildren(QLineEdit)
        message = 'There should be %s QLineEdit but got %s' % (
            5, len(children))
        self.assertEqual(len(children), 5, message)
예제 #8
0
    def set_widgets(self):
        """Set widgets on the Params tab"""

        # TODO Put the params to metadata! Now we need to import the IF class.
        # Notes: Why don't we store impact_function to class attribute?
        impact_function_id = self.parent.\
            step_fc_function.selected_function()['id']
        impact_function = self.impact_function_manager.get(
            impact_function_id)
        if not impact_function:
            return
        if_params = None
        if hasattr(impact_function, 'parameters'):
            if_params = impact_function.parameters

        text = self.tr(
            'Please set impact functions parameters.<br/>Parameters for '
            'impact function "%s" that can be modified are:' %
            impact_function_id)
        self.lblSelectIFParameters.setText(text)

        self.parameter_dialog = FunctionOptionsDialog(self)
        self.parameter_dialog.set_dialog_info(impact_function_id)
        self.parameter_dialog.build_form(if_params)

        if self.twParams:
            self.twParams.hide()

        self.twParams = self.parameter_dialog.tabWidget
        self.layoutIFParams.addWidget(self.twParams)
예제 #9
0
    def test_parse_input(self):
        function_input = {
            'thresholds': lambda: [1.0],
            'postprocessors': {
                'Gender': {
                    'on': lambda: True
                },
                'Age': {
                    'on': lambda: True,
                    'params': {
                        'youth_ratio': lambda: 0.263,
                        'elderly_ratio': lambda: 0.078,
                        'adult_ratio': lambda: 0.659
                    }
                }
            }
        }

        dialog = FunctionOptionsDialog()
        result = dialog.parse_input(function_input)
        print result
        expected = OrderedDict([
            ('thresholds', [1.0]),
            ('postprocessors',
             OrderedDict([('Gender', OrderedDict([('on', True)])),
                          ('Age',
                           OrderedDict([('on', True),
                                        ('params',
                                         OrderedDict([('elderly_ratio', 0.078),
                                                      ('youth_ratio', 0.263),
                                                      ('adult_ratio', 0.659)]))
                                        ]))]))
        ])
        # noinspection PyPep8Naming
        self.maxDiff = None
        self.assertDictEqual(result, expected)
    def test_build_widget(self):
        dialog = FunctionOptionsDialog()

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [2.3]  # default value

        value = dialog.build_widget(dialog.configLayout, 'foo', threshold)
        widget = dialog.findChild(QLineEdit)
        add_button = dialog.findChildren(QPushButton)[0]
        remove_button = dialog.findChildren(QPushButton)[1]
        list_widget = dialog.findChild(QListWidget)

        # initial value must be same with default
        expected_value = [2.3]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        # change to 5.9
        # select 2.3 list item
        self.click_list_widget_item(list_widget, '2.3')
        # remove 2.3 list item
        remove_button.click()
        # typing 5.9
        widget.setText('5.9')
        # add it to list
        add_button.click()
        expected_value = [5.9]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        # add 70
        widget.setText('70')
        # add it to list
        add_button.click()
        expected_value = [5.9, 70]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setText('bar')
        self.assertEqual('bar', widget.text())

        def trigger_error(error):
            message = 'Expected %s type but got %s' % (
                ValueError, type(error))
            self.assertIsInstance(error, ValueError, message)

        threshold.add_row_error_handler = trigger_error
        add_button.click()

        bool_param = BooleanParameter()
        bool_param.name = 'boolean checkbox'
        bool_param.value = True

        dialog = FunctionOptionsDialog()
        value = dialog.build_widget(dialog.configLayout, 'foo', bool_param)
        widget = dialog.findChild(QCheckBox)

        # initial value must be same with default
        expected_value = True
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setChecked(False)
        expected_value = False
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        dict_param = DictParameter()
        dict_param.name = 'Dictionary tree'
        dict_param.element_type = int
        dict_param.value = {'a': 1, 'b': 2}
        dialog = FunctionOptionsDialog()
        value = dialog.build_widget(dialog.configLayout, 'foo', dict_param)
        widget = dialog.findChild(QTreeWidget)

        # initial value must be same with default
        expected_value = {'a': 1, 'b': 2}
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = {'a': 2, 'b': 1}
        # get tree items
        tree_items = widget.invisibleRootItem()
        # set the input
        tree_items.child(0).setText(1, str(2))
        tree_items.child(1).setText(1, str(1))
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)
예제 #11
0
class StepFcParams(WizardStep, FORM_CLASS):
    """Function Centric Wizard Step: Parameters"""

    def __init__(self, parent=None):
        """Constructor for the tab.

        :param parent: parent - widget to use as parent (Wizad Dialog).
        :type parent: QWidget

        """
        WizardStep.__init__(self, parent)
        self.parameter_dialog = None
        self.twParams = None

    def is_ready_to_next_step(self):
        """Check if the step is complete. If so, there is
            no reason to block the Next button.

        :returns: True if new step may be enabled.
        :rtype: bool
        """
        return True

    def get_previous_step(self):
        """Find the proper step when user clicks the Previous button.

        :returns: The step to be switched to
        :rtype: WizardStep instance or None
        """
        new_step = self.parent.step_fc_extent
        return new_step

    def get_next_step(self):
        """Find the proper step when user clicks the Next button.

        :returns: The step to be switched to
        :rtype: WizardStep instance or None
        """
        new_step = self.parent.step_fc_summary
        return new_step

    def set_widgets(self):
        """Set widgets on the Params tab"""

        # TODO Put the params to metadata! Now we need to import the IF class.
        # Notes: Why don't we store impact_function to class attribute?
        impact_function_id = self.parent.\
            step_fc_function.selected_function()['id']
        impact_function = self.impact_function_manager.get(
            impact_function_id)
        if not impact_function:
            return
        if_params = None
        if hasattr(impact_function, 'parameters'):
            if_params = impact_function.parameters

        text = self.tr(
            'Please set impact functions parameters.<br/>Parameters for '
            'impact function "%s" that can be modified are:' %
            impact_function_id)
        self.lblSelectIFParameters.setText(text)

        self.parameter_dialog = FunctionOptionsDialog(self)
        self.parameter_dialog.set_dialog_info(impact_function_id)
        self.parameter_dialog.build_form(if_params)

        if self.twParams:
            self.twParams.hide()

        self.twParams = self.parameter_dialog.tabWidget
        self.layoutIFParams.addWidget(self.twParams)
예제 #12
0
    def test_build_widget(self):
        dialog = FunctionOptionsDialog()

        # Define threshold
        threshold = InputListParameter()
        threshold.name = 'Thresholds [m]'
        threshold.is_required = True
        threshold.element_type = float
        threshold.expected_type = list
        threshold.ordering = InputListParameter.AscendingOrder
        threshold.minimum_item_count = 1
        threshold.maximum_item_count = 3
        threshold.value = [2.3]  # default value

        value = dialog.build_widget(dialog.configLayout, 'foo', threshold)
        widget = dialog.findChild(QLineEdit)
        add_button = dialog.findChildren(QPushButton)[0]
        remove_button = dialog.findChildren(QPushButton)[1]
        list_widget = dialog.findChild(QListWidget)

        # initial value must be same with default
        expected_value = [2.3]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        # change to 5.9
        # select 2.3 list item
        self.click_list_widget_item(list_widget, '2.3')
        # remove 2.3 list item
        remove_button.click()
        # typing 5.9
        widget.setText('5.9')
        # add it to list
        add_button.click()
        expected_value = [5.9]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        # add 70
        widget.setText('70')
        # add it to list
        add_button.click()
        expected_value = [5.9, 70]
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setText('bar')
        self.assertEqual('bar', widget.text())

        def trigger_error(error):
            message = 'Expected %s type but got %s' % (ValueError, type(error))
            self.assertIsInstance(error, ValueError, message)

        threshold.add_row_error_handler = trigger_error
        add_button.click()

        bool_param = BooleanParameter()
        bool_param.name = 'boolean checkbox'
        bool_param.value = True

        dialog = FunctionOptionsDialog()
        value = dialog.build_widget(dialog.configLayout, 'foo', bool_param)
        widget = dialog.findChild(QCheckBox)

        # initial value must be same with default
        expected_value = True
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        widget.setChecked(False)
        expected_value = False
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        dict_param = DictParameter()
        dict_param.name = 'Dictionary tree'
        dict_param.element_type = int
        dict_param.value = {'a': 1, 'b': 2}
        dialog = FunctionOptionsDialog()
        value = dialog.build_widget(dialog.configLayout, 'foo', dict_param)
        widget = dialog.findChild(QTreeWidget)

        # initial value must be same with default
        expected_value = {'a': 1, 'b': 2}
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)

        expected_value = {'a': 2, 'b': 1}
        # get tree items
        tree_items = widget.invisibleRootItem()
        # set the input
        tree_items.child(0).setText(1, str(2))
        tree_items.child(1).setText(1, str(1))
        real_value = value().value
        message = 'Expected %s but got %s' % (expected_value, real_value)
        self.assertEqual(expected_value, real_value, message)