def test_max_label_zero_confidence(self) -> None: """ 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)
def test_set_empty_input(self) -> None: """ 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, {})
def test_setstate(self) -> None: """ 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
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)
def test_from_config_preseeded_mdTrue( self, m_confFromConfig: mock.MagicMock) -> None: """ 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
def test_from_config_mdTrue(self, m_confFromConfig: mock.MagicMock) -> None: """ Test that ``from_config`` appropriately passes runtime provided parameters. """ given_conf: Dict[str, Any] = {} 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
def test_get_default_config(self) -> None: """ 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
def test_set_kwargs(self) -> None: """ Test that passing a keyword arguments to ``set_classification`` returns the appropriately normalized dictionary. """ # Mock element instance 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
def test_set_input_dict(self) -> None: """ Test that passing a dictionary to ``set_classification`` returns the appropriately normalized dictionary. """ # Mock element instance 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
def test_max_label(self) -> None: """ 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)
def get_default_config(cls) -> Dict[str, Any]: """ 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())
def test_getstate(self) -> None: """ 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
def test_set_mixed(self) -> None: """ 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
def test_set_nonstandard(self) -> None: """ 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