code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
ret = None if not query: query = {} if not body: body = {} query.update(body) # body takes precedence body = query self.send_count += 1 payload = self.get_fetch_request(method, path, body) attempts = 1 max_attempts = self.attempts ...
def fetch(self, method, path, query=None, body=None, timeout=0, **kwargs)
send a Message :param method: string, something like "POST" or "GET" :param path: string, the path part of a uri (eg, /foo/bar) :param body: dict, what you want to send to "method path" :param timeout: integer, how long to wait before failing trying to send
4.086807
4.104345
0.995727
if req_payload.uuid: uuids = set([req_payload.uuid, "CONNECT"]) def callback(res_payload): #pout.v(req_payload, res_payload) #ret = req_payload.uuid == res_payload.uuid or res_payload.uuid == "CONNECT" ret = res_payload.uuid in uui...
def fetch_response(self, req_payload, **kwargs)
payload has been sent, do anything else you need to do (eg, wait for response?) :param req_payload: Payload, the payload sent to the server :returns: Payload, the response payload
4.384504
4.482994
0.97803
# http://stackoverflow.com/a/2257449/5006 def rand_id(size=8, chars=string.ascii_uppercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) payload = rand_id() self.ws.ping(payload) opcode, data = self.recv_raw(timeout, [websocket...
def ping(self, timeout=0, **kwargs)
THIS DOES NOT WORK, UWSGI DOES NOT RESPOND TO PINGS
4.701562
4.659651
1.008994
orig_timeout = self.get_timeout(timeout) timeout = orig_timeout while timeout > 0.0: start = time.time() if not self.connected: self.connect(timeout=timeout, **kwargs) with self.wstimeout(timeout, **kwargs) as timeout: logger.debug('{...
def recv_raw(self, timeout, opcodes, **kwargs)
this is very internal, it will return the raw opcode and data if they match the passed in opcodes
4.087257
4.022437
1.016115
p = Payload(raw) p._body = p.body return p
def get_fetch_response(self, raw)
This just makes the payload instance more HTTPClient like
16.484077
9.530744
1.729569
opcode, data = self.recv_raw(timeout, [websocket.ABNF.OPCODE_TEXT], **kwargs) return self.get_fetch_response(data)
def recv(self, timeout=0, **kwargs)
this will receive data and convert it into a message, really this is more of an internal method, it is used in recv_callback and recv_msg
9.861719
9.134337
1.079632
payload = None timeout = self.get_timeout(**kwargs) full_timeout = timeout while timeout > 0.0: kwargs['timeout'] = timeout start = time.time() payload = self.recv(**kwargs) if callback(payload): break p...
def recv_callback(self, callback, **kwargs)
receive messages and validate them with the callback, if the callback returns True then the message is valid and will be returned, if False then this will try and receive another message until timeout is 0
3.900116
3.711998
1.050678
body = None req = self.request res = self.response rou = self.router con = None controller_info = {} try: controller_info = rou.find(req, res) except IOError as e: logger.warning(str(e), exc_info=True) raise C...
def create_controller(self)
Create a controller to handle the request :returns: Controller, this Controller instance should be able to handle the request
3.695395
3.669464
1.007067
body = None req = self.request res = self.response rou = self.router con = None start = time.time() try: con = self.create_controller() con.call = self self.controller = con if not self.quiet: ...
def handle(self)
Called from the interface to actually handle the request.
5.247037
5.092117
1.030423
req = self.request res = self.response con = self.controller if isinstance(e, CallStop): logger.info(str(e), exc_info=True) res.code = e.code res.add_headers(e.headers) res.body = e.body elif isinstance(e, Redirect): ...
def handle_error(self, e, **kwargs)
if an exception is raised while trying to handle the request it will go through this method This method will set the response body and then also call Controller.handle_error for further customization if the Controller is available :param e: Exception, the error that was raised ...
2.487482
2.471079
1.006638
controller_prefix = self.controller_prefix _module_name_cache = self._module_name_cache if controller_prefix in _module_name_cache: return _module_name_cache[controller_prefix] module = self.get_module(controller_prefix) if hasattr(module, "__path__"): ...
def module_names(self)
get all the modules in the controller_prefix :returns: set, a set of string module names
3.23817
2.923228
1.107738
for modname in self.module_names: module = importlib.import_module(modname) yield module
def modules(self)
Returns an iterator of the actual modules, not just their names :returns: generator, each module under self.controller_prefix
4.190207
4.624832
0.906024
modules = set([prefix]) # https://docs.python.org/2/library/pkgutil.html#pkgutil.iter_modules for module_info in pkgutil.iter_modules([path]): # we want to ignore any "private" modules if module_info[1].startswith('_'): continue module_prefix = "."...
def find_modules(self, path, prefix)
recursive method that will find all the submodules of the given module at prefix with path
2.280946
2.247857
1.01472
controller_prefix = self.controller_prefix cset = self.module_names module_name = controller_prefix mod_name = module_name while path_args: mod_name += "." + path_args[0] if mod_name in cset: module_name = mod_name ...
def get_module_name(self, path_args)
returns the module_name and remaining path args. return -- tuple -- (module_name, path_args)
3.024458
2.975533
1.016443
# let's get the class class_object = getattr(module, class_name, None) if not class_object or not issubclass(class_object, Controller): class_object = None return class_object
def get_class(self, module, class_name)
try and get the class_name from the module and make sure it is a valid controller
3.609495
3.080774
1.171619
if not self.cors: raise CallError(405) req = self.request origin = req.get_header('origin') if not origin: raise CallError(400, 'Need Origin header') call_headers = [ ('Access-Control-Request-Headers', 'Access-Control-Allow-Headers'...
def OPTIONS(self, *args, **kwargs)
Handles CORS requests for this controller if self.cors is False then this will raise a 405, otherwise it sets everything necessary to satisfy the request in self.response
2.449525
2.3049
1.062747
if not self.cors: return req = self.request origin = req.get_header('origin') if origin: self.response.set_header('Access-Control-Allow-Origin', origin)
def set_cors_common_headers(self)
This will set the headers that are needed for any cors request (OPTIONS or real)
3.212393
2.963929
1.083829
req = self.request res = self.response res.set_header('Content-Type', "{};charset={}".format( self.content_type, self.encoding )) encoding = req.accept_encoding res.encoding = encoding if encoding else self.encoding res_method_na...
def handle(self, *controller_args, **controller_kwargs)
handles the request and returns the response This should set any response information directly onto self.response this method has the same signature as the request handling methods (eg, GET, POST) so subclasses can override this method and add decorators :param *controller_args: tuple...
2.773232
2.806272
0.988226
methods = [] req = self.request method_name = req.method.upper() method_names = set() members = inspect.getmembers(self) for member_name, member in members: if member_name.startswith(method_name): if member: method...
def find_methods(self)
Find the methods that could satisfy this request This will go through and find any method that starts with the request.method, so if the request was GET /foo then this would find any methods that start with GET https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html :returns: l...
3.915884
3.851302
1.016769
req = self.request args = req.controller_info["method_args"] kwargs = req.controller_info["method_kwargs"] return args, kwargs
def find_method_params(self)
Return the method params :returns: tuple (args, kwargs) that will be passed as *args, **kwargs
6.267048
5.975758
1.048745
if not logger.isEnabledFor(logging.INFO): return try: req = self.request logger.info("REQUEST {} {}?{}".format(req.method, req.path, req.query)) logger.info(datetime.datetime.strftime(datetime.datetime.utcnow(), "DATE %Y-%m-%dT%H:%M:%S.%f")) ip...
def log_start(self, start)
log all the headers and stuff at the start of the request
3.12495
2.952615
1.058367
if not logger.isEnabledFor(logging.INFO): return stop = time.time() get_elapsed = lambda start, stop, multiplier, rnd: round(abs(stop - start) * float(multiplier), rnd) elapsed = get_elapsed(start, stop, 1000.00, 1) total = "%0.1f ms" % (elapsed) logger.info("RE...
def log_stop(self, start)
log a summary line on how the request went
4.876401
4.619651
1.055578
# Open with open(path, 'rb') as yaml_definition: definition = yaml.load(yaml_definition) # Validate schema try: validate_schema(definition) except SchemaError as exc: raise LaneSchemaError(**exc.__dict__) def build(lb_def, branch=False): init_kwarg...
def build_lane_from_yaml(path)
Builds a `sparklanes.Lane` object from a YAML definition file. Parameters ---------- path: str Path to the YAML definition file Returns ------- Lane Lane, built according to definition in YAML file
2.828614
2.869608
0.985714
if not isclass(cls) or not issubclass(cls, LaneTask): raise TypeError('Tried to add non-Task `%s` to a Lane. Are you sure the task was ' 'decorated with `sparklanes.Task`?' % str(cls)) validate_params(cls, entry_mtd_name, *args, **kwargs)
def __validate_task(self, cls, entry_mtd_name, args, kwargs)
Checks if a class is a task, i.e. if it has been decorated with `sparklanes.Task`, and if the supplied args/kwargs match the signature of the task's entry method. Parameters ---------- cls : LaneTask entry_mtd_name : str Name of the method, which is called when the t...
6.415287
4.522811
1.418429
if isinstance(cls_or_branch, Branch): self.tasks.append(cls_or_branch) # Add branch with already validated tasks else: # Validate self.__validate_task(cls_or_branch, '__init__', args, kwargs) # Append self.tasks.append({'cls_or_branch...
def add(self, cls_or_branch, *args, **kwargs)
Adds a task or branch to the lane. Parameters ---------- cls_or_branch : Class *args Variable length argument list to be passed to `cls_or_branch` during instantiation **kwargs Variable length keyword arguments to be passed to `cls_or_branch` during insta...
3.621933
3.720835
0.973419
logger = make_default_logger(INTERNAL_LOGGER_NAME) logger.info('\n%s\nExecuting `%s`\n%s\n', '-'*80, self.name, '-'*80) logger.info('\n%s', str(self)) threads = [] if not self.tasks: raise LaneExecutionError('No tasks to execute!') for task_def_or_...
def run(self)
Executes the tasks in the lane in the order in which they have been added, unless `self.run_parallel` is True, then a thread is spawned for each task and executed in parallel (note that task threads are still spawned in the order in which they were added).
2.712165
2.48984
1.089293
observations = np.tile(np.array([[10, 256, 202, 97]]), (nmr_problems, 1)) nmr_tanks_ground_truth = np.ones((nmr_problems,)) * 276 return observations, nmr_tanks_ground_truth
def get_historical_data(nmr_problems)
Get the historical tank data. Args: nmr_problems (int): the number of problems Returns: tuple: (observations, nmr_tanks_ground_truth)
5.227838
3.560335
1.468355
# The number of tanks we observe per problem nmr_observed_tanks = 10 # Generate some maximum number of tanks. Basically the ground truth of the estimation problem. nmr_tanks_ground_truth = normal(nmr_problems, 1, mean=250, std=30, ctype='uint') # Generate some random tank observations obs...
def get_simulated_data(nmr_problems)
Simulate some data. This returns the simulated tank observations and the corresponding ground truth maximum number of tanks. Args: nmr_problems (int): the number of problems Returns: tuple: (observations, nmr_tanks_ground_truth)
5.914598
4.564758
1.295709
return SimpleCLFunction.from_string(''' /** * Compute the Hessian using (possibly) multiple steps with various interpolations. */ void _numdiff_hessian_element( void* data, local mot_float_type* x_tmp, mot_float_type f_x_input, uint px, uint p...
def _get_numdiff_hessian_element_func(objective_func, nmr_steps, step_ratio)
Return a function to compute one element of the Hessian matrix.
3.317901
3.323212
0.998402
nmr_params = parameters.shape[1] initial_step = np.zeros_like(parameters) if max_step_sizes is None: max_step_sizes = 0.1 if isinstance(max_step_sizes, Number): max_step_sizes = [max_step_sizes] * nmr_params max_step_sizes = np.array(max_step_sizes) for ind in range(param...
def _get_initial_step(parameters, lower_bounds, upper_bounds, max_step_sizes)
Get an initial step size to use for every parameter. This chooses the step sizes based on the maximum step size and the lower and upper bounds. Args: parameters (ndarray): The parameters at which to evaluate the gradient. A (d, p) matrix with d problems, p parameters and n samples. ...
2.259969
2.29847
0.983249
self._old_config = {k: v for k, v in _config.items()} self._apply()
def apply(self)
Apply the current action to the current runtime configuration.
8.441441
6.413828
1.316131
for key, value in self._old_config.items(): _config[key] = value
def unapply(self)
Reset the current configuration to the previous state.
9.194933
5.878249
1.56423
# In the event that no argument is supplied to the decorator, python passes the decorated # class itself as an argument. That way, we can detect if no argument (or an argument of # invalid type) was supplied. This allows passing of `entry` as both a named kwarg, and # as an arg. Isn't ne...
def Task(entry): # pylint: disable=invalid-name if not isinstance(entry, string_types)
Decorator with which classes, who act as tasks in a `Lane`, must be decorated. When a class is being decorated, it becomes a child of `LaneTask`. Parameters ---------- entry: The name of the task's "main" method, i.e. the method which is executed when task is run Returns ------- wrapper (f...
5.88268
5.808586
1.012756
if name in TaskCache.cached and not overwrite: raise CacheError('Object with name `%s` already in cache.' % name) TaskCache.cached[name] = val
def cache(self, name, val, overwrite=True)
Assigns an attribute reference to all subsequent tasks. For example, if a task caches a DataFrame `df` using `self.cache('some_df', df)`, all tasks that follow can access the DataFrame using `self.some_df`. Note that manually assigned attributes that share the same name have precedence over cach...
5.347608
5.666649
0.943698
self.exc = None try: self.task() except BaseException: self.exc = sys.exc_info()
def run(self)
Overwrites `threading.Thread.run`, to allow handling of exceptions thrown by threads from within the main app.
4.56365
3.438531
1.327209
Thread.join(self, timeout=timeout) if self.exc: msg = "Thread '%s' threw an exception `%s`: %s" \ % (self.getName(), self.exc[0].__name__, self.exc[1]) new_exc = LaneExecutionError(msg) if PY3: raise new_exc.with_traceback(s...
def join(self, timeout=None)
Overwrites `threading.Thread.join`, to allow handling of exceptions thrown by threads from within the main app.
3.512647
3.62633
0.968651
def _called_decorator(dec_func): @wraps(dec_func) def _decorator(*args, **kwargs): return dec_func() return _decorator return _called_decorator
def mock_decorator(*args, **kwargs)
Mocked decorator, needed in the case we need to mock a decorator
3.181128
3.031504
1.049356
if any(name.startswith(s) for s in mock_modules): return MockModule() return orig_import(name, *args, **kwargs)
def import_mock(name, *args, **kwargs)
Mock all modules starting with one of the mock_modules names.
4.47285
3.215867
1.390869
cl_runtime_info = cl_runtime_info or CLRuntimeInfo() cl_environments = cl_runtime_info.cl_environments for param in cl_function.get_parameters(): if param.name not in kernel_data: names = [param.name for param in cl_function.get_parameters()] missing_names = [name for n...
def apply_cl_function(cl_function, kernel_data, nmr_instances, use_local_reduction=False, cl_runtime_info=None)
Run the given function/procedure on the given set of data. This class will wrap the given CL function in a kernel call and execute that that for every data instance using the provided kernel data. This class will respect the read write setting of the kernel data elements such that output can be written bac...
2.791767
2.968414
0.940491
return_type, function_name, parameter_list, body = split_cl_function(cl_function) return SimpleCLFunction(return_type, function_name, parameter_list, body, dependencies=dependencies)
def from_string(cls, cl_function, dependencies=())
Parse the given CL function into a SimpleCLFunction object. Args: cl_function (str): the function we wish to turn into an object dependencies (list or tuple of CLLibrary): The list of CL libraries this function depends on Returns: SimpleCLFunction: the CL data type ...
4.083089
3.523357
1.158863
declarations = [] for p in self.get_parameters(): new_p = p.get_renamed(p.name.replace('.', '_')) declarations.append(new_p.get_declaration()) return declarations
def _get_parameter_signatures(self)
Get the signature of the parameters for the CL function declaration. This should return the list of signatures of the parameters for use inside the function signature. Returns: list: the signatures of the parameters for the use in the CL code.
4.749177
4.977032
0.954219
code = '' for d in self._dependencies: code += d.get_cl_code() + "\n" return code
def _get_cl_dependency_code(self)
Get the CL code for all the CL code for all the dependencies. Returns: str: The CL code with the actual code.
4.605629
4.781287
0.963261
return cl.Program(self._cl_context, kernel_source).build(' '.join(compile_flags))
def _build_kernel(self, kernel_source, compile_flags=())
Convenience function for building the kernel for this worker. Args: kernel_source (str): the kernel source to use for building the kernel Returns: cl.Program: a compiled CL kernel
7.691453
9.851299
0.780755
declarations = [] for name, data in self._kernel_data.items(): declarations.extend(data.get_kernel_parameters('_' + name)) return declarations
def _get_kernel_arguments(self)
Get the list of kernel arguments for loading the kernel data elements into the kernel. This will use the sorted keys for looping through the kernel input items. Returns: list of str: the list of parameter definitions
6.898833
6.983984
0.987808
dtypes = [] for name, data in self._kernel_data.items(): dtypes.extend(data.get_scalar_arg_dtypes()) return dtypes
def get_scalar_arg_dtypes(self)
Get the location and types of the input scalars. Returns: list: for every kernel input element either None if the data is a buffer or the numpy data type if if is a scalar.
3.862915
3.921098
0.985162
if cls.sc is not None: cls.sc.stop() cls.sc = SparkContext(master, appName, sparkHome, pyFiles, environment, batchSize, serializer, conf, gateway, jsc, profiler_cls) cls.__init_spark()
def set_sc(cls, master=None, appName=None, sparkHome=None, pyFiles=None, environment=None, batchSize=0, serializer=PickleSerializer(), conf=None, gateway=None, jsc=None, profiler_cls=BasicProfiler)
Creates and initializes a new `SparkContext` (the old one will be stopped). Argument signature is copied from `pyspark.SparkContext <https://spark.apache.org/docs/latest/api/python/pyspark.html#pyspark.SparkContext>`_.
2.571739
2.71955
0.945649
sess = SparkSession.builder if master: sess.master(master) if appName: sess.appName(appName) if conf: sess.config(conf=conf) if hive_support: sess.enableHiveSupport() cls.spark = sess.getOrCreate()
def set_spark(cls, master=None, appName=None, conf=None, hive_support=False)
Creates and initializes a new `SparkSession`. Argument signature is copied from `pyspark.sql.SparkSession <https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.SparkSession>`_.
1.94401
2.048386
0.949045
args = _parse_and_validate_args(args) logging.debug(args) dist = __make_tmp_dir() try: __package_dependencies(dist_dir=dist, additional_reqs=args['requirements'], silent=args['silent']) __package_app(tasks_pkg=args['package'], di...
def _package_and_submit(args)
Packages and submits a job, which is defined in a YAML file, to Spark. Parameters ---------- args (List): Command-line arguments
4.602906
4.67358
0.984878
class ExtendAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): if getattr(namespace, self.dest, None) is None: setattr(namespace, self.dest, []) getattr(namespace, self.dest).extend(values) parser = argparse.ArgumentP...
def _parse_and_validate_args(args)
Parse and validate arguments. During validation, it is checked whether the given files/directories exist, while also converting relative paths to absolute ones. Parameters ---------- args (List): Command-line arguments
2.640853
2.633002
1.002982
# pylint: disable=superfluous-parens if path is None: return path else: if not (os.path.isfile(path) if check_file else False) \ and not (os.path.isdir(path) if check_dir else False): raise SystemExit('Path `%s` does not exist' % path) if not os.path....
def __validate_and_fix_path(path, check_file=False, check_dir=False)
Check if a file/directory exists and converts relative paths to absolute ones
2.454223
2.335994
1.050612
pattern = re.compile(r'[\w\-_]+=.+') fixed_args = [] for arg in spark_args: if arg not in SPARK_SUBMIT_FLAGS: if not pattern.match(arg): raise SystemExit('Spark argument `%s` does not seem to be in the correct format ' '`ARG_NAME=ARG_...
def __validate_and_fix_spark_args(spark_args)
Prepares spark arguments. In the command-line script, they are passed as for example `-s master=local[4] deploy-mode=client verbose`, which would be passed to spark-submit as `--master local[4] --deploy-mode client --verbose` Parameters ---------- spark_args (List): List of spark arguments Ret...
3.520775
3.536503
0.995553
logging.info('Packaging dependencies') libs_dir = os.path.join(dist_dir, 'libs') if not os.path.isdir(libs_dir): os.mkdir(libs_dir) # Get requirements req_txt = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'requirements-submit.txt') with open(req_txt, 'r') as req: ...
def __package_dependencies(dist_dir, additional_reqs, silent)
Installs the app's dependencies from pip and packages them (as zip), to be submitted to spark. Parameters ---------- dist_dir (str): Path to directory where the packaged libs shall be located additional_reqs (str): Path to a requirements.txt, containing any of the app's additional requirements ...
2.528637
2.546094
0.993143
logging.info('Packaging application') # Package tasks tasks_dir_splits = os.path.split(os.path.realpath(tasks_pkg)) shutil.make_archive(os.path.join(dist_dir, 'tasks'), 'zip', tasks_dir_splits[0], tasks_dir_splits[1]) # P...
def __package_app(tasks_pkg, dist_dir, custom_main=None, extra_data=None)
Packages the `tasks_pkg` (as zip) to `dist_dir`. Also copies the 'main' python file to `dist_dir`, to be submitted to spark. Same for `extra_data`. Parameters ---------- tasks_pkg (str): Path to the python package containing tasks dist_dir (str): Path to the directory where the packaged code should...
2.105884
2.061528
1.021516
# spark-submit binary cmd = ['spark-submit' if spark_home is None else os.path.join(spark_home, 'bin/spark-submit')] # Supplied spark arguments if spark_args: cmd += spark_args # Packaged App & lane cmd += ['--py-files', 'libs.zip,_framework.zip,tasks.zip', 'main.py'] cmd += [...
def __run_spark_submit(lane_yaml, dist_dir, spark_home, spark_args, silent)
Submits the packaged application to spark using a `spark-submit` subprocess Parameters ---------- lane_yaml (str): Path to the YAML lane definition file dist_dir (str): Path to the directory where the packaged code is located spark_args (str): String of any additional spark config args to be passed...
4.551974
4.529057
1.00506
if not guard_name: guard_name = 'GUARD_' + hashlib.md5(cl_str.encode('utf-8')).hexdigest() return ''' # ifndef {guard_name} # define {guard_name} {func_str} # endif // {guard_name} '''.format(func_str=cl_str, guard_name=guard_name)
def add_include_guards(cl_str, guard_name=None)
Add include guards to the given string. If you are including the same body of CL code multiple times in a Kernel, it is important to add include guards (https://en.wikipedia.org/wiki/Include_guard) around them to prevent the kernel from registering the function twice. Args: cl_str (str): the p...
2.542974
2.652303
0.958779
if is_vector_ctype(cl_type): raw_type, vector_length = split_vector_ctype(cl_type) if raw_type == 'mot_float_type': if is_vector_ctype(mot_float_type): raw_type, _ = split_vector_ctype(mot_float_type) else: raw_type = mot_float_type ...
def ctype_to_dtype(cl_type, mot_float_type='float')
Get the numpy dtype of the given cl_type string. Args: cl_type (str): the CL data type to match, for example 'float' or 'float4'. mot_float_type (str): the C name of the ``mot_float_type``. The dtype will be looked up recursively. Returns: dtype: the numpy datatype
1.954459
1.989164
0.982553
scalar_dtype = ctype_to_dtype(data_type, mot_float_type) if isinstance(data, numbers.Number): data = scalar_dtype(data) if is_vector_ctype(data_type): shape = data.shape dtype = ctype_to_dtype(data_type, mot_float_type) ve = np.zeros(shape[:-1], dtype=dtype) i...
def convert_data_to_dtype(data, data_type, mot_float_type='float')
Convert the given input data to the correct numpy type. Args: data (ndarray): The value to convert to the correct numpy type data_type (str): the data type we need to convert the data to mot_float_type (str): the data type of the current ``mot_float_type`` Returns: ndarray: the...
2.053555
2.190681
0.937405
if not is_vector_ctype(ctype): raise ValueError('The given ctype is not a vector type.') for vector_length in [2, 3, 4, 8, 16]: if ctype.endswith(str(vector_length)): vector_str_len = len(str(vector_length)) return ctype[:-vector_str_len], int(ctype[-vector_str_len:]...
def split_vector_ctype(ctype)
Split a vector ctype into a raw ctype and the vector length. If the given ctype is not a vector type, we raise an error. I Args: ctype (str): the ctype to possibly split into a raw ctype and the vector length Returns: tuple: the raw ctype and the vector length
2.853749
2.750242
1.037636
cl_device_type_str = cl_device_type_str.upper() if hasattr(cl.device_type, cl_device_type_str): return getattr(cl.device_type, cl_device_type_str) return None
def device_type_from_string(cl_device_type_str)
Converts values like ``gpu`` to a pyopencl device type string. Supported values are: ``accelerator``, ``cpu``, ``custom``, ``gpu``. If ``all`` is given, None is returned. Args: cl_device_type_str (str): The string we want to convert to a device type. Returns: cl.device_type: the pyopencl ...
1.910999
2.647183
0.721899
if include_complex: with open(os.path.abspath(resource_filename('mot', 'data/opencl/complex.h')), 'r') as f: complex_number_support = f.read() else: complex_number_support = '' scipy_constants = ''' #define MACHEP DBL_EPSILON #define MAXLOG log(DBL_MAX) ...
def get_float_type_def(double_precision, include_complex=True)
Get the model floating point type definition. Args: double_precision (boolean): if True we will use the double type for the mot_float_type type. Else, we will use the single precision float type for the mot_float_type type. include_complex (boolean): if we include support for complex nu...
2.547222
2.469504
1.031471
def check_self_dependencies(input_data): for k, v in input_data.items(): if k in v: raise ValueError('Self-dependency, {} depends on itself.'.format(k)) def prepare_input_data(input_data): return {k: set(v) for k, v in input_data.items()} ...
def topological_sort(data)
Topological sort the given dictionary structure. Args: data (dict); dictionary structure where the value is a list of dependencies for that given key. For example: ``{'a': (), 'b': ('a',)}``, where ``a`` depends on nothing and ``b`` depends on ``a``. Returns: tuple: the dependencie...
2.584476
2.552967
1.012342
return np.isscalar(value) or (isinstance(value, np.ndarray) and (len(np.squeeze(value).shape) == 0))
def is_scalar(value)
Test if the given value is a scalar. This function also works with memory mapped array values, in contrast to the numpy is_scalar method. Args: value: the value to test for being a scalar value Returns: boolean: if the given value is a scalar or not
3.135341
4.820517
0.650416
if is_scalar(value): return True return np.array(value == value.flatten()[0]).all()
def all_elements_equal(value)
Checks if all elements in the given value are equal to each other. If the input is a single value the result is trivial. If not, we compare all the values to see if they are exactly the same. Args: value (ndarray or number): a numpy array or a single number. Returns: bool: true if all...
6.996349
9.544094
0.733055
if not all_elements_equal(value): raise ValueError('Not all values are equal to each other.') if is_scalar(value): return value return value.item(0)
def get_single_value(value)
Get a single value out of the given value. This is meant to be used after a call to :func:`all_elements_equal` that returned True. With this function we return a single number from the input value. Args: value (ndarray or number): a numpy array or a single number. Returns: number: a s...
5.944807
5.114616
1.162317
previous_level = logging.root.manager.disable logging.disable(highest_level) try: yield finally: logging.disable(previous_level)
def all_logging_disabled(highest_level=logging.CRITICAL)
Disable all logging temporarily. A context manager that will prevent any logging messages triggered during the body from being processed. Args: highest_level: the maximum logging level that is being blocked
2.675654
4.34649
0.61559
offset = 0 elements_left = nmr_elements while elements_left > 0: next_batch = (offset, offset + min(elements_left, max_batch_size)) yield next_batch batch_size = min(elements_left, max_batch_size) elements_left -= batch_size offset += batch_size
def split_in_batches(nmr_elements, max_batch_size)
Split the total number of elements into batches of the specified maximum size. Examples:: split_in_batches(30, 8) -> [(0, 8), (8, 15), (16, 23), (24, 29)] for batch_start, batch_end in split_in_batches(2000, 100): array[batch_start:batch_end] Yields: tuple: the start and e...
2.368134
2.565249
0.923159
diagonal_ind = np.arange(covariance.shape[1]) diagonal_els = covariance[:, diagonal_ind, diagonal_ind] result = covariance / np.sqrt(diagonal_els[:, :, None] * diagonal_els[:, None, :]) result[np.isinf(result)] = 0 return np.clip(np.nan_to_num(result), -1, 1)
def covariance_to_correlations(covariance)
Transform a covariance matrix into a correlations matrix. This can be seen as dividing a covariance matrix by the outer product of the diagonal. As post processing we replace the infinities and the NaNs with zeros and clip the result to [-1, 1]. Args: covariance (ndarray): a matrix of shape (n, p...
2.887324
2.948441
0.979271
if os.name == 'nt': # In Windows there is no fork. return list(map(func, iterable)) try: p = multiprocessing.Pool() return_data = list(p.imap(func, iterable)) p.close() p.join() return return_data except OSError: return list(map(func, iterable))
def multiprocess_mapping(func, iterable)
Multiprocess mapping the given function on the given iterable. This only works in Linux and Mac systems since Windows has no forking capability. On Windows we fall back on single processing. Also, if we reach memory limits we fall back on single cpu processing. Args: func (func): the function to a...
2.897691
3.199518
0.905665
from mot.lib.cl_function import SimpleCLFunction def separate_cl_functions(input_str): class Semantics: def __init__(self): self._functions = [] def result(self, ast): return self._functions def arglist(self, ast): ...
def parse_cl_function(cl_code, dependencies=())
Parse the given OpenCL string to a single SimpleCLFunction. If the string contains more than one function, we will return only the last, with all the other added as a dependency. Args: cl_code (str): the input string containing one or more functions. dependencies (Iterable[CLCodeObject]): ...
4.341578
3.84581
1.128911
class Semantics: def __init__(self): self._return_type = '' self._function_name = '' self._parameter_list = [] self._cl_body = '' def result(self, ast): return self._return_type, self._function_name, self._parameter_list, self._cl_bo...
def split_cl_function(cl_str)
Split an CL function into a return type, function name, parameters list and the body. Args: cl_str (str): the CL code to parse and plit into components Returns: tuple: string elements for the return type, function name, parameter list and the body
2.717132
2.685426
1.011807
logger = logging.getLogger(name) logger.setLevel(level) if not logger.handlers: handler = logging.StreamHandler(sys.stderr) handler.setLevel(level) formatter = logging.Formatter(fmt) handler.setFormatter(formatter) logger.addHandler(handler) return logger
def make_default_logger(name=INTERNAL_LOGGER_NAME, level=logging.INFO, fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Create a logger with the default configuration
1.518613
1.575236
0.964054
return self._device.get_info(cl.device_info.TYPE) == cl.device_type.GPU
def is_gpu(self)
Check if the device associated with this environment is a GPU. Returns: boolean: True if the device is an GPU, false otherwise.
4.653194
5.323621
0.874066
return self._device.get_info(cl.device_info.TYPE) == cl.device_type.CPU
def is_cpu(self)
Check if the device associated with this environment is a CPU. Returns: boolean: True if the device is an CPU, false otherwise.
5.524373
5.945763
0.929128
if isinstance(cl_device_type, str): cl_device_type = device_type_from_string(cl_device_type) device = None if platform is None: platforms = cl.get_platforms() else: platforms = [platform] for platform in platforms: devic...
def single_device(cl_device_type='GPU', platform=None, fallback_to_any_device_type=False)
Get a list containing a single device environment, for a device of the given type on the given platform. This will only fetch devices that support double (possibly only double with a pragma defined, but still, it should support double). Args: cl_device_type (cl.device_type.* or str...
2.424245
2.213524
1.095197
if isinstance(cl_device_type, str): cl_device_type = device_type_from_string(cl_device_type) runtime_list = [] if platform is None: platforms = cl.get_platforms() else: platforms = [platform] for platform in platforms: i...
def all_devices(cl_device_type=None, platform=None)
Get multiple device environments, optionally only of the indicated type. This will only fetch devices that support double point precision. Args: cl_device_type (cl.device_type.* or string): The type of the device we want, can be a opencl device type or a string matching 'GP...
2.134022
1.967847
1.084445
cl_environments = CLEnvironmentFactory.all_devices(cl_device_type=preferred_device_type) platform_names = [env.platform.name for env in cl_environments] has_amd_pro_platform = any('AMD Accelerated Parallel Processing' in name for name in platform_names) if has_amd_pro_platform:...
def smart_device_selection(preferred_device_type=None)
Get a list of device environments that is suitable for use in MOT. Basically this gets the total list of devices using all_devices() and applies a filter on it. This filter does the following: 1) if the 'AMD Accelerated Parallel Processing' is available remove all environments using the 'C...
4.334559
3.127276
1.386049
r samples_generator = _get_sample_generator(samples) return np.array(multiprocess_mapping(_MultivariateESSMultiProcessing(batch_size_generator), samples_generator()))
def multivariate_ess(samples, batch_size_generator=None)
r"""Estimate the multivariate Effective Sample Size for the samples of every problem. This essentially applies :func:`estimate_multivariate_ess` to every problem. Args: samples (ndarray, dict or generator): either a matrix of shape (d, p, n) with d problems, p parameters and n samples, or ...
11.446875
15.647846
0.73153
r samples_generator = _get_sample_generator(samples) return np.array(multiprocess_mapping(_UnivariateESSMultiProcessing(method, **kwargs), samples_generator()))
def univariate_ess(samples, method='standard_error', **kwargs)
r"""Estimate the univariate Effective Sample Size for the samples of every problem. This computes the ESS using: .. math:: ESS(X) = n * \frac{\lambda^{2}}{\sigma^{2}} Where :math:`\lambda` is the standard deviation of the chain and :math:`\sigma` is estimated using the monte ...
13.194909
17.459198
0.755757
if isinstance(samples, Mapping): def samples_generator(): for ind in range(samples[list(samples.keys())[0]].shape[0]): yield np.array([samples[s][ind, :] for s in sorted(samples)]) elif isinstance(samples, np.ndarray): def samples_generator(): for ind...
def _get_sample_generator(samples)
Get a sample generator from the given polymorphic input. Args: samples (ndarray, dict or generator): either an matrix of shape (d, p, n) with d problems, p parameters and n samples, or a dictionary with for every parameter a matrix with shape (d, n) or, finally, a generator function...
2.616847
2.447851
1.069039
r normalized_chain = chain - np.mean(chain, dtype=np.float64) lagged_mean = np.mean(normalized_chain[:len(chain) - lag] * normalized_chain[lag:], dtype=np.float64) return lagged_mean / np.var(chain, dtype=np.float64)
def get_auto_correlation(chain, lag)
r"""Estimates the auto correlation for the given chain (1d vector) with the given lag. Given a lag :math:`k`, the auto correlation coefficient :math:`\rho_{k}` is estimated as: .. math:: \hat{\rho}_{k} = \frac{E[(X_{t} - \mu)(X_{t + k} - \mu)]}{\sigma^{2}} Please note that this equation only wor...
3.218133
3.501683
0.919025
r max_lag = max_lag or min(len(chain) // 3, 1000) normalized_chain = chain - np.mean(chain, dtype=np.float64) previous_accoeff = 0 auto_corr_sum = 0 for lag in range(1, max_lag): auto_correlation_coeff = np.mean(normalized_chain[:len(chain) - lag] * normalized_chain[lag:], dtype=np.fl...
def get_auto_correlation_time(chain, max_lag=None)
r"""Compute the auto correlation time up to the given lag for the given chain (1d vector). This will halt when the maximum lag :math:`m` is reached or when the sum of two consecutive lags for any odd lag is lower or equal to zero. The auto correlation sum is estimated as: .. math:: \tau = 1 ...
2.83007
2.904671
0.974317
r sigma = (monte_carlo_standard_error(chain, batch_size_generator=batch_size_generator, compute_method=compute_method) ** 2 * len(chain)) lambda_ = np.var(chain, dtype=np.float64) return len(chain) * (lambda_ / sigma)
def estimate_univariate_ess_standard_error(chain, batch_size_generator=None, compute_method=None)
r"""Compute the univariate ESS using the standard error method. This computes the ESS using: .. math:: ESS(X) = n * \frac{\lambda^{2}}{\sigma^{2}} Where :math:`\lambda` is the standard deviation of the chain and :math:`\sigma` is estimated using the monte carlo standard error (which in turn ...
4.18621
4.764599
0.878607
r tmp = 2.0 / nmr_params log_min_ess = tmp * np.log(2) + np.log(np.pi) - tmp * (np.log(nmr_params) + gammaln(nmr_params / 2)) \ + np.log(chi2.ppf(1 - alpha, nmr_params)) - 2 * np.log(epsilon) return int(round(np.exp(log_min_ess)))
def minimum_multivariate_ess(nmr_params, alpha=0.05, epsilon=0.05)
r"""Calculate the minimum multivariate Effective Sample Size you will need to obtain the desired precision. This implements the inequality from Vats et al. (2016): .. math:: \widehat{ESS} \geq \frac{2^{2/p}\pi}{(p\Gamma(p/2))^{2/p}} \frac{\chi^{2}_{1-\alpha,p}}{\epsilon^{2}} Where :math:`p` is t...
3.544013
3.301922
1.073318
r tmp = 2.0 / nmr_params log_min_ess = tmp * np.log(2) + np.log(np.pi) - tmp * (np.log(nmr_params) + gammaln(nmr_params / 2)) \ + np.log(chi2.ppf(1 - alpha, nmr_params)) - np.log(multi_variate_ess) return np.sqrt(np.exp(log_min_ess))
def multivariate_ess_precision(nmr_params, multi_variate_ess, alpha=0.05)
r"""Calculate the precision given your multivariate Effective Sample Size. Given that you obtained :math:`ESS` multivariate effective samples in your estimate you can calculate the precision with which you approximated your desired confidence region. This implements the inequality from Vats et al. (2016),...
3.502133
2.988564
1.171845
r sample_means = np.mean(samples, axis=1, dtype=np.float64) nmr_params, chain_length = samples.shape nmr_batches = int(np.floor(chain_length / batch_size)) sigma = np.zeros((nmr_params, nmr_params)) nmr_offsets = chain_length - nmr_batches * batch_size + 1 for offset in range(nmr_offsets)...
def estimate_multivariate_ess_sigma(samples, batch_size)
r"""Calculates the Sigma matrix which is part of the multivariate ESS calculation. This implementation is based on the Matlab implementation found at: https://github.com/lacerbi/multiESS The Sigma matrix is defined as: .. math:: \Sigma = \Lambda + 2 * \sum_{k=1}^{\infty}{Cov(Y_{1}, Y_{1+k})} ...
2.830335
2.744475
1.031285
r batch_size_generator = batch_size_generator or SquareRootSingleBatch() batch_sizes = batch_size_generator.get_multivariate_ess_batch_sizes(*samples.shape) nmr_params, chain_length = samples.shape nmr_batches = len(batch_sizes) det_lambda = det(np.cov(samples)) ess_estimates = np.zeros(...
def estimate_multivariate_ess(samples, batch_size_generator=None, full_output=False)
r"""Compute the multivariate Effective Sample Size of your (single instance set of) samples. This multivariate ESS is defined in Vats et al. (2016) and is given by: .. math:: ESS = n \bigg(\frac{|\Lambda|}{|\Sigma|}\bigg)^{1/p} Where :math:`n` is the number of samples, :math:`p` the number of pa...
2.842838
2.451871
1.159456
batch_size_generator = batch_size_generator or SquareRootSingleBatch() compute_method = compute_method or BatchMeansMCSE() batch_sizes = batch_size_generator.get_univariate_ess_batch_sizes(len(chain)) return np.min(list(compute_method.compute_standard_error(chain, b) for b in batch_sizes))
def monte_carlo_standard_error(chain, batch_size_generator=None, compute_method=None)
Compute Monte Carlo standard errors for the expectations This is a convenience function that calls the compute method for each batch size and returns the lowest ESS over the used batch sizes. Args: chain (ndarray): the Markov chain batch_size_generator (UniVariateESSBatchSizeGenerator): th...
5.586039
3.032486
1.842066
if len(samples.shape) == 1: return np.mean(samples), np.std(samples, ddof=ddof) return np.mean(samples, axis=1), np.std(samples, axis=1, ddof=ddof)
def fit_gaussian(samples, ddof=0)
Calculates the mean and the standard deviation of the given samples. Args: samples (ndarray): a one or two dimensional array. If one dimensional we calculate the fit using all values. If two dimensional, we fit the Gaussian for every set of samples over the first dimension. ddof (int): ...
1.700493
1.942277
0.875515
cl_func = SimpleCLFunction.from_string(''' void compute(global mot_float_type* samples, global mot_float_type* means, global mot_float_type* stds, int nmr_samples, int low, int high){ ...
def fit_circular_gaussian(samples, high=np.pi, low=0)
Compute the circular mean for samples in a range Args: samples (ndarray): a one or two dimensional array. If one dimensional we calculate the fit using all values. If two dimensional, we fit the Gaussian for every set of samples over the first dimension. high (float): The maximum wrap p...
2.94824
2.974931
0.991028
if len(samples.shape) == 1: return _TruncatedNormalFitter()((samples, lower_bounds, upper_bounds)) def item_generator(): for ind in range(samples.shape[0]): if is_scalar(lower_bounds): lower_bound = lower_bounds else: lower_bound = lo...
def fit_truncated_gaussian(samples, lower_bounds, upper_bounds)
Fits a truncated gaussian distribution on the given samples. This will do a maximum likelihood estimation of a truncated Gaussian on the provided samples, with the truncation points given by the lower and upper bounds. Args: samples (ndarray): a one or two dimensional array. If one dimensional we ...
2.580837
2.701527
0.955325
if lower is None: lower = -np.inf if upper is None: upper = np.inf def point_iterator(): for ind in range(means_0.shape[0]): yield np.squeeze(means_0[ind]), np.squeeze(stds_0[ind]), np.squeeze(means_1[ind]), np.squeeze(stds_1[ind]) return np.array(list(multipro...
def gaussian_overlapping_coefficient(means_0, stds_0, means_1, stds_1, lower=None, upper=None)
Compute the overlapping coefficient of two Gaussian continuous_distributions. This computes the :math:`\int_{-\infty}^{\infty}{\min(f(x), g(x))\partial x}` where :math:`f \sim \mathcal{N}(\mu_0, \sigma_0^{2})` and :math:`f \sim \mathcal{N}(\mu_1, \sigma_1^{2})` are normally distributed variables. This...
2.982049
3.247653
0.918217
r mean_deviance = -2 * np.mean(ll_per_sample, axis=1) deviance_at_mean = -2 * mean_posterior_lls pd_2002 = mean_deviance - deviance_at_mean pd_2004 = np.var(ll_per_sample, axis=1) / 2.0 return {'DIC_2002': np.nan_to_num(mean_deviance + pd_2002), 'DIC_2004': np.nan_to_num(mean_devia...
def deviance_information_criterions(mean_posterior_lls, ll_per_sample)
r"""Calculates the Deviance Information Criteria (DIC) using three methods. This returns a dictionary returning the ``DIC_2002``, the ``DIC_2004`` and the ``DIC_Ando_2011`` method. The first is based on Spiegelhalter et al (2002), the second based on Gelman et al. (2004) and the last on Ando (2011). All ca...
3.089195
2.357617
1.310304
mu = params[0] sigma = params[1] if sigma == 0: return np.inf ll = np.sum(norm.logpdf(data, mu, sigma)) ll -= len(data) * np.log((norm.cdf(high, mu, sigma) - norm.cdf(low, mu, sigma))) return -ll
def truncated_normal_log_likelihood(params, low, high, data)
Calculate the log likelihood of the truncated normal distribution. Args: params: tuple with (mean, std), the parameters under which we evaluate the model low (float): the lower truncation bound high (float): the upper truncation bound data (ndarray): the one dime...
2.705406
3.044348
0.888665
if params[1] == 0: return np.array([np.inf, np.inf]) return np.array([_TruncatedNormalFitter.partial_derivative_mu(params[0], params[1], low, high, data), _TruncatedNormalFitter.partial_derivative_sigma(params[0], params[1], low, high, data)])
def truncated_normal_ll_gradient(params, low, high, data)
Return the gradient of the log likelihood of the truncated normal at the given position. Args: params: tuple with (mean, std), the parameters under which we evaluate the model low (float): the lower truncation bound high (float): the upper truncation bound data (...
3.038273
3.214513
0.945174
pd_mu = np.sum(data - mu) / sigma ** 2 pd_mu -= len(data) * ((norm.pdf(low, mu, sigma) - norm.pdf(high, mu, sigma)) / (norm.cdf(high, mu, sigma) - norm.cdf(low, mu, sigma))) return -pd_mu
def partial_derivative_mu(mu, sigma, low, high, data)
The partial derivative with respect to the mean. Args: mu (float): the mean of the truncated normal sigma (float): the std of the truncated normal low (float): the lower truncation bound high (float): the upper truncation bound data (ndarray): the one...
2.999682
3.657489
0.820148
pd_sigma = np.sum(-(1 / sigma) + ((data - mu) ** 2 / (sigma ** 3))) pd_sigma -= len(data) * (((low - mu) * norm.pdf(low, mu, sigma) - (high - mu) * norm.pdf(high, mu, sigma)) / (sigma * (norm.cdf(high, mu, sigma) - norm.cdf(low, mu, sigma)))) return -pd_...
def partial_derivative_sigma(mu, sigma, low, high, data)
The partial derivative with respect to the standard deviation. Args: mu (float): the mean of the truncated normal sigma (float): the std of the truncated normal low (float): the lower truncation bound high (float): the upper truncation bound data (nda...
3.193369
3.649824
0.874938
return { 'nmsimplex_scratch': LocalMemory( 'mot_float_type', self._nmr_parameters * 2 + (self._nmr_parameters + 1) ** 2 + 1), 'initial_simplex_scale': LocalMemory('mot_float_type', self._nmr_parameters) }
def get_kernel_data(self)
Get the kernel data needed for this optimization routine to work.
13.320233
10.907323
1.221219
return { 'subplex_scratch_float': LocalMemory( 'mot_float_type', 4 + self._var_replace_dict['NMR_PARAMS'] * 2 + self._var_replace_dict['MAX_SUBSPACE_LENGTH'] * 2 + (self._var_replace_dict['MAX_SUBSPACE_L...
def get_kernel_data(self)
Get the kernel data needed for this optimization routine to work.
5.315953
4.902423
1.084352
return { 'scratch_mot_float_type': LocalMemory( 'mot_float_type', 8 + 2 * self._var_replace_dict['NMR_OBSERVATIONS'] + 5 * self._var_replace_dict['NMR_PARAMS'] + self._var_r...
def get_kernel_data(self)
Get the kernel data needed for this optimization routine to work.
6.706178
6.133766
1.093321
elements = [] for value in bounds: if all_elements_equal(value): elements.append(Scalar(get_single_value(value), ctype='mot_float_type')) else: elements.append(Array(value, ctype='mot_float_type', as_scalar=True)) return CompositeArray(elements, 'mot_float_type',...
def _bounds_to_array(bounds)
Create a CompositeArray to hold the bounds.
6.873764
5.689745
1.208097