def GetTaskPendingMerge(self, current_task):
    next_task = self._tasks_pending_merge.PeekTask()
    if (not next_task):
        return None
    if (current_task and (next_task.merge_priority > current_task.merge_priority)):
        return None
    with self._lock:
        next_task = self._tasks_pending_merge.PopTask()
    self._tasks_merging[next_task.identifier] = next_task
    return next_task

Retrieves the first task that is pending merge or has a higher priority.

This function will check if there is a task with a higher merge priority
than the current_task being merged. If so, that task with the higher
priority is returned.

Args:
current_task (Task): current task being merged or None if no such task.

Returns:
Task: the next task to merge or None if there is no task pending merge or
with a higher priority.


def whole_subnet_maker(ip_addr, cidr):

    if ucast_ip(ip_addr, False) == False and mcast_ip(ip_addr, False) == False:
        LOGGER.critical('Function whole_subnet_maker ip_addr {item}'.format(item=ip_addr))
        raise ValueError("Not a good ipv4 address")
    if not cidr_check(cidr, False):
        LOGGER.critical('Function whole_subnet_maker cidr {item}'.format(item=cidr))
        raise ValueError("Not a good CIDR value should be 0 to 32")

    def subnet_corrector(octet, cidr):

        cidr_int = int(cidr)
        octet_int = int(octet)
        if cidr_int >= 24:
            cidr_int = __mask_conversion[cidr_int]["OCT4"]
        elif cidr_int >= 16:
            cidr_int = __mask_conversion[cidr_int]["OCT3"]
        elif cidr_int >= 8:
            cidr_int = __mask_conversion[cidr_int]["OCT2"]
        elif cidr_int >= 1:
            cidr_int = __mask_conversion[cidr_int]["OCT1"]
        cidr_count = 0
        cidr_v = 256 - cidr_int
        cidr_2 = 256 - cidr_int
        while cidr_count < 300:
            if octet_int >= cidr_count and octet_int <= cidr_2:
                    cidr_int = cidr_count
            cidr_count = cidr_2
            cidr_2 = cidr_2 + cidr_v
        return str(cidr_int)
    ip_addr_split = ip_addr.split(".")
    if int(cidr) >= 24:
        octet = subnet_corrector(ip_addr_split[3], cidr)
        completed = ip_addr_split[0] + "." + ip_addr_split[1] + "." + ip_addr_split[2] + "." + octet
        return completed
    elif int(cidr) >= 16:
        octet = subnet_corrector(ip_addr_split[2], cidr)
        completed = ip_addr_split[0] + "." + ip_addr_split[1] + "." + octet + ".0"
        return completed
    elif int(cidr) >= 8:
        octet = subnet_corrector(ip_addr_split[1], cidr)
        completed = ip_addr_split[0] + "." + octet + ".0.0"
        return completed
    elif int(cidr) >= 1:
        octet = subnet_corrector(ip_addr_split[0], cidr)
        completed = octet + ".0.0.0"
        return completed
    else:
        return "0.0.0.0"

Function to return a whole subnet value from a IP address and CIDR pair
Args:
ip_addr: Unicast or Multicast IP address or subnet in the following format 192.168.1.1, 239.1.1.1
cidr: CIDR value of 0 to 32

Returns: returns the corrected whole subnet


def __init__(self, control_handler, data_plane_handler, state, provision_info):
    self.control_handler = control_handler
    self.data_plane_handler = data_plane_handler
    self.state = state
    self.provision_info = provision_info
    with WorkerHandler._lock:
        WorkerHandler._worker_id_counter += 1
        self.worker_id = 'worker_%s' % WorkerHandler._worker_id_counter

Initialize a WorkerHandler.

Args:
control_handler:
data_plane_handler (data_plane.DataChannel):
state:
provision_info:


def dumps(collection: BioCCollection, pretty_print: bool = True) -> str:

    doc = etree.ElementTree(BioCXMLEncoder().encode(collection))
    s = etree.tostring(doc, pretty_print=pretty_print, encoding=collection.encoding,
                       standalone=collection.standalone)
    return s.decode(collection.encoding)

Serialize ``collection`` to a BioC formatted ``str``.

Args:
collection: the BioC collection
pretty_print: enables formatted XML

Returns:
a BioC formatted ``str``


def parse(self):
    (options, args) = self.parser.parse_args()
    self._set_attributes(args, options)
    return self._create_dictionary()

Parse command line arguments and options.

Returns:
Dictionary containing all given command line arguments and options.


def GetStorageMediaImageTypeIndicators(cls, path_spec, resolver_context=None):

    if (cls._storage_media_image_remainder_list is None or
        cls._storage_media_image_store is None):
      specification_store, remainder_list = cls._GetSpecificationStore(
          definitions.FORMAT_CATEGORY_STORAGE_MEDIA_IMAGE)
      cls._storage_media_image_remainder_list = remainder_list
      cls._storage_media_image_store = specification_store

    if cls._storage_media_image_scanner is None:
      cls._storage_media_image_scanner = cls._GetSignatureScanner(
          cls._storage_media_image_store)

    return cls._GetTypeIndicators(
        cls._storage_media_image_scanner, cls._storage_media_image_store,
        cls._storage_media_image_remainder_list, path_spec,
        resolver_context=resolver_context)

Determines if a file contains a supported storage media image types.

Args:
path_spec (PathSpec): path specification.
resolver_context (Optional[Context]): resolver context, where None
represents the built-in context which is not multi process safe.

Returns:
list[str]: supported format type indicators.


def expect_true(condition, msg, extras=None):

    try:
        asserts.assert_true(condition, msg, extras)
    except signals.TestSignal as e:
        logging.exception('Expected a `True` value, got `False`.')
        recorder.add_error(e)

Expects an expression evaluates to True.

If the expectation is not met, the test is marked as fail after its
execution finishes.

Args:
expr: The expression that is evaluated.
msg: A string explaining the details in case of failure.
extras: An optional field for extra information to be included in test
result.


def value_to_message(self, value):

        if not isinstance(value, self.type):
            raise EncodeError('Expected type %s, got %s: %r' %
                              (self.type.__name__,
                               type(value).__name__,
                               value))
        return value

Convert a value instance to a message.

Used by serializers to convert Python user types to underlying
messages for transmission.

Args:
value: A value of type self.type.

Returns:
An instance of type self.message_type.


def class_label_top(body_output, targets, model_hparams, vocab_size):
    del targets
    with tf.variable_scope(('class_label_modality_%d_%d' % (vocab_size, model_hparams.hidden_size))):
        x = body_output
        x = tf.reduce_mean(x, axis=[1, 2], keepdims=True)
        res = tf.layers.dense(x, vocab_size)
        return tf.expand_dims(res, 3)

Transform inputs from model space to target space.

Average over inner dims and a linear layer to logits.

Args:
body_output: A Tensor with shape [batch, ?, ?, body_output_size].
targets:
model_hparams: HParams, model hyperparmeters.
vocab_size: int, vocabulary size.

Returns:
a Tensors, each with shape [batch_size, 1, 1, 1, vocab_size]


def _prepare(f, xs_dtypes, xs_shapes):
    if context.executing_eagerly():

        def decorated_eager(*xs_data):
            return f(*map(ops.convert_to_tensor, xs_data))
        return decorated_eager
    xs = [array_ops.placeholder(x_dtype, shape=x_shape) for x_dtype, x_shape in zip(xs_dtypes, xs_shapes)]
    y = f(*xs)
    sess = ops.get_default_session()

    def decorated_graph(*xs_data):
        xs_data = [_to_numpy(a) for a in xs_data]
        return sess.run(y, feed_dict=dict(zip(xs, xs_data)))
    return decorated_graph

Return a function that executes 'f'.

In TF 2.x, this is the same as `f`.
In TF 1.x, returns a Python function that executes the graph defined by `f`
in a Session.

Args:
f: the function.
xs_dtypes: dtypes of f's arguments.
xs_shapes: shapes of f's arguments.

Returns:


def _AddVolume(self, volume):

    if volume.identifier in self._volumes:
      raise KeyError(
          'Volume object already set for volume identifier: {0:s}'.format(
              volume.identifier))

    self._volumes[volume.identifier] = volume
    self._volume_identifiers.append(volume.identifier)

Adds a volume.

Args:
volume (Volume): a volume.

Raises:
KeyError: if volume is already set for the corresponding volume
identifier.


def get_wigner_seitz_cell(self) -> List[List[np.ndarray]]:
    vec1 = self._matrix[0]
    vec2 = self._matrix[1]
    vec3 = self._matrix[2]
    list_k_points = []
    for (i, j, k) in itertools.product([(- 1), 0, 1], [(- 1), 0, 1], [(- 1), 0, 1]):
        list_k_points.append((((i * vec1) + (j * vec2)) + (k * vec3)))
    from scipy.spatial import Voronoi
    tess = Voronoi(list_k_points)
    to_return = []
    for r in tess.ridge_dict:
        if ((r[0] == 13) or (r[1] == 13)):
            to_return.append([tess.vertices[i] for i in tess.ridge_dict[r]])
    return to_return

Returns the Wigner-Seitz cell for the given lattice.

Returns:
A list of list of coordinates.
Each element in the list is a "facet" of the boundary of the
Wigner Seitz cell. For instance, a list of four coordinates will
represent a square facet.


def profile_args(_args):
    if ((_args.get('app', {}).get('optional') is not None) or (_args.get('app', {}).get('required') is not None)):
        app_args_optional = _args.get('app', {}).get('optional', {})
        app_args_required = _args.get('app', {}).get('required', {})
        default_args = _args.get('default', {})
        _args = {}
        _args.update(app_args_optional)
        _args.update(app_args_required)
        _args.update(default_args)
    elif ((_args.get('app') is not None) and (_args.get('default') is not None)):
        app_args = _args.get('app', {})
        default_args = _args.get('default', {})
        _args = {}
        _args.update(app_args)
        _args.update(default_args)
    return _args

Return args for v1, v2, or v3 structure.

Args:
_args (dict): The args section from the profile.

Returns:
dict: A collapsed version of the args dict.


def get_template(template_file='', **kwargs):

    template = get_template_object(template_file)

    LOG.info('Rendering template %s', template.filename)
    for key, value in kwargs.items():
        LOG.debug('%s => %s', key, value)

    rendered_json = template.render(**kwargs)
    LOG.debug('Rendered JSON:\n%s', rendered_json)

    return rendered_json

Get the Jinja2 template and renders with dict _kwargs_.

Args:
template_file (str): name of the template file
kwargs: Keywords to use for rendering the Jinja2 template.

Returns:
String of rendered JSON template.


def determine_framework(model: str, framework: Optional[str]=None) -> str:
    if framework is not None:
        return framework
    framework_map = {'pt': 'PyTorch', 'tf': 'TensorFlow'}
    exporter_map = {'pt': 'torch', 'tf': 'tf2onnx'}
    if os.path.isdir(model):
        if os.path.isfile(os.path.join(model, WEIGHTS_NAME)):
            framework = 'pt'
        elif os.path.isfile(os.path.join(model, TF2_WEIGHTS_NAME)):
            framework = 'tf'
        else:
            raise FileNotFoundError(f'Cannot determine framework from given checkpoint location. There should be a {WEIGHTS_NAME} for PyTorch or {TF2_WEIGHTS_NAME} for TensorFlow.')
        logger.info(f'Local {framework_map[framework]} model found.')
    elif is_torch_available():
        framework = 'pt'
    elif is_tf_available():
        framework = 'tf'
    else:
        raise OSError('Neither PyTorch nor TensorFlow found in environment. Cannot export to ONNX.')
    logger.info(f'Framework not requested. Using {exporter_map[framework]} to export to ONNX.')
    return framework

Determines the framework to use for the export.

