Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def storage(self, config=None):
"""Return test storage"""
if config is None:
return get_storage()
try:
config['of_type'] = config.pop('type')
db = Storage(**config)
self.storage_config = config
except SingletonAlreadyInstantiatedError:
db = get_storage()
except KeyError:
print(self.storage_config)
raise
return db
algorithms = {'random': {'seed': None}}
config = dict(name='experiment_test', metadata=metadata, version=1, algorithms=algorithms)
backward.populate_priors(config['metadata'])
get_storage().create_experiment(config)
parent_id = config.pop('_id')
looser = Experiment('experiment_test', version=1)
# Simulate exp2 winning the race condition
config2 = copy.deepcopy(config)
config2['version'] = 2
config2['metadata']['user_args'].append("--y~+normal(0,1)")
backward.populate_priors(config2['metadata'])
config2['refers'] = dict(parent_id=parent_id, root_id=parent_id, adapters=[])
get_storage().create_experiment(config2)
# Now exp3 losses the race condition
config3 = copy.deepcopy(config)
config3['metadata']['user_args'].pop()
config3['metadata']['user_args'].append("--z~+normal(0,1)")
backward.populate_priors(config3['metadata'])
config3['version'] = 1
with pytest.raises(ValueError) as exc_info:
looser.configure(config3)
assert 'Configuration is different and generates a branching' in str(exc_info.value)
def _set_tables(self):
self.trials = []
self.lies = []
for exp in self._experiments:
get_storage().create_experiment(exp)
for trial in self._trials:
nt = get_storage().register_trial(Trial(**trial))
self.trials.append(nt.to_dict())
for lie in self._lies:
nt = get_storage().register_lie(Trial(**lie))
self.lies.append(nt.to_dict())
def test_race_condition_wout_version(self):
"""Test that an experiment loosing the race condition during version increment raises
RaceCondition if version number was not specified.
"""
user_args = ['--x~normal(0,1)']
metadata = dict(user='tsirif', datetime=datetime.datetime.utcnow(), user_args=user_args)
algorithms = {'random': {'seed': None}}
config = dict(name='experiment_test', metadata=metadata, version=1, algorithms=algorithms)
backward.populate_priors(config['metadata'])
get_storage().create_experiment(config)
parent_id = config.pop('_id')
looser = Experiment('experiment_test', version=1)
# Simulate exp2 winning the race condition
config2 = copy.deepcopy(config)
config2['version'] = 2
config2['metadata']['user_args'].append("--y~+normal(0,1)")
backward.populate_priors(config2['metadata'])
config2['refers'] = dict(parent_id=parent_id, root_id=parent_id, adapters=[])
get_storage().create_experiment(config2)
# Now exp3 losses the race condition
config3 = copy.deepcopy(config)
config3['metadata']['user_args'].pop()
config3['metadata']['user_args'].append("--z~+normal(0,1)")
if not args.get('force'):
action = ''
while action not in ['y', 'yes', 'no', 'n']:
action = ask_question("Do you wish to proceed? (y/N)", "N").lower()
if action in ['no', 'n']:
sys.exit(0)
experiment_builder = ExperimentBuilder()
local_config = experiment_builder.fetch_full_config(args, use_db=False)
local_config['protocol'] = {'type': 'legacy', 'setup': False}
experiment_builder.setup_storage(local_config)
storage = get_storage()
upgrade_db_specifics(storage)
print('Updating documents...')
upgrade_documents(storage)
print('Database upgrade completed successfully')
def _check_for_greater_versions(self):
"""Check if experiment has children"""
# If we made it this far, new_name is actually the name of the parent.
parent = self.conflict.old_config
query = {'name': parent['name'], 'refers.parent_id': parent['_id']}
children = len(get_storage().fetch_experiments(query))
return bool(children)
def fetch_config_from_db(name, version=None):
"""Fetch configuration from database
Parameters
----------
name: str
Name of the experiment to fetch
version: int, optional
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.
"""
configs = get_storage().fetch_experiments({'name': name})
if not configs:
return {}
config = _fetch_config_version(configs, version)
if len(configs) > 1:
log.info("Many versions for experiment %s have been found. Using latest "
"version %s.", name, config['version'])
backward.populate_space(config)
return config
self.refers.setdefault('root_id', self._id)
self.refers.setdefault('adapter', [])
if self.refers['adapter'] and not isinstance(self.refers.get('adapter'), BaseAdapter):
self.refers['adapter'] = Adapter.build(self.refers['adapter'])
# try:
# self._experiment.configure(self._experiment.configuration, enable_branching=False,
# enable_update=False)
# except ValueError as e:
# if "Configuration is different and generates a branching event" in str(e):
# raise RuntimeError(
# "Configuration in the database does not correspond to the one generated by "
# "Experiment object. This is likely due to a backward incompatible update in "
# "Oríon. Please report to https://github.com/epistimio/orion/issues.") from e
# raise
self._experiment._storage = ReadOnlyStorageProtocol(get_storage())
def parent(self):
"""Get parent of the experiment, None if no parent
.. note::
The instantiation of an EVC tree is lazy, which means accessing the parent of a node
may trigger a call to database to build this parent live.
"""
if self._parent is None and self._no_parent_lookup:
self._no_parent_lookup = False
query = {'_id': self.item.refers['parent_id']}
selection = {'name': 1, 'version': 1}
experiments = get_storage().fetch_experiments(query, selection)
if experiments:
parent = experiments[0]
exp_node = ExperimentNode(name=parent['name'], version=parent.get('version', 1))
self.set_parent(exp_node)
return self._parent