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_batch_handler(simple_model):
m = simple_model
computation_context = elfi.ComputationContext(seed=123, batch_size=10)
batches = elfi.client.BatchHandler(m, computation_context, 'k2')
batches.submit()
out0, i0 = batches.wait_next()
batches.submit()
out1, i1 = batches.wait_next()
batches.reset()
batches.submit()
out0_, i0_ = batches.wait_next()
assert i0 == 0
assert i1 == 1
assert i0_ == 0
assert np.array_equal(out0['k2'], out0_['k2'])
assert not np.array_equal(out0['k2'], out1['k2'])
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
import logging
import ipyparallel as ipp
import elfi.client
logger = logging.getLogger(__name__)
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
class Client(elfi.client.ClientBase):
"""A multiprocessing client using ipyparallel.
http://ipyparallel.readthedocs.io
"""
def __init__(self, ipp_client=None, **kwargs):
"""Create an ipyparallel client for ELFI.
Parameters
----------
ipp_client : ipyparallel.Client, optional
Use this ipyparallel client with ELFI.
"""
self.ipp_client = ipp_client or ipp.Client(**kwargs)
self.view = self.ipp_client.load_balanced_view()
pool : OutputPool, optional
OutputPool both stores and provides precomputed values for batches.
max_parallel_batches : int, optional
Maximum number of batches allowed to be in computation at the same time.
Defaults to number of cores in the client
"""
model = model.model if isinstance(model, NodeReference) else model
if not model.parameter_names:
raise ValueError('Model {} defines no parameters'.format(model))
self.model = model.copy()
self.output_names = self._check_outputs(output_names)
self.client = elfi.client.get_client()
# Prepare the computation_context
context = ComputationContext(batch_size=batch_size, seed=seed, pool=pool)
self.batches = elfi.client.BatchHandler(
self.model, context=context, output_names=output_names, client=self.client)
self.computation_context = context
self.max_parallel_batches = max_parallel_batches or self.client.num_cores
if self.max_parallel_batches <= 0:
msg = 'Value for max_parallel_batches ({}) must be at least one.'.format(
self.max_parallel_batches)
if self.client.num_cores == 0:
msg += ' Client has currently no workers available. Please make sure ' \
'the cluster has fully started or set the max_parallel_batches ' \
'parameter by hand.'
raise ValueError(msg)
import itertools
import logging
import elfi.client
logger = logging.getLogger(__name__)
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
class Client(elfi.client.ClientBase):
"""Simple non-parallel client.
Responsible for sending computational graphs to be executed in an Executor
"""
def __init__(self, **kwargs):
"""Create a native client."""
self.tasks = {}
self._ids = itertools.count()
def apply(self, kallable, *args, **kwargs):
"""Add `kallable(*args, **kwargs)` to the queue of tasks. Returns immediately.
Parameters
----------
kallable : callable
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)
Defaults to number of cores in the client
"""
model = model.model if isinstance(model, NodeReference) else model
if not model.parameter_names:
raise ValueError('Model {} defines no parameters'.format(model))
self.model = model.copy()
self.output_names = self._check_outputs(output_names)
self.client = elfi.client.get_client()
# Prepare the computation_context
context = ComputationContext(batch_size=batch_size, seed=seed, pool=pool)
self.batches = elfi.client.BatchHandler(
self.model, context=context, output_names=output_names, client=self.client)
self.computation_context = context
self.max_parallel_batches = max_parallel_batches or self.client.num_cores
if self.max_parallel_batches <= 0:
msg = 'Value for max_parallel_batches ({}) must be at least one.'.format(
self.max_parallel_batches)
if self.client.num_cores == 0:
msg += ' Client has currently no workers available. Please make sure ' \
'the cluster has fully started or set the max_parallel_batches ' \
'parameter by hand.'
raise ValueError(msg)
# State and objective should contain all information needed to continue the
# inference after an iteration.
self.state = dict(n_sim=0, n_batches=0)
def set_as_default():
"""Set this as the default client."""
elfi.client.set_client()
elfi.client.set_default_class(Client)