def test_expand_single_kernel_mutli_d(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=3) # Expand SE se = grammar.base_kernels[0] expected_kernels = [ self.se0 + self.se0, self.se0 * self.se0, self.se0 + self.se1, self.se0 * self.se1, self.se0 + self.se2, self.se0 * self.se2, self.se0 + self.rq0, self.se0 * self.rq0, self.se0 + self.rq1, self.se0 * self.rq1, self.se0 + self.rq2, self.se0 * self.rq2, ] new_kernels = grammar.expand_single_kernel(se) self.assertIsInstance(new_kernels, list) self.assertEqual(len(expected_kernels), len(new_kernels)) for expected_cov, actual_cov in zip(expected_kernels, new_kernels): self.assertEqual(expected_cov.infix, actual_cov.infix)
def test_expand(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) scored_kernel = GPModel(self.se0) scored_kernel.score = 1 result = grammar.expand([scored_kernel]) self.assertIsInstance(result, list)
def test_create_grammar_default_base_kern_names_one_d(self): dim = 1 grammar = CKSGrammar() grammar.build(dim) expected = ['SE', 'RQ', 'LIN', 'PER'] actual = grammar.base_kernel_names self.assertListEqual(expected, actual)
def test_expand_full_brute_force_level_1(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) n = len(grammar.base_kernel_names) n_dim = grammar.n_dims max_number_of_models = 1000 level = 1 kernels = grammar.expand_full_brute_force(level, max_number_of_models) expected = (n * n_dim + 1) * n * n_dim self.assertEqual(expected, len(kernels))
def test_create_grammar_default_base_kern_names_multi_d(self): expected = ['SE', 'RQ'] test_cases = ( (expected, 2), (expected, 3), (expected, 10), ) for expected_base_kern_names, n_dim in test_cases: with self.subTest(n_dims=n_dim): grammar = CKSGrammar() grammar.build(n_dim) actual = grammar.base_kernel_names self.assertListEqual(expected_base_kern_names, actual)
def test_create_grammar(self): base_kernel_names = ['SE', 'RQ'] dim = 1 # Test with base kernel names and dimension arguments grammar = CKSGrammar(base_kernel_names=base_kernel_names) grammar.build(dim) self.assertEqual(base_kernel_names, grammar.base_kernel_names) self.assertEqual(len(base_kernel_names), len(grammar.base_kernel_names)) self.assertEqual(dim, grammar.n_dims) self.assertIsInstance(grammar.hyperpriors, HyperpriorMap) self.assertEqual(self.se0.infix, grammar.base_kernels[0].infix) self.assertEqual(self.rq0.infix, grammar.base_kernels[1].infix) self.assertNotEqual(self.se0.infix, grammar.base_kernels[1].infix)
def test_from_dict(self): test_cases = (BaseGrammar, CKSGrammar, Serializable) for cls in test_cases: with self.subTest(name=cls.__name__): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ', 'LIN']) grammar.build(n_dims=1) actual = cls.from_dict(grammar.to_dict()) self.assertIsInstance(actual, CKSGrammar) self.assertEqual(grammar.operators, actual.operators) self.assertEqual(grammar.base_kernel_names, actual.base_kernel_names) self.assertEqual(grammar.hyperpriors.__class__, actual.hyperpriors.__class__) self.assertEqual(grammar.n_dims, actual.n_dims) for expected_cov, actual_cov in zip(grammar.base_kernels, actual.base_kernels): self.assertEqual(expected_cov.infix, actual_cov.infix) self.assertEqual(grammar.built, actual.built)
def test_mask_kernels_multi_d(self): base_kernel_names = ['SE', 'RQ'] dim = 3 # Test with base kernel names and dimension arguments grammar = CKSGrammar(base_kernel_names=base_kernel_names) grammar.build(dim) expected_kernels = [ self.se0, self.se1, self.se2, self.rq0, self.rq1, self.rq2, ] for expected_kernel, actual_kernel in zip(expected_kernels, grammar.base_kernels): self.assertEqual(expected_kernel.infix, actual_kernel.infix)
def test_expand_single_kernel_two_dims(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) # first, tests 1d expansion of base kernel k = self.se0 expected_kernels = grammar.expand_single_kernel(k) new_kernels = [ self.se0 + self.se0, self.se0 + self.rq0, self.se0 + self.se1, self.se0 + self.rq1, self.se0 * self.se0, self.se0 * self.rq0, self.se0 * self.se1, self.se0 * self.rq1 ] self.assertIsInstance(new_kernels, list) self.assertEqual(len(expected_kernels), len(new_kernels)) new_kernels_infixes = [k.infix for k in new_kernels] expected_infixes = [k.infix for k in expected_kernels] self.assertCountEqual(expected_infixes, new_kernels_infixes) # tests combination kernel expansion k = self.se1 * self.rq1 new_kernels = grammar.expand_single_kernel(k) expected_kernels = [ self.se1 * self.rq1 + self.se0, self.se1 * self.rq1 + self.rq0, self.se1 * self.rq1 + self.se1, self.se1 * self.rq1 + self.rq1, self.se1 * self.rq1 * self.se0, self.se1 * self.rq1 * self.rq0, self.se1 * self.rq1 * self.se1, self.se1 * self.rq1 * self.rq1 ] new_kernels_infixes = [k.infix for k in new_kernels] expected_infixes = [k.infix for k in expected_kernels] self.assertCountEqual(expected_infixes, new_kernels_infixes)
def test_expand_one_dim(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ', 'LIN']) grammar.build(n_dims=1) # Expand SE se = grammar.base_kernels[0] expected_kernels = [ self.se0 + self.se0, self.se0 * self.se0, self.se0 + self.rq0, self.se0 * self.rq0, self.se0 + self.lin0, self.se0 * self.lin0, ] new_kernels = grammar.expand_single_kernel(se) self.assertIsInstance(new_kernels, list) self.assertEqual(len(expected_kernels), len(new_kernels)) for expected_cov, actual_cov in zip(expected_kernels, new_kernels): self.assertEqual(expected_cov.infix, actual_cov.infix) # Expand SE + RQ se_plus_rq = self.se0 + self.rq0 expected_kernels = [ (self.se0 + self.rq0) + self.se0, (self.se0 + self.rq0) * self.se0, (self.se0 + self.rq0) + self.rq0, (self.se0 + self.rq0) * self.rq0, (self.se0 + self.rq0) + self.lin0, (self.se0 + self.rq0) * self.lin0, ] new_kernels = grammar.expand_single_kernel(se_plus_rq) self.assertIsInstance(new_kernels, list) self.assertEqual(len(expected_kernels), len(new_kernels)) for expected_cov, actual_cov in zip(expected_kernels, new_kernels): self.assertEqual(expected_cov.infix, actual_cov.infix)
def test_to_dict(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ', 'LIN']) grammar.build(n_dims=1) actual = grammar.to_dict() self.assertIsInstance(actual, dict) self.assertIn('operators', actual) self.assertIn('base_kernel_names', actual) self.assertIn('hyperpriors', actual) self.assertIn('n_dims', actual) self.assertIn('base_kernels', actual) self.assertIn('built', actual) self.assertEqual(grammar.operators, actual['operators']) self.assertEqual(grammar.base_kernel_names, actual['base_kernel_names']) self.assertEqual(grammar.hyperpriors.to_dict(), actual['hyperpriors']) self.assertEqual(grammar.n_dims, actual['n_dims']) self.assertEqual([b.to_dict() for b in grammar.base_kernels], actual['base_kernels']) self.assertEqual(grammar.built, actual['built'])
def __init__(self, grammar: Optional[CKSGrammar] = None, base_kernel_names: Optional[List[str]] = None, fitness_fn: Union[str, Callable[[RawGPModelType], float]] = 'nbic', n_parents: int = 1, additive_form: bool = False, gp_fn: Union[str, Callable] = 'gp_regression', gp_args: Optional[dict] = None, optimizer: Optional[str] = 'scg', n_restarts_optimizer: int = 3): if grammar is None: grammar = CKSGrammar(base_kernel_names=base_kernel_names) super().__init__(grammar, fitness_fn, n_parents, additive_form, gp_fn, gp_args, optimizer, n_restarts_optimizer)
def test_expand_full_kernel(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) # first, tests 1d expansion of base kernel k = self.se0 new_kernels = grammar.expand_full_kernel(k) expected_kernels = [ self.se0 + self.se0, self.se0 + self.rq0, self.se0 + self.se1, self.se0 + self.rq1, self.se0 * self.se0, self.se0 * self.rq0, self.se0 * self.se1, self.se0 * self.rq1 ] new_kernels_infixes = [k.infix for k in new_kernels] expected_infixes = [k.infix for k in expected_kernels] self.assertCountEqual(expected_infixes, new_kernels_infixes) # tests combination kernel expansion k = self.se1 * self.rq1 new_kernels = grammar.expand_full_kernel(k) expected_kernels = [ self.se1 * self.rq1 + self.se0, self.se1 * self.rq1 + self.rq0, self.se1 * self.rq1 + self.se1, self.se1 * self.rq1 + self.rq1, self.se1 * self.rq1 * self.se0, self.se1 * self.rq1 * self.rq0, self.se1 * self.rq1 * self.se1, self.se1 * self.rq1 * self.rq1, (self.se1 + self.se0) * self.rq1, (self.se1 + self.rq0) * self.rq1, (self.se1 + self.se1) * self.rq1, (self.se1 + self.rq1) * self.rq1, (self.se1 * self.se0) * self.rq1, (self.se1 * self.rq0) * self.rq1, (self.se1 * self.se1) * self.rq1, (self.se1 * self.rq1) * self.rq1, self.se1 * (self.rq1 + self.se0), self.se1 * (self.rq1 + self.rq0), self.se1 * (self.rq1 + self.se1), self.se1 * (self.rq1 + self.rq1), self.se1 * (self.rq1 * self.se0), self.se1 * (self.rq1 * self.rq0), self.se1 * (self.rq1 * self.se1), self.se1 * (self.rq1 * self.rq1), ] new_kernels_infixes = [k.infix for k in new_kernels] expected_infixes = [k.infix for k in expected_kernels] self.assertCountEqual(expected_infixes, new_kernels_infixes)
def test_expand_full_brute_force_level_2_and_3(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) max_number_of_models = 1000 level = 2 kernels = grammar.expand_full_brute_force(level, max_number_of_models) expected = 134 self.assertEqual(expected, len(kernels)) level = 3 kernels = grammar.expand_full_brute_force(level, max_number_of_models) expected = 834 # Question: should this be = max_number_of_models ? self.assertEqual(expected, len(kernels))
def test_get_initial_candidate_covariances(self): grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=2) model_selector = CKSModelSelector(grammar) actual = model_selector._get_initial_candidate_covariances() expected = [self.se0, self.se1, self.rq0, self.rq1] self.assertIsInstance(actual, list) self.assertEqual(len(expected), len(actual)) for expected_cov, actual_cov in zip(expected, actual): self.assertEqual(expected_cov.infix, actual_cov.infix) grammar = CKSGrammar(base_kernel_names=['SE', 'RQ']) grammar.build(n_dims=1) model_selector = CKSModelSelector(grammar) actual = model_selector._get_initial_candidate_covariances() expected = [self.se0, self.rq0] self.assertIsInstance(actual, list) self.assertEqual(len(expected), len(actual)) for expected_cov, actual_cov in zip(expected, actual): self.assertEqual(expected_cov.infix, actual_cov.infix)