def test_simple_openml_source(self) -> None: #this test requires interet acess to download the data ExecutionContext.FileCache = NoneCache() simulation = ClassificationSimulation(*OpenmlClassificationSource(1116).read()) #simulation = ClassificationSimulation.from_source(OpenmlSource(273)) self.assertEqual(len(simulation.interactions), 6598) for rnd in simulation.interactions: hash(rnd.context) #make sure these are hashable hash(rnd.actions[0]) #make sure these are hashable hash(rnd.actions[1]) #make sure these are hashable self.assertEqual(len(cast(Tuple,rnd.context)), 268) self.assertIn((1,0), rnd.actions) self.assertIn((0,1), rnd.actions) self.assertEqual(len(rnd.actions),2) actual_rewards = simulation.reward(_choices(rnd)) self.assertIn(1, actual_rewards) self.assertIn(0, actual_rewards)
def test_large_from_openml(self) -> None: #this test requires interet acess to download the data ExecutionContext.FileCache = MemoryCache() OpenmlClassificationSource(154).read() #this will cause it to read and cache in memory so we don't measure read time time = min(timeit.repeat(lambda:ClassificationSimulation(*OpenmlClassificationSource(154).read()), repeat=1, number=1)) print(time) #with caching took approximately 17 seconds to encode self.assertLess(time, 30)
def test_sparse(self) -> None: feature_rows = [((0, 1), (10, 11)), ((1, 2), (20, 30)), ((2, 3), (30, 40)), ((2, 3), (30, 40))] label_column = (1, 1, 0, 2) simulation = ClassificationSimulation(feature_rows, label_column) interactions = list(simulation.read()) self.assertEqual(dict(zip(*feature_rows[0])), interactions[0].context) self.assertEqual(dict(zip(*feature_rows[1])), interactions[1].context) self.assertEqual(dict(zip(*feature_rows[2])), interactions[2].context) self.assertEqual(dict(zip(*feature_rows[3])), interactions[3].context) self.assertEqual([1, 0, 2], interactions[0].actions) self.assertEqual([1, 0, 2], interactions[1].actions) self.assertEqual([1, 0, 2], interactions[2].actions) self.assertEqual([1, 0, 2], interactions[3].actions) self.assertEqual([1, 0, 0], interactions[0].feedbacks) self.assertEqual([1, 0, 0], interactions[1].feedbacks) self.assertEqual([0, 1, 0], interactions[2].feedbacks) self.assertEqual([0, 0, 1], interactions[3].feedbacks)
def test_constructor_with_too_few_labels(self) -> None: with self.assertRaises(AssertionError): ClassificationSimulation([1, 1], [1])
def test_constructor_with_good_features_and_labels3(self) -> None: features = [(1, 2), (3, 4)] labels = ["good", "bad"] simulation = ClassificationSimulation(features, labels) self.assert_simulation_for_data(simulation, features, labels)
def test_constructor_with_good_features_and_labels1(self) -> None: features = [1, 2, 3, 4] labels = [1, 1, 0, 0] simulation = ClassificationSimulation(features, labels) self.assert_simulation_for_data(simulation, features, labels)