Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(init_full_x, create_db_instance):
"""Test if original experiment contains trial 0"""
experiment = ExperimentView('full_x')
pairs = get_name_value_pairs(experiment.fetch_trials())
assert pairs == ((('/x', 0), ), )
When loading a view the original config is used to configure the experiment, but
this process may modify the config if the version of Oríon is different. This should not be
saved in database.
"""
terrible_message = 'oh no, I have been modified!'
original_configuration = ExperimentView('supernaedo2').configuration
def modified_configuration(self):
mocked_config = copy.deepcopy(original_configuration)
mocked_config['metadata']['datetime'] = terrible_message
return mocked_config
with monkeypatch.context() as m:
m.setattr(Experiment, 'configuration', property(modified_configuration))
exp = ExperimentView('supernaedo2')
# The mock is still in place and overwrites the configuration
assert exp.configuration['metadata']['datetime'] == terrible_message
# The mock is reverted and original config is returned, but modification is still in
# metadata
assert exp.metadata['datetime'] == terrible_message
# Loading again from DB confirms the DB was not overwritten
reloaded_exp = ExperimentView('supernaedo2')
assert reloaded_exp.configuration['metadata']['datetime'] != terrible_message
def test_children_children_fetch_trials(create_db_instance):
"""Test that experiment fetch trials from grand children properly (adapters are muted)"""
experiment_name = 'supernaedo2'
root_name = 'supernaedo2'
leaf_names = ['supernaedo2.3.1']
experiment = ExperimentView(experiment_name)
exp_node = build_trimmed_tree(experiment, root_name, leaf_names)
assert exp_node.item.name == experiment_name
assert exp_node.children[0].children[0].item.name == leaf_names[0]
# 2
# |
# 2.3
# |
# 2.3.1
assert len(list(exp_node.root)) == 3
experiment.connect_to_version_control_tree(exp_node)
for node in exp_node.root:
node.item._experiment.refers['adapter'] = Adapter.build([])
def test_empty_experiment_view(self):
"""Hit user name, but exp_name does not hit the db."""
with pytest.raises(ValueError) as exc_info:
ExperimentView('supernaekei')
assert ("No experiment with given name 'supernaekei' for user 'tsirif'"
in str(exc_info.value))
def test_code_change_noeffect_backward(create_db_instance):
"""Test that all trials pass to parent when code change type is 'noeffect'"""
experiment_name = 'supernaedo2.3.1'
root_name = 'supernaedo2.3.1'
leaf_names = ['supernaedo2.3.1.1']
experiment = ExperimentView(experiment_name)
exp_node = build_trimmed_tree(experiment, root_name, leaf_names)
assert exp_node.item.name == experiment_name
assert exp_node.children[0].item.name == leaf_names[0]
# 2.3
# |
# 2.3.1
assert len(list(exp_node.root)) == 2
experiment.connect_to_version_control_tree(exp_node)
query = {'status': 'completed'}
children_trials = exp_node.children[0].item.fetch_trials(query)
assert len(children_trials) == 1
assert len(exp_node.item.fetch_trials(query)) == 2
def test_existing_experiment_view(self, create_db_instance, exp_config):
"""Hit exp_name + user's name in the db, fetch most recent entry."""
exp = ExperimentView('supernaedo2-dendi')
assert exp._experiment._init_done is False
assert exp._id == exp_config[0][0]['_id']
assert exp.name == exp_config[0][0]['name']
assert exp.configuration['refers'] == exp_config[0][0]['refers']
assert exp.metadata == exp_config[0][0]['metadata']
assert exp.pool_size == exp_config[0][0]['pool_size']
assert exp.max_trials == exp_config[0][0]['max_trials']
assert exp.version == exp_config[0][0]['version']
assert isinstance(exp.refers['adapter'], BaseAdapter)
# TODO: Views are not fully configured until configuration is refactored
# assert exp.algorithms.configuration == exp_config[0][0]['algorithms']
with pytest.raises(AttributeError):
exp.this_is_not_in_config = 5
def test_prior_change_backward(create_db_instance):
"""Test that all encoding are renamed to encoding_layer in parent"""
experiment_name = 'supernaedo2.3'
root_name = 'supernaedo2.3'
leaf_names = ['supernaedo2.3.1']
experiment = ExperimentView(experiment_name)
exp_node = build_trimmed_tree(experiment, root_name, leaf_names)
assert exp_node.item.name == experiment_name
assert exp_node.children[0].item.name == leaf_names[0]
# 2.3
# |
# 2.3.1
assert len(list(exp_node.root)) == 2
experiment.connect_to_version_control_tree(exp_node)
query = {'status': 'completed'}
children_trials = exp_node.children[0].item.fetch_trials(query)
assert len(children_trials) == 2
assert len(exp_node.item.fetch_trials(query)) == 4
def test_algo_change_backward(create_db_instance):
"""Test that all trials pass to parent when algorithm is changed"""
experiment_name = 'supernaedo2.2'
root_name = 'supernaedo2.2'
leaf_names = ['supernaedo2.2.1']
experiment = ExperimentView(experiment_name)
exp_node = build_trimmed_tree(experiment, root_name, leaf_names)
assert exp_node.item.name == experiment_name
assert exp_node.children[0].item.name == leaf_names[0]
# 2.2
# |
# 2.2.1
assert len(list(exp_node.root)) == 2
experiment.connect_to_version_control_tree(exp_node)
query = {'status': 'completed'}
children_trials = exp_node.children[0].item.fetch_trials(query)
assert len(children_trials) == 1
assert len(exp_node.item.fetch_trials(query)) == 1
def test_full_forward_full_backward(create_db_instance):
"""Test that trials are adapted properly forward from parent and backward from leafs"""
experiment_name = 'supernaedo2.3'
root_name = 'supernaedo2'
leaf_names = []
experiment = ExperimentView(experiment_name)
exp_node = build_trimmed_tree(experiment, root_name, leaf_names)
assert exp_node.item.name == experiment_name
assert exp_node.parent.name == root_name
assert exp_node.children[0].item.name == 'supernaedo2.3.1'
assert exp_node.children[0].children[0].item.name == 'supernaedo2.3.1.1'
assert exp_node.children[0].children[1].item.name == 'supernaedo2.3.1.2'
assert exp_node.children[0].children[2].item.name == 'supernaedo2.3.1.3'
# 2
# |
# 2.3
# |
# 2.3.1
# | \ \
# 2.3.1.1 2.3.1.2 2.3.1.3
assert len(list(exp_node.root)) == 6
Version to select. If None, last version will be selected. If version given is larger than
largest version available, the largest version will be selected.
"""
db_config = fetch_config_from_db(name, version)
if not db_config:
message = ("No experiment with given name '%s' and version '%s' inside database, "
"no view can be created." % (name, version if version else '*'))
raise ValueError(message)
db_config.setdefault('version', 1)
experiment = create_experiment(**db_config)
return ExperimentView(experiment)