def add_snippet_client(self, name, package, config=None):
    if name in self._snippet_clients:
        raise Error(self, 'Name "%s" is already registered with package "%s", it cannot be used again.' % (name, self._snippet_clients[name].client.package))
    for snippet_name, client in self._snippet_clients.items():
        if package == client.package:
            raise Error(self, 'Snippet package "%s" has already been loaded under name "%s".' % (package, snippet_name))
    client = snippet_client_v2.SnippetClientV2(package=package, ad=self._device, config=config)
    client.initialize()
    self._snippet_clients[name] = client

Adds a snippet client to the management.

Args:
name: string, the attribute name to which to attach the snippet
client. E.g. `name='maps'` attaches the snippet client to
`ad.maps`.
package: string, the package name of the snippet apk to connect to.
config: snippet_client_v2.Config, the configuration object for
controlling the snippet behaviors. See the docstring of the `Config`
class for supported configurations.

Raises:
Error, if a duplicated name or package is passed in.


def _orthogonal_kernel(self, ksize, cin, cout):
    if cin > cout:
        raise ValueError(f'The number of input channels (cin={cin}) cannot exceed the number of output channels (cout={cout}).')
    orth = self._orthogonal_matrix(cout)[0:cin, :]
    if ksize == 1:
        return array_ops.expand_dims(orth, 0)
    p = self._block_orth(self._symmetric_projection(cout))
    for _ in range(ksize - 2):
        temp = self._block_orth(self._symmetric_projection(cout))
        p = self._matrix_conv(p, temp)
    for i in range(ksize):
        p[i] = math_ops.matmul(orth, p[i])
    return self._dict_to_tensor(p, ksize)

Construct orthogonal kernel for convolution.

Args:
ksize: Kernel size.
cin: Number of input channels.
cout: Number of output channels.

Returns:
An [ksize, ksize, cin, cout] orthogonal kernel.
Raises:
ValueError: If cin > cout.


def monte_carlo_standard_error(chain, batch_size_generator=None, compute_method=None):
    batch_size_generator = (batch_size_generator or SquareRootSingleBatch())
    compute_method = (compute_method or BatchMeansMCSE())
    batch_sizes = batch_size_generator.get_univariate_ess_batch_sizes(len(chain))
    return np.min(list((compute_method.compute_standard_error(chain, b) for b in batch_sizes)))

Compute Monte Carlo standard errors for the expectations

This is a convenience function that calls the compute method for each batch size and returns the lowest ESS
over the used batch sizes.

Args:
chain (ndarray): the Markov chain
batch_size_generator (UniVariateESSBatchSizeGenerator): the method that generates that batch sizes
we will use. Per default it uses the :class:`SquareRootSingleBatch` method.
compute_method (ComputeMonteCarloStandardError): the method used to compute the standard error.
By default we will use the :class:`BatchMeansMCSE` method


def __init__(self, port_no=PortNo.OFPP_ANY):

        super().__init__()
        self.port_no = port_no

Create a PortStatsRequest with the optional parameters below.

Args:
port_no (:class:`int`, :class:`~pyof.v0x04.common.port.PortNo`):
:attr:`StatsType.OFPST_PORT` message must request statistics
either for a single port (specified in ``port_no``) or for all
ports (if ``port_no`` == :attr:`.PortNo.OFPP_ANY`).


def sh(self, cmd, ignore_error=False, cwd=None, shell=False, **kwargs):

        kwargs.update({
            'shell': shell,
            'cwd': cwd or self.fpath,
            'stderr': subprocess.STDOUT,
            'stdout': subprocess.PIPE,
            'ignore_error': ignore_error})
        log.debug((('cmd', cmd), ('kwargs', kwargs)))
        return sh(cmd, **kwargs)

Run a command with the current working directory set to self.fpath

Args:
cmd (str or tuple): cmdstring or listlike

Keyword Arguments:
ignore_error (bool): if False, raise an Exception if p.returncode is
not 0
cwd (str): current working dir to run cmd with
shell (bool): subprocess.Popen ``shell`` kwarg

Returns:
str: stdout output of wrapped call to ``sh`` (``subprocess.Popen``)


def auto_convert_string_cell(flagable, cell_str, position, worksheet, flags,
                             units, parens_as_neg=True):

    conversion = cell_str.strip()

    if re.search(allregex.control_wrapping_regex, cell_str):

        stripped_cell = cell_str.strip()
        mod_cell_str = stripped_cell[1:][:-1].strip()
        neg_mult = False

        if (stripped_cell[0] == '(' and stripped_cell[-1] == ')' and
                re.search(allregex.contains_numerical_regex, mod_cell_str)):

            neg_mult = True
        flagable.flag_change(flags, 'interpreted', position, worksheet,
                            flagable.FLAGS['removed-wrapping'])

        converted_value = auto_convert_cell(flagable, mod_cell_str, position,
                                            worksheet, flags, units)
        neg_mult = neg_mult and check_cell_type(converted_value, get_cell_type(0))
        if neg_mult and parens_as_neg:
            flagable.flag_change(flags, 'interpreted', position, worksheet,
                                 flagable.FLAGS['converted-wrapping-to-neg'])
        return -converted_value if neg_mult else converted_value

    elif re.search(allregex.contains_numerical_regex, cell_str):
        conversion = auto_convert_numeric_string_cell(flagable, conversion, position,
                                                      worksheet, flags, units)
    elif re.search(allregex.bool_regex, cell_str):
        flagable.flag_change(flags, 'interpreted', position, worksheet,
                             flagable.FLAGS['bool-to-int'])
        conversion = 1 if re.search(allregex.true_bool_regex, cell_str) else 0

    return conversion

Handles the string case of cell and attempts auto-conversion
for auto_convert_cell.

Args:
parens_as_neg: Converts numerics surrounded by parens to negative values


def deserialize_skycoord(d):

    if 'distance' in d:
        args = (d['lon'], d['lat'], d['distance'])
    else:
        args = (d['lon'], d['lat'])

    return coords.SkyCoord(
        *args,
        frame=d['frame'],
        representation='spherical')

Deserializes a JSONified :obj:`astropy.coordinates.SkyCoord`.

Args:
d (:obj:`dict`): A dictionary representation of a :obj:`SkyCoord` object.

Returns:
A :obj:`SkyCoord` object.


def reset_sequence(self, topic):
    if (topic in self.queues):
        self.queues[topic].reset()

Reset the expected sequence number for a topic

If the topic is unknown, this does nothing.  This behaviour is
useful when you have wildcard topics that only create queues
once they receive the first message matching the topic.

Args:
topic (string): The topic to reset the packet queue on


def describe(self, req=None, resp=None, **kwargs):
    description = {'params': OrderedDict([(name, param.describe()) for (name, param) in self.params.items()]), 'details': inspect.cleandoc((self.__class__.__doc__ or 'This resource does not have description yet')), 'name': self.__class__.__name__, 'methods': self.allowed_methods()}
    if req:
        description['path'] = req.path
    description.update(**kwargs)
    return description

Describe API resource using resource introspection.

Additional description on derrived resource class can be added using
keyword arguments and calling ``super().decribe()`` method call
like following:

.. code-block:: python

class SomeResource(BaseResource):
def describe(req, resp, **kwargs):
return super().describe(
req, resp, type='list', **kwargs
)

Args:
req (falcon.Request): request object
resp (falcon.Response): response object
kwargs (dict): dictionary of values created from resource url
template

Returns:
dict: dictionary with resource descritpion information

.. versionchanged:: 0.2.0
The `req` and `resp` parameters became optional to ease the
implementation of application-level documentation generators.


def call(self, inputs, state, **kwargs):
    return self._call_wrapped_cell(inputs, state, cell_call_fn=self.cell.call, **kwargs)

Runs the RNN cell step computation.

When `call` is being used, we assume that the wrapper object has been built,
and therefore the wrapped cells has been built via its `build` method and
its `call` method can be used directly.

This allows to use the wrapped cell and the non-wrapped cell equivalently
when using `call` and `build`.

Args:
inputs: A tensor with wrapped cell's input.
state: A tensor or tuple of tensors with wrapped cell's state.
**kwargs: Additional arguments passed to the wrapped cell's `call`.

Returns:
A pair containing:

- Output: A tensor with cell's output.
- New state: A tensor or tuple of tensors with new wrapped cell's state.


def convert_to_beam_type(typ):
    if (sys.version_info.major == 3 and sys.version_info.minor >= 10) and isinstance(typ, types.UnionType):
        typ = typing.Union[typ]
    if getattr(typ, '__module__', None) == 'typing':
        typ = convert_typing_to_builtin(typ)
    typ_module = getattr(typ, '__module__', None)
    if isinstance(typ, typing.TypeVar):
        if id(typ) not in _type_var_cache:
            new_type_variable = typehints.TypeVariable(typ.__name__)
            _type_var_cache[id(typ)] = new_type_variable
            _type_var_cache[id(new_type_variable)] = typ
        return _type_var_cache[id(typ)]
    elif isinstance(typ, str):
        _LOGGER.info('Converting string literal type hint to Any: "%s"', typ)
        return typehints.Any
    elif sys.version_info >= (3, 10) and isinstance(typ, typing.NewType):
        _LOGGER.info('Converting NewType type hint to Any: "%s"', typ)
        return typehints.Any
    elif typ_module == 'apache_beam.typehints.native_type_compatibility' and getattr(typ, '__name__', typ.__origin__.__name__) == 'TypedWindowedValue':
        pass
    elif is_typeddict(typ):
        return typehints.Dict[str, typehints.Any]
    elif typ_module not in _CONVERTED_MODULES and (not is_builtin(typ)):
        return typ
    if typ_module == 'collections.abc' and getattr(typ, '__origin__', typ) not in _CONVERTED_COLLECTIONS:
        return typ
    type_map = [_TypeMapEntry(match=is_new_type, arity=0, beam_type=typehints.Any), _TypeMapEntry(match=is_forward_ref, arity=0, beam_type=typehints.Any), _TypeMapEntry(match=is_any, arity=0, beam_type=typehints.Any), _TypeMapEntry(match=_match_is_dict, arity=2, beam_type=typehints.Dict), _TypeMapEntry(match=_match_is_exactly_iterable, arity=1, beam_type=typehints.Iterable), _TypeMapEntry(match=_match_is_primitive(list), arity=1, beam_type=typehints.List), _TypeMapEntry(match=_match_is_primitive(frozenset), arity=1, beam_type=typehints.FrozenSet), _TypeMapEntry(match=_match_is_set, arity=1, beam_type=typehints.Set), _TypeMapEntry(match=match_is_named_tuple, arity=0, beam_type=typehints.Any), _TypeMapEntry(match=_match_is_primitive(tuple), arity=-1, beam_type=typehints.Tuple), _TypeMapEntry(match=_match_is_union, arity=-1, beam_type=typehints.Union), _TypeMapEntry(match=_match_issubclass(collections.abc.Generator), arity=3, beam_type=typehints.Generator), _TypeMapEntry(match=_match_issubclass(collections.abc.Iterator), arity=1, beam_type=typehints.Iterator), _TypeMapEntry(match=_match_is_exactly_collection, arity=1, beam_type=typehints.Collection), _TypeMapEntry(match=_match_issubclass(TypedWindowedValue), arity=1, beam_type=typehints.WindowedValue), _TypeMapEntry(match=_match_is_exactly_sequence, arity=1, beam_type=typehints.Sequence), _TypeMapEntry(match=_match_is_exactly_mapping, arity=2, beam_type=typehints.Mapping)]
    matched_entry = next((entry for entry in type_map if entry.match(typ)), None)
    if not matched_entry:
        _LOGGER.info('Using Any for unsupported type: %s', typ)
        return typehints.Any
    args = _get_args(typ)
    len_args = len(args)
    if len_args == 0 and len_args != matched_entry.arity:
        arity = matched_entry.arity
        if _match_issubclass(typing.Tuple)(typ):
            args = (typehints.TypeVariable('T'), Ellipsis)
        elif _match_is_union(typ):
            raise ValueError('Unsupported Union with no arguments.')
        elif _match_issubclass(typing.Generator)(typ):
            args = (typehints.TypeVariable('T_co'), type(None), type(None))
        elif _match_issubclass(typing.Dict)(typ):
            args = (typehints.TypeVariable('KT'), typehints.TypeVariable('VT'))
        elif _match_issubclass(typing.Iterator)(typ) or _match_is_exactly_iterable(typ):
            args = (typehints.TypeVariable('T_co'),)
        else:
            args = (typehints.TypeVariable('T'),) * arity
    elif matched_entry.arity == -1:
        arity = len_args
    elif len_args == 1 and _safe_issubclass(getattr(typ, '__origin__', typ), collections.Counter):
        args = (args[0], int)
        len_args = 2
        arity = matched_entry.arity
    else:
        arity = matched_entry.arity
        if len_args != arity:
            raise ValueError('expecting type %s to have arity %d, had arity %d instead' % (str(typ), arity, len_args))
    typs = convert_to_beam_types(args)
    if arity == 0:
        return matched_entry.beam_type
    elif arity == 1:
        return matched_entry.beam_type[typs[0]]
    else:
        return matched_entry.beam_type[tuple(typs)]

