def test_no_params(self, dei_init): # So we don't break python dei_init.return_value = None test_params = {} factory = DescriptorElementFactory(DummyElementImpl, test_params) expected_type = 'type' expected_uuid = 'uuid' # Should construct a new DEI instance under they hood somewhere r = factory.new_descriptor(expected_type, expected_uuid) ntools.assert_true(dei_init.called) dei_init.assert_called_once_with(expected_type, expected_uuid) ntools.assert_is_instance(r, DummyElementImpl)
def test_with_params(self): v = numpy.random.randint(0, 10, 10) test_params = {'p1': 'some dir', 'vec': v} factory = DescriptorElementFactory(DummyElementImpl, test_params) ex_type = 'type' ex_uuid = 'uuid' ex_args = () ex_kwds = test_params # Should construct a new DEI instance under they hood somewhere r = factory.new_descriptor(ex_type, ex_uuid) ntools.assert_is_instance(r, DummyElementImpl) ntools.assert_equal(r._type_label, ex_type) ntools.assert_equal(r._uuid, ex_uuid) ntools.assert_equal(r.args, ex_args) ntools.assert_equal(r.kwds, ex_kwds)
def test_no_params(self): test_params = {} factory = DescriptorElementFactory(DummyElementImpl, test_params) expected_type = 'type' expected_uuid = 'uuid' expected_args = () expected_kwds = {} # Should construct a new DEI instance under they hood somewhere r = factory.new_descriptor(expected_type, expected_uuid) ntools.assert_is_instance(r, DummyElementImpl) ntools.assert_equal(r._type_label, expected_type) ntools.assert_equal(r._uuid, expected_uuid) ntools.assert_equal(r.args, expected_args) ntools.assert_equal(r.kwds, expected_kwds)
def test_with_params(self, dei_init): # So we don't break python dei_init.return_value = None v = numpy.random.randint(0, 10, 10) test_params = { 'p1': 'some dir', 'vec': v } factory = DescriptorElementFactory(DummyElementImpl, test_params) ex_type = 'type' ex_uuid = 'uuid' # Should construct a new DEI instance under they hood somewhere r = factory.new_descriptor(ex_type, ex_uuid) ntools.assert_true(dei_init.called) dei_init.assert_called_once_with(ex_type, ex_uuid, p1='some dir', vec=v) ntools.assert_is_instance(r, DummyElementImpl)
def test_with_params(self): v = numpy.random.randint(0, 10, 10) test_params = { 'p1': 'some dir', 'vec': v } factory = DescriptorElementFactory(DummyElementImpl, test_params) ex_type = 'type' ex_uuid = 'uuid' ex_args = () ex_kwds = test_params # Should construct a new DEI instance under they hood somewhere r = factory.new_descriptor(ex_type, ex_uuid) ntools.assert_is_instance(r, DummyElementImpl) ntools.assert_equal(r._type_label, ex_type) ntools.assert_equal(r._uuid, ex_uuid) ntools.assert_equal(r.args, ex_args) ntools.assert_equal(r.kwds, ex_kwds)
def test_no_save_model_pickle(self): # Test model preservation across pickling even without model cache # file paths set. classifier = LibSvmClassifier( train_params={ '-t': 0, # linear kernel '-b': 1, # enable probability estimates '-c': 2, # SVM-C parameter C '-q': '', # quite mode }, normalize=None, # DO NOT normalize descriptors ) ntools.assert_true(classifier.svm_model is None) # Empty model should not trigger __LOCAL__ content in pickle ntools.assert_not_in('__LOCAL__', classifier.__getstate__()) _ = cPickle.loads(cPickle.dumps(classifier)) # train arbitrary model (same as ``test_simple_classification``) DIM = 2 N = 1000 POS_LABEL = 'positive' NEG_LABEL = 'negative' d_factory = DescriptorElementFactory(DescriptorMemoryElement, {}) c_factory = ClassificationElementFactory( MemoryClassificationElement, {}) def make_element(argtup): (i, v) = argtup d = d_factory.new_descriptor('test', i) d.set_vector(v) return d # Constructing artificial descriptors x = numpy.random.rand(N, DIM) x_pos = x[x[:, 1] <= 0.45] x_neg = x[x[:, 1] >= 0.55] p = multiprocessing.pool.ThreadPool() d_pos = p.map(make_element, enumerate(x_pos)) d_neg = p.map(make_element, enumerate(x_neg, start=N // 2)) p.close() p.join() # Training classifier.train({POS_LABEL: d_pos, NEG_LABEL: d_neg}) # Test original classifier t_v = numpy.random.rand(DIM) t = d_factory.new_descriptor('query', 0) t.set_vector(t_v) c_expected = classifier.classify(t, c_factory) # Should see __LOCAL__ content in pickle state now p_state = classifier.__getstate__() ntools.assert_in('__LOCAL__', p_state) ntools.assert_in('__LOCAL_LABELS__', p_state) ntools.assert_in('__LOCAL_MODEL__', p_state) ntools.assert_true(len(p_state['__LOCAL_LABELS__']) > 0) ntools.assert_true(len(p_state['__LOCAL_MODEL__']) > 0) # Restored classifier should classify the same test descriptor the # same #: :type: LibSvmClassifier classifier2 = cPickle.loads(cPickle.dumps(classifier)) c_post_pickle = classifier2.classify(t, c_factory) # There may be floating point error, so extract actual confidence # values and check post round c_pp_positive = c_post_pickle[POS_LABEL] c_pp_negative = c_post_pickle[NEG_LABEL] c_e_positive = c_expected[POS_LABEL] c_e_negative = c_expected[NEG_LABEL] ntools.assert_almost_equal(c_e_positive, c_pp_positive, 5) ntools.assert_almost_equal(c_e_negative, c_pp_negative, 5)