Beispiel #1
0
    def test_propose_no_tunables(self):
        # setup
        instance = MagicMock(spec_set=BTBSession)
        instance._tunables = None

        # run
        with self.assertRaises(StopTuning):
            BTBSession.propose(instance)
Beispiel #2
0
    def test_propose_tuner_is_none(self):
        # setup
        instance = MagicMock(spec_set=BTBSession)
        instance._tuners = {'test_tunable': None}
        instance._tunable_names = ['test_tunable']
        instance._normalized_scores = None
        instance._get_next_tunable_name.return_value = 'test_tunable'

        # run
        with self.assertRaises(ValueError):
            BTBSession.propose(instance)
Beispiel #3
0
    def test_propose_normalized_scores_gt_tunable_names(self):
        # setup
        tuner = MagicMock()
        tuner.propose.return_value = 'parameters'

        instance = MagicMock(spec_set=BTBSession)
        instance.proposals = {}
        instance._normalized_scores.__len__.return_value = 1

        instance._selector.select.return_value = 'test_tunable'
        instance._tuners = {'test_tunable': tuner}
        instance._tunables = {'test_tunable': 'test_spec'}
        instance._tunable_names = ['test_tunable']

        instance._make_id.return_value = 1

        # run
        res_name, res_config = BTBSession.propose(instance)

        # assert
        assert res_name == 'test_tunable'
        assert res_config == 'parameters'

        expected_proposals = {
            1: {
                'id': 1,
                'name': 'test_tunable',
                'config': 'parameters'
            }
        }
        assert instance.proposals == expected_proposals

        instance._make_id.assert_called_once_with('test_tunable', 'parameters')
        tuner.propose.assert_called_once_with(1)
Beispiel #4
0
    def test_propose_raise_error(self):
        # setup

        tuner = MagicMock()
        tuner.propose.side_effect = [StopTuning('test')]

        instance = MagicMock(spec_set=BTBSession)
        instance._normalized_scores.__len__.return_value = 1

        instance._selector.select.return_value = 'test_tunable'
        instance._tuners = {'test_tunable': tuner}
        instance._tunables = {'test_tunable': 'test_spec'}
        instance._tunable_names = ['test_tunable']

        instance._make_id.return_value = 1

        # run
        with self.assertRaises(ValueError):
            BTBSession.propose(instance)
Beispiel #5
0
    def test_propose_normalized_scores_lt_tunable_names(self, mock_tunable, mock_isinstance):
        # setup
        mock_tunable.from_dict.return_value.get_defaults.return_value = 'defaults'
        mock_isinstance.return_value = True

        tuner = MagicMock()

        instance = MagicMock(spec_set=BTBSession)
        instance._tuner_class = tuner
        instance.proposals = {}
        instance._normalized_scores.__len__.return_value = 0
        instance._tunables = {'test_tunable': 'test_spec'}
        instance._tunable_names = ['test_tunable']

        instance._make_id.return_value = 1

        # run
        res_name, res_config = BTBSession.propose(instance)

        # assert
        assert res_name == 'test_tunable'
        assert res_config == 'defaults'

        expected_proposals = {
            1: {
                'id': 1,
                'name': 'test_tunable',
                'config': 'defaults'
            }
        }
        assert instance.proposals == expected_proposals

        instance._make_id.assert_called_once_with('test_tunable', 'defaults')
        mock_tunable.from_dict.assert_called_once_with('test_spec')
        tuner.assert_called_once_with(mock_tunable.from_dict.return_value)
        mock_tunable.from_dict.return_value.get_defaults.assert_called_once_with()

        expected_isinstance_calls = [call('test_spec', dict), call('defaults', mock_tunable)]
        mock_isinstance.has_calls(expected_isinstance_calls)
Beispiel #6
0
    def test_propose_tunable_cardinality_eq_one(self, mock_tunable, mock_isinstance):
        # setup
        mock_tunable.from_dict.return_value.cardinality = 1
        mock_tunable.from_dict.return_value.get_defaults.return_value = 'parameters'
        mock_isinstance.return_value = True

        instance = MagicMock(spec_set=BTBSession)
        instance._tuners = {}
        instance._tunable_names = ['test_tunable']
        instance.proposals = {}

        instance._make_id.return_value = 1

        # run
        tunable_name, config = BTBSession.propose(instance)

        # assert
        instance._make_id.assert_called_once_with('test_tunable', 'parameters')
        instance._tuner_class.assert_not_called()

        assert instance._tuners == {'test_tunable': None}
        assert 'test_tunable' == tunable_name
        assert 'parameters' == config