Example #1
0
 def test_max_label_zero_confidence(self):
     """
     Test that if there *are* classifications but the maximum conf is 0 that
     at least one label is returned.
     """
     # Mock classification element instance
     e = mock.MagicMock(spec_set=ClassificationElement)
     e.get_classification.return_value = {'a': 0.0}
     ClassificationElement.max_label(e)
Example #2
0
    def test_set_empty_input(self):
        """
        Test that calling ``set_classification`` with an empty dictionary
        raises a ValueError.
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        with pytest.raises(ValueError,
                           match=r"No classification labels/values given\."):
            ClassificationElement.set_classification(e, {})
Example #3
0
    def test_set_no_input(self):
        """
        Test that calling ``set_classification`` with default arguments raises
        a ValueError.
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        with pytest.raises(ValueError,
                           match="No classification labels/values "
                           "given\."):
            ClassificationElement.set_classification(e)
Example #4
0
    def test_setstate(self):
        """
        Test that the expected state representation correctly sets
        :return:
        """
        expected_typename = 'test type name'
        expected_uuid = ' test uuid;'
        given_state = (expected_typename, expected_uuid)

        #: :type: ClassificationElement
        inst = mock.MagicMock(spec_set=ClassificationElement)
        ClassificationElement.__setstate__(inst, given_state)

        assert inst.type_name == expected_typename
        assert inst.uuid == expected_uuid
Example #5
0
    def from_config(cls, config_dict, merge_default=True):
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        This method should not be called via super unless and instance of the
        class is desired.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: ClassificationElementFactory

        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        ce_type, ce_conf = cls_conf_from_config_dict(
            config_dict,  ClassificationElement.get_impls()
        )
        return ClassificationElementFactory(ce_type, ce_conf)
Example #6
0
    def test_from_config_preseeded_mdTrue(self, m_confFromConfig):
        """
        Test that parameters provided at runtime prevails over any provided
        through a given config.
        """
        given_conf = {
            "type_name": "should not get through",
            "uuid": "should not get through",
        }
        expected_typename = 'actually expected typename'
        expected_uuid = 'actually expected uuid'
        expected_conf = {
            'type_name': expected_typename,
            'uuid': expected_uuid,
        }
        expected_return = 'sim return from parent'
        m_confFromConfig.return_value = expected_return

        r = ClassificationElement.from_config(given_conf,
                                              expected_typename,
                                              expected_uuid,
                                              merge_default=True)

        m_confFromConfig.assert_called_once_with(expected_conf,
                                                 merge_default=True)
        assert r == expected_return
Example #7
0
 def test_get_default_config(self):
     """
     Test that the default configuration does not include the runtime
     specific parameters.
     """
     # Shows that override in ClassificationElement removes those
     # runtime-specific parameters.
     default = ClassificationElement.get_default_config()
     assert 'type_name' not in default
     assert 'uuid' not in default
Example #8
0
    def test_set_kwargs(self):
        """
        Test that passing a keyword arguments to ``set_classification`` returns
        the appropriately normalized dictionary.
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        expected_v = {'a': 1, 'b': 0}
        actual_v = ClassificationElement.set_classification(e, a=1, b=0)
        assert actual_v == expected_v
Example #9
0
    def test_set_input_dict(self):
        """
        Test that passing a dictionary to ``set_classification`` returns the
        appropriately normalized dictionary.
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        expected_v = {1: 0, 2: 1}
        actual_v = ClassificationElement.set_classification(e, expected_v)
        assert actual_v == expected_v
Example #10
0
    def test_max_label(self):
        """
        Test that max_label correctly returns the label key associated with
        the greatest associated confidence value.
        """
        # Mock classification element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)
        e.get_classification.return_value = {1: 0, 2: 1, 3: 0.5}

        expected_label = 2
        actual_label = ClassificationElement.max_label(e)
        self.assertEqual(actual_label, expected_label)
Example #11
0
    def get_default_config(cls):
        """
        Generate and return a default configuration dictionary for this class.
        This will be primarily used for generating what the configuration
        dictionary would look like for this class without instantiating it.

        It is not be guaranteed that the configuration dictionary returned
        from this method is valid for construction of an instance of this class.

        :return: Default configuration dictionary for the class.
        :rtype: dict

        """
        return make_default_config(ClassificationElement.get_impls())
Example #12
0
    def test_getstate(self):
        """
        Test the expected state representation of the abstract base class.
        """
        expected_typename = 'test type name'
        expected_uuid = ' test uuid;'
        expected_state = (expected_typename, expected_uuid)

        #: :type: ClassificationElement
        inst = mock.MagicMock(spec_set=ClassificationElement)
        inst.type_name = expected_typename
        inst.uuid = expected_uuid
        actual_state = ClassificationElement.__getstate__(inst)

        assert actual_state == expected_state
Example #13
0
    def test_set_mixed(self):
        """
        Test that passing mixed dictionary and keyword arguments to
        ``set_classification`` returns the appropriately normalize dictionary.
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        expected_v = {'a': .25, 1: .25, 'b': .25, 'd': .25}
        actual_v = ClassificationElement.set_classification(e, {
            'a': .25,
            1: .25
        },
                                                            b=.25,
                                                            d=.25)
        assert actual_v == expected_v
Example #14
0
def default_config():
    return {
        "utility": {
            "classify_overwrite": False,
            "parallel": {
                "use_multiprocessing": False,
                "index_extraction_cores": None,
                "classification_cores": None,
            }
        },
        "plugins": {
            "classifier":
            make_default_config(Classifier.get_impls()),
            "classification_factory":
            make_default_config(ClassificationElement.get_impls()),
            "descriptor_set":
            make_default_config(DescriptorSet.get_impls()),
        }
    }
Example #15
0
    def test_set_nonstandard(self):
        """
        Test that setting a set of label/confidence pairs where the
        confidence sums to greater than 1.0 is acceptable and returns the
        appropriately normalized dictionary.

        Many classifiers output 1-sum confidence values, but not all (e.g.
        CNN final layers like AlexNet).
        """
        # Mock element instance
        #: :type: ClassificationElement
        e = mock.MagicMock(spec_set=ClassificationElement)

        expected_v = {'a': 1, 1: 1, 'b': 1, 'd': 1}
        actual_v = ClassificationElement.set_classification(e, {
            'a': 1,
            1: 1
        },
                                                            b=1,
                                                            d=1)
        assert actual_v == expected_v
Example #16
0
    def test_from_config_mdTrue(self, m_confFromConfig):
        """
        Test that ``from_config`` appropriately passes runtime provided
        parameters.
        """
        given_conf = {}
        expected_typename = 'ex typename'
        expected_uuid = 'ex uuid'
        expected_conf = {
            'type_name': expected_typename,
            'uuid': expected_uuid,
        }
        expected_return = 'sim return from parent'

        m_confFromConfig.return_value = expected_return

        r = ClassificationElement.from_config(given_conf,
                                              expected_typename,
                                              expected_uuid,
                                              merge_default=True)

        m_confFromConfig.assert_called_once_with(expected_conf,
                                                 merge_default=True)
        assert r == expected_return