def _ConvertFieldValuePair(js, message):
    names = []
    message_descriptor = message.DESCRIPTOR
    for name in js:
        try:
            field = message_descriptor.fields_by_camelcase_name.get(name, None)
            if (not field):
                raise ParseError('Message type "{0}" has no field named "{1}".'.format(message_descriptor.full_name, name))
            if (name in names):
                raise ParseError('Message type "{0}" should not have multiple "{1}" fields.'.format(message.DESCRIPTOR.full_name, name))
            names.append(name)
            if (field.containing_oneof is not None):
                oneof_name = field.containing_oneof.name
                if (oneof_name in names):
                    raise ParseError('Message type "{0}" should not have multiple "{1}" oneof fields.'.format(message.DESCRIPTOR.full_name, oneof_name))
                names.append(oneof_name)
            value = js[name]
            if (value is None):
                message.ClearField(field.name)
                continue
            if _IsMapEntry(field):
                message.ClearField(field.name)
                _ConvertMapFieldValue(value, message, field)
            elif (field.label == descriptor.FieldDescriptor.LABEL_REPEATED):
                message.ClearField(field.name)
                if (not isinstance(value, list)):
                    raise ParseError('repeated field {0} must be in [] which is {1}.'.format(name, value))
                if (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE):
                    for item in value:
                        sub_message = getattr(message, field.name).add()
                        if ((item is None) and (sub_message.DESCRIPTOR.full_name != 'google.protobuf.Value')):
                            raise ParseError('null is not allowed to be used as an element in a repeated field.')
                        _ConvertMessage(item, sub_message)
                else:
                    for item in value:
                        if (item is None):
                            raise ParseError('null is not allowed to be used as an element in a repeated field.')
                        getattr(message, field.name).append(_ConvertScalarFieldValue(item, field))
            elif (field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE):
                sub_message = getattr(message, field.name)
                _ConvertMessage(value, sub_message)
            else:
                setattr(message, field.name, _ConvertScalarFieldValue(value, field))
        except ParseError as e:
            if (field and (field.containing_oneof is None)):
                raise ParseError('Failed to parse {0} field: {1}'.format(name, e))
            else:
                raise ParseError(str(e))
        except ValueError as e:
            raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
        except TypeError as e:
            raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))

Convert field value pairs into regular message.

Args:
js: A JSON object to convert the field value pairs.
message: A regular protocol message to record the data.

Raises:
ParseError: In case of problems converting.


def calculate_energy(self, energies):
    return sum([(amt * energies[c]) for (amt, c) in zip(self._coeffs, self._all_comp)])

Calculates the energy of the reaction.

Args:
energies ({Composition: float}): Energy for each composition.
E.g ., {comp1: energy1, comp2: energy2}.

Returns:
reaction energy as a float.


def _compute_theoretical_jacobian(f, y_shape, y_dtype, xs, param):
    x = xs[param]
    x_shape = tuple(x.shape) + (2,) if x.dtype.is_complex else x.shape
    y_factor = 2 if y_dtype.is_complex else 1
    x_size = _product(x_shape)
    x_val_size = _product(x_shape[1:])
    y_size = _product(y_shape) * y_factor
    jacobian = np.zeros((y_size, x_size), dtype=x.dtype.real_dtype.as_numpy_dtype)
    dy_data = np.zeros(y_shape, dtype=y_dtype.as_numpy_dtype)
    dy_data_flat = dy_data.ravel().view(y_dtype.real_dtype.as_numpy_dtype)
    grad_fn_unprep = backprop.gradients_function(f, [param])
    grad_fn = _prepare(lambda dy, *xs: grad_fn_unprep(*xs, dy=dy), [y_dtype] + [z.dtype for z in xs], [None] + [z.shape for z in xs])
    for row in range(y_size):
        dy_data_flat[row] = 1
        grad = _to_numpy(grad_fn(dy_data, *xs)[0])
        grad = _eval_indexed_slices(grad)
        if isinstance(grad, indexed_slices.IndexedSlicesValue):
            for i, v in zip(grad.indices, grad.values):
                c_begin = i * x_val_size
                c_end = c_begin + x_val_size
                jacobian[row, c_begin:c_end] += v.flat
        elif grad is not None:
            jacobian[row, :] = grad.ravel().view(jacobian.dtype)
        dy_data_flat[row] = 0
    if y_size == 0:
        grad = _to_numpy(grad_fn(dy_data, *xs)[0])
        if grad.shape != x.shape:
            raise ValueError('Empty gradient has wrong shape: expected %s, got %s' % (x.shape, grad.shape))
        if np.any(grad):
            raise ValueError('Empty tensor with nonzero gradients')
    logging.vlog(1, 'Theoretical Jacobian =\n%s', jacobian)
    return jacobian

Computes the theoretical Jacobian for f regarding xs[param].

One can think of the relation among f, xs and y as y = f(xs).

Args:
f: the function.
y_shape: the shape of the result.
y_dtype: the dtype of the result.
xs: a list of tensors.
param: the index of the target parameter.

Returns:
A 2-d numpy array representing the Jacobian. It has "y_size" rows
and "x_size" columns where "x_size" is the number of elements in xs[param]
and "y_size" is the number of elements in the result.

Raises:
ValueError: If result is empty but the gradient is nonzero.


def CallDhclient(
    interfaces, logger, dhclient_script=None):

  logger.info('Enabling the Ethernet interfaces %s.', interfaces)

  dhclient_command = ['dhclient']

  if dhclient_script and os.path.exists(dhclient_script):
    dhclient_command += ['-sf', dhclient_script]

  try:
    subprocess.check_call(dhclient_command + ['-x'] + interfaces)
    subprocess.check_call(dhclient_command + interfaces)
  except subprocess.CalledProcessError:
    logger.warning('Could not enable interfaces %s.', interfaces)

Configure the network interfaces using dhclient.

Args:
interfaces: list of string, the output device names to enable.
logger: logger object, used to write to SysLog and serial port.
dhclient_script: string, the path to a dhclient script used by dhclient.


def expand_path_cfg(path_cfg, alias_dict={}, overriding_kargs={}):
    if isinstance(path_cfg, str):
        return _expand_str(path_cfg, alias_dict, overriding_kargs)
    if isinstance(path_cfg, dict):
        return _expand_dict(path_cfg, alias_dict)
    return _expand_tuple(path_cfg, alias_dict, overriding_kargs)

expand a path config

Args:
path_cfg (str, tuple, dict): a config for path
alias_dict (dict): a dict for aliases
overriding_kargs (dict): to be used for recursive call


def __init__(self, **kwargs):

        if isinstance(kwargs.get("learning_rate"), float) and isinstance(kwargs.get("learning_rate_decay"), float):
            KerasModel.__init__(self, **kwargs)
        else:
            KerasModel.__init__(self, **kwargs)
            LRScheduledModel.__init__(self, **kwargs)

Initialize model with given parameters

Args:
**kwargs: dictionary of parameters


def groups_createChild(self, *, channel: str, **kwargs) -> SlackResponse:

        self._validate_xoxp_token()
        kwargs.update({"channel": channel})
        return self.api_call("groups.createChild", http_verb="GET", params=kwargs)

Clones and archives a private channel.

Args:
channel (str): The group id. e.g. 'G1234567890'


def get_gpus(num_gpu=1, worker_index=(- 1)):
    list_gpus = subprocess.check_output(['nvidia-smi', '--list-gpus']).decode()
    logging.debug('all GPUs:\n{0}'.format(list_gpus))
    gpus = [x for x in list_gpus.split('\n') if (len(x) > 0)]

    def parse_gpu(gpu_str):
        cols = gpu_str.split(' ')
        return (cols[5].split(')')[0], cols[1].split(':')[0])
    gpu_list = [parse_gpu(gpu) for gpu in gpus]
    free_gpus = []
    retries = 0
    while ((len(free_gpus) < num_gpu) and (retries < MAX_RETRIES)):
        smi_output = subprocess.check_output(['nvidia-smi', '--format=csv,noheader,nounits', '--query-compute-apps=gpu_uuid']).decode()
        logging.debug('busy GPUs:\n{0}'.format(smi_output))
        busy_uuids = [x for x in smi_output.split('\n') if (len(x) > 0)]
        for (uuid, index) in gpu_list:
            if (uuid not in busy_uuids):
                free_gpus.append(index)
        if (len(free_gpus) < num_gpu):
            logging.warn('Unable to find available GPUs: requested={0}, available={1}'.format(num_gpu, len(free_gpus)))
            retries += 1
            time.sleep((30 * retries))
            free_gpus = []
    logging.info('Available GPUs: {}'.format(free_gpus))
    if (len(free_gpus) < num_gpu):
        smi_output = subprocess.check_output(['nvidia-smi', '--format=csv', '--query-compute-apps=gpu_uuid,pid,process_name,used_gpu_memory']).decode()
        logging.info(': {0}'.format(smi_output))
        raise Exception('Unable to find {} free GPU(s)\n{}'.format(num_gpu, smi_output))
    num_available = len(free_gpus)
    if (worker_index == (- 1)):
        random.shuffle(free_gpus)
        proposed_gpus = free_gpus[:num_gpu]
    else:
        if (((worker_index * num_gpu) + num_gpu) > num_available):
            worker_index = ((worker_index * num_gpu) % num_available)
        proposed_gpus = free_gpus[(worker_index * num_gpu):((worker_index * num_gpu) + num_gpu)]
    logging.info('Proposed GPUs: {}'.format(proposed_gpus))
    return ','.join((str(x) for x in proposed_gpus))

Get list of free GPUs according to nvidia-smi.

This will retry for ``MAX_RETRIES`` times until the requested number of GPUs are available.

Args:
:num_gpu: number of GPUs desired.
:worker_index: index "hint" for allocation of available GPUs.

Returns:
Comma-delimited string of GPU ids, or raises an Exception if the requested number of GPUs could not be found.