The priority is in the following order:
1. User input via `framework`.
2. If local checkpoint is provided, use the same framework as the checkpoint.
3. Available framework in environment, with priority given to PyTorch

Args:
model (`str`):
The name of the model to export.
framework (`str`, *optional*, defaults to `None`):
The framework to use for the export. See above for priority if none provided.

Returns:
The framework to use for the export.


def _get_single_set(self, num_objects, num_features):
    data = np.random.uniform((- 1), 1, size=(num_objects, num_features))
    distances = spdistance.squareform(spdistance.pdist(data))
    distance_idx = np.argsort(distances)
    nth = np.random.randint(0, num_objects)
    nth_furthest = distance_idx[(:, nth)]
    reference = np.random.randint(0, num_objects)
    labels = nth_furthest[reference]
    object_ids = np.identity(num_objects)
    nth_matrix = np.zeros((num_objects, num_objects))
    nth_matrix[(:, nth)] = 1
    reference_object = np.zeros((num_objects, num_objects))
    reference_object[(:, reference)] = 1
    inputs = np.concatenate([data, object_ids, reference_object, nth_matrix], axis=(- 1))
    inputs = np.random.permutation(inputs)
    labels = np.expand_dims(labels, axis=0)
    return (inputs.astype(np.float32), labels.astype(np.float32))

Generate one input sequence and output label.

Each sequences of objects has a feature that consists of the feature vector
for that object plus the encoding for its ID, the reference vector ID and
the n-th value relative ID for a total feature size of:

`num_objects` * 3  + `num_features`

Args:
num_objects: int. number of objects in the sequence.
num_features: int. feature size of each object.

Returns:
1. np.ndarray (`num_objects`, (`num_features` + 3 * `num_objects`)).
2. np.ndarray (1,). Output object reference label.


def _get_table_names(statement):

    parts = statement.to_unicode().split()

    tables = set()

    for i, token in enumerate(parts):
        if token.lower() == 'from' or token.lower().endswith('join'):
            tables.add(parts[i + 1].rstrip(';'))

    return list(tables)

Returns table names found in the query.

NOTE. This routine would use the sqlparse parse tree, but vnames don't parse very well.

Args:
statement (sqlparse.sql.Statement): parsed by sqlparse sql statement.

Returns:
list of str


def passthrough_context_definition(context_params):

        check.inst_param(context_params, 'context', ExecutionContext)
        context_definition = PipelineContextDefinition(context_fn=lambda *_args: context_params)
        return {DEFAULT_CONTEXT_NAME: context_definition}

Create a context definition from a pre-existing context. This can be useful
in testing contexts where you may want to create a context manually and then
pass it into a one-off PipelineDefinition

Args:
context (ExecutionContext): The context that will provided to the pipeline.
Returns:
PipelineContextDefinition: The passthrough context definition.


def unapprove(self, **kwargs):
    path = ('%s/%s/unapprove' % (self.manager.path, self.get_id()))
    data = {}
    server_data = self.manager.gitlab.http_post(path, post_data=data, **kwargs)
    self._update_attrs(server_data)

Unapprove the merge request.

Args:
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabMRApprovalError: If the unapproval failed


def serialCmdPwdAuth(self, password_str):
    result = False
    try:
        req_start = (('0150310228' + binascii.hexlify(password_str)) + '2903')
        req_crc = self.calc_crc16(req_start[2:].decode('hex'))
        req_str = (req_start + req_crc)
        self.m_serial_port.write(req_str.decode('hex'))
        if (self.m_serial_port.getResponse(self.getContext()).encode('hex') == '06'):
            ekm_log((('Password accepted (' + self.getContext()) + ')'))
            result = True
        else:
            ekm_log((('Password call failure no 06(' + self.getContext()) + ')'))
    except:
        ekm_log((('Password call failure by exception(' + self.getContext()) + ')'))
        ekm_log(traceback.format_exc(sys.exc_info()))
    return result

Password step of set commands

This method is normally called within another serial command, so it
does not issue a termination string.  Any default password is set
in the caller parameter list, never here.

Args:
password_str (str): Required password.

Returns:
bool: True on completion and ACK.


def log(msg, level=0):
    red = '\x1b[91m'
    endc = '\x1b[0m'
    cfg = {'version': 1, 'disable_existing_loggers': False, 'formatters': {'stdout': {'format': '[%(levelname)s]: %(asctime)s - %(message)s', 'datefmt': '%x %X'}, 'stderr': {'format': ((red + '[%(levelname)s]: %(asctime)s - %(message)s') + endc), 'datefmt': '%x %X'}}, 'handlers': {'stdout': {'class': 'logging.StreamHandler', 'level': 'DEBUG', 'formatter': 'stdout'}, 'stderr': {'class': 'logging.StreamHandler', 'level': 'ERROR', 'formatter': 'stderr'}}, 'loggers': {'info': {'handlers': ['stdout'], 'level': 'INFO', 'propagate': True}, 'error': {'handlers': ['stderr'], 'level': 'ERROR', 'propagate': False}}}
    dictConfig(cfg)
    lg = ('info' if (level == 0) else 'error')
    lvl = (20 if (level == 0) else 40)
    logger = logging.getLogger(lg)
    logger.log(lvl, msg)

Logs a message to the console, with optional level paramater

Args:
- msg (str): message to send to console
- level (int): log level; 0 for info, 1 for error (default = 0)


def day(self, value=None):
    if (value is not None):
        try:
            value = int(value)
        except ValueError:
            raise ValueError('value {} need to be of type int for field `day`'.format(value))
        if (value < 1):
            raise ValueError('value need to be greater or equal 1 for field `day`')
        if (value > 31):
            raise ValueError('value need to be smaller 31 for field `day`')
    self._day = value

Corresponds to IDD Field `day`

Args:
value (int): value for IDD Field `day`
value >= 1
value <= 31
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

Raises:
ValueError: if `value` is not a valid value


def update_profiles(adapter):
    for case in adapter.cases():
        if case.get('profile_path'):
            profiles = get_profiles(adapter, case['profile_path'])
            profiled_individuals = deepcopy(case['individuals'])
            for individual in profiled_individuals:
                ind_id = individual['ind_id']
                try:
                    profile = profiles[ind_id]
                    individual['profile'] = profile
                except KeyError:
                    LOG.warning(f"sample IDs in vcf does not match for case {case['case_id']}")
            updated_case = deepcopy(case)
            updated_case['individuals'] = profiled_individuals
            adapter.add_case(updated_case, update=True)

For all cases having vcf_path, update the profile string for the samples

Args:
adapter (MongoAdapter): Adapter to mongodb


def foreach_model(self, fn):
    results = ray.get([w.foreach_model.remote(fn) for w in self.workers])
    out = []
    for r in results:
        out.extend(r)
    return out

Apply the given function to each model replica in each worker.

Returns:
List of results from applying the function.


def find_bucket(self, bucketing_id, parent_id, traffic_allocations):
    bucketing_key = BUCKETING_ID_TEMPLATE.format(bucketing_id=bucketing_id, parent_id=parent_id)
    bucketing_number = self._generate_bucket_value(bucketing_key)
    self.config.logger.debug(('Assigned bucket %s to user with bucketing ID "%s".' % (bucketing_number, bucketing_id)))
    for traffic_allocation in traffic_allocations:
        current_end_of_range = traffic_allocation.get('endOfRange')
        if (bucketing_number < current_end_of_range):
            return traffic_allocation.get('entityId')
    return None

Determine entity based on bucket value and traffic allocations.

Args:
bucketing_id: ID to be used for bucketing the user.
parent_id: ID representing group or experiment.
traffic_allocations: Traffic allocations representing traffic allotted to experiments or variations.

Returns:
Entity ID which may represent experiment or variation.


def _make_ctx_options(ctx_options, config_cls=ContextOptions):

  if not ctx_options:
    return None
  for key in list(ctx_options):
    translation = _OPTION_TRANSLATIONS.get(key)
    if translation:
      if translation in ctx_options:
        raise ValueError('Cannot specify %s and %s at the same time' %
                         (key, translation))
      ctx_options[translation] = ctx_options.pop(key)
  return config_cls(**ctx_options)

Helper to construct a ContextOptions object from keyword arguments.

Args:
ctx_options: A dict of keyword arguments.
config_cls: Optional Configuration class to use, default ContextOptions.

Note that either 'options' or 'config' can be used to pass another
Configuration object, but not both.  If another Configuration
object is given it provides default values.

Returns:
A Configuration object, or None if ctx_options is empty.


def random_tril_matrix(shape, dtype, force_well_conditioned=False, remove_upper=True):
    with ops.name_scope('random_tril_matrix'):
        tril = random_normal(shape, dtype=dtype)
        if remove_upper:
            tril = array_ops.matrix_band_part(tril, -1, 0)
        if force_well_conditioned:
            maxval = ops.convert_to_tensor(np.sqrt(2.0), dtype=dtype.real_dtype)
            diag = random_sign_uniform(shape[:-1], dtype=dtype, minval=1.0, maxval=maxval)
            tril = array_ops.matrix_set_diag(tril, diag)
        return tril

[batch] lower triangular matrix.

Args:
shape:  `TensorShape` or Python `list`.  Shape of the returned matrix.
dtype:  `TensorFlow` `dtype` or Python dtype
force_well_conditioned:  Python `bool`. If `True`, returned matrix will have
eigenvalues with modulus in `(1, 2)`.  Otherwise, eigenvalues are unit
normal random variables.
remove_upper:  Python `bool`.
If `True`, zero out the strictly upper triangle.
If `False`, the lower triangle of returned matrix will have desired
properties, but will not have the strictly upper triangle zero'd out.

Returns:
`Tensor` with desired shape and dtype.


def disconnect_container_from_network(self, container, net_id, force=False):
    data = {'Container': container}
    if force:
        if version_lt(self._version, '1.22'):
            raise InvalidVersion('Forced disconnect was introduced in API 1.22')
        data['Force'] = force
    url = self._url('/networks/{0}/disconnect', net_id)
    res = self._post_json(url, data=data)
    self._raise_for_status(res)

Disconnect a container from a network.

Args:
container (str): container ID or name to be disconnected from the
network
net_id (str): network ID
force (bool): Force the container to disconnect from a network.
Default: ``False``


def list_vms(access_token, subscription_id, resource_group):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourceGroups/', resource_group, '/providers/Microsoft.Compute/virtualMachines', '?api-version=', COMP_API])
    return do_get(endpoint, access_token)

List VMs in a resource group.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
resource_group (str): Azure resource group name.

Returns:
HTTP response. JSON body of a list of VM model views.


def create_jlink(self, args):
    jlink = pylink.JLink()
    jlink.open(args.serial_no, args.ip_addr)
    if (hasattr(args, 'tif') and (args.tif is not None)):
        if (args.tif.lower() == 'swd'):
            jlink.set_tif(pylink.JLinkInterfaces.SWD)
        else:
            jlink.set_tif(pylink.JLinkInterfaces.JTAG)
    if (hasattr(args, 'device') and (args.device is not None)):
        jlink.connect(args.device)
    return jlink

Creates an instance of a J-Link from the given arguments.

Args:
self (Command): the ``Command`` instance
args (Namespace): arguments to construct the ``JLink`` instance from

Returns:
An instance of a ``JLink``.


def is_duplicated(self, item):
    if isinstance(item, dict):
        hashable_item = json.dumps(item, sort_keys=True)
    elif isinstance(item, list):
        hashable_item = frozenset(item)
    else:
        hashable_item = item
    if (hashable_item in self._cache):
        return True
    else:
        if ((self.cache_capacity > 0) and (len(self._cache) >= self.cache_capacity)):
            self._cache.popitem(False)
        self._cache[hashable_item] = 1
        return False

Check whether the item has been in the cache

