def _preprocess_tensor_input(x, data_format, mode):
    ndim = len(x.shape)
    if mode == 'tf':
        x /= 127.5
        x -= 1.0
        return x
    elif mode == 'torch':
        x /= 255.0
        mean = [0.485, 0.456, 0.406]
        std = [0.229, 0.224, 0.225]
    else:
        if data_format == 'channels_first':
            if len(x.shape) == 3:
                x = ops.stack([x[i, ...] for i in (2, 1, 0)], axis=0)
            else:
                x = ops.stack([x[:, i, :] for i in (2, 1, 0)], axis=1)
        else:
            x = ops.stack([x[..., i] for i in (2, 1, 0)], axis=-1)
        mean = [103.939, 116.779, 123.68]
        std = None
    mean_tensor = ops.convert_to_tensor(-np.array(mean), dtype=x.dtype)
    if data_format == 'channels_first':
        mean_tensor = ops.reshape(mean_tensor, (1, 3) + (1,) * (ndim - 2))
    else:
        mean_tensor = ops.reshape(mean_tensor, (1,) * (ndim - 1) + (3,))
    x += mean_tensor
    if std is not None:
        std_tensor = ops.convert_to_tensor(np.array(std), dtype=x.dtype)
        if data_format == 'channels_first':
            std_tensor = ops.reshape(std_tensor, (-1, 1, 1))
        x /= std_tensor
    return x

Preprocesses a tensor encoding a batch of images.

Args:
x: Input tensor, 3D or 4D.
data_format: Data format of the image tensor.
mode: One of "caffe", "tf" or "torch".
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- tf: will scale pixels between -1 and 1,
sample-wise.
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.

Returns:
Preprocessed tensor.


def is_experimental_feature_activated(feature_name):
    return feature_name in os.environ.get('TF_TRT_EXPERIMENTAL_FEATURES', default='').split(',')

Determines if a TF-TRT experimental feature is enabled.

This helper function checks if an experimental feature was enabled using
the environment variable `TF_TRT_EXPERIMENTAL_FEATURES=feature_1,feature_2`.

Args:
feature_name: Name of the feature being tested for activation.


def one_or_more(e, delimiter=None):
    if (delimiter is None):
        delimiter = (lambda s, grm, pos: (s, Ignore, (pos, pos)))
    msg = 'Expected one or more of: {}'.format(repr(e))

    def match_one_or_more(s, grm=None, pos=0):
        start = pos
        (s, obj, span) = e(s, grm, pos)
        pos = span[1]
        data = ([] if (obj is Ignore) else [obj])
        try:
            while True:
                (s, obj, span) = delimiter(s, grm, pos)
                pos = span[1]
                if (obj is not Ignore):
                    data.append(obj)
                (s, obj, span) = e(s, grm, pos)
                pos = span[1]
                if (obj is not Ignore):
                    data.append(obj)
        except PegreError:
            pass
        return PegreResult(s, data, (start, pos))
    return match_one_or_more

Create a PEG function to match one or more expressions.

Args:
e: the expression to match
delimiter: an optional expression to match between the
primary *e* matches.


def int64_user_counter(namespace, name, metric, ptransform=None) -> metrics_pb2.MonitoringInfo:
    labels = create_labels(ptransform=ptransform, namespace=namespace, name=name)
    if isinstance(metric, int):
        metric = coders.VarIntCoder().encode(metric)
    return create_monitoring_info(USER_COUNTER_URN, SUM_INT64_TYPE, metric, labels)

Return the counter monitoring info for the specifed URN, metric and labels.

Args:
urn: The URN of the monitoring info/metric.
metric: The payload field to use in the monitoring info or an int value.
ptransform: The ptransform id used as a label.


def onWith(self, evnt, func):
    self.on(evnt, func)
    try:
        (yield self)
    finally:
        self.off(evnt, func)

A context manager which can be used to add a callback and remove it when
using a ``with`` statement.

Args:
evnt (str):         An event name
func (function):    A callback function to receive event tufo


def update(self, instance, validated_data):

        is_primary = validated_data.pop("is_primary", False)

        instance = super(EmailSerializer, self).update(
            instance, validated_data
        )

        if is_primary:
            instance.set_primary()

        return instance

Update the instance the serializer is bound to.

Args:
instance:
The instance the serializer is bound to.
validated_data:
The data to update the serializer with.

Returns:
The updated instance.


def join(self, other):
        r
        assert self._load == other._load, 'loads must be the same'
        self._lists.extend(other._lists)
        self._cumlen.extend([c + self._len for c in other._cumlen])
        self._len += other._len

