def sample_from_scores(scores, proportion, exclude_list): scores = normalized_scores(scores, exclude_list) np.random.seed() #necessary for mutlithreaded code n_nonzero = len([score for score in scores if score != 0]) size = min(int(len(scores) * proportion), n_nonzero) sample = np.random.choice(len(scores), size, p=scores, replace=False) if len(scores) > 0 else [] for s in sample: assert scores[s] != 0 return sample
def test_normalized_scores(self): """Test normalizing scores""" scores = np.array([0.5, None, 2, 2, -1, -0.5, None]) exclude_list = np.array([2, 1]) norm_scores = normalized_scores(scores, exclude_list) expected = np.array([0.3, 0.0, 0.0, 0.6, 0.0, 0.1, 0.0]) self.assertEquals(len(norm_scores), len(expected)) for i in range(len(norm_scores)): self.assertEqual(norm_scores[i], expected[i])