def test_inference(self): # perfect annotation, check that inferred label is correct nclasses, nannotators, nitems = 3, 5, 50*8 # create random model (this is our ground truth model) gamma = np.ones((nclasses,)) / float(nclasses) theta = np.ones((8,)) * 0.999 true_model = ModelBt(nclasses, nannotators, gamma, theta) # create random data labels = true_model.generate_labels(nitems) annotations = true_model.generate_annotations_from_labels(labels) posterior = true_model.infer_labels(annotations) testing.assert_allclose(posterior.sum(1), 1., atol=1e-6, rtol=0.) inferred = posterior.argmax(1) testing.assert_equal(inferred, labels) self.assertTrue(np.all(posterior[np.arange(nitems),inferred] > 0.999)) # at chance annotation, disagreeing annotators: get back prior gamma = ModelBt._random_gamma(nclasses) theta = np.ones((nannotators,)) / float(nclasses) model = ModelBt(nclasses, nannotators, gamma, theta) data = np.array([[MV, 0, 1, 2, MV]]) testing.assert_almost_equal(np.squeeze(model.infer_labels(data)), model.gamma, 6)
def test_inference(self): # perfect annotation, check that inferred label is correct nclasses, nannotators, nitems = 3, 5, 50 * 8 # create random model (this is our ground truth model) gamma = np.ones((nclasses, )) / float(nclasses) theta = np.ones((8, )) * 0.999 true_model = ModelBt(nclasses, nannotators, gamma, theta) # create random data labels = true_model.generate_labels(nitems) annotations = true_model.generate_annotations_from_labels(labels) posterior = true_model.infer_labels(annotations) testing.assert_allclose(posterior.sum(1), 1., atol=1e-6, rtol=0.) inferred = posterior.argmax(1) testing.assert_equal(inferred, labels) self.assertTrue(np.all(posterior[np.arange(nitems), inferred] > 0.999)) # at chance annotation, disagreeing annotators: get back prior gamma = ModelBt._random_gamma(nclasses) theta = np.ones((nannotators, )) / float(nclasses) model = ModelBt(nclasses, nannotators, gamma, theta) data = np.array([[MV, 0, 1, 2, MV]]) testing.assert_almost_equal(np.squeeze(model.infer_labels(data)), model.gamma, 6)