r"""
Args:
other (?):

CommandLine:
python -m sortedcontainers.sortedlist join2

Example:
>>> from utool.experimental.dynamic_connectivity import *  # NOQA
>>> self = EulerTourList([1, 2, 3, 2, 4, 2, 1], load=3)
>>> other = EulerTourList([0, 5, 9, 5, 0], load=3)
>>> result = self.join(other)
>>> print(result)


def _prepare_sample_data(self, submission_type):

    images = np.random.randint(0, 256,
                               size=[BATCH_SIZE, 299, 299, 3], dtype=np.uint8)
    for i in range(BATCH_SIZE):
      Image.fromarray(images[i, :, :, :]).save(
          os.path.join(self._sample_input_dir, IMAGE_NAME_PATTERN.format(i)))

    if submission_type == 'targeted_attack':
      target_classes = np.random.randint(1, 1001, size=[BATCH_SIZE])
      target_class_filename = os.path.join(self._sample_input_dir,
                                           'target_class.csv')
      with open(target_class_filename, 'w') as f:
        for i in range(BATCH_SIZE):
          f.write((IMAGE_NAME_PATTERN + ',{1}\n').format(i, target_classes[i]))

Prepares sample data for the submission.

Args:
submission_type: type of the submission.


def find_layer_idx(model, layer_name):

    layer_idx = None
    for idx, layer in enumerate(model.layers):
        if layer.name == layer_name:
            layer_idx = idx
            break

    if layer_idx is None:
        raise ValueError("No layer with name '{}' within the model".format(layer_name))
    return layer_idx

Looks up the layer index corresponding to `layer_name` from `model`.

Args:
model: The `keras.models.Model` instance.
layer_name: The name of the layer to lookup.

Returns:
The layer index if found. Raises an exception otherwise.


def ensure_dir(path):

    os.makedirs(os.path.abspath(os.path.dirname(path)), exist_ok=True)

Create all parent directories of path if they don't exist.

Args:
path. Path-like object. Create parent dirs to this path.

Return:
None.


def _get_num_inputs_outputs(op_type):

    def _is_list_arg(arg):
        return arg.number_attr or arg.type_list_attr

    def _count_args(arg_defs):
        for arg in arg_defs:
            if _is_list_arg(arg):
                return -1
        return len(arg_defs)
    op_def = op_def_registry.get(op_type)
    if not op_def:
        return (-1, -1)
    return (_count_args(op_def.input_arg), _count_args(op_def.output_arg))

Returns (num_inputs, num_outputs).

Args:
op_type: String. The type of the Operation. Used to lookup the op in the
registry.

Returns:
(num_inputs, num_outputs), for either num_inputs or num_outputs if the value
can't be statically inferred from the OpDef alone or of the OpDef lookup
fails, -1 is returned.


def build_sanitiser_node_dict(cfg, sinks_in_file):
    sanitisers = list()
    for sink in sinks_in_file:
        sanitisers.extend(sink.sanitisers)
    sanitisers_in_file = list()
    for sanitiser in sanitisers:
        for cfg_node in cfg.nodes:
            if (sanitiser in cfg_node.label):
                sanitisers_in_file.append(Sanitiser(sanitiser, cfg_node))
    sanitiser_node_dict = dict()
    for sanitiser in sanitisers:
        sanitiser_node_dict[sanitiser] = list(find_sanitiser_nodes(sanitiser, sanitisers_in_file))
    return sanitiser_node_dict

Build a dict of string -> TriggerNode pairs, where the string
is the sanitiser and the TriggerNode is a TriggerNode of the sanitiser.

Args:
cfg(CFG): cfg to traverse.
sinks_in_file(list[TriggerNode]): list of TriggerNodes containing
the sinks in the file.

Returns:
A string -> TriggerNode dict.


def scheduled_sample_prob(ground_truth_x, generated_x, batch_size, scheduled_sample_var):
    probability_threshold = scheduled_sample_var
    probability_of_generated = tf.random_uniform([batch_size])
    return tf.where((probability_of_generated > probability_threshold), generated_x, ground_truth_x)

Probability based scheduled sampling.

Args:
ground_truth_x: tensor of ground-truth data points.
generated_x: tensor of generated data points.
batch_size: batch size
scheduled_sample_var: probability of choosing from ground_truth.
Returns:
New batch with randomly selected data points.


def fetcher(date=datetime.today(), url_pattern=URL_PATTERN):
    api_url = (url_pattern % date.strftime('%Y-%m-%d'))
    headers = {'Referer': 'http:
    raw_result = requests.get(api_url, headers=headers).json()
    return raw_result

Fetch json data from n.pl

Args:
date (date) - default today
url_patter (string) - default URL_PATTERN

Returns:
dict - data from api


def connect(filename: str, mode: str='r+', *, validate: bool=True, spec_version: str='2.0.1') -> LoomConnection:
    return LoomConnection(filename, mode, validate=validate, spec_version=spec_version)

Establish a connection to a .loom file.

Args:
filename:		Path to the Loom file to open
mode:			Read/write mode, 'r+' (read/write) or 'r' (read-only), defaults to 'r+'
validate:		Validate the file structure against the Loom file format specification
spec_version:	The loom file spec version to validate against (e.g. "2.0.1" or "old")
Returns:
A LoomConnection instance.

Remarks:
This function should typically be used as a context manager (i.e. inside a ``with``-block):

.. highlight:: python
.. code-block:: python

import loompy
with loompy.connect("mydata.loom") as ds:
print(ds.ca.keys())

This ensures that the file will be closed automatically when the context block ends

Note: if validation is requested, an exception is raised if validation fails.


def __init__(self, name=None, description=None, hint=None,
                 allow_failure=False, passes=None, arguments=None):

        if name:
            self.name = name
        if description:
            self.description = description
        if hint:
            self.hint = hint

        self.allow_failure = allow_failure
        self.passes = passes
        self.arguments = arguments or {}
        self.result = None

Initialization method.

Args:
allow_failure (bool): still pass if failed or not.
arguments (dict): arguments passed to the check method when run.


def update_state(self, y_true, y_pred, sample_weight=None):
    return metrics_utils.update_confusion_matrix_variables({self._confusion_matrix_cond: self.accumulator}, y_true, y_pred, thresholds=self.thresholds, thresholds_distributed_evenly=self._thresholds_distributed_evenly, sample_weight=sample_weight)

Accumulates the metric statistics.

Args:
y_true: The ground truth values.
y_pred: The predicted values.
sample_weight: Optional weighting of each example. Defaults to 1. Can be a
`Tensor` whose rank is either 0, or the same rank as `y_true`, and must
be broadcastable to `y_true`.

Returns:
Update op.


def chunk_layer(layer: Callable, inputs: Dict[str, Any], chunk_size: int, no_batch_dims: int, low_mem: bool=False, _out: Any=None, _add_into_out: bool=False) -> Any:
    if not len(inputs) > 0:
        raise ValueError('Must provide at least one input')
    initial_dims = [shape[:no_batch_dims] for shape in _fetch_dims(inputs)]
    orig_batch_dims = tuple([max(s) for s in zip(*initial_dims)])

    def _prep_inputs(t: torch.Tensor) -> torch.Tensor:
        if not low_mem:
            if not sum(t.shape[:no_batch_dims]) == no_batch_dims:
                t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])
            t = t.reshape(-1, *t.shape[no_batch_dims:])
        else:
            t = t.expand(orig_batch_dims + t.shape[no_batch_dims:])
        return t
    prepped_inputs: Dict[str, Any] = tensor_tree_map(_prep_inputs, inputs)
    prepped_outputs = None
    if _out is not None:
        prepped_outputs = tensor_tree_map(lambda t: t.view([-1] + list(t.shape[no_batch_dims:])), _out)
    flat_batch_dim = 1
    for d in orig_batch_dims:
        flat_batch_dim *= d
    no_chunks = flat_batch_dim

    def _select_chunk(t: torch.Tensor) -> torch.Tensor:
        return t[i:i + chunk_size] if t.shape[0] != 1 else t
    i = 0
    out = prepped_outputs
    for _ in range(no_chunks):
        if not low_mem:
            select_chunk = _select_chunk
        else:
            select_chunk = partial(_chunk_slice, flat_start=i, flat_end=min(flat_batch_dim, i + chunk_size), no_batch_dims=len(orig_batch_dims))
        chunks: Dict[str, Any] = tensor_tree_map(select_chunk, prepped_inputs)
        output_chunk = layer(**chunks)
        if out is None:
            out = tensor_tree_map(lambda t: t.new_zeros((flat_batch_dim,) + t.shape[1:]), output_chunk)
        if isinstance(output_chunk, dict):

            def assign(d1: dict, d2: dict) -> None:
                for k, v in d1.items():
                    if isinstance(v, dict):
                        assign(v, d2[k])
                    elif _add_into_out:
                        v[i:i + chunk_size] += d2[k]
                    else:
                        v[i:i + chunk_size] = d2[k]
            assign(out, output_chunk)
        elif isinstance(output_chunk, tuple):
            for x1, x2 in zip(out, output_chunk):
                if _add_into_out:
                    x1[i:i + chunk_size] += x2
                else:
                    x1[i:i + chunk_size] = x2
        elif isinstance(output_chunk, torch.Tensor):
            if _add_into_out:
                out[i:i + chunk_size] += output_chunk
            else:
                out[i:i + chunk_size] = output_chunk
        else:
            raise TypeError('Not supported')
        i += chunk_size
    out = tensor_tree_map(lambda t: t.view(orig_batch_dims + t.shape[1:]), out)
    return out

Implements the "chunking" procedure described in section 1.11.8.

Layer outputs and inputs are assumed to be simple "pytrees," consisting only of (arbitrarily nested) lists, tuples,
and dicts with torch.Tensor leaves.

Args:
layer:
The layer to be applied chunk-wise
inputs:
A (non-nested) dictionary of keyworded inputs. All leaves must be tensors and must share the same batch
dimensions.
chunk_size:
The number of sub-batches per chunk. If multiple batch dimensions are specified, a "sub-batch" is defined
as a single indexing of all batch dimensions simultaneously (s.t. the number of sub-batches is the product
of the batch dimensions).
no_batch_dims:
How many of the initial dimensions of each input tensor can be considered batch dimensions.
low_mem:
Avoids flattening potentially large input tensors. Unnecessary in most cases, and is ever so slightly
slower than the default setting.
Returns:
The reassembled output of the layer on the inputs.


def __init__(self, correction_limit=88., **kwargs):

        self.correction_limit = correction_limit
        super(SunZenithCorrector, self).__init__(**kwargs)

Collect custom configuration values.

Args:
correction_limit (float): Maximum solar zenith angle to apply the
correction in degrees. Pixels beyond this limit have a
constant correction applied. Default 88.
max_sza (float): Maximum solar zenith angle in degrees that is
considered valid and correctable. Default 95.0.


def read(self, uri):
    read_response = self.connect(uri)
    fedora_graph = rdflib.Graph().parse(data=read_response.read(), format='turtle')
    return fedora_graph

Method takes uri and creates a RDF graph from Fedora Repository

Args:
uri(str): URI of Fedora URI

Returns:
rdflib.Graph


def get_output_details(self):
    return [self._get_tensor_details(i, subgraph_index=0) for i in self._interpreter.OutputIndices()]

Gets model output tensor details.

Returns:
A list in which each item is a dictionary with details about
an output tensor. The dictionary contains the same fields as
described for `get_input_details()`.


def get_containers(self, container_class):
    with self._store_lock:
        return self.store.get(container_class.CONTAINER_TYPE, [])

Thread-safe method to retrieve data from the state's store.

Args:
container_class: AttributeContainer class used to filter data.

Returns:
A list of AttributeContainer objects of matching CONTAINER_TYPE.


def load(path):

    importpath = path.replace("/", ".").replace("\\", ".")
    if importpath[-3:] == ".py":
        importpath = importpath[:-3]

    try:
        importlib.import_module(importpath)
    except (ModuleNotFoundError, TypeError):
        exec(open(path).read())

Helper function that tries to load a filepath (or python module notation)
as a python module and on failure `exec` it.

Args:
path (str): Path or module to load

The function tries to import `example.module` when either `example.module`,
`example/module` or `example/module.py` is given.


def get_workflow(workflow_id: str, workflow_version: str) -> dict:
    name = 'workflow_definitions:{}:{}'.format(workflow_id, workflow_version)
    workflow = DB.get_hash_dict(name)
    workflow['stages'] = ast.literal_eval(workflow['stages'])
    return workflow

Get a workflow definition from the Configuration Database.

Args:
workflow_id (str): Workflow identifier
workflow_version (str): Workflow version

Returns:
dict, Workflow definition dictionary


def normalize_partial_name(decl):

    if decl.cache.normalized_partial_name is None:
        decl.cache.normalized_partial_name = normalize(decl.partial_name)
    return decl.cache.normalized_partial_name

Cached variant of normalize

Args:
decl (declaration.declaration_t): the declaration

Returns:
str: normalized name


def restore(self, directory=None, file=None):

        if file is None:
            file = tf.train.latest_checkpoint(
                checkpoint_dir=(self.saver_directory if directory is None else directory),

            )
        elif directory is None:
            file = os.path.join(self.saver_directory, file)
        elif not os.path.isfile(file):
            file = os.path.join(directory, file)

        self.saver.restore(sess=self.session, save_path=file)
        self.session.run(fetches=self.list_buffer_index_reset_op)

Restore TensorFlow model. If no checkpoint file is given, the latest checkpoint is
restored. If no checkpoint directory is given, the model's default saver directory is
used (unless file specifies the entire path).

Args:
directory: Optional checkpoint directory.
file: Optional checkpoint file, or path if directory not given.


class LabelAggregation(AggregationFn, _AggModelIdMixin, _SourcePredictionMixin):

    def __init__(self, agg_func: Callable[[Iterable[int]], int], agg_model_id: Optional[str]=None, include_source_predictions: bool=False, normal_label: int=DEFAULT_NORMAL_LABEL, outlier_label: int=DEFAULT_OUTLIER_LABEL, missing_label: int=DEFAULT_MISSING_LABEL):
        self._agg = agg_func
        self._normal_label = normal_label
        self._outlier_label = outlier_label
        self._missing_label = missing_label
        _AggModelIdMixin.__init__(self, agg_model_id)
        _SourcePredictionMixin.__init__(self, include_source_predictions)

    def apply(self, predictions: Iterable[AnomalyPrediction]) -> AnomalyPrediction:

        result_dict: dict[str, Any] = {}
        _AggModelIdMixin.add_model_id(self, result_dict)
        _SourcePredictionMixin.add_source_predictions(self, result_dict, predictions)
        labels = [prediction.label for prediction in predictions if prediction.label is not None and prediction.label != self._missing_label]
        if len(labels) > 0:
            result_dict['label'] = self._agg(labels)
        elif all(map(lambda x: x.label is None, predictions)):
            result_dict['label'] = None
        else:
            result_dict['label'] = self._missing_label
        return AnomalyPrediction(**result_dict)

Aggregates anomaly predictions based on their labels.

This is an abstract base class for `AggregationFn`s that combine multiple
`AnomalyPrediction` objects into a single `AnomalyPrediction` based on
the labels of the input predictions.

Args:
agg_func (Callable[[Iterable[int]], int]): A function that aggregates
a collection of anomaly labels (integers) into a single label.
agg_model_id (Optional[str]): The model id used in aggregated predictions.
Defaults to None.
include_source_predictions (bool): If True, include the input predictions in
the `source_predictions` of the output. Defaults to False.


def restore_ops(self, reader=None):
    if self._has_registered_saver():
        raise ValueError('Unable to run individual checkpoint restore for objects with registered savers.')
    restore_ops, tensor_saveables, python_positions, _ = self.gather_ops_or_named_saveables()
    restore_ops.extend(self._checkpoint.restore_saveables(tensor_saveables, python_positions, reader=reader))
    return restore_ops

Create or fetch restore ops for this object's attributes.

Requires that the `Trackable` Python object has been bound to an object
ID in the checkpoint.

Args:
reader: A `CheckpointReader`. If None, a new instance will be created.

Returns:
A list of operations when graph building, or an empty list when executing
eagerly.


def get_object_errors(self):
    if (self._object_errors is None):
        self._object_errors = [{str(o): o.get_errors()} for o in self.objects() if o.has_error()]
    return self._object_errors

Gets a list of business error message strings
for each of the requested objects that had a business error.
If there was no error, returns an empty list

Returns:
List of strings


def on_epoch_begin(self, epoch, logs=None):
    logs = self._process_logs(logs)
    for callback in self.callbacks:
        callback.on_epoch_begin(epoch, logs)

Calls the `on_epoch_begin` methods of its callbacks.

This function should only be called during TRAIN mode.

Args:
epoch: Integer, index of epoch.
logs: Dict. Currently no data is passed to this argument for this method
but that may change in the future.


def build_album_art_full_uri(self, url):

        if not url.startswith(('http:', 'https:')):
            url = 'http:
        return url

Ensure an Album Art URI is an absolute URI.

Args:
url (str): the album art URI.

Returns:
str: An absolute URI.


def delete(self, roomId):
    check_type(roomId, basestring, may_be_none=False)
    self._session.delete(((API_ENDPOINT + '/') + roomId))

Delete a room.

Args:
roomId(basestring): The ID of the room to be deleted.

Raises:
TypeError: If the parameter types are incorrect.
ApiError: If the Webex Teams cloud returns an error.


def list_of_vars(arg_plot):

    lovs = [[[var for var in svars.split(',') if var]
             for svars in pvars.split('.') if svars]
            for pvars in arg_plot.split('-') if pvars]
    lovs = [[slov for slov in lov if slov] for lov in lovs if lov]
    return [lov for lov in lovs if lov]

Construct list of variables per plot.

Args:
arg_plot (str): string with variable names separated with
``_`` (figures), ``.`` (subplots) and ``,`` (same subplot).
Returns:
three nested lists of str

- variables on the same subplot;
- subplots on the same figure;
- figures.


def confusion_matrix(
    gold, pred, null_pred=False, null_gold=False, normalize=False, pretty_print=True
):

    conf = ConfusionMatrix(null_pred=null_pred, null_gold=null_gold)
    gold = arraylike_to_numpy(gold)
    pred = arraylike_to_numpy(pred)
    conf.add(gold, pred)
    mat = conf.compile()

    if normalize:
        mat = mat / len(gold)

    if pretty_print:
        conf.display(normalize=normalize)

    return mat

A shortcut method for building a confusion matrix all at once.

Args:
gold: an array-like of gold labels (ints)
pred: an array-like of predictions (ints)
null_pred: If True, include the row corresponding to null predictions
null_gold: If True, include the col corresponding to null gold labels
normalize: if True, divide counts by the total number of items
pretty_print: if True, pretty-print the matrix before returning


def less_equal(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return LessEqual().symbolic_call(x1, x2)
    return backend.numpy.less_equal(x1, x2)

Return the truth value of `x1 <= x2` element-wise.

Args:
x1: First input tensor.
x2: Second input tensor.

Returns:
Output tensor, element-wise comparison of `x1` and `x2`.


def rescale(self, image: 'torch.Tensor', scale: float, **kwargs) -> 'torch.Tensor':
    return image * scale

Rescale an image by a scale factor. image = image * scale.

Args:
image (`torch.Tensor`):
Image to rescale.
scale (`float`):
The scaling factor to rescale pixel values by.

Returns:
`torch.Tensor`: The rescaled image.


def _delete(self, url, data, scope):
    self._create_session(scope)
    response = self.session.delete(url, data=data)
    return (response.status_code, response.text)

Make a DELETE request using the session object to a Degreed endpoint.

Args:
url (str): The url to send a DELETE request to.
data (str): The json encoded payload to DELETE.
scope (str): Must be one of the scopes Degreed expects:
- `CONTENT_PROVIDER_SCOPE`
- `COMPLETION_PROVIDER_SCOPE`


def copy_to_device(target_device, source_device='/cpu:0'):

    def _apply_fn(dataset):
        return _CopyToDeviceDataset(dataset, target_device=target_device, source_device=source_device)
    return _apply_fn

A transformation that copies dataset elements to the given `target_device`.

Args:
target_device: The name of a device to which elements will be copied.
source_device: The original device on which `input_dataset` will be placed.

Returns:
A `Dataset` transformation function, which can be passed to
`tf.data.Dataset.apply`.


def convert_to_layout_rules(x):

  if isinstance(x, LayoutRules):
    return x
  if isinstance(x, str):
    x = _parse_string_to_list_of_pairs(x)
  return LayoutRules(x)

Converts input to a LayoutRules.

Args:
x: LayoutRules, str, or set-like of string pairs.

Returns:
LayoutRules.


def get_experiment_in_group(self, group, bucketing_id):
    experiment_id = self.bucketer.find_bucket(bucketing_id, group.id, group.trafficAllocation)
    if experiment_id:
        experiment = self.config.get_experiment_from_id(experiment_id)
        if experiment:
            self.logger.info(('User with bucketing ID "%s" is in experiment %s of group %s.' % (bucketing_id, experiment.key, group.id)))
            return experiment
    self.logger.info(('User with bucketing ID "%s" is not in any experiments of group %s.' % (bucketing_id, group.id)))
    return None

Determine which experiment in the group the user is bucketed into.

Args:
group: The group to bucket the user into.
bucketing_id: ID to be used for bucketing the user.

Returns:
Experiment if the user is bucketed into an experiment in the specified group. None otherwise.


def oauth2_callback(request):
    if ('error' in request.GET):
        reason = request.GET.get('error_description', request.GET.get('error', ''))
        reason = html.escape(reason)
        return http.HttpResponseBadRequest('Authorization failed {0}'.format(reason))
    try:
        encoded_state = request.GET['state']
        code = request.GET['code']
    except KeyError:
        return http.HttpResponseBadRequest('Request missing state or authorization code')
    try:
        server_csrf = request.session[_CSRF_KEY]
    except KeyError:
        return http.HttpResponseBadRequest('No existing session for this flow.')
    try:
        state = json.loads(encoded_state)
        client_csrf = state['csrf_token']
        return_url = state['return_url']
    except (ValueError, KeyError):
        return http.HttpResponseBadRequest('Invalid state parameter.')
    if (client_csrf != server_csrf):
        return http.HttpResponseBadRequest('Invalid CSRF token.')
    flow = _get_flow_for_token(client_csrf, request)
    if (not flow):
        return http.HttpResponseBadRequest('Missing Oauth2 flow.')
    try:
        credentials = flow.step2_exchange(code)
    except client.FlowExchangeError as exchange_error:
        return http.HttpResponseBadRequest('An error has occurred: {0}'.format(exchange_error))
    get_storage(request).put(credentials)
    signals.oauth2_authorized.send(sender=signals.oauth2_authorized, request=request, credentials=credentials)
    return shortcuts.redirect(return_url)

View that handles the user's return from OAuth2 provider.

This view verifies the CSRF state and OAuth authorization code, and on
success stores the credentials obtained in the storage provider,
and redirects to the return_url specified in the authorize view and
stored in the session.

Args:
request: Django request.

Returns:
A redirect response back to the return_url.


def run(self, host='localhost', port=8000, shutdown_timeout=60.0, **kwargs):
    print((('Running service on http:
    self.config.port = port
    self.config.host = host
    try:
        if self.event_broker:
            self.event_broker.start()
            self.loop.run_until_complete(self.announce())
        http_handler = self.app.make_handler()
        self._http_server = self.loop.create_server(http_handler, host, port)
        self._server_handler = self.loop.run_until_complete(self._http_server)
        self.loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        try:
            self.cleanup()
        except UnboundLocalError:
            pass
        self.loop.close()

This function starts the service's network intefaces.

Args:
port (int): The port for the http server.


def _FormatExpression(self, frame, expression):
    (rc, value) = _EvaluateExpression(frame, expression)
    if (not rc):
        message = _FormatMessage(value['description']['format'], value['description'].get('parameters'))
        return (('<' + message) + '>')
    return self._FormatValue(value)

Evaluates a single watched expression and formats it into a string form.

If expression evaluation fails, returns error message string.

Args:
frame: Python stack frame in which the expression is evaluated.
expression: string expression to evaluate.

Returns:
Formatted expression value that can be used in the log message.


def get_metric_group_infos(self):
    mg_defs = self.get_metric_group_definitions()
    mg_infos = []
    for mg_def in mg_defs:
        metric_infos = []
        for (metric_name, metric_type) in mg_def.types:
            metric_infos.append({'metric-name': metric_name, 'metric-type': metric_type})
        mg_info = {'group-name': mg_def.name, 'metric-infos': metric_infos}
        mg_infos.append(mg_info)
    return mg_infos

Get the faked metric group definitions for this context object
that are to be returned from its create operation, in the format
needed for the "Create Metrics Context" operation response.

Returns:

"metric-group-infos" JSON object as described for the "Create Metrics
Context "operation response.


def hwvtep_attach_vlan_vid(self, **kwargs):

        name = kwargs.pop('name')
        mac = kwargs.pop('mac')
        vlan = kwargs.pop('vlan')
        name_args = dict(name=name, vid=vlan, mac=mac)
        method_name = 'overlay_gateway_attach_vlan_mac'
        method_class = self._brocade_tunnels
        gw_attr = getattr(method_class, method_name)
        config = gw_attr(**name_args)
        output = self._callback(config)
        return output

Identifies exported VLANs in VXLAN gateway configurations.

Args:
name (str): overlay_gateway name
vlan(str):  vlan_id range
callback (function): A function executed upon completion of the
method.

Returns:
Return value of `callback`.

Raises:
None


def _allocate_subnets(self, conf):

        allocated_subnets = []
        try:
            for net_spec in conf.get('nets', {}).itervalues():
                if net_spec['type'] != 'nat':
                    continue

                gateway = net_spec.get('gw')
                if gateway:
                    allocated_subnet = self._subnet_store.acquire(
                        self.paths.uuid(), gateway
                    )
                else:
                    allocated_subnet = self._subnet_store.acquire(
                        self.paths.uuid()
                    )
                    net_spec['gw'] = str(allocated_subnet.iter_hosts().next())

                allocated_subnets.append(allocated_subnet)
        except:
            self._subnet_store.release(allocated_subnets)
            raise
        return allocated_subnets, conf

Allocate all the subnets needed by the given configuration spec

Args:
conf (dict): Configuration spec where to get the nets definitions
from

Returns:
tuple(list, dict): allocated subnets and modified conf


def show_error(self, message):
    assert isinstance(message, string_types)
    self.post('error', data=message)

Send an error message to the active client. The new error will be
displayed on any active GUI clients.

Args:
message (str): Plain-text message to display.

Returns:
None

>>> s = _syncthing()
>>> s.system.show_error('my error msg')
>>> s.system.errors()[0]
... # doctest: +ELLIPSIS
ErrorEvent(when=datetime.datetime(...), message='"my error msg"')
>>> s.system.clear_errors()
>>> s.system.errors()
[]


def user(self, email):

        LOG.info("Fetching user %s", email)
        user_obj = self.user_collection.find_one({'_id': email})

        return user_obj

Fetch a user from the database.

Args:
email(str)

Returns:
user_obj(dict)


def __init__(self, fn):
    if not callable(fn):
        raise TypeError('Expected a callable object instead of: %r' % fn)
    self._fn = fn

Initializes a PartitionFn object wrapping a callable.

Args:
fn: A callable object, which should accept the following arguments:
element - element to assign to a partition.
num_partitions - number of output partitions.
and may accept additional arguments and side inputs.

Raises:
TypeError: if fn is not a callable type.


def find_previous(a, value, index=False, return_distance=False):
    b = (a - value)
    i = np.where((b > 0))[0][0]
    d = ((value - a[(i - 1)]) / (a[i] - a[(i - 1)]))
    if index:
        if return_distance:
            return ((i - 1), d)
        else:
            return (i - 1)
    elif return_distance:
        return (a[(i - 1)], d)
    else:
        return a[(i - 1)]

Find the nearest array value, or index of the array value, before some
given value. Optionally also return the fractional distance of the given
value from that previous value.

Args:
a (ndarray)
value (float)
index (bool): whether to return the index instead of the array value.
Default: False.
return_distance(bool): whether to return the fractional distance from
the nearest value to the specified value. Default: False.

Returns:
float. The array value (or index, as int) before the specified value.
If ``return_distance==True`` then a tuple is returned, where the
second value is the distance.


def bfloat16_activations_var_getter(getter, *args, **kwargs):
    requested_dtype = kwargs['dtype']
    if (requested_dtype == tf.bfloat16):
        kwargs['dtype'] = tf.float32
    var = getter(*args, **kwargs)
    if (var.dtype.base_dtype != requested_dtype):
        var = tf.cast(var, requested_dtype)
    return var

A custom getter function for float32 parameters and bfloat16 activations.

Args:
getter: custom getter
*args: arguments
**kwargs: keyword arguments
Returns:
variables with the correct dtype.
Raises:
KeyError: if "dtype" is not provided as a kwarg.


def create_view(self, state_root_hash=None):

        if state_root_hash is None:
            state_root_hash = INIT_ROOT_KEY

        merkle_db = MerkleDatabase(self._database,
                                   merkle_root=state_root_hash)

        return StateView(merkle_db)

Creates a StateView for the given state root hash.

Args:
state_root_hash (str): The state root hash of the state view
to return.  If None, returns the state view for the
Returns:
StateView: state view locked to the given root hash.


def traverse_data(obj, use_numpy=True, buffers=None):
    if (use_numpy and all((isinstance(el, np.ndarray) for el in obj))):
        return [transform_array(el, buffers=buffers) for el in obj]
    obj_copy = []
    for item in obj:
        if (type(item) is float):
            if math.isnan(item):
                item = 'NaN'
            elif math.isinf(item):
                if (item > 0):
                    item = 'Infinity'
                else:
                    item = '-Infinity'
            obj_copy.append(item)
        elif isinstance(item, (list, tuple)):
            obj_copy.append(traverse_data(item))
        else:
            obj_copy.append(item)
    return obj_copy

Recursively traverse an object until a flat list is found.

If NumPy is available, the flat list is converted to a numpy array
and passed to transform_array() to handle ``nan``, ``inf``, and
``-inf``.

Otherwise, iterate through all items, converting non-JSON items

Args:
obj (list) : a list of values or lists
use_numpy (bool, optional) toggle NumPy as a dependency for testing
This argument is only useful for testing (default: True)


def recipe_floodlight_monitor(config, auth_read, dcm_account, sheet):
    floodlight_monitor(config, {'auth': auth_read, 'account': dcm_account, 'template': {'template': {'sheet': 'https:

Monitor floodlight impressions specified in sheet and send email alerts.

Args:
auth_read (authentication) - Credentials used for reading data.
dcm_account (string) - Specify an account_id as a number.
sheet (string) - Full Name or URL to Google Sheet, Floodlight Monitor tab will be added.


def from_gpx(gpx_track_point):

        return Point(
            lat=gpx_track_point.latitude,
            lon=gpx_track_point.longitude,
            time=gpx_track_point.time
        )

Creates a point from GPX representation

Arguments:
gpx_track_point (:obj:`gpxpy.GPXTrackPoint`)
Returns:
:obj:`Point`


def _transform_cur_commands(cur_commands, alias_table=None):
    transformed = []
    alias_table = (alias_table if alias_table else get_alias_table())
    for cmd in cur_commands:
        if ((cmd in alias_table.sections()) and alias_table.has_option(cmd, 'command')):
            transformed += alias_table.get(cmd, 'command').split()
        else:
            transformed.append(cmd)
    cur_commands[:] = transformed

Transform any aliases in cur_commands into their respective commands.

Args:
alias_table: The alias table.
cur_commands: current commands typed in the console.


def print_info(self, buf=None, format_=FileFormat.yaml, skip_attributes=None, include_release=False):
    data = self.validated_data().copy()
    data.pop('config', None)
    if self.config:
        if isinstance(self, Package):
            config_dict = self.data.get('config')
        else:
            config_dict = self.parent.data.get('config')
        data['config'] = config_dict
    if (not include_release):
        skip_attributes = (list((skip_attributes or [])) + list(package_release_keys))
    buf = (buf or sys.stdout)
    dump_package_data(data, buf=buf, format_=format_, skip_attributes=skip_attributes)

Print the contents of the package.

Args:
buf (file-like object): Stream to write to.
format_ (`FileFormat`): Format to write in.
skip_attributes (list of str): List of attributes to not print.
include_release (bool): If True, include release-related attributes,
such as 'timestamp' and 'changelog'


def _subsample_labels(self, label):
    pos_idx, neg_idx = subsample_labels(label, self.batch_size_per_image, self.positive_fraction, 0)
    label.fill_(-1)
    label.scatter_(0, pos_idx, 1)
    label.scatter_(0, neg_idx, 0)
    return label

Randomly sample a subset of positive and negative examples, and overwrite the label vector to the ignore value
(-1) for all elements that are not included in the sample.

Args:
labels (Tensor): a vector of -1, 0, 1. Will be modified in-place and returned.


def sign(x):
    return math_ops.sign(x)

Element-wise sign.

Args:
x: Tensor or variable.

Returns:
A tensor.


def reconstruct_text(tokens: List[Token]) -> str:
    return ''.join([x.text_with_ws for x in tokens])

Given a list of tokens, reconstruct the original text with as much fidelity as possible.

Args:
[tokens]:

Returns: a string.


def initialize_plugs(self, plug_types=None):
    types = (plug_types if (plug_types is not None) else self._plug_types)
    for plug_type in types:
        plug_logger = self.logger.getChild(plug_type.__name__)
        if (plug_type in self._plugs_by_type):
            continue
        try:
            if (not issubclass(plug_type, BasePlug)):
                raise InvalidPlugError(('Plug type "%s" is not an instance of BasePlug' % plug_type))
            if (plug_type.logger != _LOG):
                raise InvalidPlugError('Do not override "logger" in your plugs.', plug_type)
            plug_type.logger = plug_logger
            try:
                plug_instance = plug_type()
            finally:
                plug_type.logger = _LOG
            if (plug_instance.logger != _LOG):
                raise InvalidPlugError('Do not set "self.logger" in __init__ in your plugs', plug_type)
            else:
                plug_instance.logger = plug_logger
        except Exception:
            plug_logger.exception('Exception instantiating plug type %s', plug_type)
            self.tear_down_plugs()
            raise
        self.update_plug(plug_type, plug_instance)

Instantiate required plugs.

Instantiates plug types and saves the instances in self._plugs_by_type for
use in provide_plugs().

Args:
plug_types: Plug types may be specified here rather than passed
into the constructor (this is used primarily for unit testing
phases).


def Instance(reactor=None):

        if NodeLeader._LEAD is None:
            NodeLeader._LEAD = NodeLeader(reactor)
        return NodeLeader._LEAD

Get the local node instance.

Args:
reactor: (optional) custom reactor to use in NodeLeader.

Returns:
NodeLeader: instance.


def write(self, noautocmd=False):
    cmd = ('noautocmd write' if noautocmd else 'write')
    self._vim.command(cmd)

Writes the file of the current buffer.

Args:
noautocmd (bool): If true, write will skip autocommands.

Todo:
We should consider whether ``SourceFileInfo`` can replace most
usage of noautocmd. See #298


def __init__(self, types=None, capabilities=None, max_groups1=None,
                 max_groups2=None, max_groups3=None, max_groups4=None,
                 actions1=None, actions2=None, actions3=None, actions4=None):

        super().__init__()
        self.types = types
        self.capabilities = capabilities
        self.max_groups1 = max_groups1
        self.max_groups2 = max_groups2
        self.max_groups3 = max_groups3
        self.max_groups4 = max_groups4
        self.actions1 = actions1
        self.actions2 = actions2
        self.actions3 = actions3
        self.actions4 = actions4

Create a GroupFeatures with the optional parameters below.

Args:
types: Bitmap of OFPGT_* values supported.
capabilities: Bitmap of OFPGFC_* capability supported.
max_groups: 4-position array; Maximum number of groups for each
type.
actions: 4-position array; Bitmaps of OFPAT_* that are supported.


def visit_membership(self, relation: _evaluation.MembershipRelationNode) -> Any:
    lhs_result = self.visit(relation.left)
    rhs_result = self.visit(relation.right)
    in_lhs = lhs_result if isinstance(relation, _evaluation.InNode) else rhs_result
    in_rhs = rhs_result if isinstance(relation, _evaluation.InNode) else lhs_result
    sql_expr = f'({in_lhs.as_operand()})\nIN ({in_rhs.as_operand()})'
    return _sql_data_types.Select(select_part=_sql_data_types.RawExpression(sql_expr, _sql_data_type=_sql_data_types.Boolean, _sql_alias='mem_'), from_part=None)

Translates a FHIRPath membership relation to Standard SQL.

For the `IN` relation, the LHS operand is assumed to be a collection of a
single value. For 'CONTAINS', the RHS operand is assumed to be a collection
of a single value.

Args:
relation: The FHIRPath AST `MembershipRelation` node.

Returns:
A compiled Standard SQL expression.


def load_checkpoint(ckpt_dir_or_file):
    filename = _get_checkpoint_filename(ckpt_dir_or_file)
    if filename is None:
        raise ValueError("Couldn't find 'checkpoint' file or checkpoints in given directory %s" % ckpt_dir_or_file)
    return py_checkpoint_reader.NewCheckpointReader(filename)

Returns `CheckpointReader` for checkpoint found in `ckpt_dir_or_file`.

If `ckpt_dir_or_file` resolves to a directory with multiple checkpoints,
reader for the latest checkpoint is returned.

Example usage:

```python
import tensorflow as tf
a = tf.Variable(1.0)
b = tf.Variable(2.0)
ckpt = tf.train.Checkpoint(var_list={'a': a, 'b': b})
ckpt_path = ckpt.save('tmp-ckpt')
reader= tf.train.load_checkpoint(ckpt_path)
print(reader.get_tensor('var_list/a/.ATTRIBUTES/VARIABLE_VALUE'))  # 1.0
```

Args:
ckpt_dir_or_file: Directory with checkpoints file or path to checkpoint
file.

Returns:
`CheckpointReader` object.

Raises:
ValueError: If `ckpt_dir_or_file` resolves to a directory with no
checkpoints.


def SetHeaders(self, soap_headers, http_headers):

    self.suds_client.set_options(soapheaders=soap_headers, headers=http_headers)

Set the headers for the underlying client.

Args:
soap_headers: A SOAP element for the SOAP headers.
http_headers: A dictionary for the http headers.


def setup_modules(self, args):

    def _setup_module_thread(module_description):
        "Calls the module's setup() function and sets an Event object for it.\n\n      Args:\n        module_description (dict): Corresponding recipe module description.\n      "
        new_args = utils.import_args_from_dict(module_description['args'], vars(args), self.config)
        module = self._module_pool[module_description['name']]
        try:
            module.setup(**new_args)
        except Exception as error:
            self.add_error('An unknown error occurred: {0!s}\nFull traceback:\n{1:s}'.format(error, traceback.format_exc()), critical=True)
        self.events[module_description['name']] = threading.Event()
        self.cleanup()
    threads = []
    for module_description in self.recipe['modules']:
        t = threading.Thread(target=_setup_module_thread, args=(module_description,))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()
    self.check_errors(is_global=True)

Performs setup tasks for each module in the module pool.

Threads declared modules' setup() functions. Takes CLI arguments into
account when replacing recipe parameters for each module.

Args:
args: Command line arguments that will be used to replace the parameters
declared in the recipe.


def haversine(px, py, r=r_mm):

    lat1, lon1 = px
    lat2, lon2 = py

    dlat = math.radians(lat2 - lat1)
    dlon = math.radians(lon2 - lon1)
    lat1 = math.radians(lat1)
    lat2 = math.radians(lat2)

    a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
    c = 2 * math.asin(math.sqrt(a))

    return c * r

Calculate the haversine distance between two points
defined by (lat,lon) tuples.

Args:
px ((float,float)): lat/long position 1
py ((float,float)): lat/long position 2
r (float): Radius of sphere

Returns:
(int):  Distance in mm.


def __init__(self, exit_node: tensor_lib.Tensor, pfor_ops: List[ops.Operation], fallback_to_while_loop: bool, pfor_config: 'PForConfig'):
    self._fallback_to_while_loop = fallback_to_while_loop
    self._pfor_config = pfor_config
    self._pfor_ops = set(pfor_ops)
    self._pfor_op_ids = set((x._id for x in pfor_ops))
    assert isinstance(exit_node, tensor_lib.Tensor)
    self._while_context = exit_node.op._get_control_flow_context()
    assert isinstance(self._while_context, control_flow_ops.WhileContext)
    self._context_name = self._while_context.name
    self._condition = self._while_context.pivot.op.inputs[0]
    self._is_inside_loop = self.op_is_inside_loop(self._condition.op)
    if self._is_inside_loop:
        for e in self._while_context.loop_exits:
            assert self.op_is_inside_loop(e.op)
    self._exit_switches = []
    self._body_outputs = []
    self._next_iter_control_inputs = []
    self._enter_merges = []
    self._outputs = []
    self._enters = []
    self._direct_enters = []
    for e in self._while_context.loop_exits:
        self._outputs.append(e.op.outputs[0])
        switch = e.op.inputs[0].op
        assert switch.type == 'Switch', switch
        self._exit_switches.append(switch)
        merge = switch.inputs[0].op
        assert merge.type == 'Merge', merge
        self._enter_merges.append(merge)
        enter = merge.inputs[0].op
        assert enter.type == 'Enter', enter
        self._enters.append(enter.outputs[0])
        next_iter = merge.inputs[1].op
        assert next_iter.type == 'NextIteration', next_iter
        self._body_outputs.append(next_iter.inputs[0])
        self._next_iter_control_inputs.append(next_iter.control_inputs)
    self._is_stateful = False
    for op in ops.get_default_graph().get_operations():
        control_flow_context = op._get_control_flow_context()
        if control_flow_context is None:
            continue
        if control_flow_context.name == self._context_name:
            self._is_stateful |= _is_stateful_pfor_op(op)
            if op.type == 'Enter':
                output = op.outputs[0]
                if output not in self._enters:
                    if output.dtype in (dtypes.resource, dtypes.variant):
                        if output not in self._direct_enters:
                            self._direct_enters.append(output)
                    else:
                        self._enters.append(output)

Initializer.

Args:
exit_node: A tensor output from the while_loop.
pfor_ops: list of ops inside the current pfor loop.
fallback_to_while_loop: If True, fallback to while loop when conversion of
an op is not supported
pfor_config: PForConfig object used while constructing loop body.


def __init__(self, key_value_pairs):

        self._dict = OrderedDict()
        for key, value in key_value_pairs:
            if key not in self._dict:
                self._dict[key] = []
            self._dict[key].append(value)

        for key, value in iteritems(self._dict):
            grouping = Grouping(key, value)
            self._dict[key] = grouping

        super(Lookup, self).__init__(self._dict)

Construct a Lookup with a sequence of (key, value) tuples.

Args:
key_value_pairs:
An iterable over 2-tuples each containing a key, value pair.


def clone(self, *args, **overrides):
    clone = super(Layout, self).clone(*args, **overrides)
    clone._max_cols = self._max_cols
    return clone

Clones the Layout, overriding data and parameters.

Args:
data: New data replacing the existing data
shared_data (bool, optional): Whether to use existing data
new_type (optional): Type to cast object to
*args: Additional arguments to pass to constructor
**overrides: New keyword arguments to pass to constructor

Returns:
Cloned Layout object


def list_fastboot_devices():
    out = fastboot.FastbootProxy().devices()
    return parse_device_list(out)

List all android devices connected to the computer that are in in
fastboot mode. These are detected by fastboot.

This function doesn't raise any error if `fastboot` binary doesn't exist,
because `FastbootProxy` itself doesn't raise any error.

Returns:
A list of android device serials. Empty if there's none.


def set_user(self, user):

        self.session['user_id'] = user.key
        self.session['user_data'] = user.clean_value()
        role = self.get_role()

        self.session['role_id'] = role.key
        self.current.role_id = role.key
        self.current.user_id = user.key

        self.session['permissions'] = role.get_permissions()

Writes user data to session.

Args:
user: User object


def get_search_space(ss_indicator):
    info = nats_bench.search_space_info('nats-bench', ss_indicator)
    if ss_indicator == 'tss':
        total = info['num_nodes'] * (info['num_nodes'] - 1)
        return model_tss_spc(pg.sublist_of(total, info['op_names'], choices_distinct=False), info['num_nodes'])
    elif ss_indicator == 'sss':
        return model_sss_spc(pg.sublist_of(info['num_layers'], info['candidates'], choices_distinct=False))

The default search space in NATS-Bench.

Args:
ss_indicator: tss or sss, indicating the topology or size search space.

Returns:
A hyper model object that repesents a search space.


def find_existing_record(env, zone_id, dns_name, check_key=None, check_value=None):
    client = boto3.Session(profile_name=env).client('route53')
    pager = client.get_paginator('list_resource_record_sets')
    existingrecord = None
    for rset in pager.paginate(HostedZoneId=zone_id):
        for record in rset['ResourceRecordSets']:
            if check_key:
                if ((record['Name'].rstrip('.') == dns_name) and (record.get(check_key) == check_value)):
                    LOG.info('Found existing record: %s', record)
                    existingrecord = record
                    break
    return existingrecord

Check if a specific DNS record exists.

Args:
env (str): Deployment environment.
zone_id (str): Route53 zone id.
dns_name (str): FQDN of application's dns entry to add/update.
check_key(str): Key to look for in record. Example: "Type"
check_value(str): Value to look for with check_key. Example: "CNAME"

Returns:
json: Found Record. Returns None if no record found


def __init__(self, parameter_name, value, type_name):

    super(BasicTypeParameterError, self).__init__(parameter_name, value)
    self.type_name = type_name

Constructor for BasicTypeParameterError.

Args:
parameter_name: String; the name of the parameter which had a value
rejected.
value: The actual value passed in for the enum. Usually string.
type_name: Descriptive name of the data type expected.


def __init__(self, query_builder, field):

    self._field = field
    self._query_builder = query_builder
    self._awql = None

Creates the WHERE builder with specified query builder and field.

This class should be instantiated through _QueryBuilder.Where. Don't call
this constructor directly.

Args:
query_builder: The query builder that this WHERE builder links to.
field: The field to be used in the WHERE condition.

Returns:
The WHERE builder.


def _get_css_files(cls, extra_files):

        packager = Packager()
        css_packages = getattr(cls, 'css_packages', {})

        return dict(
            (media_target,
             cls._get_media_files(packager=packager,
                                  media_packages=media_packages,
                                  media_type='css',
                                  extra_files=extra_files.get(media_target,
                                                              [])))
            for media_target, media_packages in six.iteritems(css_packages)
        )

Return all CSS files from the Media class.

Args:
extra_files (dict):
The contents of the Media class's original :py:attr:`css`
attribute, if one was provided.

Returns:
dict:
The CSS media types and files to return for the :py:attr:`css`
attribute.


def __init__(
      self, session, output_file,
      storage_type=definitions.STORAGE_TYPE_SESSION, task=None):

    super(StorageFileWriter, self).__init__(
        session, storage_type=storage_type, task=task)
    self._merge_task_storage_path = ''
    self._output_file = output_file
    self._processed_task_storage_path = ''
    self._storage_file = None
    self._task_storage_path = None

Initializes a storage writer.

Args:
session (Session): session the storage changes are part of.
output_file (str): path to the output file.
storage_type (Optional[str]): storage type.
task(Optional[Task]): task.


def plugin_privileges(self, name):
    params = {'remote': name}
    headers = {}
    (registry, repo_name) = auth.resolve_repository_name(name)
    header = auth.get_config_header(self, registry)
    if header:
        headers['X-Registry-Auth'] = header
    url = self._url('/plugins/privileges')
    return self._result(self._get(url, params=params, headers=headers), True)

Retrieve list of privileges to be granted to a plugin.

Args:
name (string): Name of the remote plugin to examine. The
``:latest`` tag is optional, and is the default if omitted.

Returns:
A list of dictionaries representing the plugin's
permissions


def save_image(figure, filename):

    path = os.path.join(IMAGES_DIR, filename)
    figure.savefig(path, bbox_inches="tight")
    plt.close(figure)

Save an image to the docs images directory.

Args:
filename (str): The name of the file (not containing
directory info).


def _ParseOrMerge(self, lines, message):
    tokenizer = Tokenizer(lines)
    while (not tokenizer.AtEnd()):
        self._MergeField(tokenizer, message)

Converts a text representation of a protocol message into a message.

Args:
lines: Lines of a message's text representation.
message: A protocol buffer message to merge into.

Raises:
ParseError: On text parsing problems.


def is_greater(a,b):

    a_chrom = CHROM_TO_INT.get(a.chrom,0)
    b_chrom = CHROM_TO_INT.get(b.chrom,0)

    if (a_chrom == 0 or b_chrom == 0):
        return False

    if a_chrom > b_chrom:
        return True

    if a_chrom == b_chrom:
        if a.pos > b.pos:
            return True

    return False

Check if position a is greater than position b
This will look at chromosome and position.

For example a position where chrom = 2 and pos = 300 is greater than a position where
chrom = 1 and pos = 1000

If any of the chromosomes is outside [1-22,X,Y,MT] we can not say which is biggest.

Args:
a,b(Position)

Returns:
bool: True if a is greater than b


def log_first_n(level, msg, n, *args):
    count = _GetNextLogCountPerToken(_GetFileAndLine())
    log_if(level, msg, count < n, *args)

Log 'msg % args' at level 'level' only first 'n' times.

Not threadsafe.

Args:
level: The level at which to log.
msg: The message to be logged.
n: The number of times this should be called before it is logged.
*args: The args to be substituted into the msg.


def _uniquify_fetches(fetch_mappers):
    unique_fetches = []
    value_indices = []
    seen_fetches = {}
    for m in fetch_mappers:
        m_value_indices = []
        for f in m.unique_fetches():
            j = seen_fetches.get(id(f))
            if j is None:
                j = len(seen_fetches)
                seen_fetches[id(f)] = j
                unique_fetches.append(f)
            m_value_indices.append(j)
        value_indices.append(m_value_indices)
    return (unique_fetches, value_indices)

Uniquifies fetches from a list of fetch_mappers.

This is a utility function used by _ListFetchMapper and _DictFetchMapper.  It
gathers all the unique fetches from a list of mappers and builds a list
containing all of them but without duplicates (unique_fetches).

It also returns a 2-D list of integers (values_indices) indicating at which
index in unique_fetches the fetches of the mappers are located.

This list is as follows:
values_indices[mapper_index][mapper_fetch_index] = unique_fetches_index

Args:
fetch_mappers: list of fetch mappers.

Returns:
A list of fetches.
A 2-D list of integers.


def Log(self, frame):

    if not self._log_message:
      return {'isError': True,
              'description': {'format': LOG_ACTION_NOT_SUPPORTED}}

    if self._quota_recovery_start_time:
      ms_elapsed = (time.time() - self._quota_recovery_start_time) * 1000
      if ms_elapsed > self.quota_recovery_ms:

        self._quota_recovery_start_time = None
      else:

        return

    message = 'LOGPOINT: ' + _FormatMessage(
        self._definition.get('logMessageFormat', ''),
        self._EvaluateExpressions(frame))

    line = self._definition['location']['line']
    cdbg_logging_location = (NormalizePath(frame.f_code.co_filename), line,
                             _GetFrameCodeObjectName(frame))

    if native.ApplyDynamicLogsQuota(len(message)):
      self._log_message(message)
    else:
      self._quota_recovery_start_time = time.time()
      self._log_message(DYNAMIC_LOG_OUT_OF_QUOTA)
    del cdbg_logging_location
    return None

Captures the minimal application states, formats it and logs the message.

Args:
frame: Python stack frame of breakpoint hit.

Returns:
None on success or status message on error.


def no_results(channel):

    gui = ui_embed.UI(
        channel,
        "No results",
        ":c",
        modulename=modulename,
        colour=0xFF8800
    )

    return gui

Creates an embed UI for when there were no results

Args:
channel (discord.Channel): The Discord channel to bind the embed to

Returns:
ui (ui_embed.UI): The embed UI object


def FlatbufferToDict(fb, preserve_as_numpy):
    if isinstance(fb, int) or isinstance(fb, float) or isinstance(fb, str):
        return fb
    elif hasattr(fb, '__dict__'):
        result = {}
        for attribute_name in dir(fb):
            attribute = fb.__getattribute__(attribute_name)
            if not callable(attribute) and attribute_name[0] != '_':
                snake_name = CamelCaseToSnakeCase(attribute_name)
                preserve = True if attribute_name == 'buffers' else preserve_as_numpy
                result[snake_name] = FlatbufferToDict(attribute, preserve)
        return result
    elif isinstance(fb, np.ndarray):
        return fb if preserve_as_numpy else fb.tolist()
    elif hasattr(fb, '__len__'):
        return [FlatbufferToDict(entry, preserve_as_numpy) for entry in fb]
    else:
        return fb

Converts a hierarchy of FB objects into a nested dict.

We avoid transforming big parts of the flat buffer into python arrays. This
speeds conversion from ten minutes to a few seconds on big graphs.

Args:
fb: a flat buffer structure. (i.e. ModelT)
preserve_as_numpy: true if all downstream np.arrays should be preserved.
false if all downstream np.array should become python arrays
Returns:
A dictionary representing the flatbuffer rather than a flatbuffer object.


def can_handle(x, y=None):
    raise NotImplementedError

Whether the current DataAdapter could handle the input x and y.

Structure wise, x and y can be single object, or list of objects if there
multiple input/output, or dictionary of objects when the intput/output are
named.

Args:
x: input features.
y: target labels. Note that y could be None in the case of prediction.

Returns:
boolean


def run_local(self, commands):

        process = subprocess.Popen(
            commands.get('cli_command'),
            shell=self.shell,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        out, err = process.communicate()

        self.run_display_app_output(out)
        self.run_display_app_errors(err)

        return self.run_exit_code(process.returncode)

Run the App on local system.

Args:
commands (dict): A dictionary of the CLI commands.

Returns:
int: The exit code of the subprocess command.


def add_subtask(self, subtask):
    if self.stopped:
        raise InternalError('Cannot add a subtask to a parent that is already stopped')
    if (not isinstance(subtask, BackgroundTask)):
        raise ArgumentError('Subtasks must inherit from BackgroundTask, task={}'.format(subtask))
    if (subtask._loop != self._loop):
        raise ArgumentError('Subtasks must run in the same BackgroundEventLoop as their parent', subtask=subtask, parent=self)
    self.subtasks.append(subtask)

Link a subtask to this parent task.

This will cause stop() to block until the subtask has also
finished.  Calling stop will not directly cancel the subtask.
It is expected that your finalizer for this parent task will
cancel or otherwise stop the subtask.

Args:
subtask (BackgroundTask): Another task that will be stopped
when this task is stopped.


def init(module_paths, work_db, config):

    operator_names = cosmic_ray.plugins.operator_names()
    work_db.set_config(config=config)

    work_db.clear()

    for module_path in module_paths:
        module_ast = get_ast(
            module_path, python_version=config.python_version)

        for op_name in operator_names:
            operator = get_operator(op_name)(config.python_version)
            visitor = WorkDBInitVisitor(module_path, op_name, work_db,
                                        operator)
            visitor.walk(module_ast)

    apply_interceptors(work_db, config.sub('interceptors').get('enabled', ()))

Clear and initialize a work-db with work items.

Any existing data in the work-db will be cleared and replaced with entirely
new work orders. In particular, this means that any results in the db are
removed.

Args:
module_paths: iterable of pathlib.Paths of modules to mutate.
work_db: A `WorkDB` instance into which the work orders will be saved.
config: The configuration for the new session.


def delete(self, project_id):

        self.logger.debug('Deleting project by id: ' + project_id)
        url = '%(base_url)s/%(project_id)s' % {
            'base_url': self.base_url, 'project_id': project_id
        }
        r = self.gbdx_connection.delete(url)
        r.raise_for_status()

Deletes a project by id

Args:
project_id: The project id to delete

Returns:
Nothing


def enum_from_yaml(cls: Type[T_EnumFromYAML], constructor: Constructor, node: ruamel.yaml.nodes.ScalarNode) -> T_EnumFromYAML:
    return cls[node.value]

Decode YAML representation.

This is a mixin method for reading enum values from YAML. It needs to be added to the enum
as a classmethod. See the module docstring for further information on this approach and how
to implement it.

Note:
This method assumes that the name of the enumeration value was stored as a scalar node.

Args:
constructor: Constructor from the YAML object.
node: Scalar node extracted from the YAML being read.
Returns:
The constructed YAML value from the name of the enumerated value.


def _validate_state_spec(cell_state_sizes, init_state_specs):
    validation_error = ValueError('An `initial_state` was passed that is not compatible with `cell.state_size`. Received `state_spec`={}; however `cell.state_size` is {}'.format(init_state_specs, cell_state_sizes))
    flat_cell_state_sizes = nest.flatten(cell_state_sizes)
    flat_state_specs = nest.flatten(init_state_specs)
    if len(flat_cell_state_sizes) != len(flat_state_specs):
        raise validation_error
    for cell_state_spec, cell_state_size in zip(flat_state_specs, flat_cell_state_sizes):
        if not tensor_shape.TensorShape(cell_state_spec.shape[1:]).is_compatible_with(tensor_shape.TensorShape(cell_state_size)):
            raise validation_error

Validate the state spec between the initial_state and the state_size.

Args:
cell_state_sizes: list, the `state_size` attribute from the cell.
init_state_specs: list, the `state_spec` from the initial_state that is
passed in `call()`.

Raises:
ValueError: When initial state spec is not compatible with the state size.


def create_chebyshev_samples(order, dim=1):
    x_data = ((0.5 * numpy.cos(((numpy.arange(order, 0, (- 1)) * numpy.pi) / (order + 1)))) + 0.5)
    x_data = chaospy.quad.combine(([x_data] * dim))
    return x_data.T

Chebyshev sampling function.

Args:
order (int):
The number of samples to create along each axis.
dim (int):
The number of dimensions to create samples for.

Returns:
samples following Chebyshev sampling scheme mapped to the
``[0, 1]^dim`` hyper-cube and ``shape == (dim, order)``.


def dump(voevent, file, pretty_print=True, xml_declaration=True):

    file.write(dumps(voevent, pretty_print, xml_declaration))

Writes the voevent to the file object.

e.g.::

with open('/tmp/myvoevent.xml','wb') as f:
voeventparse.dump(v, f)

Args:
voevent(:class:`Voevent`): Root node of the VOevent etree.
file (io.IOBase): An open (binary mode) file object for writing.
pretty_print
pretty_print(bool): See :func:`dumps`
xml_declaration(bool): See :func:`dumps`


def master(self, task_type=None, task_id=None, rpc_layer=None):
    task_type = task_type if task_type is not None else self.task_type
    task_id = task_id if task_id is not None else self.task_id
    if task_type is not None and task_id is not None:
        return format_master_url(self.cluster_spec().task_address(task_type, task_id), rpc_layer or self.rpc_layer)
    return ''

Returns the master address to use when creating a session.

You must have set the task_type and task_id object properties before
calling this function, or pass in the `task_type` and `task_id`
parameters when using this function. If you do both, the function parameters
will override the object properties.

Note: this is only useful for TensorFlow 1.x.

Args:
task_type: (Optional) The type of the TensorFlow task of the master.
task_id: (Optional) The index of the TensorFlow task of the master.
rpc_layer: (Optional) The RPC protocol for the given cluster.

Returns:
The name or URL of the session master.


def AddArguments(cls, argument_group):

    argument_group.add_argument(
        '--viper-hash', '--viper_hash', dest='viper_hash', type=str,
        action='store', choices=viper.ViperAnalyzer.SUPPORTED_HASHES,
        default=cls._DEFAULT_HASH, metavar='HASH', help=(
            'Type of hash to use to query the Viper server, the default is: '
            '{0:s}. Supported options: {1:s}').format(
                cls._DEFAULT_HASH, ', '.join(
                    viper.ViperAnalyzer.SUPPORTED_HASHES)))

    argument_group.add_argument(
        '--viper-host', '--viper_host', dest='viper_host', type=str,
        action='store', default=cls._DEFAULT_HOST, metavar='HOST',
        help=(
            'Hostname of the Viper server to query, the default is: '
            '{0:s}'.format(cls._DEFAULT_HOST)))

    argument_group.add_argument(
        '--viper-port', '--viper_port', dest='viper_port', type=int,
        action='store', default=cls._DEFAULT_PORT, metavar='PORT', help=(
            'Port of the Viper server to query, the default is: {0:d}.'.format(
                cls._DEFAULT_PORT)))

    argument_group.add_argument(
        '--viper-protocol', '--viper_protocol', dest='viper_protocol',
        type=str, choices=viper.ViperAnalyzer.SUPPORTED_PROTOCOLS,
        action='store', default=cls._DEFAULT_PROTOCOL, metavar='PROTOCOL',
        help=(
            'Protocol to use to query Viper, the default is: {0:s}. '
            'Supported options: {1:s}').format(
                cls._DEFAULT_PROTOCOL, ', '.join(
                    viper.ViperAnalyzer.SUPPORTED_PROTOCOLS)))

Adds command line arguments the helper supports to an argument group.

This function takes an argument parser or an argument group object and adds
to it all the command line arguments this helper supports.

Args:
argument_group (argparse._ArgumentGroup|argparse.ArgumentParser):
argparse group.


def forward(self, key_value_states: torch.Tensor, attn_mask: Optional[torch.Tensor]=None):
    batch_size, num_patches = (key_value_states.shape[0], key_value_states.shape[1])
    if num_patches not in self.patch_to_query_dict.keys():
        raise KeyError(f'Number of patches {num_patches} not found in patch_to_query_dict amongst possible values {self.patch_to_query_dict.keys()}.')
    query_num = self.patch_to_query_dict[num_patches]
    queries = self.query[:query_num].unsqueeze(0).repeat(batch_size, 1, 1)
    if attn_mask is not None:
        attn_mask = attn_mask.repeat_interleave(self.num_heads, 0)
        attn_mask = attn_mask.unsqueeze(1).expand(-1, queries.size(1), -1)
    attention_out = self.cross_attn(key_value_states, queries, attn_mask=attn_mask)
    out = self.feed_forward(self.layer_norm(attention_out))
    return out

Forward pass of the Projector module.

Args:
key_value_states (`torch.Tensor`):
Input tensor of shape (batch_size, num_patches, kv_dim).
attn_mask (`torch.Tensor`, *optional*, default is None):
Attention mask.

Returns:
`torch.Tensor`: Output tensor of shape (batch_size, query_number, output_dim).


def download_links(self, dir_path):

    links = self.links
    if not path.exists(dir_path):
      makedirs(dir_path)
    for i, url in enumerate(links):
      if 'start' in self.cseargs:
        i += int(self.cseargs['start'])
      ext = self.cseargs['fileType']
      ext = '.html' if ext == '' else '.' + ext
      file_name = self.cseargs['q'].replace(' ', '_') + '_' + str(i) + ext
      file_path = path.join(dir_path, file_name)
      r = requests.get(url, stream=True)
      if r.status_code == 200:
        with open(file_path, 'wb') as f:
          r.raw.decode_content = True
          shutil.copyfileobj(r.raw, f)

Download web pages or images from search result links.

Args:
dir_path (str):
Path of directory to save downloads of :class:`api.results`.links


def write_libraries(dir, libraries):
    files = [open(os.path.join(dir, k), 'w') for (k, _) in libraries]
    for (f, (_, v)) in zip(files, libraries):
        v.write_markdown_to_file(f)
    for (f, (_, v)) in zip(files, libraries):
        v.write_other_members(f)
        f.close()

Write a list of libraries to disk.

Args:
dir: Output directory.
libraries: List of (filename, library) pairs.


def scale_vmss(access_token, subscription_id, resource_group, vmss_name, capacity):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/Microsoft.Compute/virtualMachineScaleSets/', vmss_name, '?api-version=', COMP_API])
    body = (('{"sku":{"capacity":"' + str(capacity)) + '"}}')
    return do_patch(endpoint, body, access_token)

Change the instance count of an existing VM Scale Set.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.
vmss_name (str): Name of the virtual machine scale set.
capacity (int): New number of VMs.
Returns:
HTTP response.


def set_job(self, key, func, args):
    (res, pk) = key
    (jobs, lock) = self._jobs
    task = _tasks.UpdateTask(func(*args), key)
    with lock:
        job = jobs[res].get(pk)
        had = bool(job)
        if (not job):
            job = task
            jobs[res][pk] = job
        else:
            task.cancel()
    self._log.debug('Scheduling: %s-%s (%s)', res.tag, pk, ('new task' if (not had) else 'dup'))
    return job

Get a scheduled task or set if none exists.

Returns:
- task coroutine/continuation


def get_uniform_frame_indices(total_num_frames: int, num_frames: Optional[int]=None):
    if num_frames is not None:
        indices = np.arange(0, total_num_frames, total_num_frames / num_frames).astype(int)
    else:
        indices = np.arange(0, total_num_frames).astype(int)
    return indices

Creates a numpy array for uniform sampling of `num_frame` frames from `total_num_frames`
when loading a video.

Args:
total_num_frames (`int`):
Total number of frames that a video has.
num_frames (`int`, *optional*):
Number of frames to sample uniformly. If not specified, all frames are sampled.

Returns:
np.ndarray: np array of frame indices that will be sampled.


def add_class_error(self, test_record):
    test_record.update_record()
    self.error.append(test_record)

Add a record to indicate a test class has failed before any test
could execute.

This is only called before any test is actually executed. So it only
adds an error entry that describes why the class failed to the tally
and does not affect the total number of tests requrested or exedcuted.

Args:
test_record: A TestResultRecord object for the test class.


def guarantee_const(input, name=None):
    return gen_array_ops.guarantee_const(input=input, name=name)

Promise to the TF runtime that the input tensor is a constant.

The runtime is then free to make optimizations based on this.

Returns the input tensor without modification.

Args:
input: A `Tensor`.
name: A name for this operation.

Returns:
A `Tensor`. Has the same dtype as `input`.


def mv(src, dst):
    if (not exists(src)):
        raise File404(src)
    try:
        shutil.move(src, dst)
    except Exception as e:
        raise IOError(str(e))

Move a file or directory.

If the destination already exists, this will attempt to overwrite
it.

Arguments:

src (string): path to the source file or directory.
dst (string): path to the destination file or directory.

Raises:

File404: if source does not exist.
IOError: in case of error.


def add_continue_node(self, ast_node, section_id, guards):
    node = self._add_jump_node(ast_node, guards)
    self.continues[section_id].add(node)

Grows the graph by adding a reentry node.

This node causes control flow to go back to the loop section's entry.

Args:
ast_node: ast.AST
section_id: Hashable, the node for which ast_node should be considered to
be an exit node
guards: Tuple[ast.AST, ...], the finally sections that guard ast_node


def ComputeRoot(hashes):

        if not len(hashes):
            raise Exception('Hashes must have length')
        if len(hashes) == 1:
            return hashes[0]

        tree = MerkleTree(hashes)
        return tree.Root.Hash

Compute the root hash.

Args:
hashes (list): the list of hashes to build the root from.

Returns:
bytes: the root hash.


def make_list_of_images(images, expected_ndims: int=3) -> list[ImageInput]:
    if is_batched(images):
        return images
    if is_pil_image(images):
        return [images]
    if is_valid_image(images):
        if images.ndim == expected_ndims + 1:
            images = list(images)
        elif images.ndim == expected_ndims:
            images = [images]
        else:
            raise ValueError(f'Invalid image shape. Expected either {expected_ndims + 1} or {expected_ndims} dimensions, but got {images.ndim} dimensions.')
        return images
    raise ValueError(f'Invalid image type. Expected either PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray, but got {type(images)}.')

Ensure that the output is a list of images. If the input is a single image, it is converted to a list of length 1.
If the input is a batch of images, it is converted to a list of images.

Args:
images (`ImageInput`):
Image of images to turn into a list of images.
expected_ndims (`int`, *optional*, defaults to 3):
Expected number of dimensions for a single input image. If the input image has a different number of
dimensions, an error is raised.


def get_weights_of_nn_sites(self, structure, n):

        return [e['weight'] for e in self.get_nn_info(structure, n)]

Get weight associated with each near neighbor of site with
index n in structure.

Args:
structure (Structure): input structure.
n (integer): index of site for which to determine the weights.
Returns:
weights (list of floats): near-neighbor weights.


def _exec_query(self):
    if (not self._solr_locked):
        if (not self.compiled_query):
            self._compile_query()
        try:
            solr_params = self._process_params()
            if settings.DEBUG:
                t1 = time.time()
            self._solr_cache = self.bucket.search(self.compiled_query, self.index_name, **solr_params)
            if (settings.DEBUG and (settings.DEBUG_LEVEL >= 5)):
                print(('QRY => %s\nSOLR_PARAMS => %s' % (self.compiled_query, solr_params)))
        except riak.RiakError as err:
            err.value += self._get_debug_data()
            raise
        self._solr_locked = True
        return self._solr_cache['docs']

Executes solr query if it hasn't already executed.

Returns:
Self.


def update_reorders_v2(output_file_path):
    spec = tf_upgrade_v2.TFAPIChangeSpec()
    reordered_function_names = spec.reordered_function_names
    need_kwargs_function_names = spec.function_transformers.keys()
    function_renames = spec.symbol_renames
    all_reorders = collect_function_arg_names(reordered_function_names, need_kwargs_function_names, function_renames)
    rename_lines = [get_reorder_line(name, arg_names) for name, arg_names in all_reorders.items()]
    renames_file_text = '%sreorders = {\n%s\n}\n' % (_FILE_HEADER, ',\n'.join(sorted(rename_lines)))
    file_io.write_string_to_file(output_file_path, renames_file_text)

Writes a Python dictionary mapping function name to argument order.

Args:
output_file_path: File path to write output to. Any existing contents
would be replaced.


def from_string(cls, string_input):

        correlation_grid = {}
        Exc_DFT_option = {}
        COHSEX_options = {}
        GW_options = {}
        BSE_TDDFT_options = {}

        lines = string_input.strip().split("\n")

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        nat = toks[0]
        nsp = toks[1]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        nvbands = toks[0]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        correlation_grid['n_grid'] = toks[0]
        correlation_grid['dE_grid'] = toks[1]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        Exc_DFT_option['rdVxcpsi'] = toks[0]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        COHSEX_options['nv_cohsex'] = toks[0]
        COHSEX_options['nc_cohsex'] = toks[1]
        COHSEX_options['eigMethod'] = toks[2]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        COHSEX_options['nit_cohsex'] = toks[0]
        COHSEX_options['resMethod'] = toks[1]
        COHSEX_options['scf_cohsex_wf'] = toks[2]
        COHSEX_options['mix_cohsex'] = toks[3]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        GW_options['nv_corr'] = toks[0]
        GW_options['nc_corr'] = toks[1]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        GW_options['nit_gw'] = toks[0]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['do_bse'] = toks[0]
        BSE_TDDFT_options['do_tddft'] = toks[1]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['nv_bse'] = toks[0]
        BSE_TDDFT_options['nc_bse'] = toks[1]

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        BSE_TDDFT_options['npsi_bse'] = toks[0]
        BSE_TDDFT_options['nit_bse'] = toks[1]

        lines.pop(0)
        atname = []
        i = int(nsp)
        while i != 0:
            l = lines.pop(0).strip()
            toks = l.split()
            atname.append(toks[0])
            i -= 1

        lines.pop(0)
        l = lines.pop(0).strip()
        toks = l.split()
        scale = toks[0]

        lines.pop(0)

        species = []
        coords = []
        i = int(nat)
        while i != 0:
            l = lines.pop(0).strip()
            toks = l.split()
            coords.append([float(j) for j in toks[0:3]])
            species.append(atname[int(toks[3]) - 1])
            i -= 1

        mol = Molecule(species, coords)

        return FiestaInput(mol=mol, correlation_grid=correlation_grid,
                           Exc_DFT_option=Exc_DFT_option,
                           COHSEX_options=COHSEX_options,
                           GW_options=GW_options,
                           BSE_TDDFT_options=BSE_TDDFT_options)

Read an FiestaInput from a string. Currently tested to work with
files generated from this class itself.

Args:
string_input: string_input to parse.
Returns:
FiestaInput object


def _ParseLine(self, parser_mediator, structure):

    month, day_of_month, year, hours, minutes, seconds, milliseconds = (
        structure.date_time)

    year += 2000
    time_elements_tuple = (
        year, month, day_of_month, hours, minutes, seconds, milliseconds)

    try:
      date_time = dfdatetime_time_elements.TimeElementsInMilliseconds(
          time_elements_tuple=time_elements_tuple)
    except ValueError:
      parser_mediator.ProduceExtractionWarning(
          'invalid date time value: {0!s}'.format(structure.date_time))
      return

    event_data = SkyDriveLogEventData()

    event_data.detail = structure.detail.replace('\n', ' ')
    event_data.log_level = structure.log_level
    event_data.module = structure.module
    event_data.source_code = structure.source_code

    event = time_events.DateTimeValuesEvent(
        date_time, definitions.TIME_DESCRIPTION_ADDED)
    parser_mediator.ProduceEventWithEventData(event, event_data)

Parses a logline and store appropriate attributes.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
structure (pyparsing.ParseResults): structure of tokens derived from
a line of a text file.


def insert(self, i, species, coords, validate_proximity=False, properties=None):
    new_site = Site(species, coords, properties=properties)
    if validate_proximity:
        for site in self:
            if (site.distance(new_site) < self.DISTANCE_TOLERANCE):
                raise ValueError('New site is too close to an existing site!')
    self._sites.insert(i, new_site)

Insert a site to the molecule.

Args:
i (int): Index to insert site
species: species of inserted site
coords (3x1 array): coordinates of inserted site
validate_proximity (bool): Whether to check if inserted site is
too close to an existing site. Defaults to True.
properties (dict): Dict of properties for the Site.

Returns:
New molecule with inserted site.


def get_phonopy_structure(pmg_structure):
    symbols = [site.specie.symbol for site in pmg_structure]
    return PhonopyAtoms(symbols=symbols, cell=pmg_structure.lattice.matrix, scaled_positions=pmg_structure.frac_coords)

Convert a pymatgen Structure object to a PhonopyAtoms object.

Args:
pmg_structure (pymatgen Structure): A Pymatgen structure object.


def get_remote_info(url_id):
    try:
        data = _send_request(url_id)
    except Exception as e:
        sys.stderr.write('Seeder GET error: ')
        sys.stderr.write(str(e.message))
        return None
    return _convert_to_wakat_format(data)

Download data and convert them to dict used in frontend.

Args:
url_id (str): ID used as identification in Seeder.

Returns:
dict: Dict with data for frontend or None in case of error.


def validate(self, tags, confidence):
    (intent, tags) = self.validate_with_tags(tags, confidence)
    return intent

Using this method removes tags from the result of validate_with_tags

Returns:
intent(intent): Resuts from validate_with_tags


def CredibleInterval(self, percentage=90):

        prob = (1 - percentage / 100.0) / 2
        interval = self.Value(prob), self.Value(1 - prob)
        return interval

Computes the central credible interval.

If percentage=90, computes the 90% CI.

Args:
percentage: float between 0 and 100

Returns:
sequence of two floats, low and high


def create_course_completion(self, user_id, payload):
    url = (self.enterprise_configuration.sapsf_base_url + self.global_sap_config.completion_status_api_path)
    return self._call_post_with_user_override(user_id, url, payload)

Send a completion status payload to the SuccessFactors OCN Completion Status endpoint

Args:
user_id (str): The sap user id that the completion status is being sent for.
payload (str): JSON encoded object (serialized from SapSuccessFactorsLearnerDataTransmissionAudit)
containing completion status fields per SuccessFactors documentation.

Returns:
The body of the response from SAP SuccessFactors, if successful
Raises:
HTTPError: if we received a failure response code from SAP SuccessFactors


def lunr(ref, fields, documents, languages=None):
    if ((languages is not None) and lang.LANGUAGE_SUPPORT):
        if isinstance(languages, basestring):
            languages = [languages]
        unsupported_languages = (set(languages) - set(lang.SUPPORTED_LANGUAGES))
        if unsupported_languages:
            raise RuntimeError('The specified languages {} are not supported, please choose one of {}'.format(', '.join(unsupported_languages), ', '.join(lang.SUPPORTED_LANGUAGES.keys())))
        builder = lang.get_nltk_builder(languages)
    else:
        builder = Builder()
        builder.pipeline.add(trimmer, stop_word_filter, stemmer)
        builder.search_pipeline.add(stemmer)
    builder.ref(ref)
    for field in fields:
        if isinstance(field, dict):
            builder.field(**field)
        else:
            builder.field(field)
    for document in documents:
        if isinstance(document, (tuple, list)):
            builder.add(document[0], attributes=document[1])
        else:
            builder.add(document)
    return builder.build()

A convenience function to configure and construct a lunr.Index.

Args:
ref (str): The key in the documents to be used a the reference.
fields (list): A list of strings defining fields in the documents to
index. Optionally a list of dictionaries with three keys:
`field_name` defining the document's field, `boost` an integer
defining a boost to be applied to the field, and `extractor`
a callable taking the document as a single argument and returning
a string located in the document in a particular way.
documents (list): The list of dictonaries representing the documents
to index. Optionally a 2-tuple of dicts, the first one being
the document and the second the associated attributes to it.
languages (str or list, optional): The languages to use if using
NLTK language support, ignored if NLTK is not available.

Returns:
Index: The populated Index ready to search against.


def convert_attribute_name_to_tag(value):

    if not isinstance(value, six.string_types):
        raise ValueError("The attribute name must be a string.")

    for entry in attribute_name_tag_table:
        if value == entry[0]:
            return entry[1]

    raise ValueError("Unrecognized attribute name: '{}'".format(value))

A utility function that converts an attribute name string into the
corresponding attribute tag.

For example: 'State' -> enums.Tags.STATE

Args:
value (string): The string name of the attribute.

Returns:
enum: The Tags enumeration value that corresponds to the attribute
name string.

Raises:
ValueError: if the attribute name string is not a string or if it is
an unrecognized attribute name


async def get_auth(request):
    auth_val = request.get(AUTH_KEY)
    if auth_val:
        return auth_val
    auth_policy = request.get(POLICY_KEY)
    if (auth_policy is None):
        raise RuntimeError('auth_middleware not installed')
    request[AUTH_KEY] = (await auth_policy.get(request))
    return request[AUTH_KEY]

Returns the user_id associated with a particular request.

Args:
request: aiohttp Request object.

Returns:
The user_id associated with the request, or None if no user is
associated with the request.

Raises:
RuntimeError: Middleware is not installed


def spherical_vert(script, radius=1.0, center_pt=(0.0, 0.0, 0.0)):

    function = 'sqrt((x-{})^2+(y-{})^2+(z-{})^2)<={}'.format(
        center_pt[0], center_pt[1], center_pt[2], radius)
    vert_function(script, function=function)
    return None

Select all vertices within a spherical radius

Args:
radius (float): radius of the sphere
center_pt (3 coordinate tuple or list): center point of the sphere

Layer stack:
No impacts

MeshLab versions:
2016.12
1.3.4BETA


def input_mask(self):
    inputs = self.input
    if isinstance(inputs, list):
        return [getattr(x, '_keras_mask', None) for x in inputs]
    else:
        return getattr(inputs, '_keras_mask', None)

Retrieves the input mask tensor(s) of a layer.

Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.

Returns:
Input mask tensor (potentially None) or list of input
mask tensors.

Raises:
AttributeError: if the layer is connected to
more than one incoming layers.


def transform(self, col):

        out = pd.DataFrame()

        column = col[self.col_name].replace({np.nan: None})
        out[self.col_name] = column.apply(self.get_val)

        return out

Prepare the transformer to convert data and return the processed table.

Args:
col(pandas.DataFrame): Data to transform.

Returns:
pandas.DataFrame


def get_values(self, field_name: str) -> List[object]:

        result = list()
        if self.validate_field(field_name):
            for value_key in self._kg.get(field_name):
                result.append(value_key["value"])
        return result

Get a list of all the values of a field.

Args:
field_name:

Returns: the list of values (not the keys)


def get_commands_in_namespace(namespace=None, level=1):
    from ..command import Command
    commands = {}
    if (namespace is None):
        frame = inspect.stack()[level][0]
        namespace = frame.f_globals
    elif inspect.ismodule(namespace):
        namespace = vars(namespace)
    for name in namespace:
        obj = namespace[name]
        if isinstance(obj, Command):
            commands[name] = obj
    return OrderedDict(((name, commands[name]) for name in sorted(commands)))

Get commands in namespace.

Args:
namespace (dict|module): Typically a module. If not passed, the
globals from the call site will be used.
level (int): If not called from the global scope, set this
appropriately to account for the call stack.

Returns:
OrderedDict: The commands found in the namespace, ordered by
name.

Can be used to create ``__all__`` lists::

__all__ = list(get_commands_in_namespace())


def make_batched_videos(videos) -> List[Union['np.ndarray', 'torch.Tensor']]:
    if not valid_videos:
        raise ValueError(f'Invalid video input. Expected either a list of video frames or an input of 4 or 5 dimensions, but got type {type(videos)}.')
    if is_batched_video(videos):
        pass
    elif is_valid_video(videos):
        videos = [videos]
    elif is_valid_image(videos):
        videos = [np.array(videos)[None, ...]]
    elif isinstance(videos[0], (list, tuple)) and is_valid_video(videos[0][0]):
        videos = [video for sublist in videos for video in sublist]
    return convert_pil_frames_to_video(videos)

Ensure that the input is a list of videos. If the input is a single video, it is converted to a list of length 1.
If the input is a batch of videos, it is converted to a list of 4D video arrays. Videos passed as list `PIL.Image`
frames are converted to 4D arrays.

We assume that all inputs in the list are in the same format, based on the type of the first element.

Args:
videos (`VideoInput`):
Video inputs to turn into a list of videos.


def SetCACertificatesPath(self, ca_certificates_path):

    if not ca_certificates_path:
      return

    if not os.path.exists(ca_certificates_path):
      raise errors.BadConfigOption(
          'No such certificate file: {0:s}.'.format(ca_certificates_path))

    self._ca_certs = ca_certificates_path
    logger.debug('Elasticsearch ca_certs: {0!s}'.format(ca_certificates_path))

Sets the path to the CA certificates.

Args:
ca_certificates_path (str): path to file containing a list of root
certificates to trust.

Raises:
BadConfigOption: if the CA certificates file does not exist.


def _ReadAppJsonFile(self, relative_path):

    try:
      with open(os.path.join(sys.path[0], relative_path), 'r') as f:
        return json.load(f)
    except (IOError, ValueError):
      return None

Reads JSON file from an application directory.

Args:
relative_path: file name relative to application root directory.

Returns:
Parsed JSON data or None if the file does not exist, can't be read or
not a valid JSON file.


def resolve_trust_remote_code(trust_remote_code, model_name, has_local_code, has_remote_code, error_message=None, upstream_repo=None):
    if error_message is None:
        if upstream_repo is not None:
            error_message = f'The repository {model_name} references custom code contained in {upstream_repo} which must be executed to correctly load the model. You can inspect the repository content at https:
        elif os.path.isdir(model_name):
            error_message = f'The repository {model_name} contains custom code which must be executed to correctly load the model. You can inspect the repository content at {os.path.abspath(model_name)} .\n'
        else:
            error_message = f'The repository {model_name} contains custom code which must be executed to correctly load the model. You can inspect the repository content at https:
    if trust_remote_code is None:
        if has_local_code:
            trust_remote_code = False
        elif has_remote_code and TIME_OUT_REMOTE_CODE > 0:
            prev_sig_handler = None
            try:
                prev_sig_handler = signal.signal(signal.SIGALRM, _raise_timeout_error)
                signal.alarm(TIME_OUT_REMOTE_CODE)
                while trust_remote_code is None:
                    answer = input(f'{error_message} You can inspect the repository content at https:
                    if answer.lower() in ['yes', 'y', '1']:
                        trust_remote_code = True
                    elif answer.lower() in ['no', 'n', '0', '']:
                        trust_remote_code = False
                signal.alarm(0)
            except Exception:
                raise ValueError(f'{error_message} You can inspect the repository content at https:
            finally:
                if prev_sig_handler is not None:
                    signal.signal(signal.SIGALRM, prev_sig_handler)
                    signal.alarm(0)
        elif has_remote_code:
            _raise_timeout_error(None, None)
    if has_remote_code and (not has_local_code) and (not trust_remote_code):
        raise ValueError(f'{error_message} You can inspect the repository content at https:
    return trust_remote_code

Resolves the `trust_remote_code` argument. If there is remote code to be loaded, the user must opt-in to loading
it.

Args:
trust_remote_code (`bool` or `None`):
User-defined `trust_remote_code` value.
model_name (`str`):
The name of the model repository in huggingface.co.
has_local_code (`bool`):
Whether the model has local code.
has_remote_code (`bool`):
Whether the model has remote code.
error_message (`str`, *optional*):
Custom error message to display if there is remote code to load and the user didn't opt-in. If unset, the error
message will be regarding loading a model with custom code.

Returns:
The resolved `trust_remote_code` value.


def get_config_path():
    try:
        return os.environ[environment_vars.CLOUD_SDK_CONFIG_DIR]
    except KeyError:
        pass
    if (os.name != 'nt'):
        return os.path.join(os.path.expanduser('~'), '.config', _CONFIG_DIRECTORY)
    else:
        try:
            return os.path.join(os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], _CONFIG_DIRECTORY)
        except KeyError:
            drive = os.environ.get('SystemDrive', 'C:')
            return os.path.join(drive, '\\', _CONFIG_DIRECTORY)

Returns the absolute path the the Cloud SDK's configuration directory.

Returns:
str: The Cloud SDK config path.


def api_server(connection, server_class):
    return server_class(link=xbahn.connection.link.Link(receive=connection, respond=connection))

Establishes an API Server on the supplied connection

Arguments:
- connection (xbahn.connection.Connection)
- server_class (xbahn.api.Server)

Returns:
- server_class: server instance