Convert a given typing type to a Beam type.

Args:
typ (`typing.Union[type, str]`): typing type or string literal representing
a type.

Returns:
type: The given type converted to a Beam type as far as we can do the
conversion.

Raises:
ValueError: The type was malformed.


def vmstat(stat):

    out = subprocess.check_output(["vmstat", "-s"])
    stat = stat.encode("ascii")
    for line in out.split(b"\n"):
        line = line.strip()
        if stat in line:
            return int(line.split(b" ")[0])
    raise ValueError("Can't find {} in 'vmstat' output.".format(stat))

Run vmstat and get a particular statistic.

Args:
stat: The statistic that we are interested in retrieving.

Returns:
The parsed output.


def get_factors_iterative1(n):

    todo, res = [(n, 2, [])], []
    while todo:
        n, i, combi = todo.pop()
        while i * i <= n:
            if n % i == 0:
                res += combi + [i, n
                todo.append((n
            i += 1
    return res

[summary]
Computes all factors of n.
Translated the function get_factors(...) in
a call-stack modell.

Arguments:
n {[int]} -- [to analysed number]

Returns:
[list of lists] -- [all factors]


def _create_make_unique(inputs):

  if inputs.shape.ndims != 2:
    raise ValueError("Input of top_k_with_unique must be rank-2 "
                     "but got: %s" % inputs.shape)

  height = inputs.shape[0]
  width = inputs.shape[1]
  zeros = tf.zeros([height, width], dtype=tf.int32)

  log2_ceiling = int(math.ceil(math.log(int(width), 2)))
  next_power_of_two = 1 << log2_ceiling
  count_mask = ~(next_power_of_two - 1)
  count_mask_r0 = tf.constant(count_mask)
  count_mask_r2 = tf.fill([height, width], count_mask_r0)

  smallest_normal = 1 << 23
  smallest_normal_r0 = tf.constant(smallest_normal, dtype=tf.int32)
  smallest_normal_r2 = tf.fill([height, width], smallest_normal_r0)

  low_bit_mask = ~(1 << 31)
  low_bit_mask_r0 = tf.constant(low_bit_mask, dtype=tf.int32)
  low_bit_mask_r2 = tf.fill([height, width], low_bit_mask_r0)

  iota = tf.tile(tf.expand_dims(tf.range(width, dtype=tf.int32), 0),
                 [height, 1])

  input_r2 = tf.bitcast(inputs, tf.int32)
  abs_r2 = tf.bitwise.bitwise_and(input_r2, low_bit_mask_r2)
  if_zero_r2 = tf.equal(abs_r2, zeros)
  smallest_normal_preserving_sign_r2 = tf.bitwise.bitwise_or(
      input_r2, smallest_normal_r2)
  input_no_zeros_r2 = tf.where(
      if_zero_r2, smallest_normal_preserving_sign_r2, input_r2)

  and_r2 = tf.bitwise.bitwise_and(input_no_zeros_r2, count_mask_r2)
  or_r2 = tf.bitwise.bitwise_or(and_r2, iota)
  return tf.bitcast(or_r2, tf.float32)

Replaces the lower bits of each element with iota.

The iota is used to derive the index, and also serves the purpose to
make each element unique to break ties.

Args:
inputs: A tensor with rank of 2 and dtype of tf.float32.
[batch_size, original_size].

Returns:
A tensor after element wise transformation, with dtype the same as inputs.
[batch_size, original_size].

Raises:
ValueError: If the rank of the input tensor does not equal 2.


def from_rfc3339(rfc3339_text, with_nanos=False):
    timestamp = strict_rfc3339.rfc3339_to_timestamp(rfc3339_text)
    result = datetime.datetime.utcfromtimestamp(timestamp)
    if with_nanos:
        return (result, int(((timestamp - int(timestamp)) * 1000000000.0)))
    else:
        return result

Parse a RFC 3339 date string format to datetime.date.

Example of accepted format: '1972-01-01T10:00:20.021-05:00'

- By default, the result is a datetime.datetime
- If with_nanos is true, the result is a 2-tuple, (datetime.datetime,
nanos), where the second field represents the possible nanosecond
resolution component of the second field.

Args:
rfc3339_text (string): An rfc3339 formatted date string
with_nanos (bool): Determines if nanoseconds should be parsed from the
string

Raises:
ValueError: if ``rfc3339_text`` is invalid

Returns:
:class:`datetime.datetime`: when with_nanos is False
tuple(:class:`datetime.datetime`, int): when with_nanos is True


def assert_present(self, selector, testid=None, **kwargs):

        self.info_log(
            "Assert present selector(%s) testid(%s)" % (selector, testid)
        )

        wait_until_present = kwargs.get(
            'wait_until_present',
            BROME_CONFIG['proxy_driver']['wait_until_present_before_assert_present']
        )
        self.debug_log(
            "effective wait_until_present: %s" % wait_until_present
        )

        if wait_until_present:
            element = self.wait_until_present(selector, raise_exception=False)
        else:
            element = self.is_present(selector)

        if element:
            if testid is not None:
                self.create_test_result(testid, True)

            return True
        else:
            if testid is not None:
                self.create_test_result(testid, False)

            return False

Assert that the element is present in the dom

Args:
selector (str): the selector used to find the element
test_id (str): the test_id or a str

Kwargs:
wait_until_present (bool)

Returns:
bool: True is the assertion succeed; False otherwise.


def bootstrap_results(self, state):

    def loss():
        q = self._flattened_variational_distribution()
        samples = q.sample(self.train_batch_size)
        return tf.reduce_mean(input_tensor=(q.log_prob(samples) - self._flattened_target_log_prob(samples)), axis=(- 1))
    lr = tf.convert_to_tensor(value=self.learning_rate, dtype=self._dtype)
    dtype = lr.dtype
    learning_rate = tf.compat.v2.optimizers.schedules.PiecewiseConstantDecay(list((self.num_train_steps * np.array([0.2, 0.8]).astype(dtype.as_numpy_dtype()))), [lr, (lr * 0.1), (lr * 0.01)])
    opt = tf.compat.v2.optimizers.Adam(learning_rate)

    @tf.function(autograph=False)
    def train_step():
        with tf.GradientTape() as tape:
            loss_val = loss()
        vals = tape.watched_variables()
        grads = tape.gradient(loss_val, vals)
        grads_and_vals = list(zip(grads, vals))
        opt.apply_gradients(grads_and_vals)
        return loss_val
    for step in range(self.num_train_steps):
        loss_val = train_step()
        tf.debugging.assert_all_finite(loss_val, 'NeuTra loss is NaN at step {}'.format(step))
        if self.train_debug_fn:
            self.train_debug_fn(self, step, loss_val)
    state_parts = tf.nest.flatten(state)
    flat_state_shapes = tf.nest.flatten(self.state_shape)
    batch_shape = tf.shape(input=state_parts[0])[:(- flat_state_shapes[0].ndims)]
    return self._kernel.bootstrap_results(self._flattened_variational_distribution().sample(batch_shape, seed=self.seed))

Trains the bijector and creates initial `previous_kernel_results`.

The supplied `state` is only used to determine the number of chains to run
in parallel_iterations

Args:
state: `Tensor` or Python `list` of `Tensor`s representing the initial
state(s) of the Markov chain(s). The first `r` dimensions index
independent chains, `r = tf.rank(target_log_prob_fn(*state))`.

Returns:
kernel_results: Instance of
`UncalibratedHamiltonianMonteCarloKernelResults` inside
`MetropolisHastingsResults` inside `TransformedTransitionKernelResults`
inside `SimpleStepSizeAdaptationResults`.


def __init__(self, tokenizer=None, trie=None):

        pyee.EventEmitter.__init__(self)
        self.tokenizer = tokenizer or EnglishTokenizer()
        self.trie = trie or Trie()
        self.regular_expressions_entities = []
        self._regex_strings = set()
        self.tagger = EntityTagger(self.trie, self.tokenizer, self.regular_expressions_entities)
        self.intent_parsers = []

Initialize the IntentDeterminationEngine

Args:
tokenizer(tokenizer) : tokenizer used to break up spoken text
example EnglishTokenizer()
trie(Trie): tree of matches to Entites


def _GetNetworkInfo(self, signatures_key):
    network_info = {}
    for category in signatures_key.GetSubkeys():
        for signature in category.GetSubkeys():
            profile_guid_value = signature.GetValueByName('ProfileGuid')
            if profile_guid_value:
                profile_guid = profile_guid_value.GetDataAsObject()
            else:
                continue
            default_gateway_mac_value = signature.GetValueByName('DefaultGatewayMac')
            if default_gateway_mac_value:
                default_gateway_mac = ':'.join(['{0:02x}'.format(octet) for octet in bytearray(default_gateway_mac_value.data)])
            else:
                default_gateway_mac = None
            dns_suffix_value = signature.GetValueByName('DnsSuffix')
            if dns_suffix_value:
                dns_suffix = dns_suffix_value.GetDataAsObject()
            else:
                dns_suffix = None
            network_info[profile_guid] = (default_gateway_mac, dns_suffix)
    return network_info

Retrieves the network info within the signatures subkey.

Args:
signatures_key (dfwinreg.WinRegistryKey): a Windows Registry key.

Returns:
dict[str, tuple]: a tuple of default_gateway_mac and dns_suffix per
profile identifier (GUID).


def get_op_consumers(self, src_op_name):
    return self._op_consumers[src_op_name]

Get all the downstream consumers of this op.

Only data (non-control) edges are tracked.

Args:
src_op_name: Name of the op providing the tensor being consumed.

Returns:
A list of (src_slot, dst_op_name, dst_slot) tuples. In each item of
the list:
src_slot: 0-based output slot of the op of which the output tensor
is being consumed.
dst_op_name: Name of the consuming op (e.g., "Conv2D_3/BiasAdd")
dst_slot: 0-based input slot of the consuming op that receives
the tensor from this op.


def loads(string, triples=False, cls=PENMANCodec, **kwargs):

    codec = cls(**kwargs)
    return list(codec.iterdecode(string, triples=triples))

Deserialize a list of PENMAN-encoded graphs from *string*.

Args:
string: a string containing graph data
triples: if True, read graphs as triples instead of as PENMAN
cls: serialization codec class
kwargs: keyword arguments passed to the constructor of *cls*
Returns:
a list of Graph objects


def _build(self, ids):
    if (self._existing_vocab is None):
        if (self.EMBEDDINGS not in self._initializers):
            self._initializers[self.EMBEDDINGS] = tf.initializers.random_normal()
        self._embeddings = tf.get_variable('embeddings', shape=[self._vocab_size, self._embed_dim], dtype=tf.float32, initializer=self._initializers[self.EMBEDDINGS], partitioner=self._partitioners.get(self.EMBEDDINGS, None), regularizer=self._regularizers.get(self.EMBEDDINGS, None), trainable=self._trainable)
    else:
        self._embeddings = tf.get_variable('embeddings', dtype=tf.float32, initializer=self._existing_vocab, regularizer=self._regularizers.get(self.EMBEDDINGS, None), trainable=self._trainable)
    if self._densify_gradients:
        embeddings = util.convert_gradient_to_tensor(self._embeddings)
    else:
        embeddings = self._embeddings
    return tf.nn.embedding_lookup(embeddings, ids, name='embedding_lookup')

Lookup embeddings.

Looks up an embedding vector for each value in `ids`. All ids must be within
[0, vocab_size), else an `InvalidArgumentError` is raised at runtime.

Args:
ids: Tensor of dtype int64.

Returns:
Tensor of tf.shape(ids) + [embedding_dim] and dtype float32.


def Detect(self, str_in):

    components = SplitIntoComponents(str_in)

    extracted_paths = set()
    for extractor in self.extractors:
      extracted_paths.update(extractor.Extract(components))

    results = set(extracted_paths)

    for post_processor in self.post_processors:
      processed_results = set()
      for result in results:
        processed_results.update(post_processor.Process(result))
      results = processed_results

    return results

Detects paths in a given string.

Args:
str_in: String where the paths should be detected.

Returns:
A list of paths (as strings) detected inside the given string.


def get_target(self, target):
    if (target not in self._target_cache):
        self._target_cache[target] = self._get_target(target)
    return self._target_cache[target]

Get the result of _get_target, cache it and return it.

Args:
target (str): target to find.

Returns:
Package/Module: package containing target or corresponding module.


def GetUserinfo(credentials, http=None):
    http = (http or httplib2.Http())
    url = _GetUserinfoUrl(credentials)
    (response, content) = http.request(url)
    if (response.status == http_client.BAD_REQUEST):
        credentials.refresh(http)
        url = _GetUserinfoUrl(credentials)
        (response, content) = http.request(url)
    return json.loads((content or '{}'))

Get the userinfo associated with the given credentials.

This is dependent on the token having either the userinfo.email or
userinfo.profile scope for the given token.

Args:
credentials: (oauth2client.client.Credentials) incoming credentials
http: (httplib2.Http, optional) http instance to use

Returns:
The email address for this token, or None if the required scopes
aren't available.


def moment_by_moment_schedule(device: Device, circuit: Circuit):
    schedule = Schedule(device)
    t = Timestamp()
    for moment in circuit:
        if (not moment.operations):
            continue
        for op in moment.operations:
            scheduled_op = ScheduledOperation.op_at_on(op, t, device)
            schedule.include(scheduled_operation=scheduled_op)
            device.validate_scheduled_operation(schedule, scheduled_op)
        max_duration = max((device.duration_of(op) for op in moment.operations))
        t += max_duration
    return schedule

Returns a schedule aligned with the moment structure of the Circuit.

This method attempts to create a schedule in which each moment of a circuit
is scheduled starting at the same time. Given the constraints of the
given device, such a schedule may not be possible, in this case the
the method will raise a ValueError with a description of the conflict.

The schedule that is produced will take each moments and schedule the
operations in this moment in a time slice of length equal to the maximum
time of an operation in the moment.

Returns:
A Schedule for the circuit.

Raises:
ValueError: if the scheduling cannot be done.


def retry_api_check(exception):
    if isinstance(exception, apiclient.errors.HttpError):
        if (exception.resp.status in TRANSIENT_HTTP_ERROR_CODES):
            _print_error('Retrying...')
            return True
    if isinstance(exception, socket.error):
        if (exception.errno in TRANSIENT_SOCKET_ERROR_CODES):
            _print_error('Retrying...')
            return True
    if isinstance(exception, oauth2client.client.AccessTokenRefreshError):
        _print_error('Retrying...')
        return True
    if isinstance(exception, SSLError):
        _print_error('Retrying...')
        return True
    if isinstance(exception, ServerNotFoundError):
        _print_error('Retrying...')
        return True
    return False

Return True if we should retry. False otherwise.

Args:
exception: An exception to test for transience.

Returns:
True if we should retry. False otherwise.


def pickle_load(cls, filepath):

        if os.path.isdir(filepath):

            for dirpath, dirnames, filenames in os.walk(filepath):
                fnames = [f for f in filenames if f == cls.PICKLE_FNAME]
                if fnames:
                    if len(fnames) == 1:
                        filepath = os.path.join(dirpath, fnames[0])
                        break
                    else:
                        err_msg = "Found multiple databases:\n %s" % str(fnames)
                        raise RuntimeError(err_msg)
            else:
                err_msg = "Cannot find %s inside directory %s" % (cls.PICKLE_FNAME, filepath)
                raise ValueError(err_msg)

        with open(filepath, "rb") as fh:
            new = pickle.load(fh)

        from .flows import Flow
        flow_workdirs, new.flows = new.flows, []
        for flow in map(Flow.pickle_load, flow_workdirs):
            new.add_flow(flow)

        return new

Loads the object from a pickle file.

Args:
filepath: Filename or directory name. It filepath is a directory, we
scan the directory tree starting from filepath and we
read the first pickle database. Raise RuntimeError if multiple
databases are found.


def PublishEvent(cls, event_name, msg, token=None):
    cls.PublishMultipleEvents({event_name: [msg]}, token=token)

Publish the message into all listeners of the event.

We send the message to all event handlers which contain this
string in their EVENT static member. This allows the event to be
sent to multiple interested listeners.

Args:
event_name: An event name.
msg: The message to send to the event handler.
token: ACL token.

Raises:
ValueError: If the message is invalid. The message must be a Semantic
Value (instance of RDFValue) or a full GrrMessage.


def reshard(self, checkpoint_values: List[tensor.Tensor], shape_and_slice_spec: List[str]) -> tensor.Tensor:
    del shape_and_slice_spec
    if len(checkpoint_values) != 1:
        raise ValueError('Default reshard expects a single checkpoint value.')
    return checkpoint_values[0]

Reshards the checkpoint values as read from the checkpoint file.

Override this to reshard/modify the restored values
Args:
checkpoint_values: The values returned by the restore op, as read from
file.
shape_and_slice_spec: The shape and slice spec required by the caller.

Returns:
List of restored Tensor values after being resharded.


def consume(self, msg):
    msg['body'] = crypto.sign(msg['body'], **self.hub.config)
    super(SigningRelayConsumer, self).consume(msg)

Sign the message prior to sending the message.

Args:
msg (dict): The message to sign and relay.


def Validate(self, value):
    if (value is None):
        return
    if (not isinstance(value, self.rdfclass)):
        try:
            return self.rdfclass(value)
        except rdfvalue.InitializeError:
            raise TypeValueError(('Value for arg %s should be an %s' % (self.name, self.rdfclass.__name__)))
    return value

Validate an RDFValue instance.

Args:
value: An RDFValue instance or something which may be used to instantiate
the correct instance.

Raises:
TypeValueError: If the value is not a valid RDFValue instance or the
required type.

Returns:
A Valid RDFValue instance.


def compile_state_invariants(self,
            state: Sequence[tf.Tensor]) -> List[TensorFluent]:

        scope = self.state_invariant_scope(state)
        invariants = []
        with self.graph.as_default():
            with tf.name_scope('state_invariants'):
                for p in self.rddl.domain.invariants:
                    fluent = self._compile_expression(p, scope)
                    invariants.append(fluent)
                return invariants

Compiles the state invarints given current `state` fluents.

Args:
state (Sequence[tf.Tensor]): The current state fluents.

Returns:
A list of :obj:`rddl2tf.fluent.TensorFluent`.


def labels2onehot(labels: [List[str], List[List[str]], np.ndarray], classes: [list, np.ndarray]) -> np.ndarray:
    n_classes = len(classes)
    y = []
    for sample in labels:
        curr = np.zeros(n_classes)
        if isinstance(sample, list):
            for intent in sample:
                if (intent not in classes):
                    log.warning('Unknown intent {} detected. Assigning no class'.format(intent))
                else:
                    curr[np.where((np.array(classes) == intent))[0]] = 1
        else:
            curr[np.where((np.array(classes) == sample))[0]] = 1
        y.append(curr)
    y = np.asarray(y)
    return y

Convert labels to one-hot vectors for multi-class multi-label classification

Args:
labels: list of samples where each sample is a class or a list of classes which sample belongs with
classes: array of classes' names

Returns:
2d array with one-hot representation of given samples


def db_wb020(self, value=None):

        if value is not None:
            try:
                value = float(value)
            except ValueError:
                raise ValueError('value {} need to be of type float '
                                 'for field `db_wb020`'.format(value))

        self._db_wb020 = value

Corresponds to IDD Field `db_wb020`
mean coincident dry-bulb temperature to
Wet-bulb temperature corresponding to 2.0% annual cumulative frequency of occurrence

Args:
value (float): value for IDD Field `db_wb020`
Unit: C
if `value` is None it will not be checked against the
specification and is assumed to be a missing value

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


def _log_normalization(self, name='log_normalization'):
    with tf.name_scope((name or 'log_normalization_lkj')):
        logpi = np.log(np.pi)
        ans = tf.zeros_like(self.concentration)
        for k in range(1, self.dimension):
            ans += (logpi * (k / 2.0))
            ans += tf.math.lgamma((self.concentration + (((self.dimension - 1) - k) / 2.0)))
            ans -= tf.math.lgamma((self.concentration + ((self.dimension - 1) / 2.0)))
        return ans

Returns the log normalization of an LKJ distribution.

Args:
name: Python `str` name prefixed to Ops created by this function.

Returns:
log_z: A Tensor of the same shape and dtype as `concentration`, containing
the corresponding log normalizers.


def add_behaviour(self, behaviour, template=None):

        behaviour.set_agent(self)
        if issubclass(type(behaviour), FSMBehaviour):
            for _, state in behaviour.get_states().items():
                state.set_agent(self)
        behaviour.set_template(template)
        self.behaviours.append(behaviour)
        if self.is_alive():
            behaviour.start()

Adds and starts a behaviour to the agent.
If template is not None it is used to match
new messages and deliver them to the behaviour.

Args:
behaviour (spade.behaviour.CyclicBehaviour): the behaviour to be started
template (spade.template.Template, optional): the template to match messages with (Default value = None)


def train_on_batch(self, data: List[Iterable], labels: Iterable[list]) -> None:

        X, Y = self._transform_batch(data, labels)
        self.model_.train_on_batch(X, Y)

Trains model on a single batch

Args:
data: a batch of word sequences
labels: a batch of correct tag sequences
Returns:
the trained model


def qubo_circuit(
        graph: nx.Graph,
        steps: int,
        beta: Sequence,
        gamma: Sequence) -> Circuit:

    qubits = list(graph.nodes())

    circ = Circuit()
    for q0 in qubits:
        circ += H(q0)

    for p in range(0, steps):

        for q0, q1 in graph.edges():
            weight = graph[q0][q1].get('weight', 1.0)

            circ += ZZ(-weight * gamma[p] / np.pi, q0, q1)

        for q0 in qubits:
            node_weight = graph.nodes[q0].get('weight', None)
            if node_weight is not None:
                circ += RZ(node_weight, q0)

        for q0 in qubits:
            circ += RX(beta[p], q0)

    return circ

A QAOA circuit for the Quadratic Unconstrained Binary Optimization
problem (i.e. an Ising model).

Args:
graph : a networkx graph instance with optional edge and node weights
steps : number of QAOA steps
beta  : driver parameters (One per step)
gamma : cost parameters (One per step)


def support_set(self):
    roots = set()
    if self.has_attr():
        roots.update(self.parent.support_set)
    elif self.has_subscript():
        roots.update(self.parent.support_set)
        roots.update(self.qn[1].support_set)
    else:
        roots.add(self)
    return roots

Returns the set of simple symbols that this QN relies on.

This would be the smallest set of symbols necessary for the QN to
statically resolve (assuming properties and index ranges are verified
at runtime).

Examples:
'a.b' has only one support symbol, 'a'
'a[i]' has two support symbols, 'a' and 'i'


def set_sleep_timer(self, sleep_time_seconds):

        try:
            if sleep_time_seconds is None:
                sleep_time = ''
            else:
                sleep_time = format(
                    datetime.timedelta(seconds=int(sleep_time_seconds))
                )
            self.avTransport.ConfigureSleepTimer([
                ('InstanceID', 0),
                ('NewSleepTimerDuration', sleep_time),
            ])
        except SoCoUPnPException as err:
            if 'Error 402 received' in str(err):
                raise ValueError('invalid sleep_time_seconds, must be integer \
                    value between 0 and 86399 inclusive or None')
            raise
        except ValueError:
            raise ValueError('invalid sleep_time_seconds, must be integer \
                value between 0 and 86399 inclusive or None')

Sets the sleep timer.

Args:
sleep_time_seconds (int or NoneType): How long to wait before
turning off speaker in seconds, None to cancel a sleep timer.
Maximum value of 86399

Raises:
SoCoException: Upon errors interacting with Sonos controller
ValueError: Argument/Syntax errors


def save(self, path):
    data = self.encode()
    with open(path, 'wb') as out:
        out.write(data)

Save a binary copy of this report

Args:
path (string): The path where we should save the binary copy of the report


def debase64(byte_str):

    if isinstance(byte_str, str) and not PYTHON2:
        byte_str = bytes(byte_str, 'utf-8')
    return base64.b64decode(byte_str)

Decode base64 encoded bytes/strings.

Args:
- ``byte_str``:  The string or bytes to base64 encode.

Returns:
- decoded string as type str for python2 and type byte for python3.


def validate_seeded_answers_simple(answers, options, algo):

    seen_options = {}
    for answer in answers:
        if answer:
            key = options[answer['answer']].get('text')
            if options[answer['answer']].get('image_url'):
                key += options[answer['answer']].get('image_url')
            seen_options.setdefault(key, 0)
            seen_options[key] += 1

    missing_options = []
    index = 1
    for option in options:
        key = option.get('text') + option.get('image_url') if option.get('image_url') else option.get('text')
        if option.get('text') != 'n/a':
            if seen_options.get(key, 0) == 0:
                missing_options.append(_('Option ') + str(index))
            index += 1

    if missing_options:
        return {'seed_error': _('Missing option seed(s): ') + ', '.join(missing_options)}

    return None

This validator checks if the answers includes all possible options

Args:
answers (str): the answers to be checked
options (dict): all options that should exist in the answers
algo (str): selection algorithm

Returns:
None if everything is good. Otherwise, the missing option error message.


def block_matrix(A, B, C, D):
    return vstackm((hstackm((A, B)), hstackm((C, D))))

r"""Generate the operator matrix with quadrants

.. math::

\begin{pmatrix} A B \\ C D \end{pmatrix}

Args:
A (Matrix): Matrix of shape ``(n, m)``
B (Matrix): Matrix of shape ``(n, k)``
C (Matrix): Matrix of shape ``(l, m)``
D (Matrix): Matrix of shape ``(l, k)``

Returns:
Matrix: The combined block matrix ``[[A, B], [C, D]]``.


def _get_elmt_amt_in_rxt(self, rxt):
    return sum([rxt.get_el_amount(e) for e in self.pd.elements])

Computes total number of atoms in a reaction formula for elements
not in external reservoir. This method is used in the calculation
of reaction energy per mol of reaction formula.

Args:
rxt (Reaction): a reaction.

Returns:
Total number of atoms for non_reservoir elements.


def set_size(a, validate_indices=True):
    a = sparse_tensor.convert_to_tensor_or_sparse_tensor(a, name='a')
    if not isinstance(a, sparse_tensor.SparseTensor):
        raise TypeError('Expected `SparseTensor`, got %s.' % a)
    if a.values.dtype.base_dtype not in _VALID_DTYPES:
        raise TypeError(f'Invalid dtype `{a.values.dtype}` not in supported dtypes: `{_VALID_DTYPES}`.')
    return gen_set_ops.set_size(a.indices, a.values, a.dense_shape, validate_indices)

Compute number of unique elements along last dimension of `a`.

Args:
a: `SparseTensor`, with indices sorted in row-major order.
validate_indices: Whether to validate the order and range of sparse indices
in `a`. Note that setting this to `false` allows for undefined behavior
when calling this function with invalid indices.

Returns:
`int32` `Tensor` of set sizes. For `a` ranked `n`, this is a `Tensor` with
rank `n-1`, and the same 1st `n-1` dimensions as `a`. Each value is the
number of unique elements in the corresponding `[0...n-1]` dimension of `a`.

Raises:
TypeError: If `a` is an invalid types.


def search(self, terms):

        messages = self._connection.get("search/%s" % urllib.quote_plus(terms), key="messages")
        if messages:
            messages = [Message(self, message) for message in messages]
        return messages

Search transcripts.

Args:
terms (str): Terms for search

Returns:
array. Messages


def iuptri(items, diago=True, with_inds=False):

    for (ii, item1) in enumerate(items):
        for (jj, item2) in enumerate(items):
            do_yield = (jj >= ii) if diago else (jj > ii)
            if do_yield:
                if with_inds:
                    yield (ii, jj), (item1, item2)
                else:
                    yield item1, item2

A generator that yields the upper triangle of the matrix (items x items)

Args:
items: Iterable object with elements [e0, e1, ...]
diago: False if diagonal matrix elements should be excluded
with_inds: If True, (i,j) (e_i, e_j) is returned else (e_i, e_j)

>>> for (ij, mate) in iuptri([0,1], with_inds=True):
...     print("ij:", ij, "mate:", mate)
ij: (0, 0) mate: (0, 0)
ij: (0, 1) mate: (0, 1)
ij: (1, 1) mate: (1, 1)


def __init__(self, hostname, auth=AnonymousAuth()):

        self._hostname = self._construct_full_hostname(hostname)
        _logger.debug("Hostname is %s" % self._hostname)
        self._auth_info = auth

Initializer for the base class.

Save the hostname to use for all requests as well as any
authentication info needed.

Args:
hostname: The host for the requests.
auth: The authentication info needed for any requests.


def power(maf=0.5,beta=0.1, N=100, cutoff=5e-8):

	assert maf>=0.0 and maf<=0.5, "maf needs to be between 0.0 and 0.5, got %f" % maf
	if beta<0.0:
		beta=-beta
	std_beta = 1.0/np.sqrt(N*(2.0 * maf*(1.0-maf)))
	non_centrality = beta
	beta_samples = np.random.normal(loc=non_centrality, scale=std_beta)
	n_grid = 100000
	beta_in = np.arange(0.5/(n_grid+1.0),(n_grid-0.5)/(n_grid+1.0),1.0/(n_grid+1.0))
	beta_theoretical = ((st.norm.isf(beta_in)* std_beta) + non_centrality)
	pvals = st.chi2.sf( (beta_theoretical/std_beta)*(beta_theoretical/std_beta) ,1.0)

	power = (pvals<cutoff).mean()
	return power, pvals

estimate power for a given allele frequency, effect size beta and sample size N

Assumption:

z-score = beta_ML distributed as p(0) = N(0,1.0(maf*(1-maf)*N))) under the null hypothesis
the actual beta_ML is distributed as p(alt) = N( beta , 1.0/(maf*(1-maf)N) )

Arguments:
maf:	minor allele frequency of the SNP
beta:	effect size of the SNP
N:		sample size (number of individuals)
Returns:
power:	probability to detect a SNP in that study with the given parameters


def price(self, valuation_date, market, model=None, pricing_context=None, name=None):
    model = model or rc.InterestRateModelType.LOGNORMAL_RATE
    name = name or self._name + '_price'
    with tf.name_scope(name):
        swap_annuity = self._swap.annuity(valuation_date, market, model)
        forward_swap_rate = self._swap.par_rate(valuation_date, market, model)
        strike = self._swap.fixed_rate
        expiry_time = dates.daycount_actual_365_fixed(start_date=valuation_date, end_date=self._expiry_date, dtype=self._dtype)
        if model == rc.InterestRateModelType.LOGNORMAL_RATE:
            option_value = self._price_lognormal_rate(market, pricing_context, forward_swap_rate, strike, expiry_time)
        else:
            raise ValueError('Unsupported model.')
        return self._swap.notional[-1] * swap_annuity * option_value

Returns the present value of the swaption on the valuation date.

Args:
valuation_date: A scalar `DateTensor` specifying the date on which
valuation is being desired.
market: A namedtuple of type `InterestRateMarket` which contains the
necessary information for pricing the FRA instrument.
model: An optional input of type `InterestRateModelType` to specify which
model to use for pricing.
Default value: `None` in which case LOGNORMAL_RATE model is used.
pricing_context: An optional input to provide additional parameters (such
as model parameters) relevant for pricing.
name: Python str. The name to give to the ops created by this function.
Default value: `None` which maps to 'price'.

Returns:
A Rank 1 `Tensor` of real type containing the modeled price of each IRS
contract based on the input market data.

Raises:
ValueError: If an unsupported model is supplied to the function.


def ingress(self, envelope, http_headers, operation):

    if self._logger.isEnabledFor(logging.DEBUG):
      self._logger.debug(_RESPONSE_XML_LOG_LINE,
                         etree.tostring(envelope, pretty_print=True))

    if self._logger.isEnabledFor(logging.WARN):
      warn_data = {}
      header = envelope.find(_HEADER_XPATH)
      fault = envelope.find(_FAULT_XPATH)
      if fault is not None:
        warn_data['faultMessage'] = fault.find('faultstring').text

        if header is not None:
          header_data = {
              re.sub(_REMOVE_NS_REGEXP, '', child.tag): child.text
              for child in header[0]}
          warn_data.update(header_data)

        if 'serviceName' not in warn_data:
          warn_data['serviceName'] = operation.binding.wsdl.services.keys()[0]

        if 'methodName' not in warn_data:
          warn_data['methodName'] = operation.name

        self._logger.warn('Error summary: %s', warn_data)

    return envelope, http_headers

Overrides the ingress function for response logging.

Args:
envelope: An Element with the SOAP request data.
http_headers: A dict of the current http headers.
operation: The SoapOperation instance.

Returns:
A tuple of the envelope and headers.


def add_sample_tag_value(self, tag_name, new_sample_values):

        if tag_name in self.format_tags:
            msg = "New format value [{}] already exists.".format(tag_name)
            raise KeyError(msg)

        if not self._samples_match(new_sample_values):
            raise KeyError("Sample name values must match "
                           "existing sample names")
        for sample in self.sample_tag_values.keys():
            value = str(new_sample_values[sample])
            self.sample_tag_values[sample][tag_name] = value

Appends a new format tag-value for all samples.

Args:
tag_name: string tag name; must not already exist
new_sample

Raises:
KeyError: if tag_name to be added already exists


def update_restore_inputs(self, checkpoint_key: str, shape_and_slice_spec: str) -> tuple[Sequence[str], Sequence[str]]:
    keys = []
    slices = []
    logging.info('Updating restore v2 inputs for %s: %s', checkpoint_key, shape_and_slice_spec)
    for i, layout in enumerate(self._to_shard_layout):
        sub_checkpoint_key = checkpoint_key.replace(self._main_checkpoint_name, self._checkpoint_local_names[i])
        logging.info('Will read sub key %s: %s', sub_checkpoint_key, layout.unsharded_shape)
        keys.append(sub_checkpoint_key)
        slices.append(_shard_info_str(layout.unsharded_shape, trackable_base.ShardInfo(offset=[0, 0], shape=layout.unsharded_shape)))
    return (keys, slices)

Updates checkpoint key and slice spec acorrding to the resharding plan.

Args:
checkpoint_key: The input checkpoint key to be read.
shape_and_slice_spec: The shape and slice spec of the checkpoint key to be
read.

Returns:
A tuple of (keys, slices) that should be passed to restore_v2 inorder to
reshard according to the resharding plan. The restored tensors from
restore_v2 op will usually be passed to reshard method of this class to
get the final resharded value.


def eulers_totient(n):

    if not isinstance(n, int):
        raise TypeError("Expecting a strictly positive integer")
    if n <= 0:
        raise ValueError("Expecting a strictly positive integer")

    if n == 1:
        return 1

    result = 0
    for i in range(1, n):
        if gcd(i, n) == 1:
            result += 1
    return result

Calculate the value of Euler's totient for a given integer

Args:
n (int): strictly positive integer

Returns:
The value of Euler's totient for n

Raises:
TypeError: If either n or k is not an integer
ValueError: If either n or k is negative, or if k is strictly greater than n


def write_uint8(self, value, little_endian=True):
    if little_endian:
        endian = '<'
    else:
        endian = '>'
    return self.pack(('%sB' % endian), value)

Pack the value as an unsigned byte and write 1 byte to the stream.

Args:
value:
little_endian (bool): specify the endianness. (Default) Little endian.

Returns:
int: the number of bytes written.


def check_copies(overwrite: bool=False, file: Optional[str]=None):
    buffer = {}
    if file is None:
        all_files = glob.glob(os.path.join(TRANSFORMERS_PATH, '***.py'), recursive=True)
        all_files = list(all_files) + list(all_test_files)
    else:
        all_files = [file]
    diffs = []
    for filename in all_files:
        new_diffs = is_copy_consistent(filename, overwrite, buffer)
        diffs += [f'- {filename}: copy does not match {d[0]} at line {d[1]}' for d in new_diffs]
    if not overwrite and len(diffs) > 0:
        diff = '\n'.join(diffs)
        raise Exception('Found the following copy inconsistencies:\n' + diff + '\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.')

Check every file is copy-consistent with the original. Also check the model list in the main README and other
READMEs are consistent.

Args:
overwrite (`bool`, *optional*, defaults to `False`):
Whether or not to overwrite the copies when they don't match.
file (`bool`, *optional*):
The path to a specific file to check and/or fix.


def _is_in_control_flow(self, op):
    return control_flow_util.IsInCond(op)

Returns true if the given op is inside a tf.cond or in tf.while_loop.

Args:
op: A tensorflow op that should be checked whether in control flow or not.
Returns:
A boolean value whether the op is in control flow or not.


def dice_loss(inputs: Tensor, labels: Tensor, num_masks: int) -> Tensor:
    probs = inputs.sigmoid().flatten(1)
    numerator = 2 * (probs * labels).sum(-1)
    denominator = probs.sum(-1) + labels.sum(-1)
    loss = 1 - (numerator + 1) / (denominator + 1)
    loss = loss.sum() / num_masks
    return loss

Compute the DICE loss, similar to generalized IOU for masks as follows:

$$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x \cap y }{x \cup y + 1}} $$

In practice, since `labels` is a binary mask, (only 0s and 1s), dice can be computed as follow

$$ \mathcal{L}_{\text{dice}(x, y) = 1 - \frac{2 * x * y }{x + y + 1}} $$

Args:
inputs (`torch.Tensor`):
A tensor representing a mask.
labels (`torch.Tensor`):
A tensor with the same shape as inputs. Stores the binary classification labels for each element in inputs
(0 for the negative class and 1 for the positive class).
num_masks (`int`):
The number of masks present in the current batch, used for normalization.

Returns:
`torch.Tensor`: The computed loss.


def start_new_feature(**cc_kwargs):

    project = Project.from_path(pathlib.Path.cwd().resolve())
    contrib_dir = project.get('contrib', 'module_path')

    with tempfile.TemporaryDirectory() as tempdir:

        output_dir = tempdir
        cc_kwargs['output_dir'] = output_dir
        rendered_dir = render_feature_template(**cc_kwargs)

        src = rendered_dir
        dst = contrib_dir
        synctree(src, dst, onexist=_fail_if_feature_exists)

    logger.info('Start new feature successful.')

Start a new feature within a ballet project

Renders the feature template into a temporary directory, then copies the
feature files into the proper path within the contrib directory.

Args:
**cc_kwargs: options for the cookiecutter template

Raises:
ballet.exc.BalletError: the new feature has the same name as an
existing one


def get_data(self,
                 file_path=sys.stdin,
                 delimiter=',',
                 categories_delimiter=None):

        if file_path == sys.stdin:
            logger.info('Read data from standard input')
            lines = [line.replace('\n', '') for line in file_path]
        else:
            logger.info('Read data from file ' + file_path)
            with open(file_path) as file:
                lines = list(file)
        columns = lines[0].rstrip('\n').split(delimiter)[1:]
        categories = None
        if categories_delimiter:
            columns, categories = zip(*[c.split(categories_delimiter, 1)
                                        for c in columns])
        size = len(columns)
        data = [list(map(int, l.split(delimiter)[1:]))
                for l in lines[1:size + 1]]
        return DesignStructureMatrix(data, columns, categories)

Implement get_dsm method from Provider class.

Parse CSV to return an instance of DSM.

Args:
file_path (str/fd): path or file descriptor.
delimiter (str): character(s) used as delimiter for columns.
categories_delimiter (str):
character(s) used as delimiter for categories and keys
(first column).

Returns:
DSM: instance of DSM.


def expand_source_files(filenames, cwd=None):
    out = []
    for f in expand_globpaths(filenames.split(), cwd):
        if path_utils.isdir(f):
            out += recursive_glob(path_utils.join(f, '**', '*.py'))
        elif f.endswith('.py'):
            out.append(f)
        elif is_file_script(f, cwd):
            out.append(f)
    return set(out)

Expand a space-separated string of filenames passed in as sources.

This is a helper function for handling command line arguments that specify a
list of source files and directories.

Any directories in filenames will be scanned recursively for .py files.
Any files that do not end with ".py" will be dropped.

Args:
filenames: A space-separated string of filenames to process.
cwd: An optional working directory to expand relative paths

Returns:
A set of full paths to .py files


def _maybe_create_attribute(self, name, default_value):
    if not hasattr(self, name):
        self.__setattr__(name, default_value)

Create the attribute with the default value if it hasn't been created.

This is useful for fields that is used for tracking purpose,
_trainable_weights, or _layers. Note that user could create a layer subclass
and assign an internal field before invoking the Layer.__init__(), the
__setattr__() need to create the tracking fields and __init__() need to not
override them.

Args:
name: String, the name of the attribute.
default_value: Object, the default value of the attribute.


def survey_basis(self, keys=None, alias=None, step=None):

        if keys is None:
            keys = [k for k, v in self.data.items() if isinstance(v, Curve)]
        else:
            keys = utils.flatten_list(keys)

        starts, stops, steps = [], [], []
        for k in keys:
            d = self.get_curve(k, alias=alias)
            if keys and (d is None):
                continue
            try:
                starts.append(d.basis[0])
                stops.append(d.basis[-1])
                steps.append(d.basis[1] - d.basis[0])
            except Exception as e:
                pass
        if starts and stops and steps:
            step = step or min(steps)
            return np.arange(min(starts), max(stops)+1e-9, step)
        else:
            return None

Look at the basis of all the curves in ``well.data`` and return a
basis with the minimum start, maximum depth, and minimum step.

Args:
keys (list): List of strings: the keys of the data items to
survey, if not all of them.
alias (dict): a dictionary mapping mnemonics to lists of mnemonics.
step (float): a new step, if you want to change it.

Returns:
ndarray. The most complete common basis.


def to_molden(cartesian_list, buf=None, sort_index=True, overwrite=True, float_format='{:.6f}'.format):
    if sort_index:
        cartesian_list = [molecule.sort_index() for molecule in cartesian_list]
    give_header = (((((((('[MOLDEN FORMAT]\n' + '[N_GEO]\n') + str(len(cartesian_list))) + '\n') + '[GEOCONV]\n') + 'energy\n{energy}') + 'max-force\n{max_force}') + 'rms-force\n{rms_force}') + '[GEOMETRIES] (XYZ)\n').format
    values = (len(cartesian_list) * '1\n')
    energy = [str(m.metadata.get('energy', 1)) for m in cartesian_list]
    energy = ('\n'.join(energy) + '\n')
    header = give_header(energy=energy, max_force=values, rms_force=values)
    coordinates = [x.to_xyz(sort_index=sort_index, float_format=float_format) for x in cartesian_list]
    output = (header + '\n'.join(coordinates))
    if (buf is not None):
        if overwrite:
            with open(buf, mode='w') as f:
                f.write(output)
        else:
            with open(buf, mode='x') as f:
                f.write(output)
    else:
        return output

Write a list of Cartesians into a molden file.

.. note:: Since it permamently writes a file, this function
is strictly speaking **not sideeffect free**.
The list to be written is of course not changed.

Args:
cartesian_list (list):
buf (str): StringIO-like, optional buffer to write to
sort_index (bool): If sort_index is true, the Cartesian
is sorted by the index before writing.
overwrite (bool): May overwrite existing files.
float_format (one-parameter function): Formatter function
to apply to column’s elements if they are floats.
The result of this function must be a unicode string.

Returns:
formatted : string (or unicode, depending on data and options)


def GetMessages(self, formatter_mediator, event):

    if self.DATA_TYPE != event.data_type:
      raise errors.WrongFormatter('Unsupported data type: {0:s}.'.format(
          event.data_type))

    event_values = event.CopyToDict()

    priority_level = event_values.get('level', None)
    if isinstance(priority_level, py2to3.INTEGER_TYPES):
      event_values['level'] = '{0:s} ({1:d})'.format(
          self._PRIORITY_LEVELS.get(priority_level, 'UNKNOWN'), priority_level)

    read_uid = event_values.get('read_uid', None)
    if read_uid == -1:
      event_values['read_uid'] = 'ALL'

    read_gid = event_values.get('read_gid', None)
    if read_gid == -1:
      event_values['read_gid'] = 'ALL'

    return self._ConditionalFormatMessages(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.

Raises:
WrongFormatter: if the event object cannot be formatted by the formatter.


def markdown_to_html_with_extensions(text, options=0, extensions=None):

    if extensions is None:
        extensions = []

    core_extensions_ensure_registered()

    cmark_extensions = []
    for extension_name in extensions:
        extension = find_syntax_extension(extension_name)
        if extension is None:
            raise ValueError('Unknown extension {}'.format(extension_name))
        cmark_extensions.append(extension)

    parser = parser_new(options=options)

    try:
        for extension in cmark_extensions:
            parser_attach_syntax_extension(parser, extension)

        parser_feed(parser, text)

        root = parser_finish(parser)

        if _cmark.lib.cmark_node_get_type(root) == _cmark.lib.CMARK_NODE_NONE:
            raise ValueError('Error parsing markdown!')

        extensions_ll = parser_get_syntax_extensions(parser)

        output = render_html(root, options=options, extensions=extensions_ll)

    finally:
        parser_free(parser)

    return output

Render the given text to Markdown, using extensions.

This is a high-level wrapper over the various functions needed to enable
extensions, attach them to a parser, and render html.

Args:
text (str): The text to render to Markdown.
options (int): The cmark options.
extensions (Sequence[str]): The list of extension names to use.

Returns:
str: The rendered markdown.


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

        if "widget" not in kwargs:
            kwargs["widget"] = PasswordConfirmationInput(
                confirm_with=kwargs.pop('confirm_with', None))

        super(PasswordConfirmationField, self).__init__(*args, **kwargs)

Init method.

Args:
*args (): Django's args for a form field.
**kwargs (): Django's kwargs for a form field. Should contain a
confirm_with keyword argument to point to the password field.


def add(self, term):
    if isinstance(term, Conjunction):
        for term_ in term.terms:
            self.add(term_)
    elif isinstance(term, Term):
        self._terms.append(term)
    else:
        raise TypeError('Not a Term or Conjunction')

Add a term to the conjunction.

Args:
term (:class:`Term`, :class:`Conjunction`): term to add;
if a :class:`Conjunction`, all of its terms are added
to the current conjunction.
Raises:
:class:`TypeError`: when *term* is an invalid type


def set_domain_workgroup(workgroup):
    if six.PY2:
        workgroup = _to_unicode(workgroup)
    with salt.utils.winapi.Com():
        conn = wmi.WMI()
        comp = conn.Win32_ComputerSystem()[0]
        res = comp.JoinDomainOrWorkgroup(Name=workgroup.upper())
    return (True if (not res[0]) else False)

Set the domain or workgroup the computer belongs to.

.. versionadded:: 2019.2.0

Returns:
bool: ``True`` if successful, otherwise ``False``

CLI Example:

.. code-block:: bash

salt 'minion-id' system.set_domain_workgroup LOCAL


def get_simulated_data(nmr_problems):

    nmr_observed_tanks = 10

    nmr_tanks_ground_truth = normal(nmr_problems, 1, mean=250, std=30, ctype='uint')

    observations = uniform(nmr_problems, nmr_observed_tanks, low=0, high=nmr_tanks_ground_truth, ctype='uint')

    return observations, nmr_tanks_ground_truth

Simulate some data.

This returns the simulated tank observations and the corresponding ground truth maximum number of tanks.

Args:
nmr_problems (int): the number of problems

Returns:
tuple: (observations, nmr_tanks_ground_truth)


def l2_distance_sq(t1, t2, name=None):
    with tf.name_scope(name, 'l2_distance_sq', [t1, t2]) as scope:
        t1 = tf.convert_to_tensor(t1, name='t1')
        t2 = tf.convert_to_tensor(t2, name='t2')
        return length_squared(tf.subtract(t1, t2), name=scope)

Square of l2 distance between t1 and t2.

Args:
t1: A tensor.
t2: A tensor that is the same size as t1.
name: Optional name for this op.
Returns:
The l2 distance between t1 and t2.


def RunStateMethod(self, method_name, request=None, responses=None):

    if self.rdf_flow.pending_termination:
      self.Error(error_message=self.rdf_flow.pending_termination.reason)
      return

    client_id = self.rdf_flow.client_id

    deadline = self.rdf_flow.processing_deadline
    if deadline and rdfvalue.RDFDatetime.Now() > deadline:
      raise flow.FlowError("Processing time for flow %s on %s expired." %
                           (self.rdf_flow.flow_id, self.rdf_flow.client_id))

    self.rdf_flow.current_state = method_name
    if request and responses:
      logging.debug("Running %s for flow %s on %s, %d responses.", method_name,
                    self.rdf_flow.flow_id, client_id, len(responses))
    else:
      logging.debug("Running %s for flow %s on %s", method_name,
                    self.rdf_flow.flow_id, client_id)

    try:
      try:
        method = getattr(self, method_name)
      except AttributeError:
        raise ValueError("Flow %s has no state method %s" %
                         (self.__class__.__name__, method_name))

      responses = flow_responses.Responses.FromResponses(
          request=request, responses=responses)

      if responses.status is not None:
        self.SaveResourceUsage(responses.status)

      stats_collector_instance.Get().IncrementCounter("grr_worker_states_run")

      if method_name == "Start":
        stats_collector_instance.Get().IncrementCounter(
            "flow_starts", fields=[self.rdf_flow.flow_class_name])
        method()
      else:
        method(responses)

      if self.replies_to_process:
        if self.rdf_flow.parent_hunt_id and not self.rdf_flow.parent_flow_id:
          self._ProcessRepliesWithHuntOutputPlugins(self.replies_to_process)
        else:
          self._ProcessRepliesWithFlowOutputPlugins(self.replies_to_process)

        self.replies_to_process = []

    except Exception as e:

      stats_collector_instance.Get().IncrementCounter(
          "flow_errors", fields=[self.rdf_flow.flow_class_name])
      logging.exception("Flow %s on %s raised %s.", self.rdf_flow.flow_id,
                        client_id, utils.SmartUnicode(e))

      self.Error(
          error_message=utils.SmartUnicode(e), backtrace=traceback.format_exc())

Completes the request by calling the state method.

Args:
method_name: The name of the state method to call.
request: A RequestState protobuf.
responses: A list of FlowMessages responding to the request.


def _pick_or_create_inserted_op_moment_index(
            self, splitter_index: int, op: ops.Operation,
            strategy: InsertStrategy) -> int:

        if (strategy is InsertStrategy.NEW or
                strategy is InsertStrategy.NEW_THEN_INLINE):
            self._moments.insert(splitter_index, ops.Moment())
            return splitter_index

        if strategy is InsertStrategy.INLINE:
            if (0 <= splitter_index - 1 < len(self._moments) and
                    self._can_add_op_at(splitter_index - 1, op)):
                return splitter_index - 1

            return self._pick_or_create_inserted_op_moment_index(
                splitter_index, op, InsertStrategy.NEW)

        if strategy is InsertStrategy.EARLIEST:
            if self._can_add_op_at(splitter_index, op):
                p = self._prev_moment_available(op, splitter_index)
                return p or 0

            return self._pick_or_create_inserted_op_moment_index(
                splitter_index, op, InsertStrategy.INLINE)

        raise ValueError('Unrecognized append strategy: {}'.format(strategy))

Determines and prepares where an insertion will occur.

Args:
splitter_index: The index to insert at.
op: The operation that will be inserted.
strategy: The insertion strategy.

Returns:
The index of the (possibly new) moment where the insertion should
occur.

Raises:
ValueError: Unrecognized append strategy.


def CleanVacuousVersions(clients=None, dry_run=True):
    if (not clients):
        index = client_index.CreateClientIndex()
        clients = index.LookupClients(['.'])
    clients.sort()
    with data_store.DB.GetMutationPool() as pool:
        logging.info('checking %d clients', len(clients))
        for batch in collection.Batch(clients, 10000):
            client_infos = data_store.DB.MultiResolvePrefix(batch, ['aff4:', 'aff4:'], data_store.DB.ALL_TIMESTAMPS)
            for (client, type_list) in client_infos:
                cleared = 0
                kept = 0
                updates = []
                for (a, _, ts) in type_list:
                    if (ts != 0):
                        updates.append((ts, a))
                updates = sorted(updates)
                dirty = True
                for (ts, a) in updates:
                    if (a == 'aff4:type'):
                        if dirty:
                            kept += 1
                            dirty = False
                        else:
                            cleared += 1
                            if (not dry_run):
                                pool.DeleteAttributes(client, ['aff4:type'], start=ts, end=ts)
                                if (pool.Size() > 1000):
                                    pool.Flush()
                    else:
                        dirty = True
                logging.info('%s: kept %d and cleared %d', client, kept, cleared)

A script to remove no-op client versions.

This script removes versions of a client when it is identical to the previous,
in the sense that no versioned attributes were changed since the previous
client version.

Args:
clients: A list of ClientURN, if empty cleans all clients.
dry_run: whether this is a dry run


def _populate(cls, as_of=None, delete=False):
    billing_cycle_helper = get_billing_cycle()
    billing_cycles_exist = BillingCycle.objects.exists()
    try:
        current_billing_cycle = BillingCycle.objects.as_of(date=as_of)
    except BillingCycle.DoesNotExist:
        current_billing_cycle = None
    if (not billing_cycles_exist):
        delete = False
    if (billing_cycles_exist and (not current_billing_cycle)):
        raise CannotPopulateForDateOutsideExistingCycles()
    omit_current = (current_billing_cycle and delete)
    stop_date = (as_of + relativedelta(years=settings.SWIFTWIND_BILLING_CYCLE_YEARS))
    date_ranges = billing_cycle_helper.generate_date_ranges(as_of, stop_date=stop_date, omit_current=omit_current)
    date_ranges = list(date_ranges)
    beginning_date = date_ranges[0][0]
    with db_transaction.atomic():
        if delete:
            cls.objects.filter(start_date__gte=beginning_date).delete()
        for (start_date, end_date) in date_ranges:
            exists = BillingCycle.objects.filter(date_range=(start_date, end_date)).exists()
            if exists:
                if delete:
                    raise Exception('It should not be possible to get here as future billing cycles have just been deleted')
                else:
                    pass
            else:
                BillingCycle.objects.create(date_range=(start_date, end_date))

Populate the table with billing cycles starting from `as_of`

Args:
as_of (date): The date at which to begin the populating
delete (bool): Should future billing cycles be deleted?


def from_string(cls, data, sigfigs=8):

        lines = data.split("\n")[:-1]
        struc_lines = {"HEADER": [], "VERS": [], "SYMGRP": [],
                       "STRUC": [], "CLASS": [], "SITE": []}
        for line in lines:
            if line != "" and not line.isspace():
                if not line[0].isspace():
                    cat = line.split()[0]
                if cat in struc_lines:
                    struc_lines[cat].append(line)
                else:
                    pass
        for cat in struc_lines:
            struc_lines[cat] = " ".join(struc_lines[cat]).replace("= ", "=")

        structure_tokens = {"ALAT": None,
                            "PLAT": [],
                            "CLASS": [],
                            "SITE": []}

        for cat in ["STRUC", "CLASS", "SITE"]:
            fields = struc_lines[cat].split("=")
            for f, field in enumerate(fields):
                token = field.split()[-1]
                if token == "ALAT":
                    alat = round(float(fields[f+1].split()[0]), sigfigs)
                    structure_tokens["ALAT"] = alat
                elif token == "ATOM":
                    atom = fields[f+1].split()[0]
                    if not bool(re.match("E[0-9]*$", atom)):
                        if cat == "CLASS":
                            structure_tokens["CLASS"].append(atom)
                        else:
                            structure_tokens["SITE"].append({"ATOM": atom})
                    else:
                        pass
                elif token in ["PLAT", "POS"]:
                    try:
                        arr = np.array([round(float(i), sigfigs)
                                       for i in fields[f+1].split()])
                    except ValueError:
                        arr = np.array([round(float(i), sigfigs)
                                       for i in fields[f+1].split()[:-1]])
                    if token == "PLAT":
                        structure_tokens["PLAT"] = arr.reshape([3, 3])
                    elif not bool(re.match("E[0-9]*$", atom)):
                        structure_tokens["SITE"][-1]["POS"] = arr
                    else:
                        pass
                else:
                    pass
        try:
            spcgrp_index = struc_lines["SYMGRP"].index("SPCGRP")
            spcgrp = struc_lines["SYMGRP"][spcgrp_index:spcgrp_index+12]
            structure_tokens["SPCGRP"] = spcgrp.split("=")[1].split()[0]
        except ValueError:
            pass

        for token in ["HEADER", "VERS"]:
                try:
                    value = re.split(token + r"\s*", struc_lines[token])[1]
                    structure_tokens[token] = value.strip()
                except IndexError:
                    pass
        return LMTOCtrl.from_dict(structure_tokens)

Creates a CTRL file object from a string. This will mostly be
used to read an LMTOCtrl object from a CTRL file. Empty spheres
are ignored.

Args:
data: String representation of the CTRL file.

Returns:
An LMTOCtrl object.


def _call_post_with_user_override(self, sap_user_id, url, payload):

        SAPSuccessFactorsEnterpriseCustomerConfiguration = apps.get_model(
            'sap_success_factors',
            'SAPSuccessFactorsEnterpriseCustomerConfiguration'
        )
        oauth_access_token, _ = SAPSuccessFactorsAPIClient.get_oauth_access_token(
            self.enterprise_configuration.sapsf_base_url,
            self.enterprise_configuration.key,
            self.enterprise_configuration.secret,
            self.enterprise_configuration.sapsf_company_id,
            sap_user_id,
            SAPSuccessFactorsEnterpriseCustomerConfiguration.USER_TYPE_USER
        )

        response = requests.post(
            url,
            data=payload,
            headers={
                'Authorization': 'Bearer {}'.format(oauth_access_token),
                'content-type': 'application/json'
            }
        )

        return response.status_code, response.text

Make a post request with an auth token acquired for a specific user to a SuccessFactors endpoint.

Args:
sap_user_id (str): The user to use to retrieve an auth token.
url (str): The url to post to.
payload (str): The json encoded payload to post.


def apply(self, flag_set: AbstractSet[Flag], operand: AbstractSet[Flag]) \
            -> FrozenSet[Flag]:

        if self == FlagOp.ADD:
            return frozenset(flag_set | operand)
        elif self == FlagOp.DELETE:
            return frozenset(flag_set - operand)
        else:
            return frozenset(operand)

Apply the flag operation on the two sets, returning the result.

Args:
flag_set: The flag set being operated on.
operand: The flags to use as the operand.


def parse_time_indices(s):

  if not s.startswith('['):
    s = '[' + s + ']'
  parsed = command_parser._parse_slices(s)
  if len(parsed) != 1:
    raise ValueError(
        'Invalid number of slicing objects in time indices (%d)' % len(parsed))
  else:
    return parsed[0]

Parse a string as time indices.

Args:
s: A valid slicing string for time indices. E.g., '-1', '[:]', ':', '2:10'

Returns:
A slice object.

Raises:
ValueError: If `s` does not represent valid time indices.


def delete(table, keyset):
    delete = Mutation.Delete(table=table, key_set=keyset._to_pb())
    return _Mutator(mutation=Mutation(delete=delete), rows=0, cells=0, operation=WriteMutation._OPERATION_DELETE, kwargs={'table': table, 'keyset': keyset})

Delete one or more table rows.

Args:
table: Name of the table to be modified.
keyset: Keys/ranges identifying rows to delete.


def build_global(self, global_node):
    config_block_lines = self.__build_config_block(global_node.config_block)
    return config.Global(config_block=config_block_lines)

parse `global` section, and return the config.Global

Args:
global_node (TreeNode):  `global` section treenode

Returns:
config.Global: an object


def _get_free_gpu(max_gpu_utilization=40, min_free_memory=0.5, num_gpu=1):

    def get_gpu_info():
        gpu_info = subprocess.check_output(['nvidia-smi', '--format=csv,noheader,nounits', '--query-gpu=index,memory.total,memory.free,memory.used,utilization.gpu']).decode()
        gpu_info = gpu_info.split('\n')
        gpu_info_array = []
        for line in gpu_info:
            if (len(line) > 0):
                (gpu_id, total_memory, free_memory, used_memory, gpu_util) = line.split(',')
                gpu_memory_util = (float(used_memory) / float(total_memory))
                gpu_info_array.append((float(gpu_util), gpu_memory_util, gpu_id))
        return gpu_info_array
    num_times_to_average = 5
    current_array = []
    for ind in range(num_times_to_average):
        current_array.append(get_gpu_info())
        time.sleep(1)
    num_gpus = len(current_array[0])
    avg_array = [(0, 0, str(x)) for x in range(num_gpus)]
    for ind in range(num_times_to_average):
        for gpu_ind in range(num_gpus):
            avg_array[gpu_ind] = ((avg_array[gpu_ind][0] + current_array[ind][gpu_ind][0]), (avg_array[gpu_ind][1] + current_array[ind][gpu_ind][1]), avg_array[gpu_ind][2])
    for gpu_ind in range(num_gpus):
        avg_array[gpu_ind] = ((float(avg_array[gpu_ind][0]) / num_times_to_average), (float(avg_array[gpu_ind][1]) / num_times_to_average), avg_array[gpu_ind][2])
    avg_array.sort()
    gpus_found = 0
    gpus_to_use = ''
    free_memory = 1.0
    for current_gpu in avg_array:
        if ((current_gpu[0] < max_gpu_utilization) and ((1 - current_gpu[1]) > min_free_memory)):
            if (gpus_found == 0):
                gpus_to_use = current_gpu[2]
                free_memory = (1 - current_gpu[1])
            else:
                gpus_to_use = ((gpus_to_use + ',') + current_gpu[2])
                free_memory = min(free_memory, (1 - current_gpu[1]))
            gpus_found = (gpus_found + 1)
        if (gpus_found == num_gpu):
            break
    return (gpus_to_use, free_memory)

Get available GPUs according to utilization thresholds.

Args:
:max_gpu_utilization: percent utilization threshold to consider a GPU "free"
:min_free_memory: percent free memory to consider a GPU "free"
:num_gpu: number of requested GPUs

Returns:
A tuple of (available_gpus, minimum_free_memory), where available_gpus is a comma-delimited string of GPU ids, and minimum_free_memory
is the lowest amount of free memory available on the available_gpus.


def parent(self) -> 'KeyPath':
    if self.is_root:
        raise KeyError('Parent of a root KeyPath does not exist.')
    return KeyPath(self._keys[:-1])

The ``KeyPath`` object for current node's parent.

Example::

path = pg.KeyPath.parse('a.b.c.')
assert path.parent == 'a.b'

Returns:
A ``KeyPath`` object for the parent of current node.

Raises:
KeyError: If current path is the root.


def _CopyDateFromString(self, date_string):
    date_string_length = len(date_string)
    if (date_string_length < 10):
        raise ValueError('Date string too short.')
    if ((date_string[4] != '-') or (date_string[7] != '-')):
        raise ValueError('Invalid date string.')
    try:
        year = int(date_string[0:4], 10)
    except ValueError:
        raise ValueError('Unable to parse year.')
    try:
        month = int(date_string[5:7], 10)
    except ValueError:
        raise ValueError('Unable to parse month.')
    try:
        day_of_month = int(date_string[8:10], 10)
    except ValueError:
        raise ValueError('Unable to parse day of month.')
    days_per_month = self._GetDaysPerMonth(year, month)
    if ((day_of_month < 1) or (day_of_month > days_per_month)):
        raise ValueError('Day of month value out of bounds.')
    return (year, month, day_of_month)

Copies a date from a string.

Args:
date_string (str): date value formatted as: YYYY-MM-DD

Returns:
tuple[int, int, int]: year, month, day of month.

Raises:
ValueError: if the date string is invalid or not supported.


def deserialize_block(value):
    block = Block()
    block.ParseFromString(value)
    return BlockWrapper(block=block)

Deserialize a byte string into a BlockWrapper

Args:
value (bytes): the byte string to deserialze

Returns:
BlockWrapper: a block wrapper instance


def OR(self):
    clone = copy.deepcopy(self)
    clone.adapter._QUERY_GLUE = ' OR '
    return clone

Switches default query joiner from " AND " to " OR "

Returns:
Self. Queryset object.


def build(self, spec, reset=True):

        if reset:
            self.reset()

        with self.model:

            self.mu = 0.

            for t in spec.terms.values():

                data = t.data
                label = t.name
                dist_name = t.prior.name
                dist_args = t.prior.args

                n_cols = t.data.shape[1]

                coef = self._build_dist(spec, label, dist_name,
                                        shape=n_cols, **dist_args)

                if t.random:
                    self.mu += coef[t.group_index][:, None] * t.predictor
                else:
                    self.mu += pm.math.dot(data, coef)[:, None]

            y = spec.y.data
            y_prior = spec.family.prior
            link_f = spec.family.link
            if isinstance(link_f, string_types):
                link_f = self.links[link_f]
            else:
                link_f = link_f
            y_prior.args[spec.family.parent] = link_f(self.mu)
            y_prior.args['observed'] = y
            y_like = self._build_dist(spec, spec.y.name, y_prior.name,
                                      **y_prior.args)

            self.spec = spec

Compile the PyMC3 model from an abstract model specification.
Args:
spec (Model): A bambi Model instance containing the abstract
specification of the model to compile.
reset (bool): if True (default), resets the PyMC3BackEnd instance
before compiling.


def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
    vision_data = {}
    if image_sizes is not None:
        num_image_tokens = []
        for height, width in image_sizes:
            height, width = smart_resize(height, width, self.image_processor.spatial_factor, self.image_processor.min_pixels, self.image_processor.max_pixels)
            height = height
            width = width
            image_seq_length = height * (width + 1)
            num_image_tokens.append(image_seq_length)
        num_image_patches = [1] * len(image_sizes)
        vision_data.update({'num_image_tokens': num_image_tokens, 'num_image_patches': num_image_patches})
    return MultiModalData(**vision_data)

Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

Args:
image_sizes (`List[List[int]]`, *optional*):
The input sizes formatted as (height, width) per each image.

Returns:
`MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
input modalities, along with other useful data.


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 write_reactions(self, stream, reactions, properties=None):

        self._write_entries(
            stream, reactions, self.convert_reaction_entry, properties)

Write iterable of reactions as YAML object to stream.

Args:
stream: File-like object.
compounds: Iterable of reaction entries.
properties: Set of reaction properties to output (or None to output
all).


def Cleanse(obj, encoding='utf-8'):
    if isinstance(obj, int):
        return obj
    elif isinstance(obj, float):
        if (obj == _INFINITY):
            return 'Infinity'
        elif (obj == _NEGATIVE_INFINITY):
            return '-Infinity'
        elif math.isnan(obj):
            return 'NaN'
        else:
            return obj
    elif isinstance(obj, bytes):
        return tf.compat.as_text(obj, encoding)
    elif isinstance(obj, (list, tuple)):
        return [Cleanse(i, encoding) for i in obj]
    elif isinstance(obj, set):
        return [Cleanse(i, encoding) for i in sorted(obj)]
    elif isinstance(obj, dict):
        return {Cleanse(k, encoding): Cleanse(v, encoding) for (k, v) in obj.items()}
    else:
        return obj

Makes Python object appropriate for JSON serialization.

- Replaces instances of Infinity/-Infinity/NaN with strings.
- Turns byte strings into unicode strings.
- Turns sets into sorted lists.
- Turns tuples into lists.

Args:
obj: Python data structure.
encoding: Charset used to decode byte strings.

Returns:
Unicode JSON data structure.


def _ConsumeSingleByteString(self):
    text = self.token
    if ((len(text) < 1) or (text[0] not in _QUOTES)):
        raise self._ParseError(('Expected string but found: %r' % (text,)))
    if ((len(text) < 2) or (text[(- 1)] != text[0])):
        raise self._ParseError(('String missing ending quote: %r' % (text,)))
    try:
        result = text_encoding.CUnescape(text[1:(- 1)])
    except ValueError as e:
        raise self._ParseError(str(e))
    self.NextToken()
    return result

Consume one token of a string literal.

String literals (whether bytes or text) can come in multiple adjacent
tokens which are automatically concatenated, like in C or Python.  This
method only consumes one token.

Returns:
The token parsed.
Raises:
ParseError: When the wrong format data is found.


def fulfill_transaction(transaction, *, private_keys):

    if not isinstance(private_keys, (list, tuple)):
        private_keys = [private_keys]

    if isinstance(private_keys, tuple):
        private_keys = list(private_keys)

    transaction_obj = Transaction.from_dict(transaction)
    try:
        signed_transaction = transaction_obj.sign(private_keys)
    except KeypairMismatchException as exc:
        raise MissingPrivateKeyError('A private key is missing!') from exc

    return signed_transaction.to_dict()

Fulfills the given transaction.

Args:
transaction (dict): The transaction to be fulfilled.
private_keys (:obj:`str` | :obj:`list` | :obj:`tuple`): One or
more private keys to be used for fulfilling the
transaction.

Returns:
dict: The fulfilled transaction payload, ready to be sent to a
BigchainDB federation.

Raises:
:exc:`~.exceptions.MissingPrivateKeyError`: If a private
key is missing.


def _ExpandDirectories(filenames):
    expanded = set()
    for filename in filenames:
        if (not os.path.isdir(filename)):
            expanded.add(filename)
            continue
        for (root, _, files) in os.walk(filename):
            for loopfile in files:
                fullname = os.path.join(root, loopfile)
                if fullname.startswith(('.' + os.path.sep)):
                    fullname = fullname[len(('.' + os.path.sep)):]
                expanded.add(fullname)
    filtered = []
    for filename in expanded:
        if (os.path.splitext(filename)[1][1:] in GetAllExtensions()):
            filtered.append(filename)
    return filtered

Searches a list of filenames and replaces directories in the list with
all files descending from those directories. Files with extensions not in
the valid extensions list are excluded.

Args:
filenames: A list of files or directories

Returns:
A list of all files that are members of filenames or descended from a
directory in filenames


def raw_search(self, *args, **kwargs):

        limit = 50
        try:
            limit = kwargs['limit']
        except KeyError:
            pass

        self._mail.select("inbox")

        try:
            date = kwargs['date']
            date_str = date.strftime("%d-%b-%Y")
            _, email_ids = self._mail.search(None, '(SINCE "%s")' % date_str)
        except KeyError:
            _, email_ids = self._mail.search(None, 'ALL')

        email_ids = email_ids[0].split()

        matching_uids = []
        for _ in range(1, min(limit, len(email_ids))):
            email_id = email_ids.pop()
            rfc_body = self._mail.fetch(email_id, "(RFC822)")[1][0][1]

            match = True
            for expr in args:
                if re.search(expr, rfc_body) is None:
                    match = False
                    break

            if match:
                uid = re.search(
                    "UID\\D*(\\d+)\\D*", self._mail.fetch(email_id, 'UID')[1][0]).group(1)
                matching_uids.append(uid)

        return matching_uids

Find the a set of emails matching each regular expression passed in against the (RFC822) content.

Args:
*args: list of regular expressions.

Kwargs:
limit (int) - Limit to how many of the most resent emails to search through.
date (datetime) - If specified, it will filter avoid checking messages older
than this date.


def __init__(self, value_type: typing.Optional[typing.Union[typing.Type[typing.Any], typing.Tuple[typing.Type[typing.Any], ...]]], default: typing.Any=MISSING_VALUE, transform: typing.Optional[typing.Callable[[typing.Any], typing.Any]]=None, is_noneable: bool=False, frozen: bool=False):
    super().__init__()
    self._value_type = value_type
    self._is_noneable = is_noneable
    self._frozen = False
    self._default = MISSING_VALUE
    self._transform = transform
    self.set_default(default)
    self._frozen = frozen

Constructor of ValueSpecBase.

This class provides common facilities for implementing ValueSpec,
including type check, default value assignment, noneable handling,
missing value handling, and etc. Subclasses only need to handle value
specific logics in `apply`, `extend`, and `is_compatible`.

Args:
value_type: Type or tuples of type or None. When a not-none value_type is
present, type check will be performed.
default: (Optional) Default value. If not specified, it always require
user to provide. Or it can be any value that can be accepted by this
spec, or None, which automatically add Noneable property to the spec.
transform: (Optional) user-defined function to be called on the input
of `apply`. It could be used as a type converter or a custom
validator which may raise errors.
is_noneable: (Optional) If True, None is acceptable for this spec.
frozen: If True, values other than the default value is not accceptable.


def Print(self, output_writer):

    if self._extensions:
      output_writer.Write('\textensions: {0:s}\n'.format(
          ', '.join(self._extensions)))

Prints a human readable version of the filter.

Args:
output_writer (CLIOutputWriter): output writer.


def compute_expand_dims_output_shape(input_shape, axis):
    input_shape = list(input_shape)
    if axis is None:
        axis = len(input_shape)
    axis = to_tuple_or_list(axis)
    out_ndim = len(axis) + len(input_shape)
    axis = [canonicalize_axis(a, out_ndim) for a in axis]
    shape_iter = iter(input_shape)
    new_shape = [1 if ax in axis else next(shape_iter) for ax in range(out_ndim)]
    return tuple(new_shape)

Compute the output shape for the `expand_dims` operation.

Args:
input_shape: Input shape.
axis: int or sequence of ints for the axis to expand.

Returns:
Tuple of ints: The output shape after the `expand_dims` operation.


def take_node_screenshot(self, element, screenshot_path):
        from PIL import Image

        temp_path = os.path.join(tempdir, screenshot_path)

        el_x = int(element.location['x'])
        el_y = int(element.location['y'])
        el_height = int(element.size['height'])
        el_width = int(element.size['width'])

        if el_height == 0 or el_width == 0:
            self.debug_log("take_node_screenshot cannot be taken because element width or height equal zero")
            return False

        bounding_box = (
            el_x,
            el_y,
            (el_x + el_width),
            (el_y + el_height)
        )

        self._driver.save_screenshot(temp_path)

        base_image = Image.open(temp_path)

        cropped_image = base_image.crop(bounding_box)

        base_image = base_image.resize(cropped_image.size)

        base_image.paste(cropped_image, (0, 0))

        base_image.save(screenshot_path)

Take a screenshot of a node

Args:
element (object): the proxy_element
screenshot_path (str): the path where the screenshot will be saved


def generated_tag_data(tags):

    generated_tags = []
    for key, value in tags.items():
        generated_tags.append({
            'Key': key,
            'Value': value,
        })
    return generated_tags

Convert :obj:`dict` to S3 Tag list.

Args:
tags (dict): Dictonary of tag key and tag value passed.

Returns:
list: List of dictionaries.


def __init__(self, port=None, queue_id=None):

        super().__init__(action_type=ActionType.OFPAT_ENQUEUE, length=16)
        self.port = port
        self.queue_id = queue_id

Create an ActionEnqueue with the optional parameters below.

Args:
port (physical port or :attr:`.Port.OFPP_IN_PORT`): Queue's port.
queue_id (int): Where to enqueue the packets.


def get_data(name, train_batch_size, test_batch_size):
    if (name not in ['mnist', 'cifar10']):
        raise ValueError(("Expected dataset 'mnist' or 'cifar10', but got %s" % name))
    dataset = getattr(tf.keras.datasets, name)
    num_classes = 10
    raw_data = dataset.load_data()
    ((images_train, labels_train), (images_test, labels_test)) = raw_data
    images_train = (images_train.astype(np.float32) / 255.0)
    images_test = (images_test.astype(np.float32) / 255.0)
    labels_train = labels_train.astype(np.int32).squeeze()
    labels_test = labels_test.astype(np.int32).squeeze()
    if (images_train.ndim == 3):
        images_train = np.expand_dims(images_train, (- 1))
        images_test = np.expand_dims(images_test, (- 1))
    train_data = tf.data.Dataset.from_tensor_slices((images_train, labels_train))
    test_data = tf.data.Dataset.from_tensor_slices((images_test, labels_test))
    train_iterator = train_data.shuffle(buffer_size=len(images_train)).batch(train_batch_size).repeat().make_one_shot_iterator()
    test_iterator = test_data.batch(test_batch_size).make_initializable_iterator()
    return dict(train_iterator=train_iterator, test_iterator=test_iterator, num_classes=num_classes)

Gets training and testing dataset iterators.

Args:
name: String. Name of dataset, either 'mnist' or 'cifar10'.
train_batch_size: Integer. Batch size for training.
test_batch_size: Integer. Batch size for testing.

Returns:
Dict containing:
train_iterator: A tf.data.Iterator, over training data.
test_iterator: A tf.data.Iterator, over test data.
num_classes: Integer. Number of class labels.


def get_timestamp(self, url, xpath=None):
    if (not path.exists(self.db_path)):
        return None
    if (self._query(url, xpath).count() > 0):
        return self._query(url, xpath).one().queried_on

Get time stamp of cached query result.

If DB has not yet been initialized or url/xpath has not been queried yet, return None.

Args:
url (str): If given, clear specific item only. Otherwise remove the DB file.
xpath (str): xpath to search (may be ``None``)

Returns:
datetime.datetime: cached response timestamp, None if not available