If the item has not been seen before, then hash it and put it into
the cache, otherwise indicates the item is duplicated. When the cache
size exceeds capacity, discard the earliest items in the cache.

Args:
item (object): The item to be checked and stored in cache. It must
be immutable or a list/dict.
Returns:
bool: Whether the item has been in cache.


def avg(vals, count=None):

    sum = 0
    for v in vals:
        sum += v
    if count is None:
        count = len(vals)
    return float(sum) / count

Returns the average value

Args:
vals: List of numbers to calculate average from.
count: Int of total count that vals was part of.

Returns:
Float average value throughout a count.


def list_to_file(orig_list, file_name, file_location):

    file = __os.path.join(file_location, file_name)

    def add_line_break(list_line):

        list_line = ('%s\n' % (list_line,))
        return list_line
    write_file = open(file, "a")
    for orig_list_line in orig_list:
        write_file.write(add_line_break(str(orig_list_line)))
    write_file.close()
    return file_name

Function to export a list to a text file
Args:
orig_list: The list you want exported
file_name: The name of the exported file
file_location: The location of the file, derive from the os module

Returns: returns the filename info


def ae_latent_softmax(latents_pred, latents_discrete_hot, vocab_size, hparams):

  with tf.variable_scope("latent_logits"):
    latents_logits = tf.layers.dense(latents_pred, vocab_size,
                                     name="logits_dense")
    if hparams.logit_normalization:
      latents_logits *= tf.rsqrt(1e-8 +
                                 tf.reduce_mean(tf.square(latents_logits)))
    loss = tf.nn.softmax_cross_entropy_with_logits_v2(
        labels=latents_discrete_hot, logits=latents_logits)

    sample = multinomial_sample(latents_logits,
                                vocab_size,
                                hparams.sampling_method,
                                hparams.sampling_temp)
    return sample, loss

Latent prediction and loss.

Args:
latents_pred: Tensor of shape [..., depth].
latents_discrete_hot: Tensor of shape [..., vocab_size].
vocab_size: an int representing the vocab size.
hparams: HParams.

Returns:
sample: Tensor of shape [...], a sample from a multinomial distribution.
loss: Tensor of shape [...], the softmax cross-entropy.


def push(self, x):
    if not math.isnan(x):
        self._sorted_items.add(x)
    if self._window_mode == WindowMode.SLIDING:
        if len(self._queue) >= self._window_size and (not math.isnan((old_x := self.pop()))):
            self._sorted_items.discard(old_x)
        super().push(x)

Pushes a new value, maintains the sorted list, and manages the window.

Args:
x: The new value to be pushed.


def safe_url(self, url, errors='strict'):
    if (url is not None):
        url = quote(self.s(url, errors=errors), safe='~')
    return url

URL encode value for safe HTTP request.

Args:
url (string): The string to URL Encode.

Returns:
(string): The urlencoded string.


def _submit_request(self, url, params=None, data=None, headers=None, method='GET'):
    if (headers is None):
        headers = {}
    if (self._auth_header is not None):
        headers['Authorization'] = self._auth_header
    try:
        if (method == 'POST'):
            result = requests.post(url, params=params, data=data, headers=headers)
        elif (method == 'GET'):
            result = requests.get(url, params=params, data=data, headers=headers)
        result.raise_for_status()
        return (result.status_code, result.json())
    except requests.exceptions.HTTPError as e:
        return (e.response.status_code, e.response.reason)
    except RemoteDisconnected as e:
        raise CliException(e)
    except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL) as e:
        raise CliException(e)
    except requests.exceptions.ConnectionError as e:
        raise CliException('Unable to connect to "{}": make sure URL is correct'.format(self._base_url))

Submits the given request, and handles the errors appropriately.

Args:
url (str): the request to send.
params (dict): params to be passed along to get/post
data (bytes): the data to include in the request.
headers (dict): the headers to include in the request.
method (str): the method to use for the request, "POST" or "GET".

Returns:
tuple of (int, str): The response status code and the json parsed
body, or the error message.

Raises:
`CliException`: If any issues occur with the URL.


def getprops(self, prop_names):
    attempts = DEFAULT_GETPROPS_ATTEMPTS
    results = {}
    for attempt in range(attempts):
        raw_output = self.shell(['getprop'], timeout=DEFAULT_GETPROP_TIMEOUT_SEC)
        properties = self._parse_getprop_output(raw_output)
        if properties:
            for name in prop_names:
                if name in properties:
                    results[name] = properties[name]
            break
        if attempt < attempts - 1:
            time.sleep(DEFAULT_GETPROPS_RETRY_SLEEP_SEC)
    return results

Get multiple properties of the device.

This is a convenience wrapper for `adb shell getprop`. Use this to
reduce the number of adb calls when getting multiple properties.

Args:
prop_names: list of strings, the names of the properties to get.

Returns:
A dict containing name-value pairs of the properties requested, if
they exist.


def set_child_node(self, name, node):

        assert isinstance(node, TreeMapNode)
        self._nodes[name] = node
        node.set_parent(self)

Add one child node to this node.

Args:
name (str): Name of the child.
node (TreeMapNode): Node to add.

Warning:
No test is done to see whether or not a node was already attached with that name. If this is the case, the
new node takes the place of the old one that is now unreachable. See :meth:`set_unique_child_node`.


def _PrintSessionsOverview(self, storage_reader):

    table_view = views.ViewsFactory.GetTableView(
        self._views_format_type, title='Sessions')

    for session in storage_reader.GetSessions():
      start_time = timelib.Timestamp.CopyToIsoFormat(
          session.start_time)
      session_identifier = uuid.UUID(hex=session.identifier)
      session_identifier = '{0!s}'.format(session_identifier)
      table_view.AddRow([session_identifier, start_time])

    table_view.Write(self._output_writer)

Prints a sessions overview.

Args:
storage_reader (StorageReader): storage reader.


def getJsonFromApi(view, request):

	jsonText = view(request)
	jsonText = json.loads(jsonText.content.decode('utf-8'))
	return jsonText

Return json from querying Web Api

Args:
view: django view function.
request: http request object got from django.

Returns: json format dictionary


def padded_cross_entropy_loss(logits, labels, smoothing, vocab_size):

  with tf.name_scope("loss", [logits, labels]):
    logits, labels = _pad_tensors_to_same_length(logits, labels)

    with tf.name_scope("smoothing_cross_entropy", [logits, labels]):
      confidence = 1.0 - smoothing
      low_confidence = (1.0 - confidence) / tf.to_float(vocab_size - 1)
      soft_targets = tf.one_hot(
          tf.cast(labels, tf.int32),
          depth=vocab_size,
          on_value=confidence,
          off_value=low_confidence)
      xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(
          logits=logits, labels=soft_targets)

      normalizing_constant = -(
          confidence * tf.log(confidence) + tf.to_float(vocab_size - 1) *
          low_confidence * tf.log(low_confidence + 1e-20))
      xentropy -= normalizing_constant

    weights = tf.to_float(tf.not_equal(labels, 0))
    return xentropy * weights, weights

Calculate cross entropy loss while ignoring padding.

Args:
logits: Tensor of size [batch_size, length_logits, vocab_size]
labels: Tensor of size [batch_size, length_labels]
smoothing: Label smoothing constant, used to determine the on and off values
vocab_size: int size of the vocabulary
Returns:
Returns a float32 tensor with shape
[batch_size, max(length_logits, length_labels)]


def _order_pases(self, passes):

        passes = set(passes)

        pass_deps = {}

        for opt in passes:
            _, before, after = self._known_passes[opt]

            if opt not in pass_deps:
                pass_deps[opt] = set()

            for after_pass in after:
                pass_deps[opt].add(after_pass)

            for other in before:
                if other not in passes:
                    continue

                if other not in pass_deps:
                    pass_deps[other] = set()

                pass_deps[other].add(opt)

        return toposort_flatten(pass_deps)

Topologically sort optimization passes.

This ensures that the resulting passes are run in order
respecting before/after constraints.

Args:
passes (iterable): An iterable of pass names that should
be included in the optimization passes run.