def module_selected(self, module_name, module_ui):
    if (self.current_button == self.module_buttons[module_name]):
        return
    self.module_buttons[module_name].config(bg='
    if (self.current_button is not None):
        self.current_button.config(bg='white')
    self.current_button = self.module_buttons[module_name]
    self.clear_ui()
    try:
        module_ui_frame = ModuleUIBaseFrame(self.module_ui, module_name, module_ui)
        module_ui_frame.grid(column=0, row=0, sticky='W E N S')
    except Exception as e:
        logger.error('Could not load UI for {}'.format(module_name))
        logger.exception(e)
        tk.Label(self.module_ui, text='Could not load UI for {}'.format(module_name)).grid(column=0, row=0, padx=0, pady=0, sticky='W E N S')

Called when a module is selected

Args:
module_name (str): The name of the module
module_ui: The function to call to create the module's UI


def prune(self, regex=r".*"):

        return filetree(self.root, ignore=self.ignore, regex=regex)

Prune leaves of filetree according to specified
regular expression.

Args:
regex (str): Regular expression to use in pruning tree.


def initialize_all_tables(name='init_all_tables'):
    return tables_initializer(name)

Returns an Op that initializes all tables of the default graph.

Args:
name: Optional name for the initialization op.

Returns:
An Op that initializes all tables.  Note that if there are
not tables the returned Op is a NoOp.


def call(self, hidden_states: tf.Tensor, attention_mask: tf.Tensor, layer_head_mask: tf.Tensor, training: bool=False):
    residual = hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    hidden_states, self_attn_weights, _ = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, layer_head_mask=layer_head_mask, training=training)
    tf.debugging.assert_equal(shape_list(hidden_states), shape_list(residual), message=f'Self attn modified the shape of query {shape_list(residual)} to {shape_list(hidden_states)}')
    hidden_states = self.dropout(hidden_states, training=training)
    hidden_states = residual + hidden_states
    residual = hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = self.activation_dropout(hidden_states, training=training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = self.dropout(hidden_states, training=training)
    hidden_states = residual + hidden_states
    return (hidden_states, self_attn_weights)

Args:
hidden_states (`tf.Tensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
attention_mask (`tf.Tensor`): attention mask of size
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
layer_head_mask (`tf.Tensor`): mask for attention heads in a given layer of size
`(encoder_attention_heads,)`


def from_versions(cls, versions):
    range = cls(None)
    range.bounds = []
    for version in dedup(sorted(versions)):
        lower = _LowerBound(version, True)
        upper = _UpperBound(version, True)
        bound = _Bound(lower, upper)
        range.bounds.append(bound)
    return range

Create a range from a list of versions.

This method creates a range that contains only the given versions and
no other. Typically the range looks like (for eg) "==3|==4|==5.1".

Args:
versions: List of Version objects.

Returns:
`VersionRange` object.


def sum2diag(A, D, out=None):
    A = asarray(A, float)
    D = asarray(D, float)
    if (out is None):
        out = copy(A)
    else:
        copyto(out, A)
    einsum('ii->i', out)[:] += D
    return out

r"""Add values ``D`` to the diagonal of matrix ``A``.

Args:
A (array_like): Left-hand side.
D (array_like or float): Values to add.
out (:class:`numpy.ndarray`, optional): copy result to.

Returns:
:class:`numpy.ndarray`: Resulting matrix.


def log_softmax(x, axis=-1):
    if any_symbolic_tensors((x,)):
        return LogSoftmax(axis).symbolic_call(x)
    if isinstance(axis, tuple):
        axis_to_keep = [v for v in range(len(x.shape)) if v not in axis]
        x_transposed = backend.numpy.transpose(x, axes=(*axis_to_keep, *axis))
        x_reshaped = backend.numpy.reshape(x_transposed, (*[x.shape[v] for v in axis_to_keep], -1))
        x = backend.nn.log_softmax(x_reshaped, axis=-1)
        x = backend.numpy.reshape(x, x_transposed.shape)
        x = backend.numpy.transpose(x, axes=list(backend.numpy.argsort([*axis_to_keep, *axis])))
        return x
    else:
        return backend.nn.log_softmax(x, axis=axis)

Log-softmax activation function.

It is defined as:
`f(x) = x - max(x) - log(sum(exp(x - max(x))))`

Args:
x: Input tensor.
axis: Integer, axis along which the log-softmax is applied.
Defaults to `-1`.

Returns:
A tensor with the same shape as `x`.

Example:

>>> x = np.array([-1., 0., 1.])
>>> x_log_softmax = keras.ops.log_softmax(x)
>>> print(x_log_softmax)
array([-2.40760596, -1.40760596, -0.40760596], shape=(3,), dtype=float64)


def add_state_sensors(self, agent_name, sensors):

        if isinstance(sensors, list):
            for sensor in sensors:
                self.add_state_sensors(agent_name, sensor)
        else:
            if agent_name not in self._sensor_map:
                self._sensor_map[agent_name] = dict()

            self._sensor_map[agent_name][sensors] = self._client.malloc(agent_name + "_" + Sensors.name(sensors),
                                                                        Sensors.shape(sensors),
                                                                        Sensors.dtype(sensors))

Adds a sensor to a particular agent. This only works if the world you are running also includes
that particular sensor on the agent.

Args:
agent_name (str): The name of the agent to add the sensor to.
sensors (:obj:`HolodeckSensor` or list of :obj:`HolodeckSensor`): Sensors to add to the agent.
Should be objects that inherit from :obj:`HolodeckSensor`.


def _parse_description(self, config):
    value = DESCRIPTION_RE.search(config).group('value')
    return dict(description=value)

_parse_description scans the provided configuration block and
extracts the vrf description value. The return dict is intended to
be merged into the response dict.

Args:
config (str): The vrf configuration block from the nodes
running configuration

Returns:
dict: resource dict attribute


def print_run_bidirectional_blast(reference, other_genome, dbtype, outdir):
    if (dbtype == 'nucl'):
        command = 'blastn'
    elif (dbtype == 'prot'):
        command = 'blastp'
    else:
        raise ValueError('dbtype must be "nucl" or "prot"')
    (r_folder, r_name, r_ext) = utils.split_folder_and_path(reference)
    (g_folder, g_name, g_ext) = utils.split_folder_and_path(other_genome)
    r_vs_g_name = ((r_name + '_vs_') + g_name)
    r_vs_g = (r_vs_g_name + '_blast.out')
    if (op.exists(op.join(outdir, r_vs_g)) and (os.stat(op.join(outdir, r_vs_g)).st_size != 0)):
        log.debug('{} vs {} BLAST already run'.format(r_name, g_name))
    else:
        cmd = '{} -query {} -db {} -outfmt 6 -out {}'.format(command, reference, g_name, r_vs_g)
        utils.write_torque_script(command=cmd, err=r_vs_g_name, out=r_vs_g_name, name=r_vs_g_name, outfile=(op.join(outdir, r_vs_g_name) + '.sh'), walltime='00:15:00', queue='regular')
    g_vs_r_name = ((g_name + '_vs_') + r_name)
    g_vs_r = (g_vs_r_name + '_blast.out')
    if (op.exists(op.join(outdir, g_vs_r)) and (os.stat(op.join(outdir, g_vs_r)).st_size != 0)):
        log.debug('{} vs {} BLAST already run'.format(g_name, r_name))
    else:
        cmd = '{} -query {} -db {} -outfmt 6 -out {}'.format(command, other_genome, r_name, g_vs_r)
        utils.write_torque_script(command=cmd, err=g_vs_r_name, out=g_vs_r_name, name=g_vs_r_name, outfile=(op.join(outdir, g_vs_r_name) + '.sh'), walltime='00:15:00', queue='regular')

Write torque submission files for running bidirectional blast on a server and print execution command.

Args:
reference (str): Path to "reference" genome, aka your "base strain"
other_genome (str): Path to other genome which will be BLASTed to the reference
dbtype (str): "nucl" or "prot" - what format your genome files are in
outdir (str): Path to folder where Torque scripts should be placed


def log(self, msg):

        time = self.get_time()

        msg = "{:s}\t {:s}".format(time, msg)

        self.history.append(msg)
        self.history_model.insertRow(0, QtGui.QStandardItem(msg))

log function
Args:
msg: the text message to be logged


def new_space(self, name=None, bases=None, formula=None, *, refs=None, source=None, is_derived=False, prefix=''):
    from modelx.core.space import StaticSpaceImpl
    if (name is None):
        name = self.spacenamer.get_next(self.namespace, prefix)
    if (name in self.namespace):
        raise ValueError(("Name '%s' already exists." % name))
    if ((not prefix) and (not is_valid_name(name))):
        raise ValueError(("Invalid name '%s'." % name))
    space = self._new_space(name=name, formula=formula, refs=refs, source=source, is_derived=is_derived)
    self._set_space(space)
    self.model.spacegraph.add_space(space)
    if (bases is not None):
        if isinstance(bases, StaticSpaceImpl):
            bases = [bases]
        space.add_bases(bases)
    return space

Create a new child space.

Args:
name (str): Name of the space. If omitted, the space is
created automatically.
bases: If specified, the new space becomes a derived space of
the `base` space.
formula: Function whose parameters used to set space parameters.
refs: a mapping of refs to be added.
arguments: ordered dict of space parameter names to their values.
source: A source module from which cell definitions are read.
prefix: Prefix to the autogenerated name when name is None.


def get_naive(dt):

  if not dt.tzinfo:
    return dt
  if hasattr(dt, "asdatetime"):
    return dt.asdatetime()
  return dt.replace(tzinfo=None)

Gets a naive datetime from a datetime.

datetime_tz objects can't just have tzinfo replaced with None, you need to
call asdatetime.

Args:
dt: datetime object.

Returns:
datetime object without any timezone information.


def create_cosmosdb_account(access_token, subscription_id, rgname, account_name, location, cosmosdb_kind):
    endpoint = ''.join([get_rm_endpoint(), '/subscriptions/', subscription_id, '/resourcegroups/', rgname, '/providers/Microsoft.DocumentDB/databaseAccounts/', account_name, '?api-version=', COSMOSDB_API])
    cosmosdb_body = {'location': location, 'kind': cosmosdb_kind, 'properties': {'databaseAccountOfferType': 'Standard', 'locations': [{'failoverPriority': 0, 'locationName': location}]}}
    body = json.dumps(cosmosdb_body)
    return do_put(endpoint, body, access_token)

Create a new Cosmos DB account in the named resource group, with the named location.

Args:
access_token (str): A valid Azure authentication token.
subscription_id (str): Azure subscription id.
rgname (str): Azure resource group name.
account_name (str): Name of the new Cosmos DB account.
location (str): Azure data center location. E.g. westus.
cosmosdb_kind (str): Database type. E.g. GlobalDocumentDB.

Returns:
HTTP response. JSON body of storage account properties.


def get_attribute_id(self, attribute_key):
    attribute = self.attribute_key_map.get(attribute_key)
    has_reserved_prefix = attribute_key.startswith(RESERVED_ATTRIBUTE_PREFIX)
    if attribute:
        if has_reserved_prefix:
            self.logger.warning(('Attribute %s unexpectedly has reserved prefix %s; using attribute ID instead of reserved attribute name.' % (attribute_key, RESERVED_ATTRIBUTE_PREFIX)))
        return attribute.id
    if has_reserved_prefix:
        return attribute_key
    self.logger.error(('Attribute "%s" is not in datafile.' % attribute_key))
    self.error_handler.handle_error(exceptions.InvalidAttributeException(enums.Errors.INVALID_ATTRIBUTE_ERROR))
    return None

Get attribute ID for the provided attribute key.

Args:
attribute_key: Attribute key for which attribute is to be fetched.

Returns:
Attribute ID corresponding to the provided attribute key.


def generate_example(config, ext='json'):

    template_name = 'example.{0}'.format(ext.lower())
    template = ENV.get_template(template_name)
    return template.render(config=config)

Generate an example file based on the given Configuration object.

Args:
config (confpy.core.configuration.Configuration): The configuration
object on which to base the example.
ext (str): The file extension to render. Choices: JSON and INI.

Returns:
str: The text of the example file.


def maybe_reduce(nodes):
    (_, num_nodes) = nodes.shape
    if (num_nodes < 2):
        return (False, nodes)
    elif (num_nodes == 2):
        projection = _PROJECTION0
        denom = _PROJ_DENOM0
    elif (num_nodes == 3):
        projection = _PROJECTION1
        denom = _PROJ_DENOM1
    elif (num_nodes == 4):
        projection = _PROJECTION2
        denom = _PROJ_DENOM2
    elif (num_nodes == 5):
        projection = _PROJECTION3
        denom = _PROJ_DENOM3
    else:
        raise _helpers.UnsupportedDegree((num_nodes - 1), supported=(0, 1, 2, 3, 4))
    projected = (_helpers.matrix_product(nodes, projection) / denom)
    relative_err = projection_error(nodes, projected)
    if (relative_err < _REDUCE_THRESHOLD):
        return (True, reduce_pseudo_inverse(nodes))
    else:
        return (False, nodes)

r"""Reduce nodes in a curve if they are degree-elevated.

.. note::

This is a helper for :func:`_full_reduce`. Hence there is no
corresponding Fortran speedup.

We check if the nodes are degree-elevated by projecting onto the
space of degree-elevated curves of the same degree, then comparing
to the projection. We form the projection by taking the corresponding
(right) elevation matrix :math:`E` (from one degree lower) and forming
:math:`E^T \left(E E^T\right)^{-1} E`.

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

Returns:
Tuple[bool, numpy.ndarray]: Pair of values. The first indicates
if the ``nodes`` were reduced. The second is the resulting nodes,
either the reduced ones or the original passed in.

Raises:
.UnsupportedDegree: If the curve is degree 5 or higher.


def get_course_completions(self, enterprise_customer, days):

        return PersistentCourseGrade.objects.filter(
            passed_timestamp__gt=datetime.datetime.now() - datetime.timedelta(days=days)
        ).filter(
            user_id__in=enterprise_customer.enterprise_customer_users.values_list('user_id', flat=True)
        )

Get course completions via PersistentCourseGrade for all the learners of given enterprise customer.

Arguments:
enterprise_customer (EnterpriseCustomer): Include Course enrollments for learners
of this enterprise customer.
days (int): Include course enrollment of this number of days.

Returns:
(list): A list of PersistentCourseGrade objects.


def speed_clustering(clf, points, min_time):
    changepoints = detect_changepoints(points, min_time)
    cp_info = []
    for i in range(0, (len(changepoints) - 1)):
        from_index = changepoints[i]
        to_index = changepoints[(i + 1)]
        info = classify(clf, points[from_index:to_index], min_time, from_index, to_index)
        if info:
            cp_info.append(info)
    return group_modes(cp_info)

Transportation mode infering, based on changepoint segmentation

Args:
clf (:obj:`Classifier`): Classifier to use
points (:obj:`list` of :obj:`Point`)
min_time (float): Min time, in seconds, before do another segmentation
Returns:
:obj:`list` of :obj:`dict`


def insert(parent: ScheduleComponent, time: int, child: ScheduleComponent, name: str=None) -> Schedule:
    return union(parent, (time, child), name=name)

Return a new schedule with the `child` schedule inserted into the `parent` at `start_time`.

Args:
parent: Schedule to be inserted into
time: Time to be inserted defined with respect to `parent`
child: Schedule to insert
name: Name of the new schedule. Defaults to name of parent


def _get_populate_from_value(instance, field_name: Union[str, Tuple[str]], language: str):

        if callable(field_name):
            return field_name(instance)

        def get_field_value(name):
            value = resolve_object_property(instance, name)
            with translation.override(language):
                return str(value)

        if isinstance(field_name, tuple) or isinstance(field_name, list):
            value = '-'.join([
                value
                for value in [get_field_value(name) for name in field_name]
                if value
            ])
            return value

        return get_field_value(field_name)

Gets the value to create a slug from in the specified language.

Arguments:
instance:
The model that the field resides on.

field_name:
The name of the field to generate a slug for.

language:
The language to generate the slug for.

Returns:
The text to generate a slug for.


def _build_update_ops(self, mean, variance, is_training):

    def build_update_ops():

      update_mean_op = moving_averages.assign_moving_average(
          variable=self._moving_mean,
          value=tf.reshape(mean, (self._num_channels,)),
          decay=self._decay_rate,
          zero_debias=False,
          name="update_moving_mean").op

      update_variance_op = moving_averages.assign_moving_average(
          variable=self._moving_variance,
          value=tf.reshape(variance, (self._num_channels,)),
          decay=self._decay_rate,
          zero_debias=False,
          name="update_moving_variance").op

      return update_mean_op, update_variance_op

    def build_no_ops():
      return (tf.no_op(), tf.no_op())

    is_training_const = utils.constant_value(is_training)
    if is_training_const is None or is_training_const:
      update_mean_op, update_variance_op = utils.smart_cond(
          is_training,
          build_update_ops,
          build_no_ops,
      )
      return (update_mean_op, update_variance_op)
    else:
      return None

Builds the moving average update ops when using moving variance.

Args:
mean: The mean value to update with.
variance: The variance value to update with.
is_training: Boolean Tensor to indicate if we're currently in
training mode.

Returns:
Tuple of `(update_mean_op, update_variance_op)` when `is_training` is or
could be `True`. Returns `None` when `is_training=False`.


def get_default(__func: Callable, __arg: str) -> str:

    return signature(__func).parameters[__arg].default

Fetch default value for a function argument

Args:
__func: Function to inspect
__arg: Argument to extract default value for


def _ConvertRowToUnicode(self, parser_mediator, row):

    for key, value in iter(row.items()):
      if isinstance(value, py2to3.UNICODE_TYPE):
        continue

      try:
        row[key] = value.decode(self._encoding)
      except UnicodeDecodeError:
        replaced_value = value.decode(self._encoding, errors='replace')
        parser_mediator.ProduceExtractionWarning(
            'error decoding DSV value: {0:s} as {1:s}, characters have been '
            'replaced in {2:s}'.format(key, self._encoding, replaced_value))
        row[key] = replaced_value

    return row

Converts all strings in a DSV row dict to Unicode.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
row (dict[str, bytes]): a row from a DSV file, where the dictionary
key contains the column name and the value a binary string.

Returns:
dict[str, str]: a row from the DSV file, where the dictionary key
contains the column name and the value a Unicode string.


def update(self, file_path, new_data={}, **kwargs):

        data = new_data.copy()
        file_path = file_path.replace('/', '%2F')
        data['file_path'] = file_path
        path = '%s/%s' % (self.path, file_path)
        self._check_missing_update_attrs(data)
        return self.gitlab.http_put(path, post_data=data, **kwargs)

Update an object on the server.

Args:
id: ID of the object to update (can be None if not required)
new_data: the update data for the object
**kwargs: Extra options to send to the server (e.g. sudo)

Returns:
dict: The new object data (*not* a RESTObject)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabUpdateError: If the server cannot perform the request


def points_random_3d(count, range_x=(-10.0, 10.0), range_y=(-10.0, 10.0), range_z=(-10.0, 10.0), seed=None) -> VAO:

    random.seed(seed)

    def gen():
        for _ in range(count):
            yield random.uniform(*range_x)
            yield random.uniform(*range_y)
            yield random.uniform(*range_z)

    data = numpy.fromiter(gen(), count=count * 3, dtype=numpy.float32)

    vao = VAO("geometry:points_random_3d", mode=moderngl.POINTS)
    vao.buffer(data, '3f', ['in_position'])

    return vao

Generates random positions inside a confied box.

Args:
count (int): Number of points to generate

Keyword Args:
range_x (tuple): min-max range for x axis: Example (-10.0. 10.0)
range_y (tuple): min-max range for y axis: Example (-10.0. 10.0)
range_z (tuple): min-max range for z axis: Example (-10.0. 10.0)
seed (int): The random seed

Returns:
A :py:class:`demosys.opengl.vao.VAO` instance


def register(self, address, retry=True):
    logger.debug(('<%s> Sending REGISTER request to: %s' % (str(self.cuuid), str(address))))
    if (not self.listener.listening):
        logger.warning('Neteria client is not listening.')
    message = {'method': 'REGISTER', 'cuuid': str(self.cuuid)}
    if self.encryption:
        message['encryption'] = [self.encryption.n, self.encryption.e]
    self.listener.send_datagram(serialize_data(message, self.compression, encryption=False), address)
    if retry:
        self.register_retries = 0
    self.listener.call_later(self.timeout, self.retransmit, {'method': 'REGISTER', 'address': address})

This function will send a register packet to the discovered Neteria
server.

Args:
address (tuple): A tuple of the (address, port) to send the register
request to.
retry (boolean): Whether or not we want to reset the current number
of registration retries to 0.

Returns:
None

Examples:
>>> address
('192.168.0.20', 40080)


def attention_bias_to_padding(attention_bias, cast_fn=tf.to_float):

  return tf.squeeze(cast_fn(tf.less(attention_bias, -1)), axis=[1, 2])

Inverse of attention_bias_ignore_padding().

Args:
attention_bias: a `Tensor` with shape [batch, 1, 1, memory_length], as
returned by attention_bias_ignore_padding().
cast_fn: function used to cast to output type.

Returns:
a Tensor with shape [batch, memory_length] with 1.0 in padding positions
and 0.0 in non-padding positions. Type is determined by cast_fn.


def network_connect(self, value):

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

The network_connect property.

Args:
value (string). the property value.


def adjust_saturation(img, saturation_factor):

    if not _is_pil_image(img):
        raise TypeError('img should be PIL Image. Got {}'.format(type(img)))

    enhancer = ImageEnhance.Color(img)
    img = enhancer.enhance(saturation_factor)
    return img

Adjust color saturation of an image.

Args:
img (PIL Image): PIL Image to be adjusted.
saturation_factor (float):  How much to adjust the saturation. 0 will
give a black and white image, 1 will give the original image while
2 will enhance the saturation by a factor of 2.

Returns:
PIL Image: Saturation adjusted image.


def convert(self):
    assert not self._converted
    if self._input_graph_def:
        self._convert_graph_def()
    else:
        self._convert_saved_model()
    return self._converted_graph_def

Run the TF-TRT conversion.

Returns:
The converted GraphDef for TF 1.x.


def put(self, path, value, timeout=None, event_timeout=None):

        future = self.put_async(path, value)
        self.wait_all_futures(
            future, timeout=timeout, event_timeout=event_timeout)
        return future.result()

Puts a value to a path and returns when it completes

Args:
path (list): The path to put to
value (object): The value to set
timeout (float): time in seconds to wait for responses, wait forever
if None
event_timeout: maximum time in seconds to wait between each response
event, wait forever if None

Returns:
The value after the put completes


def heightmap_get_interpolated_value(hm: np.ndarray, x: float, y: float) -> float:
    return float(lib.TCOD_heightmap_get_interpolated_value(_heightmap_cdata(hm), x, y))

Return the interpolated height at non integer coordinates.

Args:
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions.
x (float): A floating point x coordinate.
y (float): A floating point y coordinate.

Returns:
float: The value at ``x``, ``y``.


def lresolve(self, path):
    path = make_string_path(path)
    if (path == self.root.name):
        return self.root
    path = self._path_without_trailing_separators(path)
    path = self._original_path(path)
    (parent_directory, child_name) = self.splitpath(path)
    if (not parent_directory):
        parent_directory = self.cwd
    try:
        parent_obj = self.resolve(parent_directory)
        assert parent_obj
        if (not isinstance(parent_obj, FakeDirectory)):
            if ((not self.is_windows_fs) and isinstance(parent_obj, FakeFile)):
                self.raise_io_error(errno.ENOTDIR, path)
            self.raise_io_error(errno.ENOENT, path)
        return parent_obj.get_entry(child_name)
    except KeyError:
        self.raise_io_error(errno.ENOENT, path)

Search for the specified object, resolving only parent links.

This is analogous to the stat/lstat difference.  This resolves links
*to* the object but not of the final object itself.

Args:
path: Specifies target FakeFile object to retrieve.

Returns:
The FakeFile object corresponding to path.

Raises:
IOError: if the object is not found.


def append(self, event, category=None):

        date = datetime.datetime.now()
        self.store.insert(0, (date, event, category))
        if len(self.store) > self.size:
            del self.store[-1]

Adds a new event to the trace store.
The event may hava a category

Args:
event (spade.message.Message): the event to be stored
category (str, optional): a category to classify the event (Default value = None)


def _matmul_2d(a, b, **kwargs):
    ragged_err = 'The matrices in `a` and `b` may not be ragged in their innermost dimension.'
    checks = []
    if isinstance(a, ragged_tensor.RaggedTensor):
        original_size = array_ops.size(a.flat_values)
        a = a.to_tensor()
        checks.append(check_ops.assert_equal(original_size, array_ops.size(a), message=ragged_err))
    if isinstance(b, ragged_tensor.RaggedTensor):
        original_size = array_ops.size(b.flat_values)
        b = b.to_tensor()
        checks.append(check_ops.assert_equal(original_size, array_ops.size(b), message=ragged_err))
    with ops.control_dependencies(checks):
        return math_ops.matmul(a, b, **kwargs)

Multiplies potentially ragged 2D tensors.

Args:
a: A 2D Tensor or RaggedTensor with `shape=[I, J]`
b: A 2D Tensor or RaggedTensor with `shape=[J, K]`
**kwargs: Additional arguments for `tf.matmul` (e.g. transpose_a).

Returns:
A 2D Tensor with `shape=[I, K]`.


def generate_output_notices(self, source, key, val, line='1', hr=True, show_name=False, colorize=True):
    output = generate_output(line=line, short=(HR_RDAP[source][key]['_short'] if hr else key), name=(HR_RDAP[source][key]['_name'] if (hr and show_name) else None), is_parent=(False if ((val is None) or (len(val) == 0)) else True), value=('None' if ((val is None) or (len(val) == 0)) else None), colorize=colorize)
    if (val is not None):
        count = 0
        for item in val:
            title = item['title']
            description = item['description']
            links = item['links']
            if (count > 0):
                output += generate_output(line=str((int(line) + 1)), is_parent=True, colorize=colorize)
            output += generate_output(line=str((int(line) + 1)), short=(HR_RDAP_COMMON[key]['title']['_short'] if hr else 'title'), name=(HR_RDAP_COMMON[key]['title']['_name'] if (hr and show_name) else None), value=title, colorize=colorize)
            output += generate_output(line=str((int(line) + 1)), short=(HR_RDAP_COMMON[key]['description']['_short'] if hr else 'description'), name=(HR_RDAP_COMMON[key]['description']['_name'] if (hr and show_name) else None), value=description.replace('\n', '\n{0}'.format(generate_output(line='3'))), colorize=colorize)
            output += self.generate_output_list(source=source, key='links', val=links, line=str((int(line) + 1)), hr=hr, show_name=show_name, colorize=colorize)
            count += 1
    return output

The function for generating CLI output RDAP notices results.

Args:
source (:obj:`str`): The parent key 'network' or 'objects'
(required).
key (:obj:`str`): The event key 'events' or 'events_actor'
(required).
val (:obj:`dict`): The event dictionary (required).
line (:obj:`str`): The line number (0-4). Determines indentation.
Defaults to '0'.
hr (:obj:`bool`): Enable human readable key translations. Defaults
to True.
show_name (:obj:`bool`): Show human readable name (default is to
only show short). Defaults to False.
colorize (:obj:`bool`): Colorize the console output with ANSI
colors. Defaults to True.

Returns:
str: The generated output.


def _prepare_4d_causal_attention_mask_with_cache_position(attention_mask: torch.Tensor, sequence_length: int, target_length: int, dtype: torch.dtype, cache_position: torch.Tensor, batch_size: int, **kwargs):
    if attention_mask is not None and attention_mask.dim() == 4:
        causal_mask = attention_mask
    else:
        min_dtype = torch.finfo(dtype).min
        causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=cache_position.device)
        if sequence_length != 1:
            causal_mask = torch.triu(causal_mask, diagonal=1)
        causal_mask *= torch.arange(target_length, device=cache_position.device) > cache_position.reshape(-1, 1)
        causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
        if attention_mask is not None:
            causal_mask = causal_mask.clone()
            mask_length = attention_mask.shape[-1]
            padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :].to(causal_mask.device)
            padding_mask = padding_mask == 0
            causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(padding_mask, min_dtype)
    return causal_mask

Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
`(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

Args:
attention_mask (`torch.Tensor`):
A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
`(batch_size, 1, query_length, key_value_length)`.
sequence_length (`int`):
The sequence length being processed.
target_length (`int`):
The target length: when generating with static cache, the mask should be as long as the static cache,
to account for the 0 padding, the part of the cache that is not filled yet.
dtype (`torch.dtype`):
The dtype to use for the 4D attention mask.
cache_position (`torch.Tensor`):
Indices depicting the position of the input sequence tokens in the sequence.
batch_size (`torch.Tensor`):
Batch size.


def check_candidate_exists(self, basepath, candidates):
    checked = []
    for item in candidates:
        abspath = os.path.join(basepath, item)
        if os.path.exists(abspath):
            checked.append(abspath)
    return checked

Check that at least one candidate exist into a directory.

Args:
basepath (str): Directory path where to search for candidate.
candidates (list): List of candidate file paths.

Returns:
list: List of existing candidates.


def layer_postprocess(layer_input, layer_output, hparams):

  return layer_prepostprocess(
      layer_input,
      layer_output,
      sequence=hparams.layer_postprocess_sequence,
      dropout_rate=hparams.layer_prepostprocess_dropout,
      norm_type=hparams.norm_type,
      depth=None,
      epsilon=hparams.norm_epsilon,
      dropout_broadcast_dims=comma_separated_string_to_integer_list(
          getattr(hparams, "layer_prepostprocess_dropout_broadcast_dims", "")),
      default_name="layer_postprocess")

Apply layer postprocessing.

See layer_prepostprocess() for details.

A hyperparameters object is passed for convenience.  The hyperparameters
that may be used are:

layer_postprocess_sequence
layer_prepostprocess_dropout
norm_type
hidden_size
norm_epsilon

Args:
layer_input: a Tensor
layer_output: a Tensor
hparams: a hyperparameters object.

Returns:
a Tensor


def variable_dtype(self):
    return self._variable_dtype

The variable dtype of this policy.

This is the dtype layers will create their variables in, unless a layer
explicitly chooses a different dtype. If this is different than
`DTypePolicy.compute_dtype`, Layers will cast variables to
the compute dtype to avoid type errors.

Variable regularizers are run in the variable dtype, not the compute
dtype.

Returns:
The variable dtype of this policy, as a string.


def write_to(self, content, content_type):

    try:
      self._api.object_upload(self._bucket, self._key, content, content_type)
    except Exception as e:
      raise e

Writes text content to this item.

Args:
content: the text content to be written.
content_type: the type of text content.
Raises:
Exception if there was an error requesting the item's content.


def upload_rows(bq_legacy_client: BigQueryLegacyClient, table_metadata: TableMetadata, rows: List[Dict[str, Any]]) -> List[str]:
    if len(rows) > 0:
        result = bq_legacy_client.insert_rows_json(table_metadata.full_table_id, rows)
        for row in result:
            if 'errors' in row and len(row['errors']) > 0:
                return [str(e) for e in row['errors']]
    return []

Upload a List of Dict rows to a BigQuery table, by appending.

Note: Does NOT support nested columns.

Args:
* bq_legacy_client: BigQuery Legacy API client
* table_metadata: TableMetadata object
* column: Column name to select

Returns:
* List of errors, if any


def startProducing(self, consumer):
    self._consumer = consumer
    self._current_deferred = defer.Deferred()
    self._sent = 0
    self._paused = False
    if (not hasattr(self, '_chunk_headers')):
        self._build_chunk_headers()
    if self._data:
        block = ''
        for field in self._data:
            block += self._chunk_headers[field]
            block += self._data[field]
            block += '\r\n'
        self._send_to_consumer(block)
    if self._files:
        self._files_iterator = self._files.iterkeys()
        self._files_sent = 0
        self._files_length = len(self._files)
        self._current_file_path = None
        self._current_file_handle = None
        self._current_file_length = None
        self._current_file_sent = 0
        result = self._produce()
        if result:
            return result
    else:
        return defer.succeed(None)
    return self._current_deferred

Start producing.

Args:
consumer: Consumer


def ones_comp_sum16(num1: int, num2: int) -> int:

    carry = 1 << 16
    result = num1 + num2
    return result if result < carry else result + 1 - carry

Calculates the 1's complement sum for 16-bit numbers.

Args:
num1: 16-bit number.
num2: 16-bit number.

Returns:
The calculated result.


def get_version_details(self, version_name):
    name = ('%s/versions/%s' % (self._full_model_name, version_name))
    return self._api.projects().models().versions().get(name=name).execute()

Get details of a version.

Args:
version: the name of the version in short form, such as "v1".
Returns: a dictionary containing the version details.


def on_test_batch_begin(self, batch, logs=None):

Called at the beginning of a batch in `evaluate` methods.

Also called at the beginning of a validation batch in the `fit`
methods, if validation data is provided.

Subclasses should override for any actions to run.

Note that if the `steps_per_execution` argument to `compile` in
`tf.keras.Model` is set to `N`, this method will only be called every `N`
batches.

Args:
batch: Integer, index of batch within the current epoch.
logs: Dict, contains the return value of `model.test_step`. Typically,
the values of the `Model`'s metrics are returned.  Example:
`{'loss': 0.2, 'accuracy': 0.7}`.


def add_child(self, total, prefix=None, width=300):
    self.child_bar = NotebookProgressBar(total, prefix=prefix, parent=self, width=width)
    return self.child_bar

Add a child progress bar displayed under the table of metrics. The child progress bar is returned (so it can be
easily updated).

Args:
total (`int`): The number of iterations for the child progress bar.
prefix (`str`, *optional*): A prefix to write on the left of the progress bar.
width (`int`, *optional*, defaults to 300): The width (in pixels) of the progress bar.


def _create_package_hierarchy(prefix=settings.TEMP_DIR, book_id=None):
    root_dir = _get_package_name(book_id=book_id, prefix=prefix)
    if os.path.exists(root_dir):
        shutil.rmtree(root_dir)
    os.mkdir(root_dir)
    original_dir = os.path.join(root_dir, 'original')
    metadata_dir = os.path.join(root_dir, 'metadata')
    os.mkdir(original_dir)
    os.mkdir(metadata_dir)
    return (root_dir, original_dir, metadata_dir)

Create hierarchy of directories, at it is required in specification.

`root_dir` is root of the package generated using :attr:`settings.TEMP_DIR`
and :func:`_get_package_name`.

`orig_dir` is path to the directory, where the data files are stored.

`metadata_dir` is path to the directory with MODS metadata.

Args:
book_id (str, default None): UUID of the book.
prefix (str, default settings.TEMP_DIR): Where the package will be
stored. Default :attr:`settings.TEMP_DIR`.

Warning:
If the `root_dir` exists, it is REMOVED!

Returns:
list of str: root_dir, orig_dir, metadata_dir


def __getitem___(self, py_type):
    raise NotImplementedError

Given a type creates a TypeConstraint instance parameterized by the type.

This function serves as a factory function which creates TypeConstraint
instances. Additionally, implementations by sub-classes should perform any
sanity checking of the passed types in this method in order to rule-out
disallowed behavior. Such as, attempting to create a TypeConstraint whose
parameterized type is actually an object instance.

Args:
py_type: An instance of a Python type or TypeConstraint.

Returns: An instance of a custom TypeConstraint for this CompositeTypeHint.

Raises:
TypeError: If the passed type violates any contraints for this particular
TypeHint.


def _BiasAddGrad(op: ops.Operation, received_grad):
    try:
        data_format = op.get_attr('data_format')
    except ValueError:
        data_format = None
    return (received_grad, gen_nn_ops.bias_add_grad(out_backprop=received_grad, data_format=data_format))

Return the gradients for the 2 inputs of bias_op.

The first input of unused_bias_op is the tensor t, and its gradient is
just the gradient the unused_bias_op received.

The second input of unused_bias_op is the bias vector which has one fewer
dimension than "received_grad" (the batch dimension.)  Its gradient is the
received gradient Summed on the batch dimension, which is the first dimension.

Args:
op: The BiasOp for which we need to generate gradients.
received_grad: Tensor.  The gradients passed to the BiasOp.

Returns:
Two tensors, the first one for the "tensor" input of the BiasOp,
the second one for the "bias" input of the BiasOp.


def update_reserved_vlan_range(self, id_or_uri, vlan_pool, force=False):

        uri = self._client.build_uri(id_or_uri) + "/reserved-vlan-range"
        return self._client.update(resource=vlan_pool, uri=uri, force=force, default_values=self.DEFAULT_VALUES)

Updates the reserved vlan ID range for the fabric.

Note:
This method is only available on HPE Synergy.

Args:
id_or_uri: ID or URI of fabric.
vlan_pool (dict): vlan-pool data to update.
force:  If set to true, the operation completes despite any problems with network connectivity or errors
on the resource itself. The default is false.

Returns:
dict: The fabric


def build_ellipse(X, Y):

    x_mean = np.mean(X)
    y_mean = np.mean(Y)

    cov_matrix = np.cov(np.vstack((X, Y)))
    U, s, V = linalg.svd(cov_matrix, full_matrices=False)

    chi_95 = np.sqrt(4.61)
    width = np.sqrt(cov_matrix[0][0]) * chi_95 * 2
    height = np.sqrt(cov_matrix[1][1]) * chi_95 * 2

    eigenvector = V.T[0]
    angle = np.arctan(eigenvector[1] / eigenvector[0])

    return x_mean, y_mean, width, height, angle

Construct ellipse coordinates from two arrays of numbers.

Args:
X (1D array_like)
Y (1D array_like)

Returns:
float: The mean of `X`.
float: The mean of `Y`.
float: The width of the ellipse.
float: The height of the ellipse.
float: The angle of orientation of the ellipse.


def drift_fn(self):
    return self._drift_fn

Python callable calculating instantaneous drift.

The callable should accept two real `Tensor` arguments of the same dtype.
The first argument is the scalar time t, the second argument is the value of
Ito process X - `Tensor` of shape
`batch_shape + sample_shape + [dim]`, where `batch_shape` represents a batch
of models and `sample_shape` represents samples for each of the models. The
result is value of drift a(t, X). The return value of the callable is a real
`Tensor` of the same dtype as the input arguments and of shape
`batch_shape + sample_shape + [dim]`. For example, `sample_shape` can stand
for `[num_samples]` for Monte Carlo sampling, or
`[num_grid_points_1, ..., num_grid_points_dim]` for Finite Difference
solvers.

Returns:
The instantaneous drift rate callable.


def sequence_edit_distance(predictions, labels, weights_fn=common_layers.weights_nonzero):
    if (weights_fn is not common_layers.weights_nonzero):
        raise ValueError('Only weights_nonzero can be used for this metric.')
    with tf.variable_scope('edit_distance', values=[predictions, labels]):
        predictions = tf.to_int32(tf.squeeze(tf.argmax(predictions, axis=(- 1)), axis=(2, 3)))
        nonzero_idx = tf.where(tf.not_equal(predictions, 0))
        sparse_outputs = tf.SparseTensor(nonzero_idx, tf.gather_nd(predictions, nonzero_idx), tf.shape(predictions, out_type=tf.int64))
        labels = tf.squeeze(labels, axis=(2, 3))
        nonzero_idx = tf.where(tf.not_equal(labels, 0))
        label_sparse_outputs = tf.SparseTensor(nonzero_idx, tf.gather_nd(labels, nonzero_idx), tf.shape(labels, out_type=tf.int64))
        distance = tf.reduce_sum(tf.edit_distance(sparse_outputs, label_sparse_outputs, normalize=False))
        reference_length = tf.to_float(common_layers.shape_list(nonzero_idx)[0])
        return ((distance / reference_length), reference_length)

Average edit distance, ignoring padding 0s.

The score returned is the edit distance divided by the total length of
reference truth and the weight returned is the total length of the truth.

Args:
predictions: Tensor of shape [`batch_size`, `length`, 1, `num_classes`] and
type tf.float32 representing the logits, 0-padded.
labels: Tensor of shape [`batch_size`, `length`, 1, 1] and type tf.int32
representing the labels of same length as logits and 0-padded.
weights_fn: ignored. The weights returned are the total length of the ground
truth labels, excluding 0-paddings.

Returns:
(edit distance / reference length, reference length)

Raises:
ValueError: if weights_fn is not common_layers.weights_nonzero.


def convert(framework: str, model: str, output: Path, opset: int, tokenizer: Optional[str]=None, use_external_format: bool=False, pipeline_name: str='feature-extraction', **model_kwargs):
    warnings.warn('The `transformers.convert_graph_to_onnx` package is deprecated and will be removed in version 5 of Transformers', FutureWarning)
    print(f'ONNX opset version set to: {opset}')
    nlp = load_graph_from_args(pipeline_name, framework, model, tokenizer, **model_kwargs)
    if not output.parent.exists():
        print(f'Creating folder {output.parent}')
        makedirs(output.parent.as_posix())
    elif len(listdir(output.parent.as_posix())) > 0:
        raise Exception(f'Folder {output.parent.as_posix()} is not empty, aborting conversion')
    if framework == 'pt':
        convert_pytorch(nlp, opset, output, use_external_format)
    else:
        convert_tensorflow(nlp, opset, output)

Convert the pipeline object to the ONNX Intermediate Representation (IR) format

Args:
framework: The framework the pipeline is backed by ("pt" or "tf")
model: The name of the model to load for the pipeline
output: The path where the ONNX graph will be stored
opset: The actual version of the ONNX operator set to use
tokenizer: The name of the model to load for the pipeline, default to the model's name if not provided
use_external_format:
Split the model definition from its parameters to allow model bigger than 2GB (PyTorch only)
pipeline_name: The kind of pipeline to instantiate (ner, question-answering, etc.)
model_kwargs: Keyword arguments to be forwarded to the model constructor

Returns:


def scan_chain_len(self, scan_chain):

        res = self._dll.JLINKARM_MeasureSCLen(scan_chain)
        if res < 0:
            raise errors.JLinkException(res)
        return res

Retrieves and returns the number of bits in the scan chain.

Args:
self (JLink): the ``JLink`` instance
scan_chain (int): scan chain to be measured

Returns:
Number of bits in the specified scan chain.

Raises:
JLinkException: on error.


def __convertRlocToRouterId(self, xRloc16):

        routerList = []
        routerList = self.__sendCommand('router list')[0].split()
        print routerList
        print xRloc16

        for index in routerList:
            router = []
            cmd = 'router %s' % index
            router = self.__sendCommand(cmd)

            for line in router:
                if 'Done' in line:
                    break
                elif 'Router ID' in line:
                    routerid = line.split()[2]
                elif 'Rloc' in line:
                    rloc16 = line.split()[1]
                else:
                    pass

            if isinstance(xRloc16, str):
                rloc16 = '0x' + rloc16
                if rloc16 == xRloc16:
                    return routerid
            elif isinstance(xRloc16, int):
                if int(rloc16, 16) == xRloc16:
                    return routerid
            else:
                pass

        return None

mapping Rloc16 to router id

Args:
xRloc16: hex rloc16 short address

Returns:
actual router id allocated by leader


def _query(self, url, xpath):

        return self.session.query(CachedRequest).filter(CachedRequest.url == url).filter(CachedRequest.xpath == xpath)

Base query for an url and xpath

Args:
url (str): URL to search
xpath (str): xpath to search (may be ``None``)


def save_dataframes(self, outdir, prefix='df_'):
    dfs = list(filter((lambda x: x.startswith(prefix)), dir(self)))
    counter = 0
    for df in dfs:
        outpath = ssbio.utils.outfile_maker(inname=df, outext='.csv', outdir=outdir)
        my_df = getattr(self, df)
        if (not isinstance(my_df, pd.DataFrame)):
            raise TypeError('{}: object is not a Pandas DataFrame'.format(df))
        if my_df.empty:
            log.debug('{}: empty dataframe, not saving'.format(df))
        else:
            my_df.to_csv(outpath)
            log.debug('{}: saved dataframe'.format(outpath))
            counter += 1
    log.debug('Saved {} dataframes at {}'.format(counter, outdir))

Save all attributes that start with "df" into a specified directory.

Args:
outdir (str): Path to output directory
prefix (str): Prefix that dataframe attributes start with


def is10(msg):
    if allzeros(msg):
        return False
    d = hex2bin(data(msg))
    if (d[0:8] != '00010000'):
        return False
    if (bin2int(d[9:14]) != 0):
        return False
    if ((d[14] == '1') and (bin2int(d[16:23]) < 5)):
        return False
    if ((d[14] == '0') and (bin2int(d[16:23]) > 4)):
        return False
    return True

Check if a message is likely to be BDS code 1,0

Args:
msg (String): 28 bytes hexadecimal message string

Returns:
bool: True or False


def split_locator(self, path):

        relative = self.relpath(path)
        try:
            locator, tail = relative.split('/', 1)
        except ValueError:
            locator = relative
            tail = ''
        return locator, tail

Split the path into a pair (locator, path).

args:
path (str): Absolute path or URL.

Returns:
tuple of str: locator, path.


def forward(self, pixel_values: torch.FloatTensor, spatial_shapes: torch.LongTensor) -> torch.Tensor:
    target_dtype = self.patch_embedding.weight.dtype
    patch_embeds = self.patch_embedding(pixel_values.to(dtype=target_dtype))
    positional_embeddings = self.position_embedding.weight.reshape(self.position_embedding_size, self.position_embedding_size, -1)
    resized_positional_embeddings = self.resize_positional_embeddings(positional_embeddings, spatial_shapes, max_length=pixel_values.shape[1])
    embeddings = patch_embeds + resized_positional_embeddings
    return embeddings

Args:
pixel_values (`torch.FloatTensor`):
Pixel values of shape (batch_size, max_num_patches, num_channels * patch_size * patch_size)
spatial_shapes (`List[Tuple[int, int]]`):
Spatial shapes of shape (batch_size, 2) to resize the positional embeddings to


def get_autosave_filename(self, filename):
    try:
        autosave_filename = self.name_mapping[filename]
    except KeyError:
        autosave_dir = get_conf_path('autosave')
        if (not osp.isdir(autosave_dir)):
            try:
                os.mkdir(autosave_dir)
            except EnvironmentError as error:
                action = _('Error while creating autosave directory')
                msgbox = AutosaveErrorDialog(action, error)
                msgbox.exec_if_enabled()
        autosave_filename = self.create_unique_autosave_filename(filename, autosave_dir)
        self.name_mapping[filename] = autosave_filename
        self.stack.sig_option_changed.emit('autosave_mapping', self.name_mapping)
        logger.debug('New autosave file name')
    return autosave_filename

Get name of autosave file for specified file name.

This function uses the dict in `self.name_mapping`. If `filename` is
in the mapping, then return the corresponding autosave file name.
Otherwise, construct a unique file name and update the mapping.

Args:
filename (str): original file name


def _validate_none_or_type(t):

    def _validate(setting):
        "\n        Check the setting to make sure it's the right type.\n\n        Args:\n            setting (object): The setting to check.\n\n        Returns:\n            object: The unmodified object if it's the proper type.\n\n        Raises:\n            ValueError: If the setting is the wrong type.\n        "
        if ((setting is not None) and (not isinstance(setting, t))):
            raise ValueError('"{}" is not "{}"'.format(setting, t))
        return setting
    return _validate

Create a validator that checks if a setting is either None or a given type.

Args:
t: The type to assert.

Returns:
callable: A callable that will validate a setting for that type.


def _retrieve_problem(self, id_):
    future = Future(self, id_, self.return_matrix, None)
    self.client._poll(future)
    return future

Resume polling for a problem previously submitted.

Args:
id_: Identification of the query.

Returns:
:obj: `Future`


def find1(self, kw: YangIdentifier, arg: str=None, pref: YangIdentifier=None, required: bool=False) -> Optional['Statement']:
    for sub in self.substatements:
        if ((sub.keyword == kw) and (sub.prefix == pref) and ((arg is None) or (sub.argument == arg))):
            return sub
    if required:
        raise StatementNotFound(str(self), kw)

Return first substatement with the given parameters.

Args:
kw: Statement keyword (local part for extensions).
arg: Argument (all arguments will match if ``None``).
pref: Keyword prefix (``None`` for built-in statements).
required: Should an exception be raised on failure?

Raises:
StatementNotFound: If `required` is ``True`` and the
statement is not found.


def create_dir(self, directory_path, perm_bits=PERM_DEF):
    directory_path = self.make_string_path(directory_path)
    directory_path = self.absnormpath(directory_path)
    self._auto_mount_drive_if_needed(directory_path)
    if self.exists(directory_path, check_link=True):
        self.raise_os_error(errno.EEXIST, directory_path)
    path_components = self._path_components(directory_path)
    current_dir = self.root
    new_dirs = []
    for component in path_components:
        directory = self._directory_content(current_dir, component)[1]
        if (not directory):
            new_dir = FakeDirectory(component, filesystem=self)
            new_dirs.append(new_dir)
            current_dir.add_entry(new_dir)
            current_dir = new_dir
        else:
            if S_ISLNK(directory.st_mode):
                directory = self.resolve(directory.contents)
            current_dir = directory
            if ((directory.st_mode & S_IFDIR) != S_IFDIR):
                self.raise_os_error(errno.ENOTDIR, current_dir.path)
    for new_dir in new_dirs:
        new_dir.st_mode = (S_IFDIR | perm_bits)
    self._last_ino += 1
    current_dir.st_ino = self._last_ino
    return current_dir

Create `directory_path`, and all the parent directories.

Helper method to set up your test faster.

Args:
directory_path: The full directory path to create.
perm_bits: The permission bits as set by `chmod`.

Returns:
The newly created FakeDirectory object.

Raises:
OSError: if the directory already exists.


def _is_user_included_op(self, op):
    for opname_re in self._parameters.included_opname_re_list:
        if opname_re.match(op.name):
            return True
    for optype_re in self._parameters.included_optype_re_list:
        if optype_re.match(op.type):
            return True
    return False

Checks whether the op is included in the tensor tracer flags.

Args:
op: tf Operation
Returns:
True, if the op is included.
An op is included if:
- Its op name is given in included_opnames
- Its op type is given in included_optypes
- The op is at most _trace_ops_before_included hops before an included op
- The op is at most _trace_ops_after_included hops after an included op


def _generate_async(self, generator):
    first_value_future = self._workers.submit(next, generator)

    def get_first_element(future=first_value_future):
        '\n            Get first element value from future.\n\n            Args:\n                future (concurrent.futures._base.Future): First value future.\n\n            Returns:\n                Evaluated value\n            '
        try:
            (yield future.result())
        except StopIteration:
            return
    return chain(get_first_element(), generator)

Return the previous generator object after having run the first element
evaluation as a background task.

Args:
generator (iterable): A generator function.

Returns:
iterable: The generator function with first element evaluated
in background.


def list_devices(self):
    raw_device_list = tf_session.TF_SessionListDevices(self._session)
    device_list = []
    size = tf_session.TF_DeviceListCount(raw_device_list)
    for i in range(size):
        name = tf_session.TF_DeviceListName(raw_device_list, i)
        device_type = tf_session.TF_DeviceListType(raw_device_list, i)
        memory = tf_session.TF_DeviceListMemoryBytes(raw_device_list, i)
        incarnation = tf_session.TF_DeviceListIncarnation(raw_device_list, i)
        device_list.append(_DeviceAttributes(name, device_type, memory, incarnation))
    tf_session.TF_DeleteDeviceList(raw_device_list)
    return device_list

Lists available devices in this session.

```python
devices = sess.list_devices()
for d in devices:
print(d.name)
```

Where:
Each element in the list has the following properties
name: A string with the full name of the device. ex:
`/job:worker/replica:0/task:3/device:CPU:0`
device_type: The type of the device (e.g. `CPU`, `GPU`, `TPU`.)
memory_limit: The maximum amount of memory available on the device.
Note: depending on the device, it is possible the usable memory could
be substantially less.

Raises:
tf.errors.OpError: If it encounters an error (e.g. session is in an
invalid state, or network errors occur).

Returns:
A list of devices in the session.


def filename(self, fname, timestep=None, suffix='', force_legacy=False):
    if (timestep is not None):
        fname += '{:05d}'.format(timestep)
    fname += suffix
    if ((not force_legacy) and self.hdf5):
        fpath = (self.hdf5 / fname)
    else:
        fpath = ((self.par['ioin']['output_file_stem'] + '_') + fname)
        fpath = (self.path / fpath)
    return fpath

Return name of StagYY output file.

Args:
fname (str): name stem.
timestep (int): snapshot number, set to None if this is not
relevant.
suffix (str): optional suffix of file name.
force_legacy (bool): force returning the legacy output path.
Returns:
:class:`pathlib.Path`: the path of the output file constructed
with the provided segments.


def GetMessages(self, formatter_mediator, event):

    event_values = event.CopyToDict()

    text_pieces = []
    for key, value in event_values.items():
      if key in definitions.RESERVED_VARIABLE_NAMES:
        continue
      text_pieces.append('{0:s}: {1!s}'.format(key, value))

    event_values['attribute_driven'] = ' '.join(text_pieces)
    event_values['data_type'] = self.DATA_TYPE

    return self._FormatMessages(
        self.FORMAT_STRING, self.FORMAT_STRING_SHORT, event_values)

Determines the formatted message strings for an event object.

Args:
formatter_mediator (FormatterMediator): mediates the interactions
between formatters and other components, such as storage and Windows
EventLog resources.
event (EventObject): event.

Returns:
tuple(str, str): formatted message string and short message string.


def add_droplets(self, droplet_ids):

        return self.get_data(
            "load_balancers/%s/droplets/" % self.id,
            type=POST,
            params={"droplet_ids": droplet_ids}
        )

Assign a LoadBalancer to a Droplet.

Args:
droplet_ids (obj:`list` of `int`): A list of Droplet IDs


def emit_tree_format(tree, verbose=False):

    if verbose:
        print("Converting: " + repr(tree))
    ret_str = __recursive_formatter(tree)
    return ret_str

Returns a tree representation of a parse tree.

Arguments:
tree:           the parse tree whose tree representation is to be generated
verbose (bool): if True prints the parse tree to be formatted

Returns:
str:  tree-like representation of the parse tree


def _read_pem_file(cls, filename, from_package=False):
    _logger.debug('Reading PEM {0}.'.format(filename))
    if from_package:
        return wpull.util.filter_pem(wpull.util.get_package_data(filename))
    with open(filename, 'rb') as in_file:
        return wpull.util.filter_pem(in_file.read())

Read the PEM file.

Returns:
iterable: An iterable of certificates. The certificate data
is :class:`byte`.


def sg_flatten(tensor, opt):
    r
    dim = np.prod(tensor.get_shape().as_list()[1:])
    return tf.reshape(tensor, [-1, dim], name=opt.name)

r"""Reshapes a tensor to `batch_size x -1`.

See `tf.reshape()` in tensorflow.

Args:
tensor: A `Tensor` (automatically given by chain).
opt:
name: If provided, it replaces current tensor's name.

Returns:
A 2-D tensor.


def match(pattern, path):

    try:
        levels, recursive, re_pattern = _PATTERN_CACHE[(pattern, True)]
    except KeyError:
        levels, recursive, re_pattern = _translate_glob(pattern, case_sensitive=True)
        _PATTERN_CACHE[(pattern, True)] = (levels, recursive, re_pattern)
    return bool(re_pattern.match(path))

Compare a glob pattern with a path (case sensitive).

Arguments:
pattern (str): A glob pattern.
path (str): A path.

Returns:
bool: ``True`` if the path matches the pattern.

Example:

>>> from fs.glob import match
>>> match("**/*.py", "/fs/glob.py")
True


def concatechain(*generators: types.FrameGenerator, separator: str=''):
    while True:
        try:
            next_ = [next(gen) for gen in generators]
            (yield separator.join(next_))
        except StopIteration as exc:
            return exc.value

Return a generator that in each iteration takes one value from each of the
supplied generators, joins them together with the specified separator and
yields the result. Stops as soon as any iterator raises StopIteration and
returns the value contained in it.

Primarily created for chaining string generators, hence the name.

Args:
generators: Any number of generators that yield types that can be
joined together with the separator string.
separator: A separator to insert between each value yielded by
the different generators.
Returns:
A generator that yields strings that are the concatenation of one value
from each of the generators, joined together with the separator string.


def load_settings(self, path):
    if (not os.path.exists(path)):
        raise exceptions.ConfigurationError("The server configuration file ('{0}') could not be located.".format(path))
    self._logger.info('Loading server configuration settings from: {0}'.format(path))
    parser = configparser.ConfigParser()
    parser.read(path)
    self._parse_settings(parser)
    self.parse_auth_settings(parser)

Load configuration settings from the file pointed to by path.

This will overwrite all current setting values.

Args:
path (string): The path to the configuration file containing
the settings to load. Required.
Raises:
ConfigurationError: Raised if the path does not point to an
existing file or if a setting value is invalid.


def on_smart_contract_event(self, sc_event: NotifyEvent):

        if not isinstance(sc_event, NotifyEvent):
            logger.info("Not Notify Event instance")
            return
        if sc_event.ShouldPersist:
            if sc_event.notify_type in [NotifyType.TRANSFER, NotifyType.REFUND, NotifyType.MINT]:
                self._events_to_write.append(sc_event)

Listener for NotifyEvent
Args:
sc_event (NotifyEvent): event to check whether it should be persisted


def daily_from_hourly(df):

    df_daily = pd.DataFrame()

    if 'temp' in df:
        df_daily['temp'] = df.temp.resample('D').mean()
        df_daily['tmin'] = df.temp.groupby(df.temp.index.date).min()
        df_daily['tmax'] = df.temp.groupby(df.temp.index.date).max()

    if 'precip' in df:
        df_daily['precip'] = df.precip.resample('D').sum()

    if 'glob' in df:
        df_daily['glob'] = df.glob.resample('D').mean()

    if 'hum' in df:
        df_daily['hum'] = df.hum.resample('D').mean()

    if 'hum' in df:
        df_daily['hum_min'] = df.hum.groupby(df.hum.index.date).min()

    if 'hum' in df:
        df_daily['hum_max'] = df.hum.groupby(df.hum.index.date).max()

    if 'wind' in df:
        df_daily['wind'] = df.wind.resample('D').mean()

    if 'ssd' in df:
        df_daily['ssd'] = df.ssd.resample('D').sum() / 60

    df_daily.index.name = None
    return df_daily

Aggregates data (hourly to daily values) according to the characteristics
of each variable (e.g., average for temperature, sum for precipitation)

Args:
df: dataframe including time series with one hour time steps

Returns:
dataframe (daily)


def handle_unexpected_exception(exc):

    try:
        write_logfile()
        addendum = 'Please see the log file for more information.'
    except IOError:
        addendum = 'Unable to write log file.'
    try:
        message = str(exc)
        return '{}{}{}'.format(message, '\n' if message else '', addendum)
    except Exception:
        return str(exc)

Return an error message and write a log file if logging was not enabled.

Args:
exc: The unexpected exception.

Returns:
A message to display to the user concerning the unexpected exception.


def _generate_placements(self, width, height):
    skyline = self._skyline
    points = collections.deque()
    left_index = right_index = 0
    support_height = skyline[0].top
    support_index = 0
    placements = self._placement_points_generator(skyline, width)
    for p in placements:
        if ((p + width) > skyline[right_index].right):
            for right_index in range((right_index + 1), len(skyline)):
                if (skyline[right_index].top >= support_height):
                    support_index = right_index
                    support_height = skyline[right_index].top
                if ((p + width) <= skyline[right_index].right):
                    break
        if (p >= skyline[left_index].right):
            left_index += 1
        if (support_index < left_index):
            support_index = left_index
            support_height = skyline[left_index].top
            for i in range(left_index, (right_index + 1)):
                if (skyline[i].top >= support_height):
                    support_index = i
                    support_height = skyline[i].top
        if ((support_height + height) <= self.height):
            points.append((Rectangle(p, support_height, width, height), left_index, right_index))
    return points

Generate a list with

Arguments:
skyline (list): SkylineHSegment list
width (number):

Returns:
tuple (Rectangle, fitness):
Rectangle: Rectangle in valid position
left_skyline: Index for the skyline under the rectangle left edge.
right_skyline: Index for the skyline under the rectangle right edte.


def reciprocal(x):
    if any_symbolic_tensors((x,)):
        return Reciprocal().symbolic_call(x)
    return backend.numpy.reciprocal(x)

Return the reciprocal of the argument, element-wise.

Calculates `1/x`.

Args:
x: Input tensor.

Returns:
Output tensor, element-wise reciprocal of `x`.


def cross_replica_sum(x, group_assignment=None, name=None):
    if group_assignment is None:
        group_assignment = _create_default_group_assignment()
    return gen_tpu_ops.cross_replica_sum(x, group_assignment, name=name)

Sum the input tensor across replicas according to group_assignment.

Args:
x: The local tensor to the sum.
group_assignment: Optional 2d int32 lists with shape [num_groups,
num_replicas_per_group]. `group_assignment[i]` represents the replica ids
in the ith subgroup.
name: Optional op name.

Returns:
A `Tensor` which is summed across replicas.


def is_valid_bibtex(reference):
    sio = StringIO(remove_non_ascii(reference))
    parser = bibtex.Parser()
    errors.set_strict_mode(False)
    bib_data = parser.parse_stream(sio)
    return (len(bib_data.entries) > 0)

Use pybtex to validate that a reference is in proper BibTeX format

Args:
reference: A String reference in BibTeX format.

Returns:
Boolean indicating if reference is valid bibtex.


def query(self, expr, **kwargs):

        columns = self.columns

        def query_builder(df, **kwargs):

            df = df.copy()
            df.index = pandas.RangeIndex(len(df))
            df.columns = columns
            df.query(expr, inplace=True, **kwargs)
            df.columns = pandas.RangeIndex(len(df.columns))
            return df

        func = self._prepare_method(query_builder, **kwargs)
        new_data = self._map_across_full_axis(1, func)

        new_index = self.compute_index(0, new_data, True)

        return self.__constructor__(new_data, new_index, self.columns, self.dtypes)

Query columns of the DataManager with a boolean expression.

Args:
expr: Boolean expression to query the columns with.

Returns:
DataManager containing the rows where the boolean expression is satisfied.


def ShouldCheckNamespaceIndentation(nesting_state, is_namespace_indent_item, raw_lines_no_comments, linenum):
    is_forward_declaration = IsForwardClassDeclaration(raw_lines_no_comments, linenum)
    if (not (is_namespace_indent_item or is_forward_declaration)):
        return False
    if IsMacroDefinition(raw_lines_no_comments, linenum):
        return False
    return IsBlockInNameSpace(nesting_state, is_forward_declaration)

This method determines if we should apply our namespace indentation check.

Args:
nesting_state: The current nesting state.
is_namespace_indent_item: If we just put a new class on the stack, True.
If the top of the stack is not a class, or we did not recently
add the class, False.
raw_lines_no_comments: The lines without the comments.
linenum: The current line number we are processing.

Returns:
True if we should apply our namespace indentation check. Currently, it
only works for classes and namespaces inside of a namespace.


def reduce(reducer, data, chunk_size=DEFAULT_CHUNK_SIZE):

    if not chunk_size:
        return finalize(reducer, fold(reducer, data))

    chunks = generate_chunks(data, chunk_size)
    intermediate = fold(reducer, next(chunks))
    for chunk in chunks:
        intermediate = merge(reducer, intermediate, fold(reducer, chunk))

    return finalize(reducer, intermediate)

Repeatedly call fold and merge on data and then finalize.

Arguments:
data: Input for the fold function.
reducer: The IReducer to use.
chunk_size: How many items should be passed to fold at a time?

Returns:
Return value of finalize.


def load(cls, config: Optional[Config] = None):

        if cls._dfk is not None:
            raise RuntimeError('Config has already been loaded')

        if config is None:
            cls._dfk = DataFlowKernel(Config())
        else:
            cls._dfk = DataFlowKernel(config)

        return cls._dfk

Load a DataFlowKernel.

Args:
- config (Config) : Configuration to load. This config will be passed to a
new DataFlowKernel instantiation which will be set as the active DataFlowKernel.
Returns:
- DataFlowKernel : The loaded DataFlowKernel object.


def GetMetadataLegacy(client, token=None):

  if isinstance(client, rdfvalue.RDFURN):
    client_fd = aff4.FACTORY.Open(client, mode="r", token=token)
  else:
    client_fd = client

  metadata = ExportedMetadata()

  metadata.client_urn = client_fd.urn
  metadata.client_age = client_fd.urn.age

  metadata.hostname = utils.SmartUnicode(
      client_fd.Get(client_fd.Schema.HOSTNAME, ""))

  metadata.os = utils.SmartUnicode(client_fd.Get(client_fd.Schema.SYSTEM, ""))

  metadata.uname = utils.SmartUnicode(client_fd.Get(client_fd.Schema.UNAME, ""))

  metadata.os_release = utils.SmartUnicode(
      client_fd.Get(client_fd.Schema.OS_RELEASE, ""))

  metadata.os_version = utils.SmartUnicode(
      client_fd.Get(client_fd.Schema.OS_VERSION, ""))

  kb = client_fd.Get(client_fd.Schema.KNOWLEDGE_BASE)
  usernames = ""
  if kb:
    usernames = [user.username for user in kb.users] or ""
  metadata.usernames = utils.SmartUnicode(usernames)

  metadata.mac_address = utils.SmartUnicode(
      client_fd.Get(client_fd.Schema.MAC_ADDRESS, ""))

  system_labels = set()
  user_labels = set()
  for l in client_fd.GetLabels():
    if l.owner == "GRR":
      system_labels.add(l.name)
    else:
      user_labels.add(l.name)

  metadata.labels = ",".join(sorted(system_labels | user_labels))

  metadata.system_labels = ",".join(sorted(system_labels))

  metadata.user_labels = ",".join(sorted(user_labels))

  metadata.hardware_info = client_fd.Get(client_fd.Schema.HARDWARE_INFO)

  metadata.kernel_version = client_fd.Get(client_fd.Schema.KERNEL)

  return metadata

Builds ExportedMetadata object for a given client id.

Note: This is a legacy aff4-only implementation.
TODO(user): deprecate as soon as REL_DB migration is done.

Args:
client: RDFURN of a client or VFSGRRClient object itself.
token: Security token.

Returns:
ExportedMetadata object with metadata of the client.


def calculate_etag(file_path):

        stat = file_path.stat()
        etag = "%x-%x" % (stat.st_mtime_ns, stat.st_size)
        return etag

Calculate an etag value

Args:
a_file (pathlib.Path): The filepath to the

Returns:
String of the etag value to be sent back in header


def __init__(self, project_id, credentials):

    self._project_id = project_id
    self._credentials = credentials

Initializes an instance of a Context object.

Args:
project_id: the current cloud project.
credentials: the credentials to use to authorize requests.


def take_parenting(self, inst):
    if (self is inst):
        return
    for decl in inst.declarations:
        decl.parent = self
        self.declarations.append(decl)
    inst.declarations = []

Takes parenting from inst and transfers it to self.

Args:
inst (namespace_t): a namespace declaration


def project_and_occlude_texture(texture, surface, angle=DEFAULT_ANGLE):
    projected_surface = project_surface(surface, angle)
    projected_surface = _remove_hidden_parts(projected_surface)
    texture_y = map_texture_to_surface(texture, projected_surface)
    (texture_x, _) = texture
    return (texture_x, texture_y)

Projects a texture onto a surface with occluded areas removed.

Args:
texture (texture): the texture to map to the projected surface
surface (surface): the surface to project
angle (float): the angle to project at, in degrees (0 = overhead, 90 = side view)

Returns:
layer: A layer.


def AddStopTimeObject(self, stoptime, schedule=None, problems=None):
    if (schedule is None):
        schedule = self._schedule
    if (schedule is None):
        warnings.warn('No longer supported. _schedule attribute is used to get stop_times table', DeprecationWarning)
    if (problems is None):
        problems = schedule.problem_reporter
    new_secs = stoptime.GetTimeSecs()
    cursor = schedule._connection.cursor()
    cursor.execute('SELECT max(stop_sequence), max(arrival_secs), max(departure_secs) FROM stop_times WHERE trip_id=?', (self.trip_id,))
    row = cursor.fetchone()
    if (row[0] is None):
        stoptime.stop_sequence = 1
        if (new_secs == None):
            problems.OtherProblem(('No time for first StopTime of trip_id "%s"' % (self.trip_id,)))
    else:
        stoptime.stop_sequence = (row[0] + 1)
        prev_secs = max(row[1], row[2])
        if ((new_secs != None) and (new_secs < prev_secs)):
            problems.OtherProblem(('out of order stop time for stop_id=%s trip_id=%s %s < %s' % (util.EncodeUnicode(stoptime.stop_id), util.EncodeUnicode(self.trip_id), util.FormatSecondsSinceMidnight(new_secs), util.FormatSecondsSinceMidnight(prev_secs))))
    self._AddStopTimeObjectUnordered(stoptime, schedule)

Add a StopTime object to the end of this trip.

Args:
stoptime: A StopTime object. Should not be reused in multiple trips.
schedule: Schedule object containing this trip which must be
passed to Trip.__init__ or here
problems: ProblemReporter object for validating the StopTime in its new
home

Returns:
None


def apply_filter(objs, selector, mode):
    indices_to_delete = []
    presumption = (DELETE if (mode == KEEP) else KEEP)
    for (i, obj) in enumerate(objs):
        timer.log(('Applying selector: %s' % selector))
        marks = {k: mode for k in selector_to_ids(selector, obj, mode)}
        timer.log('done applying selector')
        timer.log('filtering object...')
        filter_object(obj, marks, presumption=presumption)
        timer.log('done filtering')
        if (obj is None):
            indices_to_delete.append(i)
    for index in reversed(indices_to_delete):
        del objs[index]

Apply selector to transform each object in objs.

This operates in-place on objs. Empty objects are removed from the list.

Args:
mode: either KEEP (to keep selected items & their ancestors) or DELETE
(to delete selected items and their children).


def SetTimeZone(self, time_zone):

    try:
      self._time_zone = pytz.timezone(time_zone)
    except (AttributeError, pytz.UnknownTimeZoneError):
      raise ValueError('Unsupported timezone: {0!s}'.format(time_zone))

Sets the time zone.

Args:
time_zone (str): time zone.

Raises:
ValueError: if the timezone is not supported.


def standardize_cell(cell, to_primitive=False, no_idealize=False, symprec=1e-05, angle_tolerance=(- 1.0)):
    _set_no_error()
    (lattice, _positions, _numbers, _) = _expand_cell(cell)
    if (lattice is None):
        return None
    num_atom = len(_positions)
    positions = np.zeros(((num_atom * 4), 3), dtype='double', order='C')
    positions[:num_atom] = _positions
    numbers = np.zeros((num_atom * 4), dtype='intc')
    numbers[:num_atom] = _numbers
    num_atom_std = spg.standardize_cell(lattice, positions, numbers, num_atom, (to_primitive * 1), (no_idealize * 1), symprec, angle_tolerance)
    _set_error_message()
    if (num_atom_std > 0):
        return (np.array(lattice.T, dtype='double', order='C'), np.array(positions[:num_atom_std], dtype='double', order='C'), np.array(numbers[:num_atom_std], dtype='intc'))
    else:
        return None

Return standardized cell.

Args:
cell, symprec, angle_tolerance:
See the docstring of get_symmetry.
to_primitive:
bool: If True, the standardized primitive cell is created.
no_idealize:
bool: If True,  it is disabled to idealize lengths and angles of
basis vectors and positions of atoms according to crystal
symmetry.
Return:
The standardized unit cell or primitive cell is returned by a tuple of
(lattice, positions, numbers).
If it fails, None is returned.


def _RemoveUsers(self, remove_users):

    for username in remove_users:
      self.utils.RemoveUser(username)
      self.user_ssh_keys.pop(username, None)
    self.invalid_users -= set(remove_users)

Deprovision Linux user accounts that do not appear in account metadata.

Args:
remove_users: list, the username strings of the Linux accounts to remove.


def __init__(self, regex: Optional[str]=None):
    super().__init__()
    self._regex = re.compile(regex) if regex else None

Constructor.

Args:
regex: An optional regular expression. If set to None, any string value is
acceptable.


def _format_data_list_with_options(self, data_list):
    if self._options and self._options.experimental_replication_mode == InputReplicationMode.PER_REPLICA and (not self._options.experimental_fetch_to_device):
        return [data_list]
    else:
        return data_list

Change the data in to a list type if required.

The OwnedMultiDeviceIterator returns the list data type,
while the PER_REPLICA iterator (when used with prefetch disabled)
returns without the enclosed list. This is to fix the inconsistency.
Args:
data_list: data_list
Returns:
list


def from_dict(cls, d):

        defect = MontyDecoder().process_decoded( d["defect"])
        uncorrected_energy = d["uncorrected_energy"]
        corrections = d.get("corrections", None)
        parameters = d.get("parameters", None)
        entry_id = d.get("entry_id", None)

        return cls(defect, uncorrected_energy, corrections=corrections,
                   parameters=parameters, entry_id=entry_id)

Reconstitute a DefectEntry object from a dict representation created using
as_dict().
Args:
d (dict): dict representation of DefectEntry.
Returns:
DefectEntry object


def _get_normalizations(prices, forwards, strikes, discount_factors):
    strikes_abs = tf.abs(strikes)
    forwards_abs = tf.abs(forwards)
    orientations = strikes_abs >= forwards_abs
    normalization = tf.where(orientations, strikes_abs, forwards_abs)
    normalization = tf.where(tf.equal(normalization, 0), tf.ones_like(normalization), normalization)
    normalized_prices = prices / normalization
    if discount_factors is not None:
        normalized_prices /= discount_factors
    else:
        discount_factors = tf.ones_like(normalized_prices)
    return (normalized_prices, normalization, discount_factors)

Returns the normalized prices, normalization factors, and discount_factors.

The normalization factors is the larger of strikes and forwards.
If `discount_factors` is not None, these are the discount factors to expiry.
If None, no discounting is applied and 1's are returned.

Args:
prices: A real `Tensor` of any shape. The observed market prices of the
assets.
forwards: A real `Tensor` of the same shape and dtype as `prices`. The
current forward prices to expiry.
strikes: A real `Tensor` of the same shape and dtype as `prices`. The strike
prices of the options.
discount_factors: A real `Tensor` of same dtype as the `prices`.

Returns:
the normalized prices, normalization factors, and discount_factors.


def write_version_info(filename, git_version):
    if b'"' in git_version or b'\\' in git_version:
        git_version = b'git_version_is_invalid'
    contents = '\n\n\n
    open(filename, 'w').write(contents)

Write a c file that defines the version functions.

Args:
filename: filename to write to.
git_version: the result of a git describe.


def parse_meta(self, meta):
    res = {}
    for (key, val) in meta.items():
        if (not val):
            continue
        elif isinstance(val, dict):
            res[key] = self.parse_meta(val)
        elif val.startswith('current_user.'):
            res[key] = self.get_path_attribute(current_user, val)
        elif val.startswith('original.'):
            res[key] = self.get_path_attribute(self.get_original(), val)
        else:
            res[key] = self.get_path_attribute(self, val)
    return res

Parses the meta field in the message, copies it's keys into a new
dict and replaces the values, which should be attribute paths relative
to the passed in object, with the current value at the end of that
path. This function will run recursively when it encounters other dicts
inside the meta dict.

Args:
meta (dict):
The dictionary of mappings to pull structure of the meta from.

Returns:
dict:
A copy of the keys from the meta dict with the values pulled
from the paths.


def __init__(
        self,
        indices,
        size,
        l2_regularization=0.0,
        l1_regularization=0.0,
        named_tensors=None,
        scope='embedding',
        summary_labels=()
    ):

        self.indices = indices
        self.size = size
        self.l2_regularization = l2_regularization
        self.l1_regularization = l1_regularization
        super(Embedding, self).__init__(named_tensors=named_tensors, scope=scope, summary_labels=summary_labels)

Embedding layer.

Args:
indices: Number of embedding indices.
size: Embedding size.
l2_regularization: L2 regularization weight.
l1_regularization: L1 regularization weight.


def WatchMetadata(
      self, handler, metadata_key='', recursive=True, timeout=None):

    while True:
      response = self._HandleMetadataUpdate(
          metadata_key=metadata_key, recursive=recursive, wait=True,
          timeout=timeout)
      try:
        handler(response)
      except Exception as e:
        self.logger.exception('Exception calling the response handler. %s.', e)

Watch for changes to the contents of the metadata server.

Args:
handler: callable, a function to call with the updated metadata contents.
metadata_key: string, the metadata key to watch for changes.
recursive: bool, True if we should recursively watch for metadata changes.
timeout: int, timeout in seconds for returning metadata output.


def file_path_from_hash(self, file_hash, path=None, hash_list=None):
    if (hash_list is None):
        hash_list = list(file_hash)
    if (not hash_list):
        raise IOError('Directory structure is too full!')
    if (not path):
        path = os.path.join(self.path, hash_list.pop(0))
    files = os.listdir(path)
    if (file_hash in files):
        full_path = os.path.join(path, file_hash)
        if os.path.isfile(full_path):
            return PathAndHash(path=full_path, hash=file_hash)
        return PathAndHash(path=(full_path + '/'), hash=file_hash)
    next_path = os.path.join(path, hash_list.pop(0))
    if (not os.path.exists(next_path)):
        raise IOError('File not found in the structure.')
    return self.file_path_from_hash(file_hash=file_hash, path=next_path, hash_list=hash_list)

For given `file_hash`, return path on filesystem.

Args:
file_hash (str): Hash of the file, for which you wish to know the
path.
path (str, default None): Recursion argument, don't set this.
hash_list (list, default None): Recursion argument, don't set this.

Returns:
str: Path for given `file_hash` contained in :class:`.PathAndHash`\
object.

Raises:
IOError: If the file with corresponding `file_hash` is not in \
storage.


def expand_batch_coordinates(bc, length_factor):

  assert bc.get_shape().as_list() == [1, None, 1]

  bc *= tf.constant([[1] * length_factor])

  bc = tf.reshape(bc, [1, -1, 1])

  return bc

Duplicate elements of bc by length_factor.

Args:
bc (tf.Tensor): int32 tensor of shape [1, length, 1]
length_factor (int):

Returns:
tf.Tensor: of shape [1, length*length_factor, 1] where every elements has
been duplicated length_factor times.


def Deserialize(self, reader: BinaryReader):

        super(ValidatorState, self).Deserialize(reader)
        self.PublicKey = ECDSA.Deserialize_Secp256r1(reader)
        self.Registered = reader.ReadBool()
        self.Votes = reader.ReadFixed8()

Deserialize full object.

Args:
reader (neocore.IO.BinaryReader):


def _parse_apps_to_sync(self):
    apps_to_sync = set()
    section_title = 'applications_to_sync'
    if self._parser.has_section(section_title):
        apps_to_sync = set(self._parser.options(section_title))
    return apps_to_sync

Parse the applications to backup in the config.

Returns:
set


def compute_cost(A2, Y):

    m = Y.shape[1]

    logprobs = np.multiply(np.log(A2), Y) + np.multiply(np.log(1 - A2), (1 - Y))
    cost = -np.sum(logprobs) / m

    cost = np.squeeze(cost)

    assert (isinstance(cost, float))

    return cost

Computes the cross-entropy cost given in equation (13)

Arguments:
A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
Y -- "true" labels vector of shape (1, number of examples)
parameters -- python dictionary containing your parameters W1, b1, W2 and b2

Returns:
cost -- cross-entropy cost given equation (13)


def check_imported(module):

        imported = True
        module_info = ('', '', '')

        try:
            importlib.import_module(module)
            module_info = imp.find_module(module)
        except ImportError:
            imported = False

        module_path = module_info[1]
        description = module_info[2]

        if not description:

            imported = False
        elif not description and not module_path:

            imported = False
        elif module_path is not None and (
            'dist-packages' in module_path or 'site-packages' in module_path
        ):

            imported = False
        return imported

Check whether the provide module can be imported (package installed).

Args:
module (str): The name of the module to check availability.

Returns:
bool: True if the module can be imported, False otherwise.


def ddot(L, R, left=None, out=None):
    L = asarray(L, float)
    R = asarray(R, float)
    if (left is None):
        ok = ((min(L.ndim, R.ndim) == 1) and (max(L.ndim, R.ndim) == 2))
        if (not ok):
            msg = 'Wrong array layout. One array should have'
            msg += ' ndim=1 and the other one ndim=2.'
            raise ValueError(msg)
        left = (L.ndim == 1)
    if left:
        if (out is None):
            out = copy(R)
        L = L.reshape((list(L.shape) + ([1] * (R.ndim - 1))))
        return multiply(L, R, out=out)
    else:
        if (out is None):
            out = copy(L)
        return multiply(L, R, out=out)

r"""Dot product of a matrix and a diagonal one.

Args:
L (array_like): Left matrix.
R (array_like): Right matrix.
out (:class:`numpy.ndarray`, optional): copy result to.

Returns:
:class:`numpy.ndarray`: Resulting matrix.


def resolve_pname(self, pname: PrefName,
                      mid: ModuleId) -> Tuple[YangIdentifier, ModuleId]:

        p, s, loc = pname.partition(":")
        try:
            mdata = self.modules[mid]
        except KeyError:
            raise ModuleNotRegistered(*mid) from None
        try:
            return (loc, mdata.prefix_map[p]) if s else (p, mdata.main_module)
        except KeyError:
            raise UnknownPrefix(p, mid) from None

Return the name and module identifier in which the name is defined.

Args:
pname: Name with an optional prefix.
mid: Identifier of the module in which `pname` appears.

Raises:
ModuleNotRegistered: If `mid` is not registered in the data model.
UnknownPrefix: If the prefix specified in `pname` is not declared.


def deroot(self, label='OLDROOT'):
    if (self.root.edge_length is not None):
        self.root.add_child(Node(edge_length=self.root.edge_length, label=label))
        self.root.edge_length = None

If the tree has a root edge, drop the edge to be a child of the root node

Args:
``label`` (``str``): The desired label of the new child