Exemplo n.º 1
0
def test_serialize_deserialize_pickle():
    """
    Test that serialization and the deserialization of a
    MemoryDetectionElement instance with populated detection components
    results in a different but equivalent instance.
    """
    expected_uuid = 'some-uuid'
    expected_bbox = AxisAlignedBoundingBox([0, 0], [1, 2])
    expected_ce_map = {'a': .24, 'b': 0.5, 0: .26}
    expected_ce = MemoryClassificationElement('ce-type', expected_uuid)
    expected_ce.set_classification(expected_ce_map)

    e1 = MemoryDetectionElement(expected_uuid)
    e1._bbox = expected_bbox
    e1._classification = expected_ce

    buff = pickle.dumps(e1)

    #: :type: MemoryDetectionElement
    e2 = pickle.loads(buff)
    assert e2 is not e1
    assert e2.uuid == expected_uuid

    e2_bbox, e2_ce = e2.get_detection()
    # Intentionally checking e2_bbox is not the same instance
    assert e2_bbox is not expected_bbox  # lgtm[py/comparison-using-is]
    assert e2_bbox == expected_bbox
    assert e2_ce is not expected_ce
    assert e2_ce == expected_ce
    assert e2_ce.get_classification() == expected_ce_map
Exemplo n.º 2
0
 def test_get_config_nonempty(self):
     """
     Test that configuration returned is empty.
     """
     e = MemoryClassificationElement('test', 0)
     e._c = {'a': 1.0, 'b': 0.0}
     assert e.get_config() == {}
Exemplo n.º 3
0
 def test_has_classification_nonempty(self):
     """
     Test that has_classifications returns true when there is a valid
     internal map.
     """
     e = MemoryClassificationElement('test', 0)
     e._c = {'a': 1, 'b': 0}
     assert e.has_classifications() is True
Exemplo n.º 4
0
    def test_serialization_nonempty(self):
        e = MemoryClassificationElement('test', 0)
        e.set_classification(a=0, b=1)

        expected_map = {'a': 0, 'b': 1}
        assert e._c == expected_map
        e2 = cPickle.loads(cPickle.dumps(e))
        assert e2._c == expected_map
Exemplo n.º 5
0
 def test_get_classification(self):
     """
     Test that a valid classification map is returned from
     """
     expected_map = {'a': 1, 'b': 0}
     e = MemoryClassificationElement('test', 0)
     e._c = expected_map
     assert e.get_classification() == expected_map
Exemplo n.º 6
0
    def test_set_classification(self):
        """
        Test setting valid classification map.
        """
        e = MemoryClassificationElement('test', 0)

        expected_map = {'a': 1, 'b': 0}
        e.set_classification(expected_map)
        assert e._c == expected_map
Exemplo n.º 7
0
 def test_get_classification_empty(self):
     """
     Test that NoClassificationError is raised when there is no or an empty
     classification map set.
     """
     e = MemoryClassificationElement('test', 0)
     e._c = None
     with pytest.raises(NoClassificationError,
                        match="No classification labels/values"):
         e.get_classification()
Exemplo n.º 8
0
    def test_init(self):
        """
        Test that construction sets the appropriate attributes.
        """
        expected_typename = 'ex typename'
        expected_uuid = 'ex uuid'

        #: :type: mock.MagicMock | MemoryClassificationElement
        m = mock.MagicMock(spec_set=MemoryClassificationElement)
        MemoryClassificationElement.__init__(m, expected_typename,
                                             expected_uuid)
        assert hasattr(m, '_c')
        assert m._c is None
        assert hasattr(m, '_c_lock')
        assert isinstance(m._c_lock, type(threading.RLock()))
Exemplo n.º 9
0
    def test_serialization_empty(self):
        e = MemoryClassificationElement('test', 0)
        # Keep it empty.

        expected_map = None
        assert e._c == expected_map
        e2 = cPickle.loads(cPickle.dumps(e))
        assert e2._c == expected_map
Exemplo n.º 10
0
 def test_configuration(self):
     """
     Test standard configuration
     """
     ex_tname = 'test'
     ex_uuid = 0
     inst = MemoryClassificationElement(ex_tname, ex_uuid)
     configuration_test_helper(inst, {'type_name', 'uuid'},
                               (ex_tname, ex_uuid))
Exemplo n.º 11
0
 def test_has_classifications_empty(self):
     """
     Test that has_classifications returns false when the internal map
     has either not been set or is an empty dictionary.
     """
     e = MemoryClassificationElement('test', 0)
     e._c = None
     assert e.has_classifications() is False
     e._c = {}
     assert e.has_classifications() is False
Exemplo n.º 12
0
    def test_classify(self, m_sc_get_impls):
        """
        Test calling the GET /classify endpoint under nominal conditions.
        """
        # Mock classifier instantiation to return stub classifier class
        # instance.
        m_sc_get_impls.return_value = {StubClassifier}

        # Setup descriptor set to have descriptors we test query for
        mock_descriptors = [
            DescriptorMemoryElement('', 'a').set_vector([0.4]),
            DescriptorMemoryElement('', 'b').set_vector([0.5]),
            DescriptorMemoryElement('', 'c').set_vector([0.6]),
        ]
        self.app.descriptor_set.get_many_descriptors = mock.MagicMock(
            return_value=mock_descriptors)
        # Mock stub classifier return
        mock_classifications = [
            MemoryClassificationElement('', 'a'),
            MemoryClassificationElement('', 'b'),
            MemoryClassificationElement('', 'c'),
        ]
        mock_classifications[0].set_classification({
            'positive': 0.6,
            'negative': 0.4
        })
        mock_classifications[1].set_classification({
            'positive': 0.5,
            'negative': 0.5
        })
        mock_classifications[2].set_classification({
            'positive': 0.4,
            'negative': 0.6
        })
        StubClassifier.classify_elements = mock.MagicMock(
            return_value=iter(mock_classifications))

        with self.app.test_client() as tc:
            # Initialize a new session.
            tc.post("/session", data=dict(sid="0"))

            # Setup nominal IQR Session state for classifier generation and
            # application.
            iqr_session = self.app.controller.get_session("0")
            # Mock descriptors on IQR session
            iqr_session.external_positive_descriptors.add(
                DescriptorMemoryElement('', 0).set_vector([0.0]))
            iqr_session.positive_descriptors.add(
                DescriptorMemoryElement('', 1).set_vector([0.1]))
            iqr_session.external_negative_descriptors.add(
                DescriptorMemoryElement('', 2).set_vector([1.0]))
            iqr_session.negative_descriptors.add(
                DescriptorMemoryElement('', 3).set_vector([0.9]))

            #: :type: flask.Response
            r = tc.get('/classify',
                       query_string=dict(sid=0,
                                         uuids=json.dumps(['a', 'b', 'c'])))
            self.assertStatusCode(r, 200)
            # We expect the UIDs returned to be in the same order as input and
            # for the expected classification "positive" probabilities as in
            # the mocked classification results.
            r_json = r.json
            assert r_json['sid'] == '0'
            assert r_json['uuids'] == ['a', 'b', 'c']
            assert r_json['proba'] == [0.6, 0.5, 0.4]
Exemplo n.º 13
0
 def test_get_config_empty(self):
     """
     Test that configuration returned is empty.
     """
     e = MemoryClassificationElement('test', 0)
     assert e.get_config() == {}
Exemplo n.º 14
0
 def test_is_usable(self):
     """
     Test that this implementation is usable (should always be)
     """
     assert MemoryClassificationElement.is_usable()