def __security_definitions_descriptor(self, issuers):
    if (not issuers):
        result = {_DEFAULT_SECURITY_DEFINITION: {'authorizationUrl': '', 'flow': 'implicit', 'type': 'oauth2', 'x-google-issuer': 'https:
        return result
    result = {}
    for (issuer_key, issuer_value) in issuers.items():
        result[issuer_key] = {'authorizationUrl': '', 'flow': 'implicit', 'type': 'oauth2', 'x-google-issuer': issuer_value.issuer}
        if issuer_value.jwks_uri:
            result[issuer_key]['x-google-jwks_uri'] = issuer_value.jwks_uri
    return result

Create a descriptor for the security definitions.

Args:
issuers: dict, mapping issuer names to Issuer tuples

Returns:
The dict representing the security definitions descriptor.


def tool_cancellation(self) -> str | None:
    if not self.part.function_response:
        return None
    if self.part.function_response.name != 'tool_cancellation':
        return None
    if not self.part.function_response.response:
        return None
    return self.part.function_response.response.get('function_call_id', None)

Returns an id of a function call to be cancelled.

If the part is not a tool cancellation request, returns None.

Returns:
The id of the function call to be cancelled or None if this part is not a
tool cancellation from the model.


def decode_field(self, field, value):

    if isinstance(field, messages.BytesField):
      try:

        padded_value = self.__pad_value(str(value), 4, '=')
        return base64.urlsafe_b64decode(padded_value)
      except (TypeError, UnicodeEncodeError), err:
        raise messages.DecodeError('Base64 decoding error: %s' % err)

    return super(EndpointsProtoJson, self).decode_field(field, value)

Decode a JSON value to a python value.

Args:
field: A ProtoRPC field instance.
value: A serialized JSON value.

Returns:
A Python value compatible with field.


def _CheckLogicalLines(self, llines, list_of_expected):
    actual = []
    for lline in llines:
        filtered_values = [ft.value for ft in lline.tokens if ft.name not in pytree_utils.NONSEMANTIC_TOKENS]
        actual.append((lline.depth, filtered_values))
    self.assertEqual(list_of_expected, actual)

Check that the given LogicalLines match expectations.

Args:
llines: list of LogicalLine
list_of_expected: list of (depth, values) pairs. Non-semantic tokens are
filtered out from the expected values.


def as_text_with_reasoning(content: ProcessorContentTypes, *, strict: bool=False) -> tuple[str, str]:
    text_parts = []
    thought_parts = []
    for mime, p in ProcessorContent(content).items():
        if is_text(mime):
            if p.part.thought:
                thought_parts.append(p.text)
            else:
                text_parts.append(p.text)
        elif strict:
            raise ValueError(f'Unsupported content type {mime}.')
    return (''.join(text_parts), ''.join(thought_parts))

Returns a tuple of the final and reasoning text representing content.

The returned tuple contains two elements:
- The first element (index 0) is a string representing the main text
extracted
from the input `content`.
- The second element (index 1) is a string representing the reasoning or
thoughts associated with the input `content`.

Args:
content: The content to process. This can be of various types as defined by
`ProcessorContentTypes`.
strict: If True, unsupported content types will raise a ValueError.
Otherwise, they will be ignored.

Returns:
A tuple containing two strings: (text, reasoning).


def _operation_status_message(self):
    metadata = self._op['metadata']
    if (not self._op['done']):
        if (('events' in metadata) and metadata['events']):
            last_event = metadata['events'][(- 1)]
            msg = last_event['description']
            ds = last_event['startTime']
        else:
            msg = 'Pending'
            ds = metadata['createTime']
    else:
        ds = metadata['endTime']
        if ('error' in self._op):
            msg = self._op['error']['message']
        else:
            msg = 'Success'
    return (msg, google_base.parse_rfc3339_utc_string(ds))

Returns the most relevant status string and last updated date string.

This string is meant for display only.

Returns:
A printable status string and date string.


def Main(url):

    web_scrape = WebScraping()

    web_scrape.readable_web_pdf = WebPDFReading()

    document = web_scrape.scrape(url)

    auto_abstractor = AutoAbstractor()

    auto_abstractor.tokenizable_doc = MeCabTokenizer()

    abstractable_doc = TopNRankAbstractor()

    result_dict = auto_abstractor.summarize(document, abstractable_doc)

    [print(sentence) for sentence in result_dict["summarize_result"]]


def get_all_plugin_assets(graph=None):
    if graph is None:
        graph = ops.get_default_graph()
    out = []
    for name in graph.get_collection(_PLUGIN_ASSET_PREFIX):
        collection = graph.get_collection(_PLUGIN_ASSET_PREFIX + name)
        if len(collection) != 1:
            raise ValueError('Collection for %s had %d items, expected 1' % (name, len(collection)))
        out.append(collection[0])
    return out

Retrieve all PluginAssets stored in the graph collection.

Args:
graph: Optionally, the graph to get assets from. If unspecified, the default
graph is used.

Returns:
A list with all PluginAsset instances in the graph.

Raises:
ValueError: if we unexpectedly find a collection with the wrong number of
PluginAssets.


def match_validator(expression):
    if isinstance(expression, str):
        compiled = re.compile(expression)
    elif hasattr(expression, 'match'):
        compiled = expression
    else:
        raise TypeError('Provided match is nor a string nor has a match method (like re expressions)')

    def validator(value):
        if (not compiled.match(value)):
            raise ValidationError('{} does not match pattern: {}'.format(value, (compiled.pattern if hasattr(compiled, 'pattern') else compiled)))
    return validator

Return validator function that will check if matches given expression.

Args:
match: if string then this will be converted to regular expression
using ``re.compile``. Can be also any object that has ``match()``
method like already compiled regular regular expression or custom
matching object/class.


def _GetSpecificationStore(cls, format_category):

    specification_store = specification.FormatSpecificationStore()
    remainder_list = []

    for analyzer_helper in iter(cls._analyzer_helpers.values()):
      if not analyzer_helper.IsEnabled():
        continue

      if format_category in analyzer_helper.format_categories:
        format_specification = analyzer_helper.GetFormatSpecification()

        if format_specification is not None:
          specification_store.AddSpecification(format_specification)
        else:
          remainder_list.append(analyzer_helper)

    return specification_store, remainder_list

Retrieves the specification store for specified format category.

Args:
format_category (str): format category.

Returns:
tuple[FormatSpecificationStore, list[AnalyzerHelper]]: a format
specification store and remaining analyzer helpers that do not have
a format specification.


def Relay(self, inventory):
    inventory = InvPayload(type=inventory.InventoryType, hashes=[inventory.Hash.ToBytes()])
    m = Message('inv', inventory)
    self.SendSerializedMessage(m)
    return True

Wrap the inventory in a InvPayload object and send it over the write to the remote node.

Args:
inventory:

Returns:
bool: True (fixed)


def users_setPresence(self, *, presence: str, **kwargs) -> SlackResponse:
    kwargs.update({'presence': presence})
    return self.api_call('users.setPresence', json=kwargs)

Manually sets user presence.

Args:
presence (str): Either 'auto' or 'away'.


def _sample(self, nmr_samples, thinning=1, return_output=True):
    kernel_data = self._get_kernel_data(nmr_samples, thinning, return_output)
    sample_func = self._get_compute_func(nmr_samples, thinning, return_output)
    sample_func.evaluate(kernel_data, self._nmr_problems, use_local_reduction=all((env.is_gpu for env in self._cl_runtime_info.cl_environments)), cl_runtime_info=self._cl_runtime_info)
    self._sampling_index += (nmr_samples * thinning)
    if return_output:
        return (kernel_data['samples'].get_data(), kernel_data['log_likelihoods'].get_data(), kernel_data['log_priors'].get_data())

Sample the given number of samples with the given thinning.

If ``return_output`` we will return the samples, log likelihoods and log priors. If not, we will advance the
state of the sampler without returning storing the samples.

Args:
nmr_samples (int): the number of iterations to advance the sampler
thinning (int): the thinning to apply
return_output (boolean): if we should return the output

Returns:
None or tuple: if ``return_output`` is True three ndarrays as (samples, log_likelihoods, log_priors)


def fit1d(samples, e, remove_zeros=False, **kw):
    samples = samples[(~ np.isnan(samples))]
    length = (len(e) - 1)
    (hist, _) = np.histogramdd(samples, (e,))
    hist = (hist / sum(hist))
    (basis, knots) = spline_base1d(length, marginal=hist, **kw)
    non_zero = (hist > 0)
    model = linear_model.BayesianRidge()
    if remove_zeros:
        model.fit(basis[(non_zero, :)], hist[(:, np.newaxis)][(non_zero, :)])
    else:
        hist[(~ non_zero)] = np.finfo(float).eps
        model.fit(basis, hist[(:, np.newaxis)])
    return (model.predict(basis), hist, knots)

Fits a 1D distribution with splines.

Input:
samples: Array
Array of samples from a probability distribution
e: Array
Edges that define the events in the probability
distribution. For example, e[0] < x <= e[1] is
the range of values that are associated with the
first event.
**kw: Arguments that are passed on to spline_bse1d.

Returns:
distribution: Array
An array that gives an estimate of probability for
events defined by e.
knots: Array
Sequence of knots that were used for the spline basis


def _ConvertMessageDescriptor(self, desc_proto, package=None, file_desc=None, scope=None, syntax=None):
    if package:
        desc_name = '.'.join((package, desc_proto.name))
    else:
        desc_name = desc_proto.name
    if (file_desc is None):
        file_name = None
    else:
        file_name = file_desc.name
    if (scope is None):
        scope = {}
    nested = [self._ConvertMessageDescriptor(nested, desc_name, file_desc, scope, syntax) for nested in desc_proto.nested_type]
    enums = [self._ConvertEnumDescriptor(enum, desc_name, file_desc, None, scope) for enum in desc_proto.enum_type]
    fields = [self._MakeFieldDescriptor(field, desc_name, index) for (index, field) in enumerate(desc_proto.field)]
    extensions = [self._MakeFieldDescriptor(extension, desc_name, index, is_extension=True) for (index, extension) in enumerate(desc_proto.extension)]
    oneofs = [descriptor.OneofDescriptor(desc.name, '.'.join((desc_name, desc.name)), index, None, [], desc.options) for (index, desc) in enumerate(desc_proto.oneof_decl)]
    extension_ranges = [(r.start, r.end) for r in desc_proto.extension_range]
    if extension_ranges:
        is_extendable = True
    else:
        is_extendable = False
    desc = descriptor.Descriptor(name=desc_proto.name, full_name=desc_name, filename=file_name, containing_type=None, fields=fields, oneofs=oneofs, nested_types=nested, enum_types=enums, extensions=extensions, options=_OptionsOrNone(desc_proto), is_extendable=is_extendable, extension_ranges=extension_ranges, file=file_desc, serialized_start=None, serialized_end=None, syntax=syntax)
    for nested in desc.nested_types:
        nested.containing_type = desc
    for enum in desc.enum_types:
        enum.containing_type = desc
    for (field_index, field_desc) in enumerate(desc_proto.field):
        if field_desc.HasField('oneof_index'):
            oneof_index = field_desc.oneof_index
            oneofs[oneof_index].fields.append(fields[field_index])
            fields[field_index].containing_oneof = oneofs[oneof_index]
    scope[_PrefixWithDot(desc_name)] = desc
    self._descriptors[desc_name] = desc
    return desc

Adds the proto to the pool in the specified package.

Args:
desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
package: The package the proto should be located in.
file_desc: The file containing this message.
scope: Dict mapping short and full symbols to message and enum types.
syntax: string indicating syntax of the file ("proto2" or "proto3")

Returns:
The added descriptor.


def asdate(self):
    return datetime.date(self.year, self.month, self.day)

Return this datetime_tz as a date object.

Returns:
This datetime_tz as a date object.


def insert_varargs_and_kwargs(self, args: Iterable[str]):
    varargs_names = []
    kwargs_names = []
    for name in args:
        if self.has_param(name):
            continue
        if pytd_utils.ANON_PARAM.match(name):
            varargs_names.append(name)
        else:
            kwargs_names.append(name)
    new_param_names = self.param_names + tuple(sorted(varargs_names)) + tuple(sorted(kwargs_names))
    return self._replace(param_names=new_param_names)

Insert varargs and kwargs from args into the signature.

Args:
args: An iterable of passed arg names.

Returns:
A copy of this signature with the passed varargs and kwargs inserted.


def valid(self, name):

    name = re.sub('[^0-9a-zA-Z_]', '', name)
    if re.match('[0-9]', name):
      name = '_' + name
    return name

Ensure a variable name is valid.

Note: Assumes variable names are ASCII, which isn't necessarily true in
Python 3.

Args:
name: A proposed variable name.

Returns:
A valid version of the name.


def get_filetypes(self):
    if (not self.is_requestable()):
        return [resource.get_file_type() for resource in self.get_resources()]
    return self._get_stringlist_from_commastring('file_types')

Return list of filetypes in your data

Returns:
List[str]: List of filetypes


def read_eof(self, echo=None):

        d = b''
        while True:
            try:
                d += self.read(1, echo)
            except EOFError:
                return d

Read until the channel is closed.

Args:
echo(bool): Whether to write the read data to stdout.

Returns:
bytes: The read data.


def stat(filename, retry_params=None, _account_id=None):
    common.validate_file_path(filename)
    api = storage_api._get_storage_api(retry_params=retry_params, account_id=_account_id)
    (status, headers, content) = api.head_object(api_utils._quote_filename(filename))
    errors.check_status(status, [200], filename, resp_headers=headers, body=content)
    file_stat = common.GCSFileStat(filename=filename, st_size=common.get_stored_content_length(headers), st_ctime=common.http_time_to_posix(headers.get('last-modified')), etag=headers.get('etag'), content_type=headers.get('content-type'), metadata=common.get_metadata(headers))
    return file_stat

Get GCSFileStat of a Google Cloud storage file.

Args:
filename: A Google Cloud Storage filename of form '/bucket/filename'.
retry_params: An api_utils.RetryParams for this call to GCS. If None,
the default one is used.
_account_id: Internal-use only.

Returns:
a GCSFileStat object containing info about this file.

Raises:
errors.AuthorizationError: if authorization failed.
errors.NotFoundError: if an object that's expected to exist doesn't.


def load_cobra_model(self, model):
    self.model = ModelPro(model)
    for g in self.model.genes:
        if self.genes_dir:
            g.root_dir = self.genes_dir
        g.protein.pdb_file_type = self.pdb_file_type
    self.genes = self.model.genes
    log.info('{}: loaded model'.format(model.id))
    log.info('{}: number of reactions'.format(len(self.model.reactions)))
    log.info('{}: number of reactions linked to a gene'.format(ssbio.core.modelpro.true_num_reactions(self.model)))
    log.info('{}: number of genes (excluding spontaneous)'.format(ssbio.core.modelpro.true_num_genes(self.model, custom_spont_id=self.custom_spont_id)))
    log.info('{}: number of metabolites'.format(len(self.model.metabolites)))
    log.warning('IMPORTANT: All Gene objects have been transformed into GenePro objects, and will be for any new ones')

Load a COBRApy Model object into the GEM-PRO project.

Args:
model (Model): COBRApy ``Model`` object


def match_all_args(ctx: 'context.Context', node: cfg.CFGNode, func: '_function_base.NativeFunction|_interpreter_function.InterpreterFunction', args: 'Args') -> 'tuple[Args, Sequence[tuple[Exception, str, _base.BaseValue]]]':
    positional_names = func.get_positional_names()
    needs_checking = True
    errors = []
    while needs_checking:
        try:
            func.match_args(node, args)
        except error_types.FailedFunctionCall as e:
            if isinstance(e, error_types.WrongKeywordArgs):
                errors.append((e, e.extra_keywords[0], None))
                for i in e.extra_keywords:
                    args = args.delete_namedarg(i)
            elif isinstance(e, error_types.DuplicateKeyword):
                errors.append((e, e.duplicate, None))
                args = args.delete_namedarg(e.duplicate)
            elif isinstance(e, error_types.MissingParameter):
                errors.append((e, e.missing_parameter, None))
                args = args.replace_namedarg(e.missing_parameter, ctx.new_unsolvable(node))
            elif isinstance(e, error_types.WrongArgTypes):
                arg_name = e.bad_call.bad_param.name
                for name, value in e.bad_call.passed_args:
                    if name != arg_name:
                        continue
                    errors.append((e, name, value))
                    try:
                        pos = positional_names.index(name)
                    except ValueError:
                        args = args.replace_namedarg(name, ctx.new_unsolvable(node))
                    else:
                        args = args.replace_posarg(pos, ctx.new_unsolvable(node))
                    break
                else:
                    raise AssertionError(f'Mismatched parameter {arg_name} not found in passed_args') from e
            else:
                raise
        else:
            needs_checking = False
    return (args, errors)

Call match_args multiple times to find all type errors.

Args:
ctx: The abstract context.
node: The current CFG node.
func: An abstract function
args: An Args object to match against func

Returns:
A tuple of (new_args, errors)
where new_args = args with all incorrectly typed values set to Any
errors = a list of [(type mismatch error, arg name, value)]

Reraises any error that is not InvalidParameters


def wait(self, timeout=None):
    with self._put_wait_lock, self._queue_lock:
        logging.info('Waiting for all global closures to be finished.')
        while not self._error and (not self._queue.empty() or self._inflight_closure_count > 0):
            if not self._stop_waiting_condition.wait(timeout=timeout):
                return False
        self._raise_if_error()
        return True

Wait for all closures to be finished before returning.

If `mark_failed` was called before or during `wait`, the error from the
first invocation of `mark_failed` will be raised.

Args:
timeout: A float specifying a timeout for the wait in seconds.

Returns:
True unless the given timeout expired, in which case it returns False.


def __init__(self, target_pixels=None, **kwargs):

    super(DiabeticRetinopathyDetectionConfig, self).__init__(**kwargs)
    self._target_pixels = target_pixels

BuilderConfig for DiabeticRetinopathyDetection.

Args:
target_pixels: If given, rescale the images so that the total number of
pixels is roughly this value.
**kwargs: keyword arguments forward to super.


def _unpack_sequence(self, state, n_before, n_after=-1):
    assert n_after >= -1
    state, seq = state.pop()
    options = []
    nontuple_seq = self.ctx.program.NewVariable()
    has_slurp = n_after > -1
    count = n_before + max(n_after, 0)
    nondeterministic_iterable = False
    for b in abstract_utils.expand_type_parameter_instances(seq.bindings):
        if b.data.full_name in ('builtins.set', 'builtins.frozenset'):
            nondeterministic_iterable = True
        tup = self._get_literal_sequence(b.data)
        if tup is not None:
            if has_slurp and len(tup) >= count:
                options.append(self._restructure_tuple(state, tup, n_before, n_after))
                continue
            elif len(tup) == count:
                options.append(tup)
                continue
            else:
                self.ctx.errorlog.bad_unpacking(self.frames, len(tup), count)
        if b.IsVisible(state.node):
            nontuple_seq.PasteBinding(b, state.node)
    if nontuple_seq.bindings:
        state, itr = self._get_iter(state, nontuple_seq)
        state, itr_result = self._call(state, itr, '__next__', ())
    elif not options:
        itr_result = self.ctx.new_unsolvable(state.node)
    else:
        itr_result = None
    if itr_result:
        option = [itr_result for _ in range(count)]
        if has_slurp:
            slurp = self.ctx.convert.build_list_of_type(state.node, itr_result)
            option = option[:n_before] + [slurp] + option[n_before:]
        options.append(option)
    values = tuple((self.ctx.convert.build_content(value, discard_concrete_values=False) for value in zip(*options)))
    if len(values) > 1 and nondeterministic_iterable:
        self.ctx.errorlog.nondeterministic_unpacking(self.frames)
    for value in reversed(values):
        if not value.bindings:
            value = self.ctx.convert.empty.to_variable(state.node)
        state = state.push(value)
    return state

Pops a tuple (or other iterable) and pushes it onto the VM's stack.

Supports destructuring assignment with potentially a single list variable
that slurps up the remaining elements:
1. a, b, c = ...  # UNPACK_SEQUENCE
2. a, *b, c = ... # UNPACK_EX

Args:
state: The current VM state
n_before: Number of elements before the list (n_elements for case 1)
n_after: Number of elements after the list (-1 for case 1)

Returns:
The new state.


def cancel(self):

        if not self.id:
            raise WorkflowError('Workflow is not running.  Cannot cancel.')

        if self.batch_values:
            self.workflow.batch_workflow_cancel(self.id)
        else:
            self.workflow.cancel(self.id)

Cancel a running workflow.

Args:
None

Returns:
None


def _start_app_and_connect(self):

        self._check_app_installed()
        self.disable_hidden_api_blacklist()

        persists_shell_cmd = self._get_persist_command()

        self.log.info('Launching snippet apk %s with protocol %d.%d',
                      self.package, _PROTOCOL_MAJOR_VERSION,
                      _PROTOCOL_MINOR_VERSION)
        cmd = _LAUNCH_CMD % (persists_shell_cmd, self.package)
        start_time = time.time()
        self._proc = self._do_start_app(cmd)

        line = self._read_protocol_line()
        match = re.match('^SNIPPET START, PROTOCOL ([0-9]+) ([0-9]+)$', line)
        if not match or match.group(1) != '1':
            raise ProtocolVersionError(self._ad, line)

        line = self._read_protocol_line()
        match = re.match('^SNIPPET SERVING, PORT ([0-9]+)$', line)
        if not match:
            raise ProtocolVersionError(self._ad, line)
        self.device_port = int(match.group(1))

        self.host_port = utils.get_available_host_port()
        self._adb.forward(
            ['tcp:%d' % self.host_port,
             'tcp:%d' % self.device_port])
        self.connect()

        self.log.debug('Snippet %s started after %.1fs on host port %s',
                       self.package, time.time() - start_time, self.host_port)

Starts snippet apk on the device and connects to it.

After prechecks, this launches the snippet apk with an adb cmd in a
standing subprocess, checks the cmd response from the apk for protocol
version, then sets up the socket connection over adb port-forwarding.

Args:
ProtocolVersionError, if protocol info or port info cannot be
retrieved from the snippet apk.


def _contains(self, item):

        if self is item:
            return True
        for m in self.modules:
            if item in m:
                return True
        for p in self.packages:
            if item in p:
                return True
        return False

Whether given item is contained inside the node modules/packages.

Args:
item (Package/Module): a package or module.

Returns:
bool: True if self is item or item in self's packages/modules.


def _get_sorted_inputs(filename, delimiter="\n"):

  tf.logging.info("Getting sorted inputs")
  with tf.gfile.Open(filename) as f:
    text = f.read()
    records = text.split(delimiter)
    inputs = [record.strip() for record in records]

    if not inputs[-1]:
      inputs.pop()
  input_lens = [(i, -len(line.split())) for i, line in enumerate(inputs)]
  sorted_input_lens = sorted(input_lens, key=operator.itemgetter(1))

  sorted_keys = {}
  sorted_inputs = []
  for i, (index, _) in enumerate(sorted_input_lens):
    sorted_inputs.append(inputs[index])
    sorted_keys[index] = i
  return sorted_inputs, sorted_keys

Returning inputs sorted according to decreasing length.

This causes inputs of similar lengths to be processed in the same batch,
facilitating early stopping for short sequences.

Longer sequences are sorted first so that if you're going to get OOMs,
you'll see it in the first batch.

Args:
filename: path to file with inputs, 1 per line.
delimiter: str, delimits records in the file.

Returns:
a sorted list of inputs


def mean_absolute_error(y_true, y_pred):
    y_pred = tensor_conversion.convert_to_tensor_v2_with_dispatch(y_pred)
    y_true = math_ops.cast(y_true, y_pred.dtype)
    return backend.mean(math_ops.abs(y_pred - y_true), axis=-1)

Computes the mean absolute error between labels and predictions.

`loss = mean(abs(y_true - y_pred), axis=-1)`

Standalone usage:

>>> y_true = np.random.randint(0, 2, size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = tf.keras.losses.mean_absolute_error(y_true, y_pred)
>>> assert loss.shape == (2,)
>>> assert np.array_equal(
...     loss.numpy(), np.mean(np.abs(y_true - y_pred), axis=-1))

Args:
y_true: Ground truth values. shape = `[batch_size, d0, .. dN]`.
y_pred: The predicted values. shape = `[batch_size, d0, .. dN]`.

Returns:
Mean absolute error values. shape = `[batch_size, d0, .. dN-1]`.


def launch_batch_workflow(self, batch_workflow):

        url = '%(base_url)s/batch_workflows' % {
            'base_url': self.base_url
        }
        try:
            r = self.gbdx_connection.post(url, json=batch_workflow)
            batch_workflow_id = r.json()['batch_workflow_id']
            return batch_workflow_id
        except TypeError as e:
            self.logger.debug('Batch Workflow not launched, reason: {0}'.format(e))

Launches GBDX batch workflow.

Args:
batch_workflow (dict): Dictionary specifying batch workflow tasks.

Returns:
Batch Workflow id (str).


def in_coord_list_pbc(fcoord_list, fcoord, atol=1e-08):
    return (len(find_in_coord_list_pbc(fcoord_list, fcoord, atol=atol)) > 0)

Tests if a particular fractional coord is within a fractional coord_list.

Args:
fcoord_list: List of fractional coords to test
fcoord: A specific fractional coord to test.
atol: Absolute tolerance. Defaults to 1e-8.

Returns:
True if coord is in the coord list.


def get_models(self, uniprot_acc):
    if (uniprot_acc in self.all_models):
        return self.all_models[uniprot_acc]
    else:
        log.error('{}: no SWISS-MODELs available'.format(uniprot_acc))
        return None

Return all available models for a UniProt accession number.

Args:
uniprot_acc (str): UniProt ACC/ID

Returns:
dict: All available models in SWISS-MODEL for this UniProt entry


def unflat_take(items_list, unflat_index_list):
    return [(unflat_take(items_list, xs) if isinstance(xs, list) else take(items_list, xs)) for xs in unflat_index_list]

r"""
Returns nested subset of items_list

Args:
items_list (list):
unflat_index_list (list): nested list of indices

CommandLine:
python -m utool.util_list --exec-unflat_take

SeeAlso:
ut.take

Example:
>>> # DISABLE_DOCTEST
>>> from utool.util_list import *  # NOQA
>>> items_list = [1, 2, 3, 4, 5]
>>> unflat_index_list = [[0, 1], [2, 3], [0, 4]]
>>> result = unflat_take(items_list, unflat_index_list)
>>> print(result)
[[1, 2], [3, 4], [1, 5]]


def check_config(config):

    for section, expected_section_keys in SECTION_KEYS.items():
        section_content = config.get(section)
        if not section_content:
            raise ConfigurationError("Config file badly formed! Section {} is missing."
                                     .format(section))
        elif not _section_is_healthy(section_content, expected_section_keys):
            raise ConfigurationError("The {} section of the configuration file is badly formed!"
                                     .format(section))

Check that all sections of the config contain the keys that they should.

Args:
config (defaultdict): A defaultdict.
Raises:
ConfigurationError


def get_builtin_code_from_operator_code(opcode):
    if hasattr(opcode, 'BuiltinCode') and callable(opcode.BuiltinCode):
        return max(opcode.BuiltinCode(), opcode.DeprecatedBuiltinCode())
    return max(opcode.builtinCode, opcode.deprecatedBuiltinCode)

Return the builtin code of the given operator code.

The following method is introduced to resolve op builtin code shortage
problem. The new builtin operator will be assigned to the extended builtin
code field in the flatbuffer schema. Those methods helps to hide builtin code
details.

Args:
opcode: Operator code.

Returns:
The builtin code of the given operator code.


def get(self, identifier, default=None):
    split_label = (tuple(identifier.split('.')) if isinstance(identifier, str) else tuple(identifier))
    if (len(split_label) == 1):
        identifier = split_label[0]
        return self.__dict__.get(identifier, default)
    path_item = self
    for identifier in split_label:
        if ((path_item == default) or (path_item is None)):
            return default
        path_item = path_item.get(identifier, default)
    return path_item

Get a node of the AttrTree using its path string.

Args:
identifier: Path string of the node to return
default: Value to return if no node is found

Returns:
The indexed node of the AttrTree


def element_creator(namespace=None):
    ELEMENT_MAKER = _objectify.ElementMaker(namespace=namespace, annotate=False)

    def create_elem(tag, attr=None, text=None):
        ':class:`objectify.Element` wrapper with namespace defined.\n\n        Args:\n            tag (str): Tag name\n            attr (dict): Default attributes for tag\n            text (str): Text content for the tag\n\n        Returns:\n            _objectify.ObjectifiedElement: objectify element\n        '
        if (not attr):
            attr = {}
        if text:
            element = getattr(ELEMENT_MAKER, tag)(text, **attr)
        else:
            element = getattr(ELEMENT_MAKER, tag)(**attr)
        return element
    return create_elem

Create a simple namespace-aware objectify element creator.

Args:
namespace (str): Namespace to work in

Returns:
function: Namespace-aware element creator


def on_created(self, event):
    if (not self._event_error):
        self.logger.info(u'Change detected from a create on: %s', event.src_path)
        self.compile_dependencies(event.src_path)

Called when a new file or directory is created.

Todo:
This should be also used (extended from another class?) to watch
for some special name file (like ".boussole-watcher-stop" create to
raise a KeyboardInterrupt, so we may be able to unittest the
watcher (click.CliRunner is not able to send signal like CTRL+C
that is required to watchdog observer loop)

Args:
event: Watchdog event, either ``watchdog.events.DirCreatedEvent``
or ``watchdog.events.FileCreatedEvent``.


def compute_author_match_score(x_authors, y_authors):
    if ((not x_authors) or (not y_authors)):
        return 0.0
    matches = get_number_of_author_matches(x_authors, y_authors)
    max_length = max(len(x_authors), len(y_authors))
    return (matches / float(max_length))

Return the matching score of 2 given lists of authors.

Args:
x_authors (list(dict)): first schema-compliant list of authors.
y_authors (list(dict)): second schema-compliant list of authors.

Returns:
float: matching score of authors.


def __init__(self, xid=None, command=None, group_type=None, group_id=None,
                 buckets=None):

        super().__init__(xid)
        self.command = command
        self.group_type = group_type
        self.group_id = group_id
        self.buckets = buckets

Create a GroupMod with the optional parameters below.

Args:
xid (int): Header's transaction id. Defaults to random.
command (GroupModCommand): One of OFPGC_*.
group_type (GroupType): One of OFPGT_*.
group_id (int): Group identifier.
buckets (:class:`ListOfBuckets`): The length of the bucket
array is inferred from the length field in the header.


def new(cls, script, commit, params, campaign_dir, overwrite=False):
    if (not Path(campaign_dir).is_absolute()):
        raise ValueError('Path is not absolute')
    if (Path(campaign_dir).exists() and (not overwrite)):
        raise FileExistsError('The specified directory already exists')
    elif (Path(campaign_dir).exists() and overwrite):
        campaign_dir_name = os.path.basename(campaign_dir)
        folder_contents = set(os.listdir(campaign_dir))
        allowed_files = set((['data', ('%s.json' % campaign_dir_name)] + [os.path.basename(os.path.normpath(f)) for f in glob.glob(os.path.join(campaign_dir, '.*'))]))
        if (not folder_contents.issubset(allowed_files)):
            raise ValueError('The specified directory cannot be overwritten because it contains user files.')
        shutil.rmtree(campaign_dir)
    os.makedirs(campaign_dir)
    tinydb = TinyDB(os.path.join(campaign_dir, ('%s.json' % os.path.basename(campaign_dir))))
    config = {'script': script, 'commit': commit, 'params': sorted(params)}
    tinydb.table('config').insert(config)
    return cls(tinydb, campaign_dir)

Initialize a new class instance with a set configuration and filename.

The created database has the same name of the campaign directory.

Args:
script (str): the ns-3 name of the script that will be used in this
campaign;
commit (str): the commit of the ns-3 installation that is used to
run the simulations.
params (list): a list of the parameters that can be used on the
script.
campaign_dir (str): The path of the file where to save the DB.
overwrite (bool): Whether or not existing directories should be
overwritten.


def add_tools(self, *tools):
    for tool in tools:
        if (not isinstance(tool, Tool)):
            raise ValueError('All arguments to add_tool must be Tool subclasses.')
        self.toolbar.tools.append(tool)

Adds tools to the plot.

Args:
*tools (Tool) : the tools to add to the Plot

Returns:
None


def __init__(self, module_to_name, members, filename_to_library_map,
               path_prefix):

    self._module_to_name = module_to_name
    self._members = members
    self._filename_to_library_map = filename_to_library_map
    self._path_prefix = path_prefix

Creates a new Index.

Args:
module_to_name: Dictionary mapping modules to short names.
members: Dictionary mapping member name to (fullname, member).
filename_to_library_map: A list of (filename, Library) pairs. The order
corresponds to the order in which the libraries appear in the index.
path_prefix: Prefix to add to links in the index.


def findAllSingle(self, selfValue):

        resultList = []
        for element in selfValue:
            if isinstance(element, Single):
                resultList.append(element)
                resultList += element.findAllSingle()
        return resultList

Looks for all the single values and subclasses *recursively* and returns a list of them

Args:
selfValue: A list of single, str, int. Normally just ``self.value``

Returns:
list: A list contains only singles and subclasses.


def forward(self, key_value_states, hidden_states, attn_mask=None):
    query = self.q_proj(self.layer_norm(hidden_states))
    key_value_states = self.layer_norm_kv(key_value_states)
    key = self.k_proj(key_value_states)
    value = self.v_proj(key_value_states)
    attn_output, _ = self.multihead_attn(query, key, value, attn_mask=attn_mask)
    attn_output = self.dropout(self.linear(attn_output))
    return attn_output

Forward pass of the AriaCrossAttention module.

Args:
key_value_states (`torch.Tensor`):
Input tensor for key and value.
hidden_states (`torch.Tensor`):
Input tensor for query.
attn_mask (`torch.Tensor`, *optional*, defaults to None):
Attention mask.

Returns:
torch.Tensor:
Output tensor after cross-attention.


def write_compartments(self, stream, compartments, adjacencies,
                           properties=None):

        def convert(entry):
            return self.convert_compartment_entry(
                entry, adjacencies.get(entry.id))

        self._write_entries(stream, compartments, convert, properties)

Write iterable of compartments as YAML object to stream.

Args:
stream: File-like object.
compartments: Iterable of compartment entries.
adjacencies: Dictionary mapping IDs to adjacent compartment IDs.
properties: Set of compartment properties to output (or None to
output all).


def _reduce_pseudo_inverse(nodes):

    _, num_nodes = np.shape(nodes)
    if num_nodes == 2:
        reduction = _REDUCTION0
        denom = _REDUCTION_DENOM0
    elif num_nodes == 3:
        reduction = _REDUCTION1
        denom = _REDUCTION_DENOM1
    elif num_nodes == 4:
        reduction = _REDUCTION2
        denom = _REDUCTION_DENOM2
    elif num_nodes == 5:
        reduction = _REDUCTION3
        denom = _REDUCTION_DENOM3
    else:
        raise _helpers.UnsupportedDegree(num_nodes - 1, supported=(1, 2, 3, 4))

    result = _helpers.matrix_product(nodes, reduction)
    result /= denom
    return result

Performs degree-reduction for a B |eacute| zier curve.

Does so by using the pseudo-inverse of the degree elevation
operator (which is overdetermined).

.. note::

There is also a Fortran implementation of this function, which
will be used if it can be built.

Args:
nodes (numpy.ndarray): The nodes in the curve.

Returns:
numpy.ndarray: The reduced nodes.

Raises:
.UnsupportedDegree: If the degree is not 1, 2, 3 or 4.


def get(self, key, mem_map=True):
    self.raise_error_if_not_open()
    if (key in self._file):
        data = self._file[key]
        if (not mem_map):
            data = data[()]
        return data
    else:
        return None

Read and return the data stored for the given key.

Args:
key (str): The key to read the data from.
mem_map (bool): If ``True`` returns the data as
memory-mapped array, otherwise a copy is returned.

Note:
The container has to be opened in advance.

Returns:
numpy.ndarray: The stored data.


def __init__(self, desc='Loading...', end='', timeout=0.1):
    self.desc = desc
    self.end = end
    self.timeout = timeout
    self._thread = Thread(target=self._animate, daemon=True)
    self.steps = ['⢿', '⣻', '⣽', '⣾', '⣷', '⣯', '⣟', '⡿']
    self.done = False

A loader-like context manager

Args:
desc (str, optional): The loader's description. Defaults to "Loading...".
end (str, optional): Final print. Defaults to "Done!".
timeout (float, optional): Sleep time between prints. Defaults to 0.1.


def ikey(self, value):

        if value == self._defaults['iKey'] and 'iKey' in self._values:
            del self._values['iKey']
        else:
            self._values['iKey'] = value

The ikey property.

Args:
value (string). the property value.


def extend(self, base: 'ValueSpec') -> 'ValueSpec':

Extends a base spec with current spec's rules.

Args:
base: Base ValueSpec to extend.

Returns:
ValueSpec itself.

Raises:
TypeError: When this value spec cannot extend from base.


def __init__(self, *elements, **kwargs):

        if not all([isinstance(e, Row) or issubclass(type(e), Box)
                    for e in elements]):
            raise TypeError('All elements of Column must '
                            'be Row or Box instances')
        width = kwargs.pop('width', 12)
        if width not in range(1, 13):
            raise ValueError('Column width must be between 1 and 12')

        self.type = 'column'
        self.elements = elements
        self.width = width

Init method.

Args:
*elements (): the rows or boxes.
**kwargs: the width can be passed through the keyword args [1-12].


def swap(self, old_chunks, new_chunk):
    indexes = [self.index(chunk) for chunk in old_chunks]
    del self[indexes[0]:(indexes[(- 1)] + 1)]
    self.insert(indexes[0], new_chunk)

Swaps old consecutive chunks with new chunk.

Args:
old_chunks (:obj:`budou.chunk.ChunkList`): List of consecutive Chunks to
be removed.
new_chunk (:obj:`budou.chunk.Chunk`): A Chunk to be inserted.


def plot(self, pts_per_edge, color=None, ax=None):

        if ax is None:
            ax = _plot_helpers.new_axis()
        _plot_helpers.add_patch(ax, color, pts_per_edge, *self._edges)
        return ax

Plot the current curved polygon.

Args:
pts_per_edge (int): Number of points to plot per curved edge.
color (Optional[Tuple[float, float, float]]): Color as RGB profile.
ax (Optional[matplotlib.artist.Artist]): matplotlib axis object
to add plot to.

Returns:
matplotlib.artist.Artist: The axis containing the plot. This
may be a newly created axis.


def get_changed_files(self) -> List[str]:
    out = shell_tools.output_of('git', 'diff', '--name-only', self.compare_commit_id, self.actual_commit_id, '--', cwd=self.destination_directory)
    return [e for e in out.split('\n') if e.strip()]

Get the files changed on one git branch vs another.

Returns:
List[str]: File paths of changed files, relative to the git repo
root.


def Format(pb):
    if isinstance(pb, message.Message):
        return dict(((desc.number, value) for desc, value in pb.ListFields()))
    elif _IsMap(pb):
        return dict(pb.items())
    elif _IsRepeatedContainer(pb):
        return dict(enumerate(list(pb)))
    else:
        return pb

Returns a dictionary or unchanged pb bases on its type.

Specifically, this function returns a dictionary that maps tag
number (for messages) or element index (for repeated fields) to
value, or just pb unchanged if it's neither.

Args:
pb: A proto2 message or a primitive.
Returns:
A dict or unchanged pb.


def stop(self, name: str) -> None:

        if not self._timing:
            return
        now = get_now_utc_pendulum()

        if not self._stack:
            raise AssertionError("MultiTimer.stop() when nothing running")
        if self._stack[-1] != name:
            raise AssertionError(
                "MultiTimer.stop({}) when {} is running".format(
                    repr(name), repr(self._stack[-1])))

        self._totaldurations[name] += now - self._starttimes[name]
        self._stack.pop()

        if self._stack:
            last = self._stack[-1]
            self._starttimes[last] = now

Stop a named timer.

Args:
name: timer to stop


async def selfplay(state, flagfile='selfplay'):
    output_dir = os.path.join(fsdb.selfplay_dir(), state.output_model_name)
    holdout_dir = os.path.join(fsdb.holdout_dir(), state.output_model_name)
    lines = (await run('bazel-bin/cc/selfplay', '--flagfile={}.flags'.format(os.path.join(FLAGS.flags_dir, flagfile)), '--model={}'.format(state.best_model_path), '--output_dir={}'.format(output_dir), '--holdout_dir={}'.format(holdout_dir), '--seed={}'.format(state.seed)))
    result = '\n'.join(lines[(- 6):])
    logging.info(result)
    stats = parse_win_stats_table(result, 1)[0]
    num_games = stats.total_wins
    logging.info('Black won %0.3f, white won %0.3f', (stats.black_wins.total / num_games), (stats.white_wins.total / num_games))
    pattern = os.path.join(output_dir, '*', '*.zz')
    random.seed(state.seed)
    tf.set_random_seed(state.seed)
    np.random.seed(state.seed)
    buffer = example_buffer.ExampleBuffer(sampling_frac=1.0)
    logging.info('Writing golden chunk from "{}"'.format(pattern))
    buffer.parallel_fill(tf.gfile.Glob(pattern))
    buffer.flush(os.path.join(fsdb.golden_chunk_dir(), (state.output_model_name + '.tfrecord.zz')))

Run selfplay and write a training chunk to the fsdb golden_chunk_dir.

Args:
state: the RL loop State instance.
flagfile: the name of the flagfile to use for selfplay, either 'selfplay'
(the default) or 'boostrap'.


def first_dna(self) -> geno.DNA:
    if self.next_dna.__code__ is CustomHyper.next_dna.__code__:
        raise NotImplementedError(f'{self.__class__!r} must implement method `next_dna` to be used in dynamic evaluation mode.')
    return self.next_dna(None)

Returns the first DNA of current sub-space.

Returns:
A string-valued DNA.


def validate_context(
        self, context: Mapping[str, Any]
    ) -> Tuple[bool, List[Tuple[str, str]]]:

        url = f'{self.endpoint}/terms/{context["id"]}'

        res = requests.get(url)
        if res.status_code == 200:
            return (True, [])
        else:
            return (False, [("WARNING", f'Context {context["id"]} not found at {url}')])

Validate context

Args:
context (Mapping[str, Any]): context dictionary of type, id and label

Returns:
Tuple[bool, List[Tuple[str, str]]]:
bool: Is valid?  Yes = True, No = False
List[Tuple[str, str]]: Validation issues, empty if valid, tuple is ('ERROR|WARNING', msg)
e.g. [('WARNING', "Context ID not found")]


def compute(self, x_arr, y_arr):

        y_arr += 1e-08
        return np.sum(x_arr * np.log(x_arr / y_arr), axis=-1)

Compute distance.

Args:
x_arr:      `np.ndarray` of vectors.
y_arr:      `np.ndarray` of vectors.

Retruns:
`np.ndarray` of distances.


def check_version(version, range_=None):
    if (range_ and (version not in range_)):
        raise RezBindError(('found version %s is not within range %s' % (str(version), str(range_))))

Check that the found software version is within supplied range.

Args:
version: Version of the package as a Version object.
range_: Allowable version range as a VersionRange object.


def update_model_handler(self, key: str, model_path: str, previous_key: str):
    if self._key_to_last_update[key] == model_path:
        return
    self._key_to_last_update[key] = model_path
    if key not in self._mh_map:
        self._mh_map[key] = deepcopy(self._mh_map[previous_key])
    self._mh_map[key].update_model_path(model_path)
    if key in self._tag_map:
        tag_to_remove = self._tag_map[key]
        shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
        shared_handle.release(model_to_remove)
        del self._tag_map[key]
        del self._proxy_map[tag_to_remove]

Updates the model path of this model handler and removes it from memory so
that it can be reloaded with the updated path. No-ops if no model update
needs to be applied.
Args:
key: the key associated with the model we'd like to update.
model_path: the new path to the model we'd like to load.
previous_key: the key that is associated with the old version of this
model. This will often be the same as the current key, but sometimes
we will want to keep both the old and new models to serve different
cohorts. In that case, the keys should be different.


def seek(self, offset, whence=os.SEEK_SET):

    if not self._is_open:
      raise IOError('Not opened.')

    if self._current_offset < 0:
      raise IOError(
          'Invalid current offset: {0:d} value less than zero.'.format(
              self._current_offset))

    if whence == os.SEEK_CUR:
      offset += self._current_offset

    elif whence == os.SEEK_END:
      if self._decrypted_stream_size is None:
        self._decrypted_stream_size = self._GetDecryptedStreamSize()
        if self._decrypted_stream_size is None:
          raise IOError('Invalid decrypted stream size.')

      offset += self._decrypted_stream_size

    elif whence != os.SEEK_SET:
      raise IOError('Unsupported whence.')

    if offset < 0:
      raise IOError('Invalid offset value less than zero.')

    if offset != self._current_offset:
      self._current_offset = offset
      self._realign_offset = True

Seeks to an offset within the file-like object.

Args:
offset (int): offset to seek.
whence (Optional[int]): value that indicates whether offset is an
absolute or relative position within the file.

Raises:
IOError: if the seek failed.
OSError: if the seek failed.


def error(self, status=None):

    def decorator(callback):
        self._error_handlers[status] = callback
        return callback
    return decorator

Decorator to add a callback that generates error page.

The *status* parameter specifies the HTTP response status code
for which the decorated callback should be invoked. If the
*status* argument is not specified, then the decorated callable
is considered to be a fallback callback.

A fallback callback, when defined, is invoked to generate the
error page for any HTTP response representing an error when
there is no error handler defined explicitly for the response
code of the HTTP response.

Arguments:
status(int, optional): HTTP response status code.

Returns:
function: Decorator function to add error handler.


def get_decomposition(self, comp):
    (facet, simplex) = self._get_facet_and_simplex(comp)
    decomp_amts = simplex.bary_coords(self.pd_coords(comp))
    return {self.qhull_entries[f]: amt for (f, amt) in zip(facet, decomp_amts) if (abs(amt) > PhaseDiagram.numerical_tol)}

Provides the decomposition at a particular composition.

Args:
comp: A composition

Returns:
Decomposition as a dict of {Entry: amount}


def tags():
    return shell.run('git tag --sort=v:refname', capture=True, never_pretend=True).stdout.strip().splitlines()

Returns all tags in the repo.

Returns:
list[str]: List of all tags in the repo, sorted as versions.

All tags returned by this function will be parsed as if the contained
versions (using ``v:refname`` sorting).


def constant(x: A) -> Callable[..., A]:

    def constanted(*args, **kwargs):
        return x

    return constanted

Produce a function that always returns a supplied value.

Args:
x: Any object.

Returns:
A function that accepts any number of positional and keyword arguments, discards them, and returns ``x``.


def getctime(self, path=None, client_kwargs=None, header=None):

        return self._getctime_from_header(
            self.head(path, client_kwargs, header))

Return the creation time of path.

Args:
path (str): File path or URL.
client_kwargs (dict): Client arguments.
header (dict): Object header.

Returns:
float: The number of seconds since the epoch
(see the time module).


def check_alive(self, worker_name):
    if self._context_handle:
        return pywrap_tfe.TFE_ContextCheckAlive(self._context_handle, worker_name)
    else:
        raise ValueError('Context is not initialized.')

Checks whether a remote worker is alive or not.

Args:
worker_name: a string representing the remote worker. It must be a fully
specified name like "/job:worker/replica:0/task:0".

Returns:
a boolean indicating whether the remote worker is alive or not.

Raises:
ValueError: if context is not initialized.


def document(self, document_id=None):

        if document_id is None:
            document_id = _auto_id()

        child_path = self._path + (document_id,)
        return self._client.document(*child_path)

Create a sub-document underneath the current collection.

Args:
document_id (Optional[str]): The document identifier
within the current collection. If not provided, will default
to a random 20 character string composed of digits,
uppercase and lowercase and letters.

Returns:
~.firestore_v1beta1.document.DocumentReference: The child
document.


def load(self, raw):

        try:
            self._load(raw)
        except (KeyError, ValueError) as e:
            raise_from(exception.ParseException('Parse error in %s' % (type(self)), raw), e)

Unserialize from raw representation. (Wrapper)

Args:
raw (dict): Raw.
Raises:
ParseException: If there was an error parsing data.


def _GetCompressedStreamTypes(self, mediator, path_spec):

    try:
      type_indicators = analyzer.Analyzer.GetCompressedStreamTypeIndicators(
          path_spec, resolver_context=mediator.resolver_context)
    except IOError as exception:
      type_indicators = []

      warning_message = (
          'analyzer failed to determine compressed stream type indicators '
          'with error: {0!s}').format(exception)
      mediator.ProduceExtractionWarning(warning_message, path_spec=path_spec)

    return type_indicators

Determines if a data stream contains a compressed stream such as: gzip.

Args:
mediator (ParserMediator): mediates the interactions between
parsers and other components, such as storage and abort signals.
path_spec (dfvfs.PathSpec): path specification of the data stream.

Returns:
list[str]: dfVFS compressed stream type indicators found in
the data stream.


def __setitem__(self, key, layout):
    if key in self._layout_map:
        raise ValueError(f'{key} already exist in the LayoutMap with value {self._layout_map[key]}. Please make sure to not use duplicated keys.')
    if isinstance(layout, tuple):
        layout = TensorLayout(axes=layout, device_mesh=None)
    if not isinstance(layout, TensorLayout):
        raise ValueError(f'{layout} should be a TensorLayout type, got {type(layout)}')
    self._maybe_populate_device_mesh(layout)
    self._layout_map[key] = layout

Insert TensorLayout to the LayoutMap.

Args:
key: String key for the `TensorLayout`.
layout: The `TensorLayout`. As a shortcut, tuple of string and None
are also acceptable, and will be converted to `TensorLayout`.


def number_of_decimals(num):
    r
    exp = decimal.Decimal(str(num)).as_tuple().exponent
    return max(0, -exp)

r"""
Args:
num (float):

References:
stackoverflow.com/questions/6189956/finding-decimal-places

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_alg import *  # NOQA
>>> num = 15.05
>>> result = number_of_decimals(num)
>>> print(result)
2


def sg_symbol_from_int_number(int_number, hexagonal=True):
    syms = []
    for (n, v) in get_symm_data('space_group_encoding').items():
        if (v['int_number'] == int_number):
            syms.append(n)
    if (len(syms) == 0):
        raise ValueError('Invalid international number!')
    if (len(syms) == 2):
        if hexagonal:
            syms = list(filter((lambda s: s.endswith('H')), syms))
        else:
            syms = list(filter((lambda s: (not s.endswith('H'))), syms))
    return syms.pop()

Obtains a SpaceGroup name from its international number.

Args:
int_number (int): International number.
hexagonal (bool): For rhombohedral groups, whether to return the
hexagonal setting (default) or rhombohedral setting.

Returns:
(str) Spacegroup symbol


def time2timestr(time, fmt='hhmmss'):

    if fmt.count(':') == 2:
        if not fmt.index('h') < fmt.index('m') < fmt.index('s'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m, s = fmt.split(':')
    elif fmt.count(':') == 1:
        if not fmt.index('h') < fmt.index('m'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m = fmt.split(':')
        s = None
    elif any(c not in 'hms' for c in fmt) or len(fmt) != 6:
        raise ValueError('Invalid character in format string. {}'.format(
                VALID_TIME_FORMATS_TEXT))
    else:
        if not fmt.index('h') < fmt.index('m') < fmt.index('s'):
            raise ValueError('Invalid format string. {}'.format(
                    VALID_TIME_FORMATS_TEXT))
        h, m, s = fmt[:-4], fmt[-4:-2], fmt[-2:]
    for string, char in ((h, 'h'), (m, 'm'), (s, 's')):
        if string is not None and any(c != char for c in string):
            raise ValueError('Invalid date format: {} is not {}'.\
                    format(char, string))
    if len(h) == 2:
        fmt = fmt.replace('hh', '%H', 1)
    elif len(h) == 1:
        fmt = fmt.replace('h', 'X%H', 1)
    else:
        raise ValueError('Invalid format string, hour must have 1 or 2 digits')
    if len(m) == 2:
        fmt = fmt.replace('mm', '%M', 1)
    else:
        raise ValueError('Invalid format string, minutes must have 2 digits')
    if s is not None and len(s) == 2:
        fmt = fmt. replace('ss', '%S', 1)
    elif s is not None:
        raise ValueError('Invalid format string, seconds must have 2 digits')
    return time.strftime(fmt).replace('X0','X').replace('X','')

Turns a datetime.time object into a string. The string must have one of the
formats from VALID_TIME_FORMATS_TEXT to make it compatible with
timestr2time.

Args:
time (datetime.time) the time to be translated
fmt (str) a format string.
Returns:
(str) that represents a time.
Raises:
ValueError if the format is not valid.


def wait_for(self, timeout=None, **kwargs):
    if (len(kwargs) == 0):
        raise ArgumentError('You must specify at least one message field to wait on')
    spec = MessageSpec(**kwargs)
    future = self._add_waiter(spec)
    future.add_done_callback((lambda x: self._remove_waiter(spec, future)))
    return asyncio.wait_for(future, timeout=timeout)

Wait for a specific matching message or timeout.

You specify the message by passing name=value keyword arguments to
this method.  The first message received after this function has been
called that has all of the given keys with the given values will be
returned when this function is awaited.

If no matching message is received within the specified timeout (if
given), then asyncio.TimeoutError will be raised.

This function only matches a single message and removes itself once
the message is seen or the timeout expires.

Args:
timeout (float): Optional timeout, defaults to None for no timeout.
**kwargs: Keys to match in the message with their corresponding values.
You must pass at least one keyword argument so there is something
to look for.

Returns:
awaitable: The response


def update_batch(self, loss_per_instance):
    if (self.batch_indices is None):
        raise TensorForceError('Need to call get_batch before each update_batch call.')
    for (index, loss) in zip(self.batch_indices, loss_per_instance):
        new_priority = ((np.abs(loss) + self.prioritization_constant) ** self.prioritization_weight)
        self.observations._move(index, new_priority)
        self.none_priority_index += 1

Computes priorities according to loss.

Args:
loss_per_instance:


def rename_keys(d: Dict[str, Any], mapping: Dict[str, str]) -> Dict[str, Any]:

    result = {}
    for k, v in d.items():
        if k in mapping:
            k = mapping[k]
        result[k] = v
    return result

Returns a copy of the dictionary ``d`` with its keys renamed according to
``mapping``.

Args:
d: the starting dictionary
mapping: a dictionary of the format ``{old_key_name: new_key_name}``

Returns:
a new dictionary

Keys that are not in ``mapping`` are left unchanged.
The input parameters are not modified.


def copy_submission_locally(self, cloud_path):
    local_path = os.path.join(self.download_dir, os.path.basename(cloud_path))
    cmd = ['gsutil', 'cp', cloud_path, local_path]
    if (subprocess.call(cmd) != 0):
        logging.error("Can't copy submission locally")
        return None
    return local_path

Copies submission from Google Cloud Storage to local directory.

Args:
cloud_path: path of the submission in Google Cloud Storage

Returns:
name of the local file where submission is copied to


def ExtractEventsFromSources(self):
    self._CheckStorageFile(self._storage_file_path, warn_about_existing=True)
    scan_context = self.ScanSource(self._source_path)
    self._source_type = scan_context.source_type
    self._status_view.SetMode(self._status_view_mode)
    self._status_view.SetSourceInformation(self._source_path, self._source_type, artifact_filters=self._artifact_filters, filter_file=self._filter_file)
    status_update_callback = self._status_view.GetExtractionStatusUpdateCallback()
    self._output_writer.Write('\n')
    self._status_view.PrintExtractionStatusHeader(None)
    self._output_writer.Write('Processing started.\n')
    session = engine.BaseEngine.CreateSession(artifact_filter_names=self._artifact_filters, command_line_arguments=self._command_line_arguments, debug_mode=self._debug_mode, filter_file_path=self._filter_file, preferred_encoding=self.preferred_encoding, preferred_time_zone=self._preferred_time_zone, preferred_year=self._preferred_year)
    storage_writer = storage_factory.StorageFactory.CreateStorageWriter(self._storage_format, session, self._storage_file_path)
    if (not storage_writer):
        raise errors.BadConfigOption('Unsupported storage format: {0:s}'.format(self._storage_format))
    single_process_mode = self._single_process_mode
    if (self._source_type == dfvfs_definitions.SOURCE_TYPE_FILE):
        single_process_mode = True
    if single_process_mode:
        extraction_engine = single_process_engine.SingleProcessEngine()
    else:
        extraction_engine = multi_process_engine.TaskMultiProcessEngine(use_zeromq=self._use_zeromq)
    if (self._source_type in self._SOURCE_TYPES_TO_PREPROCESS):
        self._PreprocessSources(extraction_engine)
    configuration = self._CreateProcessingConfiguration(extraction_engine.knowledge_base)
    self._SetExtractionParsersAndPlugins(configuration, session)
    self._SetExtractionPreferredTimeZone(extraction_engine.knowledge_base)
    try:
        filter_find_specs = extraction_engine.BuildFilterFindSpecs(self._artifact_definitions_path, self._custom_artifacts_path, extraction_engine.knowledge_base, self._artifact_filters, self._filter_file)
    except errors.InvalidFilter as exception:
        raise errors.BadConfigOption('Unable to build filter specification: {0!s}'.format(exception))
    processing_status = None
    if single_process_mode:
        logger.debug('Starting extraction in single process mode.')
        processing_status = extraction_engine.ProcessSources(self._source_path_specs, storage_writer, self._resolver_context, configuration, filter_find_specs=filter_find_specs, status_update_callback=status_update_callback)
    else:
        logger.debug('Starting extraction in multi process mode.')
        processing_status = extraction_engine.ProcessSources(session.identifier, self._source_path_specs, storage_writer, configuration, enable_sigsegv_handler=self._enable_sigsegv_handler, filter_find_specs=filter_find_specs, number_of_worker_processes=self._number_of_extraction_workers, status_update_callback=status_update_callback, worker_memory_limit=self._worker_memory_limit)
    self._status_view.PrintExtractionSummary(processing_status)

Processes the sources and extracts events.

Raises:
BadConfigOption: if the storage file path is invalid or the storage
format not supported or an invalid filter was specified.
SourceScannerError: if the source scanner could not find a supported
file system.
UserAbort: if the user initiated an abort.


def call(self, inputs, state):
    _check_rnn_cell_input_dtypes([inputs, state])
    num_proj = self._num_units if self._num_proj is None else self._num_proj
    sigmoid = math_ops.sigmoid
    if self._state_is_tuple:
        c_prev, m_prev = state
    else:
        c_prev = array_ops.slice(state, [0, 0], [-1, self._num_units])
        m_prev = array_ops.slice(state, [0, self._num_units], [-1, num_proj])
    input_size = inputs.get_shape().with_rank(2).dims[1].value
    if input_size is None:
        raise ValueError('Could not infer input size from inputs.get_shape()[-1]')
    lstm_matrix = math_ops.matmul(array_ops.concat([inputs, m_prev], 1), self._kernel)
    lstm_matrix = nn_ops.bias_add(lstm_matrix, self._bias)
    i, j, f, o = array_ops.split(value=lstm_matrix, num_or_size_splits=4, axis=1)
    if self._use_peepholes:
        c = sigmoid(f + self._forget_bias + self._w_f_diag * c_prev) * c_prev + sigmoid(i + self._w_i_diag * c_prev) * self._activation(j)
    else:
        c = sigmoid(f + self._forget_bias) * c_prev + sigmoid(i) * self._activation(j)
    if self._cell_clip is not None:
        c = clip_ops.clip_by_value(c, -self._cell_clip, self._cell_clip)
    if self._use_peepholes:
        m = sigmoid(o + self._w_o_diag * c) * self._activation(c)
    else:
        m = sigmoid(o) * self._activation(c)
    if self._num_proj is not None:
        m = math_ops.matmul(m, self._proj_kernel)
        if self._proj_clip is not None:
            m = clip_ops.clip_by_value(m, -self._proj_clip, self._proj_clip)
    new_state = LSTMStateTuple(c, m) if self._state_is_tuple else array_ops.concat([c, m], 1)
    return (m, new_state)

Run one step of LSTM.

Args:
inputs: input Tensor, must be 2-D, `[batch, input_size]`.
state: if `state_is_tuple` is False, this must be a state Tensor, `2-D,
[batch, state_size]`.  If `state_is_tuple` is True, this must be a tuple
of state Tensors, both `2-D`, with column sizes `c_state` and `m_state`.

Returns:
A tuple containing:

- A `2-D, [batch, output_dim]`, Tensor representing the output of the
LSTM after reading `inputs` when previous state was `state`.
Here output_dim is:
num_proj if num_proj was set,
num_units otherwise.
- Tensor(s) representing the new state of LSTM after reading `inputs` when
the previous state was `state`.  Same type and shape(s) as `state`.

Raises:
ValueError: If input size cannot be inferred from inputs via
static shape inference.


def _bdtr(k, n, p):

  ones = tf.ones_like(n - k)
  k_eq_n = tf.equal(k, n)
  safe_dn = tf.where(k_eq_n, ones, n - k)
  dk = tf.math.betainc(a=safe_dn, b=k + 1, x=1 - p)
  return tf.where(k_eq_n, ones, dk)

The binomial cumulative distribution function.

Args:
k: floating point `Tensor`.
n: floating point `Tensor`.
p: floating point `Tensor`.

Returns:
`sum_{j=0}^k p^j (1 - p)^(n - j)`.