def avg(self, vars_list: List[str]) -> 'TensorFluent':
    operand = self
    if (operand.dtype == tf.bool):
        operand = operand.cast(tf.float32)
    return self._aggregation_op(tf.reduce_mean, operand, vars_list)

Returns the TensorFluent for the avg aggregation function.

Args:
vars_list: The list of variables to be aggregated over.

Returns:
A TensorFluent wrapping the avg aggregation function.


def batch_flatten(x):
    x = array_ops.reshape(x, array_ops_stack.stack([-1, prod(shape(x)[1:])]))
    return x

Turn a nD tensor into a 2D tensor with same 0th dimension.

In other words, it flattens each data samples of a batch.

Args:
x: A tensor or variable.

Returns:
A tensor.

Examples:
Flattening a 3D tensor to 2D by collapsing the last dimension.

>>> x_batch = tf.keras.backend.ones(shape=(2, 3, 4, 5))
>>> x_batch_flatten = batch_flatten(x_batch)
>>> tf.keras.backend.int_shape(x_batch_flatten)
(2, 60)


def __init__(self, model_name: str, columns: list[str], api_key: Optional[str]=None, organization: Optional[str]=None, dimensions: Optional[int]=None, user: Optional[str]=None, max_batch_size: Optional[int]=None, **kwargs):
    self.model_name = model_name
    self.api_key = api_key
    self.organization = organization
    self.dimensions = dimensions
    self.user = user
    self.max_batch_size = max_batch_size
    super().__init__(columns=columns, **kwargs)

Embedding Config for OpenAI Text Embedding models.
Text Embeddings are generated for a batch of text using the OpenAI API.

Args:
model_name: Name of the OpenAI embedding model
columns: The columns where the embeddings will be stored in the output
api_key: OpenAI API key
organization: OpenAI organization ID
dimensions: Specific embedding dimensions to use (if model supports it)
user: End-user identifier for tracking and rate limit calculations
max_batch_size: Maximum batch size for requests to OpenAI API


def make_grid(tensor, nrow=8, padding=2, pad_value=0):

    if not (isinstance(tensor, np.ndarray) or
            (isinstance(tensor, list) and all(isinstance(t, np.ndarray) for t in tensor))):
        raise TypeError('tensor or list of tensors expected, got {}'.format(type(tensor)))

    if isinstance(tensor, list):
        tensor = np.stack(tensor, 0)

    if tensor.ndim == 2:
        tensor = tensor.reshape((1, tensor.shape[0], tensor.shape[1]))

    if tensor.ndim == 3:
        if tensor.shape[0] == 1:
            tensor = np.concatenate((tensor, tensor, tensor), 0)
        tensor = tensor.reshape((1, tensor.shape[0], tensor.shape[1], tensor.shape[2]))

    if tensor.ndim == 4 and tensor.shape[1] == 1:
        tensor = np.concatenate((tensor, tensor, tensor), 1)

    if tensor.shape[0] == 1:
        return np.squeeze(tensor)

    nmaps = tensor.shape[0]
    xmaps = min(nrow, nmaps)
    ymaps = int(math.ceil(float(nmaps) / xmaps))
    height, width = int(tensor.shape[2] + padding), int(tensor.shape[3] + padding)
    grid = np.ones((3, height * ymaps + padding, width * xmaps + padding)) * pad_value
    k = 0
    for y in range(ymaps):
        for x in range(xmaps):
            if k >= nmaps:
                break
            grid[:, y * height + padding:(y+1) * height,\
                 x * width + padding:(x+1) * width] = tensor[k]
            k = k + 1
    return grid

Make a grid of images, via numpy.

Args:
tensor (Tensor or list): 4D mini-batch Tensor of shape (B x C x H x W)
or a list of images all of the same size.
nrow (int, optional): Number of images displayed in each row of the grid.
The Final grid size is (B / nrow, nrow). Default is 8.
padding (int, optional): amount of padding. Default is 2.
pad_value (float, optional): Value for the padded pixels.


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

Returns the master string for connecting to a TensorFlow master.

Args:
task_type: (Optional) Overrides the default auto-selected task type.
task_id: (Optional) Overrides the default auto-selected task index.
rpc_layer: (Optional) Overrides the default RPC protocol TensorFlow uses
to communicate across nodes.

Returns:
A connection string for connecting to a TensorFlow master.


def create_toolbutton(entries, parent=None):

    btn = QtGui.QToolButton(parent)
    menu = QtGui.QMenu()
    actions = []

    for label, slot in entries:
        action = add_menu_action(menu, label, slot)
        actions.append(action)

    btn.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
    btn.setDefaultAction(actions[0])
    btn.setMenu(menu)
    return btn, actions

Create a toolbutton.

Args:
entries: List of (label, slot) tuples.

Returns:
`QtGui.QToolBar`.


def _fit(self, col):
    column = col[self.col_name].replace({np.nan: np.inf})
    frequencies = column.groupby(column).count().rename({np.inf: None}).to_dict()
    start = 0
    end = 0
    num_vals = len(col)
    for val in frequencies:
        prob = (frequencies[val] / num_vals)
        end = (start + prob)
        interval = (start, end)
        mean = np.mean(interval)
        std = (prob / 6)
        self.probability_map[val] = (interval, mean, std)
        start = end

Create a map of the empirical probability for each category.

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


def compose_containerized_launch_cmd(self, filepath, engine_dir, container_image):

        self.engine_file = os.path.expanduser(filepath)
        uid = str(uuid.uuid4())
        engine_json = None
        try:
            with open(self.engine_file, 'r') as f:
                engine_json = f.read()

        except OSError as e:
            logger.error("Could not open engine_json : ", self.engine_file)
            raise e

        return .format(engine_dir, engine_json, container_image, debug_option=self.debug_option, uid=uid)

Reads the json contents from filepath and uses that to compose the engine launch command.

Notes: Add this to the ipengine launch for debug logs :
--log-to-file --debug
Args:
filepath (str): Path to the engine file
engine_dir (str): CWD for the engines .
container_image (str): The container to be used to launch workers


def _find_elements(self, result, elements):
    element_mapping = {}
    result = StringIO.StringIO(result)
    for (_, e) in ET.iterparse(result, events=('end',)):
        if (not elements):
            break
        if (e.tag in elements):
            element_mapping[e.tag] = e.text
            elements.remove(e.tag)
    return element_mapping

Find interesting elements from XML.

This function tries to only look for specified elements
without parsing the entire XML. The specified elements is better
located near the beginning.

Args:
result: response XML.
elements: a set of interesting element tags.

Returns:
A dict from element tag to element value.


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

        return super(ParameterValidator, self)._validate(*args, **kwargs)

Validate a parameter dict against a parameter schema from an ocrd-tool.json

Args:
obj (dict):
schema (dict):


def fib(n):
    assert (n > 0)
    (a, b) = (1, 1)
    for i in range((n - 1)):
        (a, b) = (b, (a + b))
    return a

Fibonacci example function

Args:
n (int): integer

Returns:
int: n-th Fibonacci number


def recipe_trends_places_to_sheets_via_value(config, auth_write, secret, key, places_dataset, places_query, places_legacy, destination_sheet, destination_tab):
    twitter(config, {'auth': auth_write, 'secret': secret, 'key': key, 'trends': {'places': {'single_cell': True, 'bigquery': {'dataset': places_dataset, 'query': places_query, 'legacy': places_legacy}}}, 'out': {'sheets': {'sheet': destination_sheet, 'tab': destination_tab, 'range': 'A1'}}})

Move using hard coded WOEID values.

Args:
auth_write (authentication) - Credentials used for writing data.
secret (string) - NA
key (string) - NA
places_dataset (string) - NA
places_query (string) - NA
places_legacy (boolean) - NA
destination_sheet (string) - NA
destination_tab (string) - NA


def share(self, group_id, group_access, expires_at=None, **kwargs):

        path = '/projects/%s/share' % self.get_id()
        data = {'group_id': group_id,
                'group_access': group_access,
                'expires_at': expires_at}
        self.manager.gitlab.http_post(path, post_data=data, **kwargs)

Share the project with a group.

Args:
group_id (int): ID of the group.
group_access (int): Access level for the group.
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabCreateError: If the server failed to perform the request


def rep1sep(parser: Union[(Parser, Sequence[Input])], separator: Union[(Parser, Sequence[Input])]) -> RepeatedOnceSeparatedParser:
    if isinstance(parser, str):
        parser = lit(parser)
    if isinstance(separator, str):
        separator = lit(separator)
    return RepeatedOnceSeparatedParser(parser, separator)

Match a parser one or more times separated by another parser.

This matches repeated sequences of ``parser`` separated by ``separator``.
If there is at least one match, a list containing the values of the
``parser`` matches is returned. The values from ``separator`` are discarded.
If it does not match ``parser`` at all, it fails.

Args:
parser: Parser or literal
separator: Parser or literal


def _add_sparse_to_tensors_map(sp_input, container=None, shared_name=None, name=None):
    sp_input = _convert_to_sparse_tensor(sp_input)
    return gen_sparse_ops.add_sparse_to_tensors_map(sp_input.indices, sp_input.values, sp_input.dense_shape, container=container, shared_name=shared_name, name=name)

Add a `SparseTensor` to a `SparseTensorsMap` and return its handle.

Args:
sp_input: The input `SparseTensor`.
container: The container for the underlying `SparseTensorsMap` (optional).
shared_name: The shared name for the underlying `SparseTensorsMap`
(optional, defaults to the name of the newly created op).
name: A name prefix for the returned tensors (optional).

Returns:
A string 1-vector (1D `Tensor`), with the single element representing the
a unique handle to a `SparseTensor` stored by the `SparseTensorMap`
underlying this op.

Raises:
TypeError: If `sp_input` is not a `SparseTensor`.


def to_event(self, event_type, field_name=None, depth=None):
    if (self.ion_event is None):
        value = self
        if isinstance(self, IonPyNull):
            value = None
        self.ion_event = IonEvent(event_type, ion_type=self.ion_type, value=value, field_name=field_name, annotations=self.ion_annotations, depth=depth)
    return self.ion_event

Constructs an IonEvent from this _IonNature value.

Args:
event_type (IonEventType): The type of the resulting event.
field_name (Optional[text]): The field name associated with this value, if any.
depth (Optional[int]): The depth of this value.

Returns:
An IonEvent with the properties from this value.


def try_get_column(column_name, node, context):
    selectable = get_node_selectable(node, context)
    if (not hasattr(selectable, 'c')):
        raise AssertionError(u'Selectable "{}" does not have a column collection. Context is {}.'.format(selectable, context))
    return selectable.c.get(column_name, None)

Attempt to get a column by name from the selectable.

Args:
column_name: str, name of the column to retrieve.
node: SqlNode, the node the column is being retrieved for.
context: CompilationContext, compilation specific metadata.

Returns:
Optional[column], the SQLAlchemy column if found, None otherwise.


def flash_progress_callback(action, progress_string, percentage):

    if action.lower() != 'compare':
        return progress_bar(min(100, percentage), 100, prefix=action)

    return None

Callback that can be used with ``JLink.flash()``.

This callback generates a progress bar in the console to show the progress
of each of the steps of the flash.

Args:
action (str): the current action being invoked
progress_string (str): the current step in the progress
percentage (int): the percent to which the current step has been done

Returns:
``None``

Note:
This function ignores the compare action.


def from_json(cls, json):

    return cls(json[cls.BLOB_KEY_PARAM],
               json[cls.START_INDEX_PARAM],
               json[cls.END_INDEX_PARAM])

Creates an instance of the InputReader for the given input shard state.

Args:
json: The InputReader state as a dict-like object.

Returns:
An instance of the InputReader configured using the values of json.


def _InitializeParserObjects(self, parser_filter_expression=None):
    (self._formats_with_signatures, non_sigscan_parser_names) = parsers_manager.ParsersManager.GetFormatsWithSignatures(parser_filter_expression=parser_filter_expression)
    self._non_sigscan_parser_names = []
    for parser_name in non_sigscan_parser_names:
        if (parser_name not in ('filestat', 'usnjrnl')):
            self._non_sigscan_parser_names.append(parser_name)
    self._file_scanner = parsers_manager.ParsersManager.CreateSignatureScanner(self._formats_with_signatures)
    self._parsers = parsers_manager.ParsersManager.GetParserObjects(parser_filter_expression=parser_filter_expression)
    active_parser_names = ', '.join(sorted(self._parsers.keys()))
    logger.debug('Active parsers: {0:s}'.format(active_parser_names))
    self._filestat_parser = self._parsers.get('filestat', None)
    if ('filestat' in self._parsers):
        del self._parsers['filestat']
    self._mft_parser = self._parsers.get('mft', None)
    self._usnjrnl_parser = self._parsers.get('usnjrnl', None)
    if ('usnjrnl' in self._parsers):
        del self._parsers['usnjrnl']

Initializes the parser objects.

Args:
parser_filter_expression (Optional[str]): the parser filter expression,
None represents all parsers and plugins.

The parser filter expression is a comma separated value string that
denotes a list of parser names to include and/or exclude. Each entry
can have the value of:

* An exact match of a list of parsers, or a preset (see
data/presets.yaml for the list of predefined presets).
* A name of a single parser (case insensitive), e.g. msiecf.
* A glob name for a single parser, e.g. '*msie*' (case insensitive).


def processMailList(platformNames=[], emails=[]):

    platforms = platform_selection.getPlatformsByName(platformNames, mode="mailfy")

    results = []
    for e in emails:
        for pla in platforms:

            entities = pla.getInfo(query=e, mode="mailfy")
            if entities != {}:
                results += json.loads(entities)
    return results

Method to perform the email search.

Args:
-----
platformNames: List of names of the platforms.
emails: List of numbers to be queried.

Return:
-------
A list of verified emails.


def _check_state_for_finalize_write(self, writer_results, num_shards):
    if not writer_results:
        return ([], [], [], 0)
    src_glob = FileSystems.join(FileSystems.split(writer_results[0])[0], '*')
    dst_glob = self._get_final_name_glob(num_shards)
    src_glob_files = set((file_metadata.path for mr in FileSystems.match([src_glob]) for file_metadata in mr.metadata_list))
    dst_glob_files = set((file_metadata.path for mr in FileSystems.match([dst_glob]) for file_metadata in mr.metadata_list))
    src_files = []
    dst_files = []
    delete_files = []
    num_skipped = 0
    for shard_num, src in enumerate(writer_results):
        final_name = self._get_final_name(shard_num, num_shards)
        dst = final_name
        src_exists = src in src_glob_files
        dst_exists = dst in dst_glob_files
        if not src_exists and (not dst_exists):
            raise BeamIOError('src and dst files do not exist. src: %s, dst: %s' % (src, dst))
        if not src_exists and dst_exists:
            _LOGGER.debug('src: %s -> dst: %s already renamed, skipping', src, dst)
            num_skipped += 1
            continue
        if src_exists and dst_exists and (FileSystems.checksum(src) == FileSystems.checksum(dst)):
            _LOGGER.debug('src: %s == dst: %s, deleting src', src, dst)
            delete_files.append(src)
            continue
        src_files.append(src)
        dst_files.append(dst)
    self._report_sink_lineage(dst_glob, dst_files)
    return (src_files, dst_files, delete_files, num_skipped)

Checks writer output files' states.

Returns:
src_files, dst_files: Lists of files to rename. For each i, finalize_write
should rename(src_files[i], dst_files[i]).
delete_files: Src files to delete. These could be leftovers from an
incomplete (non-atomic) rename operation.
num_skipped: Tally of writer results files already renamed, such as from
a previous run of finalize_write().


def sub_index(self, sub, start=0, end=None):
    start_index = self.index(sub[0], start, end)
    end = self._fix_end_index(end)
    if ((start_index + len(sub)) > end):
        raise ValueError
    for i in range(1, len(sub)):
        if (sub[i] != self[(start_index + i)]):
            raise ValueError
    return start_index

Return the index of a subsequence.

This runs in O(len(sub))

Args:
sub (Sequence): An Iterable to search for
Returns:
int: The index of the first element of sub
Raises:
ValueError: If sub isn't a subsequence
TypeError: If sub isn't iterable
IndexError: If start or end are out of range


def encode(self, input_ids: jnp.ndarray, attention_mask: Optional[jnp.ndarray]=None, position_ids: Optional[jnp.ndarray]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, train: bool=False, params: Optional[dict]=None, dropout_rng: PRNGKey=None):
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.return_dict
    if attention_mask is None:
        attention_mask = jnp.ones_like(input_ids)
    if position_ids is None:
        batch_size, sequence_length = input_ids.shape
        position_ids = jnp.broadcast_to(jnp.arange(sequence_length)[None, :], (batch_size, sequence_length))
    rngs = {}
    if dropout_rng is not None:
        rngs['dropout'] = dropout_rng

    def _encoder_forward(module, input_ids, attention_mask, position_ids, **kwargs):
        encode_module = module._get_encoder_module()
        return encode_module(input_ids, attention_mask, position_ids, **kwargs)
    outputs = self.module.apply({'params': params or self.params}, input_ids=jnp.array(input_ids, dtype='i4'), attention_mask=jnp.array(attention_mask, dtype='i4'), position_ids=jnp.array(position_ids, dtype='i4'), output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, deterministic=not train, rngs=rngs, method=_encoder_forward)
    if return_dict:
        outputs = FlaxBaseModelOutput(last_hidden_state=outputs.last_hidden_state, hidden_states=outputs.hidden_states, attentions=outputs.attentions)
    return outputs

Returns:

Example:

```python
>>> from transformers import FlaxEncoderDecoderModel, BertTokenizer

>>> # initialize a bert2gpt2 from pretrained BERT and GPT2 models. Note that the cross-attention layers will be randomly initialized
>>> model = FlaxEncoderDecoderModel.from_encoder_decoder_pretrained("google-bert/bert-base-cased", "openai-community/gpt2")

>>> tokenizer = BertTokenizer.from_pretrained("google-bert/bert-base-cased")

>>> text = "My friends are cool but they eat too many carbs."
>>> input_ids = tokenizer.encode(text, return_tensors="np")
>>> encoder_outputs = model.encode(input_ids)
```


def _process_scopes(scopes):
    all_scopes = set()
    sufficient_scopes = set()
    for scope_set in scopes:
        scope_set_scopes = frozenset(scope_set.split())
        all_scopes.update(scope_set_scopes)
        sufficient_scopes.add(scope_set_scopes)
    return (all_scopes, sufficient_scopes)

Parse a scopes list into a set of all scopes and a set of sufficient scope sets.

scopes: A list of strings, each of which is a space-separated list of scopes.
Examples: ['scope1']
['scope1', 'scope2']
['scope1', 'scope2 scope3']

Returns:
all_scopes: a set of strings, each of which is one scope to check for
sufficient_scopes: a set of sets of strings; each inner set is
a set of scopes which are sufficient for access.
Example: {{'scope1'}, {'scope2', 'scope3'}}


def dot(inputs, axes=-1, **kwargs):
    return Dot(axes=axes, **kwargs)(inputs)

Functional interface to the `Dot` layer.

Args:
inputs: A list of input tensors (at least 2).
axes: Integer or tuple of integers,
axis or axes along which to take the dot product.
normalize: Whether to L2-normalize samples along the
dot product axis before taking the dot product.
If set to `True`, then the output of the dot product
is the cosine proximity between the two samples.
**kwargs: Standard layer keyword arguments.

Returns:
A tensor, the dot product of the samples from the inputs.


def permute(self, ordering: np.ndarray, *, axis: int) -> None:
    if (axis not in (0, 1)):
        raise ValueError('Axis must be 0 (rows) or 1 (columns)')
    for layer in self.layers.values():
        layer._permute(ordering, axis=axis)
    if (axis == 0):
        if (self.row_graphs is not None):
            for g in self.row_graphs.values():
                g._permute(ordering)
        for a in self.row_attrs.values():
            a._permute(ordering)
    elif (axis == 1):
        if (self.col_graphs is not None):
            for g in self.col_graphs.values():
                g._permute(ordering)
        for a in self.col_attrs.values():
            a._permute(ordering)

Permute the view, by permuting its layers, attributes and graphs

Args:
ordering (np.ndarray):	The desired ordering along the axis
axis (int):				0, permute rows; 1, permute columns


def _process_worker(call_queue, result_queue):

    while True:
        call_item = call_queue.get(block=True)
        if call_item is None:

            result_queue.put(None)
            return
        try:
            r = call_item.fn(*call_item.args, **call_item.kwargs)
        except BaseException:
            e = sys.exc_info()[1]
            result_queue.put(_ResultItem(call_item.work_id,
                                         exception=e))
        else:
            result_queue.put(_ResultItem(call_item.work_id,
                                         result=r))

Evaluates calls from call_queue and places the results in result_queue.

This worker is run in a separate process.

Args:
call_queue: A multiprocessing.Queue of _CallItems that will be read and
evaluated by the worker.
result_queue: A multiprocessing.Queue of _ResultItems that will written
to by the worker.
shutdown: A multiprocessing.Event that will be set as a signal to the
worker that it should exit when call_queue is empty.


def parse_location(location):

    def split_dms(text, hemisphere):

        out = []
        sect = []
        for i in text:
            if i.isdigit():
                sect.append(i)
            else:
                out.append(sect)
                sect = []
        d, m, s = [float(''.join(i)) for i in out]
        if hemisphere in 'SW':
            d, m, s = [-1 * x for x in (d, m, s)]
        return to_dd(d, m, s)

    for sep in ';, ':
        chunks = location.split(sep)
        if len(chunks) == 2:
            if chunks[0].endswith('N'):
                latitude = float(chunks[0][:-1])
            elif chunks[0].endswith('S'):
                latitude = -1 * float(chunks[0][:-1])
            else:
                latitude = float(chunks[0])
            if chunks[1].endswith('E'):
                longitude = float(chunks[1][:-1])
            elif chunks[1].endswith('W'):
                longitude = -1 * float(chunks[1][:-1])
            else:
                longitude = float(chunks[1])
            return latitude, longitude
        elif len(chunks) == 4:
            if chunks[0].endswith(('s', '"')):
                latitude = split_dms(chunks[0], chunks[1])
            else:
                latitude = float(chunks[0])
                if chunks[1] == 'S':
                    latitude = -1 * latitude
            if chunks[2].endswith(('s', '"')):
                longitude = split_dms(chunks[2], chunks[3])
            else:
                longitude = float(chunks[2])
                if chunks[3] == 'W':
                    longitude = -1 * longitude
            return latitude, longitude

Parse latitude and longitude from string location.

Args:
location (str): String to parse

Returns:
tuple of float: Latitude and longitude of location


def to_unicode(self, s):
    if isinstance(s, unicode):
        return s
    if isinstance(s, str):
        return unicode(s, errors='ignore')
    return s

Convert an elementary datatype to unicode.

Args:
s: the datatype to be unicoded.

Returns:
Unicoded data.


def _CheckAtLeast3DImage(image, require_static=True):
    try:
        if image.get_shape().ndims is None:
            image_shape = image.get_shape().with_rank(3)
        else:
            image_shape = image.get_shape().with_rank_at_least(3)
    except ValueError:
        raise ValueError("'image' (shape %s) must be at least three-dimensional." % image.shape)
    if require_static and (not image_shape.is_fully_defined()):
        raise ValueError("'image' must be fully defined.")
    if any((x == 0 for x in image_shape[-3:])):
        raise ValueError("inner 3 dims of 'image.shape' must be > 0: %s" % image_shape)
    if not image_shape[-3:].is_fully_defined():
        return [check_ops.assert_positive(array_ops.shape(image)[-3:], ["inner 3 dims of 'image.shape' must be > 0."]), check_ops.assert_greater_equal(array_ops.rank(image), 3, message="'image' must be at least three-dimensional.")]
    else:
        return []

Assert that we are working with a properly shaped image.

Args:
image: >= 3-D Tensor of size [*, height, width, depth]
require_static: If `True`, requires that all dimensions of `image` are known
and non-zero.

Raises:
ValueError: if image.shape is not a [>= 3] vector.

Returns:
An empty list, if `image` has fully defined dimensions. Otherwise, a list
containing an assert op is returned.


def zeros_like(x, dtype=None):
    if any_symbolic_tensors((x,)):
        return ZerosLike(dtype=dtype).symbolic_call(x)
    return backend.numpy.zeros_like(x, dtype=dtype)

Return a tensor of zeros with the same shape and type as `x`.

Args:
x: Input tensor.
dtype: Overrides the data type of the result.

Returns:
A tensor of zeros with the same shape and type as `x`.


def softmax_cross_entropy_one_hot(logits, labels, weights_fn=None):

  with tf.variable_scope("softmax_cross_entropy_one_hot",
                         values=[logits, labels]):
    del weights_fn
    cross_entropy = tf.losses.softmax_cross_entropy(
        onehot_labels=labels, logits=logits)
    return cross_entropy, tf.constant(1.0)

Calculate softmax cross entropy given one-hot labels and logits.

Args:
logits: Tensor of size [batch-size, o=1, p=1, num-classes]
labels: Tensor of size [batch-size, o=1, p=1, num-classes]
weights_fn: Function that takes in labels and weighs examples (unused)
Returns:
cross-entropy (scalar), weights


def _get_associated_classnames(self, classname, namespace, assoc_class, result_class, result_role, role):
    class_repo = self._get_class_repo(namespace)
    result_classes = self._classnamedict(result_class, namespace)
    assoc_classes = self._classnamedict(assoc_class, namespace)
    rtn_classnames_set = set()
    role = (role.lower() if role else role)
    result_role = (result_role.lower() if result_role else result_role)
    ref_clns = self._get_reference_classnames(classname, namespace, assoc_class, role)
    cls = [class_repo[cln] for cln in ref_clns]
    for cl in cls:
        for prop in six.itervalues(cl.properties):
            if (prop.type == 'reference'):
                if self._assoc_prop_matches(prop, cl.classname, assoc_classes, result_classes, result_role):
                    rtn_classnames_set.add(prop.reference_class)
    return list(rtn_classnames_set)

Get list of classnames that are associated classes for which this
classname is a target filtered by the assoc_class, role, result_class,
and result_role parameters if they are none.

This is a common method used by all of the other reference and
associator methods to create a list of reference classnames

Returns:
list of classnames that satisfy the criteria.


def deserialize_subject_info(subject_info_xml):

    try:
        return d1_common.xml.deserialize(subject_info_xml)
    except ValueError as e:
        raise d1_common.types.exceptions.InvalidToken(
            0,
            'Could not deserialize SubjectInfo. subject_info="{}", error="{}"'.format(
                subject_info_xml, str(e)
            ),
        )

Deserialize SubjectInfo XML doc to native object.

Args:
subject_info_xml: str
SubjectInfo XML doc

Returns:
SubjectInfo PyXB object


def forward(self, hidden_states: torch.Tensor, grid_thw: torch.Tensor) -> torch.Tensor:
    hidden_states = self.patch_embed(hidden_states)
    rotary_pos_emb = self.rot_pos_emb(grid_thw)
    window_index, cu_window_seqlens = self.get_window_index(grid_thw)
    cu_window_seqlens = torch.tensor(cu_window_seqlens, device=hidden_states.device, dtype=grid_thw.dtype if torch.jit.is_tracing() else torch.int32)
    cu_window_seqlens = torch.unique_consecutive(cu_window_seqlens)
    seq_len, _ = hidden_states.size()
    hidden_states = hidden_states.reshape(seq_len
    hidden_states = hidden_states[window_index, :, :]
    hidden_states = hidden_states.reshape(seq_len, -1)
    rotary_pos_emb = rotary_pos_emb.reshape(seq_len
    rotary_pos_emb = rotary_pos_emb[window_index, :, :]
    rotary_pos_emb = rotary_pos_emb.reshape(seq_len, -1)
    cu_seqlens = torch.repeat_interleave(grid_thw[:, 1] * grid_thw[:, 2], grid_thw[:, 0]).cumsum(dim=0, dtype=grid_thw.dtype if torch.jit.is_tracing() else torch.int32)
    cu_seqlens = F.pad(cu_seqlens, (1, 0), value=0)
    for layer_num, blk in enumerate(self.blocks):
        if layer_num in self.fullatt_block_indexes:
            cu_seqlens_now = cu_seqlens
        else:
            cu_seqlens_now = cu_window_seqlens
        if self.gradient_checkpointing and self.training:
            hidden_states = self._gradient_checkpointing_func(blk.__call__, hidden_states, cu_seqlens_now, rotary_pos_emb)
        else:
            hidden_states = blk(hidden_states, cu_seqlens=cu_seqlens_now, rotary_pos_emb=rotary_pos_emb)
    hidden_states = self.merger(hidden_states)
    reverse_indices = torch.argsort(window_index)
    hidden_states = hidden_states[reverse_indices, :]
    return hidden_states

Args:
hidden_states (`torch.Tensor` of shape `(seq_len, hidden_size)`):
The final hidden states of the model.
grid_thw (`torch.Tensor` of shape `(num_images_or_videos, 3)`):
The temporal, height and width of feature shape of each image in LLM.

Returns:
`torch.Tensor`: hidden_states.


def query(self, src: Any, use_inferred: bool=False) -> Any:
    return self._query(0, src, use_inferred)

Query the value from the source object based on current path.

Example::

@pg.members([
('x', pg.typing.Int()),
('y', pg.typing.Str())
])
class A(pg.Object):
pass

@pg.members([
('z', pg.typing.Object(A))
])
class B(pg.Object):
pass

b = B(z=A(x=1, y='foo'))
assert pg.KeyPath.parse('z.x').query(b) == 1

Args:
src: Source value to query.
use_inferred: If True, infer `pg.Inferential` values. Otherwise returns
their symbolic form. Applicable only for symbolic values.

Returns:
Value from src if path exists.

Raises:
KeyError: Path doesn't exist in src.
RuntimeError: Called on a KeyPath that is considered as removed.


def _is_valid(self, value):

        if hasattr(self._type, "istypeof"):
            return self._type.istypeof(value)
        else:
            return isinstance(value, self._type)

Return True if the input value is valid for insertion into the
inner list.

Args:
value: An object about to be inserted.


def set_shape(self, shape):
    self._ref().set_shape(shape)
    self.value().set_shape(shape)

Overrides the shape for this variable.

Args:
shape: the `TensorShape` representing the overridden shape.


def sort_ordered_objects(items, getter=lambda x: x):

    return sorted(items, key=lambda x: getattr(getter(x), OrderedBase.CREATION_COUNTER_FIELD, -1))

Sort an iterable of OrderedBase instances.

Args:
items (iterable): the objects to sort
getter (callable or None): a function to extract the OrderedBase instance from an object.

Examples:
>>> sort_ordered_objects([x, y, z])
>>> sort_ordered_objects(v.items(), getter=lambda e: e[1])


def rename_nodes(self, renaming_map):

        if not isinstance(renaming_map, dict):
            raise TypeError("renaming_map must be a dict")
        for node in self.traverse_preorder():
            if node.label in renaming_map:
                node.label = renaming_map[node.label]

Rename nodes in this ``Tree``

Args:
``renaming_map`` (``dict``): A dictionary mapping old labels (keys) to new labels (values)


def is_done(self, transform: Optional[AppliedPTransform]=None) -> bool:
    if transform:
        return self._is_transform_done(transform)
    for applied_ptransform in self._step_names:
        if not self._is_transform_done(applied_ptransform):
            return False
    return True

Checks completion of a step or the pipeline.

Args:
transform: AppliedPTransform to check for completion.

Returns:
True if the step will not produce additional output. If transform is None
returns true if all steps are done.


def queryString_required(strList):

	def _dec(function):
		@wraps(function)
		def _wrap(request, *args, **kwargs):
			for i in strList:
				if i not in request.GET:
					raise Http404("api does not exist")
			return function(request, *args, **kwargs)
		return _wrap
	return _dec

An decorator checking whether queryString key is valid or not
Args:
str: allowed queryString key

Returns:
if contains invalid queryString key, it will raise exception.


def modutf7_decode(data: bytes) -> str:

    parts = []
    is_usascii = True
    buf = memoryview(data)
    while buf:
        byte = buf[0]
        if is_usascii:
            if buf[0:2] == b'&-':
                parts.append('&')
                buf = buf[2:]
            elif byte == 0x26:
                is_usascii = False
                buf = buf[1:]
            else:
                parts.append(chr(byte))
                buf = buf[1:]
        else:
            for i, byte in enumerate(buf):
                if byte == 0x2d:
                    to_decode = buf[:i].tobytes()
                    decoded = _modified_b64decode(to_decode)
                    parts.append(decoded)
                    buf = buf[i + 1:]
                    is_usascii = True
                    break
    if not is_usascii:
        to_decode = buf.tobytes()
        decoded = _modified_b64decode(to_decode)
        parts.append(decoded)
    return ''.join(parts)

Decode the bytestring using modified UTF-7.

Args:
data: The encoded bytestring to decode.


def reset( self ):

        self.lattice.reset()
        for atom in self.atoms.atoms:
            atom.reset()

Reset all counters for this simulation.

Args:
None

Returns:
None


def append(self, node):

    if not isinstance(node, grammar.STATEMENTS):
      raise ValueError
    self.to_append[-1].append(node)

Append a statement to the current statement.

Note that multiple calls to append will result in the last statement to be
appended to end up at the bottom.

Args:
node: The statement to append.

Raises:
ValueError: If the given node is not a statement.


def clone(self, opts):

        topt = self.opts.copy()
        topt.update(opts)
        return self.__class__(self.modl, self.name, self.info, topt)

Create a new instance of this type with the specified options.

Args:
opts (dict): The type specific options for the new instance.


def __add__(self, other):
    ret = RichLine()
    if isinstance(other, str):
        ret.text = self.text + other
        ret.font_attr_segs = self.font_attr_segs[:]
        return ret
    elif isinstance(other, RichLine):
        ret.text = self.text + other.text
        ret.font_attr_segs = self.font_attr_segs[:]
        old_len = len(self.text)
        for start, end, font_attr in other.font_attr_segs:
            ret.font_attr_segs.append((old_len + start, old_len + end, font_attr))
        return ret
    else:
        raise TypeError('%r cannot be concatenated with a RichLine' % other)

Concatenate two chunks of maybe rich text to make a longer rich line.

Does not modify self.

Args:
other: Another piece of text to concatenate with this one.
If it is a plain str, it will be appended to this string with no
attributes.  If it is a RichLine, it will be appended to this string
with its attributes preserved.

Returns:
A new RichLine comprising both chunks of text, with appropriate
attributes applied to the corresponding substrings.


def _reciprocal_condition_number(lu_mat, one_norm):
    if (_scipy_lapack is None):
        raise OSError('This function requires SciPy for calling into LAPACK.')
    (rcond, info) = _scipy_lapack.dgecon(lu_mat, one_norm)
    if (info != 0):
        raise RuntimeError('The reciprocal 1-norm condition number could not be computed.')
    return rcond

r"""Compute reciprocal condition number of a matrix.

Args:
lu_mat (numpy.ndarray): A 2D array of a matrix :math:`A` that has been
LU-factored, with the non-diagonal part of :math:`L` stored in the
strictly lower triangle and :math:`U` stored in the upper triangle.
one_norm (float): The 1-norm of the original matrix :math:`A`.

Returns:
float: The reciprocal condition number of :math:`A`.

Raises:
OSError: If SciPy is not installed.
RuntimeError: If the reciprocal 1-norm condition number could not
be computed.


def get(self, key, default='', stringify=True):

    obj = self.__getitem__(key)
    if obj is None:
      obj = default
    elif stringify:
      obj = str(obj)
    return obj

Returns dictionary values or default.

Args:
key: string. Dictionary key to look up.
default: string. Return this value if key not found.
stringify: bool. Force all return values to string for compatibility
reasons.
Returns:
python-wrapped CF object or default if not found.


def _ParseCommon2003CachedEntry(self, value_data, cached_entry_offset):

    data_type_map = self._GetDataTypeMap(
        'appcompatcache_cached_entry_2003_common')

    try:
      cached_entry = self._ReadStructureFromByteStream(
          value_data[cached_entry_offset:], cached_entry_offset, data_type_map)
    except (ValueError, errors.ParseError) as exception:
      raise errors.ParseError(
          'Unable to parse cached entry value with error: {0!s}'.format(
              exception))

    if cached_entry.path_size > cached_entry.maximum_path_size:
      raise errors.ParseError('Path size value out of bounds.')

    path_end_of_string_size = (
        cached_entry.maximum_path_size - cached_entry.path_size)
    if cached_entry.path_size == 0 or path_end_of_string_size != 2:
      raise errors.ParseError('Unsupported path size values.')

    return cached_entry

Parses the cached entry structure common for Windows 2003, Vista and 7.

Args:
value_data (bytes): value data.
cached_entry_offset (int): offset of the first cached entry data
relative to the start of the value data.

Returns:
appcompatcache_cached_entry_2003_common: cached entry structure common
for Windows 2003, Windows Vista and Windows 7.

Raises:
ParseError: if the value data could not be parsed.


def update(self, resource, id_or_uri=None, timeout=(- 1)):
    uri = resource.pop('uri', None)
    if (not uri):
        if (not id_or_uri):
            raise ValueError('URI was not provided')
        uri = self._client.build_uri(id_or_uri)
    return self._client.update(resource=resource, uri=uri, timeout=timeout)

Updates the specified alert resource.

Args:
resource (dict): Object to update.
timeout: Timeout in seconds. Wait for task completion by default. The timeout does not abort the operation
in OneView; it just stops waiting for its completion.

Returns:
dict: Updated alert.


def to(self, *args, **kwargs) -> 'BatchFeature':
    requires_backends(self, ['torch'])
    import torch
    device = kwargs.get('device')
    non_blocking = kwargs.get('non_blocking', False)
    if device is None and len(args) > 0:
        arg = args[0]
        if is_torch_dtype(arg):
            pass
        elif isinstance(arg, str) or is_torch_device(arg) or isinstance(arg, int):
            device = arg
        else:
            raise ValueError(f'Attempting to cast a BatchFeature to type {str(arg)}. This is not supported.')

    def maybe_to(v):
        if isinstance(v, torch.Tensor) and torch.is_floating_point(v):
            return v.to(*args, **kwargs)
        elif isinstance(v, torch.Tensor) and device is not None:
            return v.to(device=device, non_blocking=non_blocking)
        else:
            return v
    self.data = {k: maybe_to(v) for k, v in self.items()}
    return self

Send all values to device by calling `v.to(*args, **kwargs)` (PyTorch only). This should support casting in
different `dtypes` and sending the `BatchFeature` to a different `device`.

Args:
args (`Tuple`):
Will be passed to the `to(...)` function of the tensors.
kwargs (`Dict`, *optional*):
Will be passed to the `to(...)` function of the tensors.
To enable asynchronous data transfer, set the `non_blocking` flag in `kwargs` (defaults to `False`).

Returns:
[`BatchFeature`]: The same instance after modification.


def check_lines(first, second):
    if (not ((first.__class__ is Linearization) and (second.__class__ is Linearization) and (first.error == 0.0) and (second.error == 0.0))):
        return (False, None)
    (s, t, success) = segment_intersection(first.start_node, first.end_node, second.start_node, second.end_node)
    if success:
        if (_helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval(t, 0.0, 1.0)):
            intersections = np.asfortranarray([[s], [t]])
            result = (intersections, False)
        else:
            result = (np.empty((2, 0), order='F'), False)
    else:
        (disjoint, params) = parallel_lines_parameters(first.start_node, first.end_node, second.start_node, second.end_node)
        if disjoint:
            result = (np.empty((2, 0), order='F'), False)
        else:
            result = (params, True)
    return (True, result)

Checks if two curves are lines and tries to intersect them.

.. note::

This is a helper for :func:`._all_intersections`.

If they are not lines / not linearized, immediately returns :data:`False`
with no "return value".

If they are lines, attempts to intersect them (even if they are parallel
and share a coincident segment).

Args:
first (Union[SubdividedCurve, Linearization]): First curve being
intersected.
second (Union[SubdividedCurve, Linearization]): Second curve being
intersected.

Returns:
Tuple[bool, Optional[Tuple[numpy.ndarray, bool]]]: A pair of

* Flag indicating if both candidates in the pair are lines.
* Optional "result" populated only if both candidates are lines.
When this result is populated, it will be a pair of

* array of parameters of intersection
* flag indicating if the two candidates share a coincident segment


def add_authorization_policy(access_token, ck_id, oid):

    path = '/ContentKeys'
    body = '{"AuthorizationPolicyId":"' + oid + '"}'
    return helper_add(access_token, ck_id, path, body)

Add Media Service Authorization Policy.

Args:
access_token (str): A valid Azure authentication token.
ck_id (str): A Media Service Asset Content Key ID.
options_id (str): A Media Service OID.

Returns:
HTTP response. JSON body.


def encoding_specs(self, spec):
    raise NotImplementedError(f'{type(self).__name__}.encoding_specs')

Returns a nest of `TypeSpec`(s) describing the encoding for `spec`.

Args:
spec: The TypeSpec whose encoding should be described.

Returns:
A nest (as defined by `tf.nest) of `tf.TypeSpec`, describing the values
that are returned by `self.encode(spec, ...)`.  All TypeSpecs in this
nest must be batchable.


def get_task_info(self):
    return (self.task_type, self.task_id)

Returns job name and task_id for the process which calls this.

This returns the job name and task index for the process which calls this
function according to its rank and cluster specification. The job name and
task index are set after a cluster is constructed by cluster_spec otherwise
defaults to None.

Returns:
A string specifying job name the process belongs to and an integer
specifying the task index the process belongs to in that job.


def dump(self, conf_file=None):
    if conf_file:
        conf_dir = os.path.dirname(conf_file)
        if (not conf_dir):
            conf_dir = self.__invoke_dir
        elif (not os.path.exists(conf_dir)):
            os.makedirs(conf_dir)
    else:
        conf_dir = self.__conf_dir
    final_conf = {}
    for (key, value) in list(self.__config.items()):
        if (key in self.__cli):
            continue
        final_conf[key] = value
    for (key, value) in list(self.__cli.items()):
        if (key.endswith('index') or (key in ['sitemap', 'output'])):
            path = self.__abspath(value, from_conf=False)
            if path:
                relpath = os.path.relpath(path, conf_dir)
                final_conf[key] = relpath
        elif (key.endswith('sources') or key.endswith('source_filters')):
            new_list = []
            for path in value:
                path = self.__abspath(path, from_conf=False)
                if path:
                    relpath = os.path.relpath(path, conf_dir)
                    new_list.append(relpath)
            final_conf[key] = new_list
        elif (key not in ['command', 'output_conf_file']):
            final_conf[key] = value
    with open((conf_file or self.conf_file or 'hotdoc.json'), 'w') as _:
        _.write(json.dumps(final_conf, sort_keys=True, indent=4))

Dump the possibly updated config to a file.

Args:
conf_file: str, the destination, or None to overwrite the
existing configuration.


def content(self):
    if (self._content is None):
        self._content = self.parse_files()
    return self._content

Return parsed data. Parse it if not already parsed.

Returns:
list: list of dictionaries (one for each parsed line).


def get_lacp_mode(self, name):

        members = self.get_members(name)
        if not members:
            return DEFAULT_LACP_MODE

        for member in self.get_members(name):
            match = re.search(r'channel-group\s\d+\smode\s(?P<value>.+)',
                              self.get_block('^interface %s' % member))
            return match.group('value')

Returns the LACP mode for the specified Port-Channel interface

Args:
name(str): The Port-Channel interface name to return the LACP
mode for from the configuration

Returns:
The configured LACP mode for the interface.  Valid mode values
are 'on', 'passive', 'active'


def send_messages(self, email_messages):

        if not email_messages:
            return

        sent_message_count = 0

        for email_message in email_messages:
            if self._send(email_message):
                sent_message_count += 1
        return sent_message_count

Sends one or more EmailMessage objects and returns the
number of email messages sent.

Args:
email_messages: A list of Django EmailMessage objects.
Returns:
An integer count of the messages sent.
Raises:
ClientError: An interaction with the Amazon SES HTTP API
failed.


def float_value_convert(dictin, dropfailedvalues=False):
    return key_value_convert(dictin, valuefn=float, dropfailedvalues=dropfailedvalues)

Convert values of dictionary to floats

Args:
dictin (DictUpperBound): Input dictionary
dropfailedvalues (bool): Whether to drop dictionary entries where key conversion fails. Defaults to False.

Returns:
Dict: Dictionary with values converted to floats


def tokenize(self, string):
    s = string
    s = re.sub('\t', ' ', s)
    s = re.sub((('(' + regex_separator) + ')'), ' \\g<1> ', s)
    s = re.sub('([^0-9]),', '\\g<1> , ', s)
    s = re.sub(',([^0-9])', ' , \\g<1>', s)
    s = re.sub("^(')", '\\g<1> ', s)
    s = re.sub((('(' + regex_not_letter_number) + ")'"), "\\g<1> '", s)
    s = re.sub((('(' + regex_clitics) + ')$'), ' \\g<1>', s)
    s = re.sub((((('(' + regex_clitics) + ')(') + regex_not_letter_number) + ')'), ' \\g<1> \\g<2>', s)
    words = s.strip().split()
    p1 = re.compile((('.*' + regex_letter_number) + '\\.'))
    p2 = re.compile('^([A-Za-z]\\.([A-Za-z]\\.)+|[A-Z][bcdfghj-nptvxz]+\\.)$')
    token_list = []
    for word in words:
        m1 = p1.match(word)
        m2 = p2.match(word)
        if (m1 and (word not in abbreviations_list) and (not m2)):
            token_list.append(word[0:word.find('.')])
            token_list.append(word[word.find('.')])
        else:
            token_list.append(word)
    return token_list

Used to parce a string into tokens

This function is to take in a string and return a list of tokens

Args:
string(str): This is a string of words or a sentance to be parsed into tokens

Returns:
list: a list of tokens from the string passed in.

Notes:
Doesn't seem to parse contractions correctly for example don't
would parse as two tokens 'do' and "n't" and this seems to be not
what we would want.  Maybe should be "don't" or maybe contractions
should be expanded into "do not" or "do","not".  This could be
done with a contraction dictionary and some preprocessing.


def _actor_property(self, event, cameo_code, actor_regex):

        if cameo_code not in self.mapping:
            return None

        arguments = self.mapping[cameo_code][event + "-arguments"]
        if not isinstance(arguments, list):
            arguments = [arguments]

        result = list()
        for a in arguments:
            match = re.search(actor_regex, a)
            if match:
                result.append(match.group(1))
        return result[0] if len(result) > 0 else None

Determine the property to use for modeling an actor
Args:
event: one of "event1", "event2" or "event3"
cameo_code: one of the cameo codes
actor_regex: one of the regexes above

Returns:


def end_episode(self, agent_indices):

    with tf.name_scope('end_episode/'):
      return tf.cond(
          self._is_training,
          lambda: self._define_end_episode(agent_indices), str)

Add episodes to the memory and perform update steps if memory is full.

During training, add the collected episodes of the batch indices that
finished their episode to the memory. If the memory is full, train on it,
and then clear the memory. A summary string is returned if requested at
this step.

Args:
agent_indices: Tensor containing current batch indices.

Returns:
Summary tensor.


def check_session_id_signature(session_id, secret_key=settings.secret_key_bytes(), signed=settings.sign_sessions()):
    secret_key = _ensure_bytes(secret_key)
    if signed:
        pieces = session_id.split('-', 1)
        if (len(pieces) != 2):
            return False
        base_id = pieces[0]
        provided_signature = pieces[1]
        expected_signature = _signature(base_id, secret_key)
        return hmac.compare_digest(encode_utf8(expected_signature), encode_utf8(provided_signature))
    else:
        return True

Check the signature of a session ID, returning True if it's valid.

The server uses this function to check whether a session ID
was generated with the correct secret key. If signed sessions are disabled,
this function always returns True.

Args:
session_id (str) : The session ID to check
secret_key (str, optional) : Secret key (default: value of 'BOKEH_SECRET_KEY' env var)
signed (bool, optional) : Whether to check anything (default: value of
'BOKEH_SIGN_SESSIONS' env var)


def _normalize_mlengine_job_id(job_id):

    match = re.search(r'\d|\{{2}', job_id)
    if match and match.start() == 0:
        job = 'z_{}'.format(job_id)
    else:
        job = job_id

    tracker = 0
    cleansed_job_id = ''
    for m in re.finditer(r'\{{2}.+?\}{2}', job):
        cleansed_job_id += re.sub(r'[^0-9a-zA-Z]+', '_',
                                  job[tracker:m.start()])
        cleansed_job_id += job[m.start():m.end()]
        tracker = m.end()

    cleansed_job_id += re.sub(r'[^0-9a-zA-Z]+', '_', job[tracker:])

    return cleansed_job_id

Replaces invalid MLEngine job_id characters with '_'.

This also adds a leading 'z' in case job_id starts with an invalid
character.

Args:
job_id: A job_id str that may have invalid characters.

Returns:
A valid job_id representation.


def process_resource(self, req, resp, resource, uri_kwargs=None):
    if ('user' in req.context):
        return
    identifier = self.identify(req, resp, resource, uri_kwargs)
    user = self.try_storage(identifier, req, resp, resource, uri_kwargs)
    if (user is not None):
        req.context['user'] = user
    elif (self.challenge is not None):
        req.context.setdefault('challenges', list()).append(self.challenge)

Process resource after routing to it.

This is basic falcon middleware handler.

Args:
req (falcon.Request): request object
resp (falcon.Response): response object
resource (object): resource object matched by falcon router
uri_kwargs (dict): additional keyword argument from uri template.
For ``falcon<1.0.0`` this is always ``None``


def __init__(self, save_steps=None, save_secs=None, output_dir=None, summary_writer=None, scaffold=None, summary_op=None):
    if scaffold is None and summary_op is None or (scaffold is not None and summary_op is not None):
        raise ValueError('Exactly one of scaffold or summary_op must be provided.')
    self._summary_op = summary_op
    self._summary_writer = summary_writer
    self._output_dir = output_dir
    self._scaffold = scaffold
    self._timer = SecondOrStepTimer(every_secs=save_secs, every_steps=save_steps)

Initializes a `SummarySaverHook`.

Args:
save_steps: `int`, save summaries every N steps. Exactly one of
`save_secs` and `save_steps` should be set.
save_secs: `int`, save summaries every N seconds.
output_dir: `string`, the directory to save the summaries to. Only used if
no `summary_writer` is supplied.
summary_writer: `SummaryWriter`. If `None` and an `output_dir` was passed,
one will be created accordingly.
scaffold: `Scaffold` to get summary_op if it's not provided.
summary_op: `Tensor` of type `string` containing the serialized `Summary`
protocol buffer or a list of `Tensor`. They are most likely an output by
TF summary methods like `tf.compat.v1.summary.scalar` or
`tf.compat.v1.summary.merge_all`. It can be passed in as one tensor; if
more than one, they must be passed in as a list.

Raises:
ValueError: Exactly one of scaffold or summary_op should be set.


def _compute_sequence_length_from_mask(mask, batch_first):
    timestep_index = 0 if not batch_first else 1
    return torch.sum(mask.int(), dim=timestep_index)

Calculate the sequence length tensor (1-D) based on the masking tensor.

The masking tensor is a 2D boolean tensor with shape [batch, timestep]. For
any timestep that should be masked, the corresponding field will be False.
Consider the following example:
a = [[True, True, False, False]
[True, True, True, False]]
It is a (2, 4) tensor, and the corresponding sequence length result should
be 1D tensor with value [2, 3]. Note that the masking tensor must be right
padded that could be checked by, e.g., `is_sequence_right_padded()`.

Args:
mask: Boolean tensor with shape [batch, timestep] or [timestep, batch]
if time_major=True.
time_major: Boolean, which indicates whether the mask is time major or
batch major.

Returns:
sequence_length: 1D int32 tensor.


class UnbatchPandas(beam.PTransform):

    def __init__(self, proxy, include_indexes=False):
        self._proxy = proxy
        self._include_indexes = include_indexes

    def expand(self, pcoll):
        return pcoll | _unbatch_transform(self._proxy, self._include_indexes)

A transform that explodes a PCollection of DataFrame or Series. DataFrame
is converterd to a schema-aware PCollection, while Series is converted to its
underlying type.

Args:
include_indexes: (optional, default: False) When unbatching a DataFrame
if include_indexes=True, attempt to include index columns in the output
schema for expanded DataFrames. Raises an error if any of the index
levels are unnamed (name=None), or if any of the names are not unique
among all column and index names.


def flux_down(self, fluxDownTop, emission=None):
    if (emission is None):
        emission = np.zeros_like(self.absorptivity)
    E = np.concatenate((np.atleast_1d(fluxDownTop), emission), axis=(- 1))
    return np.squeeze(matrix_multiply(self.Tdown, E[(..., np.newaxis)]))

Compute upwelling radiative flux at interfaces between layers.

Inputs:

* fluxUpBottom: flux up from bottom
* emission: emission from atmospheric levels (N)
defaults to zero if not given

Returns:
* vector of upwelling radiative flux between levels (N+1)
element N is the flux up to space.


def add_polyhedron(self, neighbors, center, color, opacity=1.0, draw_edges=False, edges_color=[0.0, 0.0, 0.0], edges_linewidth=2):
    points = vtk.vtkPoints()
    conv = vtk.vtkConvexPointSet()
    for i in range(len(neighbors)):
        (x, y, z) = neighbors[i].coords
        points.InsertPoint(i, x, y, z)
        conv.GetPointIds().InsertId(i, i)
    grid = vtk.vtkUnstructuredGrid()
    grid.Allocate(1, 1)
    grid.InsertNextCell(conv.GetCellType(), conv.GetPointIds())
    grid.SetPoints(points)
    dsm = vtk.vtkDataSetMapper()
    polysites = [center]
    polysites.extend(neighbors)
    self.mapper_map[dsm] = polysites
    if (vtk.VTK_MAJOR_VERSION <= 5):
        dsm.SetInputConnection(grid.GetProducerPort())
    else:
        dsm.SetInputData(grid)
    ac = vtk.vtkActor()
    ac.SetMapper(dsm)
    ac.GetProperty().SetOpacity(opacity)
    if (color == 'element'):
        myoccu = 0.0
        for (specie, occu) in center.species.items():
            if (occu > myoccu):
                myspecie = specie
                myoccu = occu
        color = [(i / 255) for i in self.el_color_mapping[myspecie.symbol]]
        ac.GetProperty().SetColor(color)
    else:
        ac.GetProperty().SetColor(color)
    if draw_edges:
        ac.GetProperty().SetEdgeColor(edges_color)
        ac.GetProperty().SetLineWidth(edges_linewidth)
        ac.GetProperty().EdgeVisibilityOn()
    self.ren.AddActor(ac)

Adds a polyhedron.

Args:
neighbors: Neighbors of the polyhedron (the vertices).
center: The atom in the center of the polyhedron.
color: Color for text as RGB.
opacity: Opacity of the polyhedron
draw_edges: If set to True, the a line will be drawn at each edge
edges_color: Color of the line for the edges
edges_linewidth: Width of the line drawn for the edges


def _FormatInAddrExToken(self, token_data):
    protocol = bsmtoken.BSM_PROTOCOLS.get(token_data.net_type, 'UNKNOWN')
    if (token_data.net_type == 4):
        ip_address = self._FormatPackedIPv6Address(token_data.ip_address[:4])
    elif (token_data.net_type == 16):
        ip_address = self._FormatPackedIPv6Address(token_data.ip_address)
    return {'protocols': protocol, 'net_type': token_data.net_type, 'address': ip_address}

Formats an extended IPv4 address token as a dictionary of values.

Args:
token_data (bsm_token_data_in_addr_ex): AUT_IN_ADDR_EX token data.

Returns:
dict[str, str]: token values.


def iter_compress(item_iter, flag_iter):

    true_items = (item for (item, flag) in zip(item_iter, flag_iter) if flag)
    return true_items

iter_compress - like numpy compress

Args:
item_iter (list):
flag_iter (list): of bools

Returns:
list: true_items

Example:
>>> # ENABLE_DOCTEST
>>> from utool.util_iter import *  # NOQA
>>> item_iter = [1, 2, 3, 4, 5]
>>> flag_iter = [False, True, True, False, True]
>>> true_items = iter_compress(item_iter, flag_iter)
>>> result = list(true_items)
>>> print(result)
[2, 3, 5]


def bind(port, socket_type, socket_proto):
    got_socket = False
    for family in (socket.AF_INET6, socket.AF_INET):
        try:
            sock = socket.socket(family, socket_type, socket_proto)
            got_socket = True
        except socket.error:
            continue
        try:
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind(('', port))
            if (socket_type == socket.SOCK_STREAM):
                sock.listen(1)
            port = sock.getsockname()[1]
        except socket.error:
            return None
        finally:
            sock.close()
    return (port if got_socket else None)

Try to bind to a socket of the specified type, protocol, and port.

This is primarily a helper function for PickUnusedPort, used to see
if a particular port number is available.

For the port to be considered available, the kernel must support at least
one of (IPv6, IPv4), and the port must be available on each supported
family.

Args:
port: The port number to bind to, or 0 to have the OS pick a free port.
socket_type: The type of the socket (ex: socket.SOCK_STREAM).
socket_proto: The protocol of the socket (ex: socket.IPPROTO_TCP).

Returns:
The port number on success or None on failure.


def modify_model_interface(input_file, output_file, input_type, output_type):
    input_type_int = _parse_type_to_int(input_type, 'input_type')
    output_type_int = _parse_type_to_int(output_type, 'output_type')
    status = _pywrap_modify_model_interface.modify_model_interface(input_file, output_file, input_type_int, output_type_int)
    if status != 0:
        raise RuntimeError('Error occurred when trying to modify the model input type from float to {input_type} and output type from float to {output_type}.'.format(input_type=input_type, output_type=output_type))

Modify a quantized model's interface (input/output) from float to integer.

Args:
input_file: Full path name to the input tflite file.
output_file: Full path name to the output tflite file.
input_type: Final input interface type.
output_type: Final output interface type.

Raises:
RuntimeError: If the modification of the model interface was unsuccessful.
ValueError: If the input_type or output_type is unsupported.


def select_and_insert(self, name, data):

        self.select_obj(name)
        self.insert_into_obj(data)

Combines selection and data insertion into one function

Args:
name: the name of the object you want to insert into
data: the data you want to insert
Returns:
None
Raises:
None


def van_dec_2d(x, skip_connections, output_shape, first_depth, hparams=None):
    with tf.variable_scope('van_dec'):
        dec = tf.layers.conv2d_transpose(x, (first_depth * 4), 3, padding='same', activation=tf.nn.relu, strides=2)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        dec = tf.contrib.layers.layer_norm(dec)
        dec = tf.layers.conv2d_transpose(dec, (first_depth * 4), 3, padding='same', activation=tf.nn.relu, strides=1)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        dec = tf.layers.conv2d_transpose(dec, (first_depth * 2), 3, padding='same', activation=tf.nn.relu, strides=1)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        dec = tf.contrib.layers.layer_norm(dec)
        dec = tf.layers.conv2d_transpose(dec, (first_depth * 2), 3, padding='same', activation=tf.nn.relu, strides=2)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        dec = tf.layers.conv2d_transpose(dec, first_depth, 3, padding='same', activation=tf.nn.relu, strides=1)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        dec = tf.contrib.layers.layer_norm(dec)
        dec = tf.layers.conv2d_transpose(dec, (output_shape[3] + 1), 3, padding='same', activation=tf.nn.relu, strides=2)
        dec = tf.nn.dropout(dec, hparams.van_keep_prob)
        out_mask = tf.layers.conv2d_transpose(dec, (output_shape[3] + 1), 3, strides=1, padding='same', activation=None)
        mask = tf.nn.sigmoid(out_mask[(:, :, :, 3:4)])
        out = out_mask[(:, :, :, :3)]
        return ((out * mask) + (skip_connections[0] * (1 - mask)))

The VAN decoder.

Args:
x: The analogy information to decode.
skip_connections: The encoder layers which can be used as skip connections.
output_shape: The shape of the desired output image.
first_depth: The depth of the first layer of the van image encoder.
hparams: The python hparams.

Returns:
The decoded image prediction.


def merge(self, merge_commit_message=None, should_remove_source_branch=False, merge_when_pipeline_succeeds=False, **kwargs):
    path = ('%s/%s/merge' % (self.manager.path, self.get_id()))
    data = {}
    if merge_commit_message:
        data['merge_commit_message'] = merge_commit_message
    if should_remove_source_branch:
        data['should_remove_source_branch'] = True
    if merge_when_pipeline_succeeds:
        data['merge_when_pipeline_succeeds'] = True
    server_data = self.manager.gitlab.http_put(path, post_data=data, **kwargs)
    self._update_attrs(server_data)

Accept the merge request.

Args:
merge_commit_message (bool): Commit message
should_remove_source_branch (bool): If True, removes the source
branch
merge_when_pipeline_succeeds (bool): Wait for the build to succeed,
then merge
**kwargs: Extra options to send to the server (e.g. sudo)

Raises:
GitlabAuthenticationError: If authentication is not correct
GitlabMRClosedError: If the merge failed


def LVMPathSpecGetVolumeIndex(path_spec):

  volume_index = getattr(path_spec, 'volume_index', None)

  if volume_index is None:
    location = getattr(path_spec, 'location', None)

    if location is None or not location.startswith('/lvm'):
      return None

    volume_index = None
    try:
      volume_index = int(location[4:], 10) - 1
    except ValueError:
      pass

    if volume_index is None or volume_index < 0:
      return None

  return volume_index

Retrieves the volume index from the path specification.

Args:
path_spec (PathSpec): path specification.

Returns:
int: volume index or None if not available.


def __init__(self, connection):

        self.connection = connection
        response = connection.response

        super(BambouHTTPError, self).__init__("[HTTP %s(%s)] %s" % (response.status_code, response.reason, response.errors))

Intializes a BambouHTTPError

Args:
connection: the Connection object


def access_vlan(self, inter_type, inter, vlan_id):

        config = ET.Element('config')
        interface = ET.SubElement(config, 'interface',
                                  xmlns=("urn:brocade.com:mgmt:"
                                         "brocade-interface"))
        int_type = ET.SubElement(interface, inter_type)
        name = ET.SubElement(int_type, 'name')
        name.text = inter
        switchport = ET.SubElement(int_type, 'switchport')
        access = ET.SubElement(switchport, 'access')
        accessvlan = ET.SubElement(access, 'accessvlan')
        accessvlan.text = vlan_id
        try:
            self._callback(config)
            return True

        except Exception as error:
            logging.error(error)
            return False

Add a L2 Interface to a specific VLAN.

Args:
inter_type: The type of interface you want to configure. Ex.
tengigabitethernet, gigabitethernet, fortygigabitethernet.
inter: The ID for the interface you want to configure. Ex. 1/0/1
vlan_id: ID for the VLAN interface being modified. Value of 2-4096.

Returns:
True if command completes successfully or False if not.

Raises:
None


def read_log(self, logfile):

        logfile.seek(0)

        field_names, _ = self._parse_bro_header(logfile)

        while 1:
            _line = next(logfile).strip()
            if not _line.startswith('
                yield self._cast_dict(dict(zip(field_names, _line.split(self.delimiter))))
            else:
                time.sleep(.1)
                break

The read_log method returns a memory efficient generator for rows in a Bro log.

Usage:
rows = my_bro_reader.read_log(logfile)
for row in rows:
do something with row

Args:
logfile: The Bro Log file.


def export(self, filepath, encoding='utf-8', gzipped=True):
    data = json.dumps(self.word_frequency.dictionary, sort_keys=True)
    write_file(filepath, encoding, gzipped, data)

Export the word frequency list for import in the future

Args:
filepath (str): The filepath to the exported dictionary
encoding (str): The encoding of the resulting output
gzipped (bool): Whether to gzip the dictionary or not


def _wrap_decorator(wrapped_function, decorator_name):

    def wrapper(wrapper_func):
        return tf_decorator.make_decorator(wrapped_function, wrapper_func, decorator_name)
    return wrapper

Indicate that one function wraps another.

This decorator wraps a function using `tf_decorator.make_decorator`
so that doc generation scripts can pick up original function
signature.
It would be better to use @functools.wrap decorator, but it would
not update function signature to match wrapped function in Python 2.

Args:
wrapped_function: The function that decorated function wraps.
decorator_name: The name of the decorator.

Returns:
Function that accepts wrapper function as an argument and returns
`TFDecorator` instance.


def method_schema(self, method: str, iterate: bool=False) -> dict:
    endpoint, method = method.rsplit('.', 1)
    resource = self.api_document
    for e in endpoint.split('.'):
        resource = resource['resources'][e]
    resource = resource['methods'][method]['response']['$ref']
    properties = self.api_document['schemas'][resource]['properties']
    schema = self.to_schema(properties)
    if iterate or ('List' in resource and resource.endswith('Response')):
        for entry in schema:
            if entry['type'] == 'RECORD':
                return entry['fields']
            elif entry['mode'] == 'REPEATED':
                entry['mode'] = 'NULLABLE'
                return [entry]
        raise 'Unahandled discovery schema.'
    else:
        return schema

Return BigQuery schema for a Discovery API function.

Use the full dot notation of the rest API function.

Args:
method: the dot notation name of the Google API function
iterate: if true, return only iterable schema

Returns:
A dictionary representation of the resource.


def from_rfc3339(value):

    return datetime.datetime.strptime(value, _RFC3339_MICROS).replace(tzinfo=pytz.utc)

Convert a microsecond-precision timestamp to datetime.

Args:
value (str): The RFC3339 string to convert.

Returns:
datetime.datetime: The datetime object equivalent to the timestamp in
UTC.


def get_numeric_features_to_observed_range(examples):

  observed_features = collections.defaultdict(list)
  for example in examples:
    for feature_name in get_numeric_feature_names(example):
      original_feature = parse_original_feature_from_example(
          example, feature_name)
      observed_features[feature_name].extend(original_feature.original_value)
  return {
      feature_name: {
          'observedMin': min(feature_values),
          'observedMax': max(feature_values),
      }
      for feature_name, feature_values in iteritems(observed_features)
  }

Returns numerical features and their observed ranges.

Args:
examples: Examples to read to get ranges.

Returns:
A dict mapping feature_name -> {'observedMin': 'observedMax': } dicts,
with a key for each numerical feature.


def FlagCxx14Features(filename, clean_lines, linenum, error):
    line = clean_lines.elided[linenum]
    include = Match('\\s*
    if (include and (include.group(1) in ('scoped_allocator', 'shared_mutex'))):
        error(filename, linenum, 'build/c++14', 5, ('<%s> is an unapproved C++14 header.' % include.group(1)))

Flag those C++14 features that we restrict.

Args:
filename: The name of the current file.
clean_lines: A CleansedLines instance containing the file.
linenum: The number of the line to check.
error: The function to call with any errors found.


def _get_permutation(tensor, n_dims, active_dim):
    if not tensor.shape:
        raise ValueError("Tensor's rank should be static")
    rank = len(tensor.shape)
    batch_rank = rank - n_dims
    if active_dim == n_dims - 1:
        return None
    perm = np.arange(rank)
    perm[rank - 1] = batch_rank + active_dim
    perm[batch_rank + active_dim] = rank - 1
    return perm

Returns the permutation that swaps the active and the last dimensions.

Args:
tensor: `Tensor` having a statically known rank.
n_dims: Number of spatial dimensions.
active_dim: The active spatial dimension.

Returns:
A list representing the permutation, or `None` if no permutation needed.

For example, with 'tensor` having rank 5, `n_dims = 3` and `active_dim = 1`
yields [0, 1, 2, 4, 3]. Explanation: we start with [0, 1, 2, 3, 4], where the
last n_dims=3 dimensions are spatial dimensions, and the first two are batch
dimensions. Among the spatial dimensions, we take the one at index 1, which
is "3", and swap it with the last dimension "4".


def get_auditwheel_output(wheel_path: str) -> None:
    stringio = io.StringIO()
    previous_stdout = sys.stdout
    sys.stdout = stringio
    auditwheel_parser = argparse.ArgumentParser(description='Cross-distro Python wheels.')
    sub_parsers = auditwheel_parser.add_subparsers(metavar='command', dest='cmd')
    main_show.configure_parser(sub_parsers)
    auditwheel_args = argparse.Namespace(WHEEL_FILE=wheel_path, verbose=1)
    main_show.execute(args=auditwheel_args, p=auditwheel_parser)
    sys.stdout = previous_stdout
    return stringio.getvalue()

Run "auditwheel show" on the wheel and return the output.

Args:
wheel_path: path of the wheel file

Returns:
"auditwheel show" output


def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
    super(Authentication, self).read(input_stream, kmip_version=kmip_version)
    local_stream = utils.BytearrayStream(input_stream.read(self.length))
    credentials = []
    while self.is_tag_next(enums.Tags.CREDENTIAL, local_stream):
        credential = objects.Credential()
        credential.read(local_stream, kmip_version=kmip_version)
        credentials.append(credential)
    if (len(credentials) == 0):
        raise ValueError('Authentication encoding missing credentials.')
    self._credentials = credentials
    self.is_oversized(local_stream)

Read the data encoding the Authentication struct and decode it into
its constituent parts.

Args:
input_stream (stream): A data stream containing encoded object
data, supporting a read method; usually a BytearrayStream
object.
kmip_version (KMIPVersion): An enumeration defining the KMIP
version with which the object will be decoded. Optional,
defaults to KMIP 1.0.


def powerset(iterable, nonempty=False, reverse=False):
    iterable = list(iterable)
    if nonempty:
        start = 1
    else:
        start = 0
    seq_sizes = range(start, (len(iterable) + 1))
    if reverse:
        seq_sizes = reversed(seq_sizes)
        iterable.reverse()
    return chain.from_iterable((combinations(iterable, r) for r in seq_sizes))

Generate the power set of an iterable.

Args:
iterable (Iterable): The iterable from which to generate the power set.

Keyword Args:
nonempty (boolean): If True, don't include the empty set.
reverse (boolean): If True, reverse the order of the powerset.

Returns:
Iterable: An iterator over the power set.

Example:
>>> ps = powerset(np.arange(2))
>>> list(ps)
[(), (0,), (1,), (0, 1)]
>>> ps = powerset(np.arange(2), nonempty=True)
>>> list(ps)
[(0,), (1,), (0, 1)]
>>> ps = powerset(np.arange(2), nonempty=True, reverse=True)
>>> list(ps)
[(1, 0), (1,), (0,)]


def get_tri_area(pts):

    a, b, c = pts[0], pts[1], pts[2]
    v1 = np.array(b) - np.array(a)
    v2 = np.array(c) - np.array(a)
    area_tri = abs(sp.linalg.norm(sp.cross(v1, v2)) / 2)
    return area_tri

Given a list of coords for 3 points,
Compute the area of this triangle.

Args:
pts: [a, b, c] three points


def ExtractEvents(self, parser_mediator, registry_key, **kwargs):

    for subkey in registry_key.GetSubkeys():
      values_dict = {}
      values_dict['subkey_name'] = subkey.name

      name_values = subkey.name.split('&')
      number_of_name_values = len(name_values)

      if number_of_name_values != 4:
        logger.warning(
            'Expected 4 &-separated values in: {0:s}'.format(subkey.name))

      if number_of_name_values >= 1:
        values_dict['device_type'] = name_values[0]
      if number_of_name_values >= 2:
        values_dict['vendor'] = name_values[1]
      if number_of_name_values >= 3:
        values_dict['product'] = name_values[2]
      if number_of_name_values >= 4:
        values_dict['revision'] = name_values[3]

      event_data = windows_events.WindowsRegistryEventData()
      event_data.key_path = registry_key.path
      event_data.offset = registry_key.offset
      event_data.regvalue = values_dict
      event_data.source_append = self._SOURCE_APPEND

      if subkey.number_of_subkeys == 0:

        event = time_events.DateTimeValuesEvent(
            subkey.last_written_time, definitions.TIME_DESCRIPTION_WRITTEN)
        parser_mediator.ProduceEventWithEventData(event, event_data)
        continue

      for device_key in subkey.GetSubkeys():
        values_dict['serial'] = device_key.name

        friendly_name_value = device_key.GetValueByName('FriendlyName')
        if friendly_name_value:
          values_dict['friendly_name'] = friendly_name_value.GetDataAsObject()
        else:
          values_dict.pop('friendly_name', None)

        parent_id_prefix_value = device_key.GetValueByName('ParentIdPrefix')
        if parent_id_prefix_value:
          values_dict['parent_id_prefix'] = (
              parent_id_prefix_value.GetDataAsObject())
        else:
          values_dict.pop('parent_id_prefix', None)

        event = time_events.DateTimeValuesEvent(
            subkey.last_written_time, definitions.TIME_DESCRIPTION_WRITTEN)
        parser_mediator.ProduceEventWithEventData(event, event_data)

        event = time_events.DateTimeValuesEvent(
            device_key.last_written_time, definitions.TIME_DESCRIPTION_WRITTEN)
        parser_mediator.ProduceEventWithEventData(event, event_data)

        device_parameter_key = device_key.GetSubkeyByName('Device Parameters')
        if device_parameter_key:
          event = time_events.DateTimeValuesEvent(
              device_parameter_key.last_written_time,
              definitions.TIME_DESCRIPTION_WRITTEN)
          parser_mediator.ProduceEventWithEventData(event, event_data)

        log_configuration_key = device_key.GetSubkeyByName('LogConf')
        if log_configuration_key:
          event = time_events.DateTimeValuesEvent(
              log_configuration_key.last_written_time,
              definitions.TIME_DESCRIPTION_WRITTEN)
          parser_mediator.ProduceEventWithEventData(event, event_data)

        properties_key = device_key.GetSubkeyByName('Properties')
        if properties_key:
          event = time_events.DateTimeValuesEvent(
              properties_key.last_written_time,
              definitions.TIME_DESCRIPTION_WRITTEN)
          parser_mediator.ProduceEventWithEventData(event, event_data)

Extracts events from a Windows Registry key.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
registry_key (dfwinreg.WinRegistryKey): Windows Registry key.


def CompressedHistograms(self, run, tag):

    accumulator = self.GetAccumulator(run)
    return accumulator.CompressedHistograms(tag)

Retrieve the compressed histogram events associated with a run and tag.

Args:
run: A string name of the run for which values are retrieved.
tag: A string name of the tag for which values are retrieved.

Raises:
KeyError: If the run is not found, or the tag is not available for
the given run.

Returns:
An array of `event_accumulator.CompressedHistogramEvents`.


def get_stored_version(connection):
    if (connection.engine.name == 'sqlite'):
        version = connection.execute('PRAGMA user_version').fetchone()[0]
        if (version == 0):
            raise VersionIsNotStored
        return version
    elif (connection.engine.name == 'postgresql'):
        try:
            r = connection.execute('SELECT version FROM {}.user_version;'.format(POSTGRES_SCHEMA_NAME)).fetchone()
            if (not r):
                raise VersionIsNotStored
            version = r[0]
        except ProgrammingError:
            raise VersionIsNotStored
        return version
    else:
        raise DatabaseError('Do not know how to get version from {} engine.'.format(connection.engine.name))

Returns database version.

Args:
connection (sqlalchemy connection):

Raises: Assuming user_version pragma (sqlite case) and user_version table (postgresql case)
exist because they created with the database creation.

Returns:
int: version of the database.


def assert_corofunction(**kw):
    for (name, value) in kw.items():
        if (not asyncio.iscoroutinefunction(value)):
            raise TypeError('paco: {} must be a coroutine function'.format(name))

Asserts if a given values are a coroutine function.

Arguments:
**kw (mixed): value to check if it is an iterable.

Raises:
TypeError: if assertion fails.


def operate_magmom(self, magmom):

        magmom = Magmom(magmom)

        transformed_moment = self.apply_rotation_only(magmom.global_moment) * \
            np.linalg.det(self.rotation_matrix) * self.time_reversal

        return Magmom.from_global_moment_and_saxis(transformed_moment, magmom.saxis)

Apply time reversal operator on the magnetic moment. Note that
magnetic moments transform as axial vectors, not polar vectors.

See 'Symmetry and magnetic structures', Rodríguez-Carvajal and
Bourée for a good discussion. DOI: 10.1051/epjconf/20122200010

Args:
magmom: Magnetic moment as electronic_structure.core.Magmom
class or as list or np array-like

Returns:
Magnetic moment after operator applied as Magmom class


def gui(discord_token, discord_client_id):
    logger.info('Starting Modis in GUI')
    import tkinter as tk
    logger.debug('Loading packages')
    from modis.discord_modis import gui as discord_modis_gui
    from modis.reddit_modis import gui as reddit_modis_gui
    from modis.facebook_modis import gui as facebook_modis_gui
    logger.debug('Initialising window')
    root = tk.Tk()
    root.minsize(width=800, height=400)
    root.geometry('800x600')
    root.title('Modis Control Panel')
    root.iconbitmap('{}/assets/modis.ico'.format(file_dir))
    'notebook = ttk.Notebook(root)\n    notebook.grid(column=0, row=0, padx=0, pady=0, sticky="W E N S")\n\n
    discord = discord_modis_gui.Frame(root, discord_token, discord_client_id)
    discord.grid(column=0, row=0, padx=0, pady=0, sticky='W E N S')
    root.columnconfigure(0, weight=1)
    root.rowconfigure(0, weight=1)
    discord.columnconfigure(0, weight=1)
    discord.rowconfigure(0, weight=1)
    logger.debug('GUI initialised')
    root.mainloop()

Start Modis in gui format.

Args:
discord_token (str): The bot token for your Discord application
discord_client_id: The bot's client ID


def depth(self, local: bool = True) -> int:

        G = self.graph
        if not local:
            def remove_local(dagc: DAGCircuit) \
                    -> Generator[Operation, None, None]:
                for elem in dagc:
                    if dagc.graph.degree[elem] > 2:
                        yield elem
            G = DAGCircuit(remove_local(self)).graph

        return nx.dag_longest_path_length(G) - 1

Return the circuit depth.

Args:
local:  If True include local one-qubit gates in depth
calculation. Else return the multi-qubit gate depth.


def get_template_path(filename):
    if os.path.isfile(filename):
        return os.path.abspath(filename)
    for i in sys.path:
        if os.path.isfile(os.path.join(i, filename)):
            return os.path.abspath(os.path.join(i, filename))
    return None

Find raw template in working directory or in sys.path.

template_path from config may refer to templates colocated with the Stacker
config, or files in remote package_sources. Here, we emulate python module
loading to find the path to the template.

Args:
filename (str): Template filename.

Returns:
Optional[str]: Path to file, or None if no file found


def report_to_rows(report):
    if type(report) is GeneratorType:
        leftovers = ''
        for chunk in report:
            data, extra = chunk.rsplit('\n', 1)
            for row in csv_to_rows(leftovers + data):
                yield row
            leftovers = extra
    else:
        for row in csv_to_rows(report):
            yield row

Helper to convert DBM files into iterator of rows, memory efficient.

Usage example:

```
filename, report = report_file(...)
rows = report_to_rows(report)
```

Args:
* report: (iterator or file) Either an iterator or file that will be
converted to rows.

Returns:
* Iterator of lists representing each row.


def addFixedEffect(self,F=None,A=None):

        if A==None:
            A = SP.eye(self.P)
        if F==None:
            F = SP.ones((self.N,1))

        assert A.shape[1]==self.P, 'Incompatible shape'
        assert F.shape[0]==self.N, 'Incompatible shape'

        if F.shape[1]>1:
            for m in range(F.shape[1]):
                self.vd.addFixedEffTerm(A,F[:,m:m+1])
        else:
            self.vd.addFixedEffTerm(A,F)

        self.gp      = None
        self.init    = False
        self.fast    = False
        self.optimum = None

        self.cache['Sigma']   = None
        self.cache['Hessian'] = None
        self.cache['Lparams'] = None
        self.cache['paramsST']= None

add fixed effect to the model

Args:
F: fixed effect matrix [N,1]
A: design matrix [K,P] (e.g. SP.ones((1,P)) common effect; SP.eye(P) any effect)


async def start_server_in_loop(runner, hostname, port, agent):

    await runner.setup()
    agent.web.server = aioweb.TCPSite(runner, hostname, port)
    await agent.web.server.start()
    logger.info(f"Serving on http:

Listens to http requests and sends them to the webapp.

Args:
runner: AppRunner to process the http requests
hostname: host name to listen from.
port: port to listen from.
agent: agent that owns the web app.


def require_representation(self, req):
    try:
        (type_, subtype, _) = parse_mime_type(req.content_type)
        content_type = '/'.join((type_, subtype))
    except:
        raise falcon.HTTPUnsupportedMediaType(description='Invalid Content-Type header: {}'.format(req.content_type))
    if (content_type == 'application/json'):
        body = req.stream.read()
        return json.loads(body.decode('utf-8'))
    else:
        raise falcon.HTTPUnsupportedMediaType(description='only JSON supported, got: {}'.format(content_type))

Require raw representation dictionary from falcon request object.

This does not perform any field parsing or validation but only uses
allowed content-encoding handler to decode content body.

Note:
Currently only JSON is allowed as content type.

Args:
req (falcon.Request): request object

Returns:
dict: raw dictionary of representation supplied in request body


def file_name(self, value):

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

The file_name property.

Args:
value (string). the property value.


def append(self, other, ignore_index=False):
    if (not isinstance(other, self.__class__)):
        raise ValueError('May only append instances of same type.')
    if (type(ignore_index) is bool):
        new_frame = self._frame.append(other._frame, ignore_index=ignore_index, verify_integrity=True)
    else:
        new_frame = self._frame.append(other._frame, ignore_index=True, verify_integrity=True)
        if (type(ignore_index) is int):
            new_frame.index = range(ignore_index, (ignore_index + len(new_frame)))
        else:
            new_frame.index = ignore_index
    return self.__class__(new_frame)

Append rows of `other` to the end of this frame, returning a new object.

Wrapper around the :meth:`pandas.DataFrame.append` method.

Args:
other (Cartesian):
ignore_index (sequence, bool, int): If it is a boolean, it
behaves like in the description of
:meth:`pandas.DataFrame.append`.
If it is a sequence, it becomes the new index.
If it is an integer,
``range(ignore_index, ignore_index + len(new))``
becomes the new index.

Returns:
Cartesian:


def _get_object_type(filename, filepath):
    filename_no_ext = os.path.splitext(filename)[0].lower()
    if filename_no_ext.endswith(PrecompiledExampleType.test_ends):
        object_type = PRECOMPILED_OBJECT_TYPE_UNIT_TEST
    elif PrecompiledExampleType.katas in filepath.split(os.sep):
        object_type = PRECOMPILED_OBJECT_TYPE_KATA
    elif PrecompiledExampleType.examples in filepath.split(os.sep):
        object_type = PRECOMPILED_OBJECT_TYPE_EXAMPLE
    else:
        object_type = PRECOMPILED_OBJECT_TYPE_UNSPECIFIED
    return object_type

Get type of an object based on it filename/filepath

Args:
filename: object's filename
filepath: object's filepath

Returns: type of the object (example, kata, unit-test)


def index_max(x, idx, y):
    return _index_update_helper(tf_np.ndarray._with_index_max, x, idx, y)

Pure equivalent of `x[idx] = maximum(x[idx], y)`.

Returns the value of x that would result from the NumPy-style indexed
assignment `x[idx] = maximum(x[idx], y)`. Because it's a pure function, `x`
itself won't be changed.

Args:
x: an array with the values to be updated.
idx: a Numpy-style index, consisting of `None`, integers, slice objects,
ellipses, ndarrays with integer dtypes, or a tuple of the above.
y: the array of updates. `y` must be broadcastable to the shape of the array
that would be returned by `x[idx]`.

Returns:
The updated version of `x`.


def twopercent(station_code):

    temp = None
    try:
        fin = open('%s/%s' % (env.WEATHER_DATA_PATH,
                              _basename(station_code, 'ddy')))
        for line in fin:
            value = re.search(, line)
            if value:
                temp = float(value.groups()[0])
    except IOError:
        pass

    if not temp:

        try:
            fin = open('%s/%s' % (env.WEATHER_DATA_PATH,
                                  _basename(station_code, 'stat')))
            flag = 0
            tdata = []
            for line in fin:
                if line.find('2%') is not -1:
                    flag = 3
                if flag > 0:
                    tdata.append(line.split('\t'))
                    flag -= 1
            temp = float(tdata[2][5].strip())
        except IOError:
            pass
    if temp:
        return temp
    else:
        raise Exception("Error: 2% High Temperature not found")

Two percent high design temperature for a location.

Degrees in Celcius

Args:
station_code (str): Weather Station Code

Returns:
float degrees Celcius


def simulate_measurement(self, index: int) -> bool:

        args = self._shard_num_args({'index': index})
        prob_one = np.sum(self._pool.map(_one_prob_per_shard, args))
        result = bool(np.random.random() <= prob_one)

        args = self._shard_num_args({
            'index': index,
            'result': result,
            'prob_one': prob_one
        })
        self._pool.map(_collapse_state, args)
        return result

Simulates a single qubit measurement in the computational basis.

Args:
index: Which qubit is measured.

Returns:
True iff the measurement result corresponds to the |1> state.


def __init__(self, ignore):
    super().__init__()
    self._ignore = ignore

Initialize the visitor.

Args:
ignore: A list of prefixes to ignore. Typically, this list includes things
something like like "builtins.", since we don't want to convert builtin
types to late types. (And, more generally, types of modules that are
always loaded by pytype don't need to be late types)


def _default_ising_beta_range(h, J):
    abs_h = [abs(hh) for hh in h.values() if (hh != 0)]
    abs_J = [abs(jj) for jj in J.values() if (jj != 0)]
    abs_biases = (abs_h + abs_J)
    if (not abs_biases):
        return [0.1, 1.0]
    min_delta_energy = min(abs_biases)
    abs_bias_dict = {k: abs(v) for (k, v) in h.items()}
    for ((k1, k2), v) in J.items():
        abs_bias_dict[k1] += abs(v)
        abs_bias_dict[k2] += abs(v)
    max_delta_energy = max(abs_bias_dict.values())
    hot_beta = (np.log(2) / max_delta_energy)
    cold_beta = (np.log(100) / min_delta_energy)
    return [hot_beta, cold_beta]

Determine the starting and ending beta from h J

Args:
h (dict)

J (dict)

Assume each variable in J is also in h.

We use the minimum bias to give a lower bound on the minimum energy gap, such at the
final sweeps we are highly likely to settle into the current valley.


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, object_queries: torch.Tensor, output_attentions: Optional[bool]=None):
    residual = hidden_states
    hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, object_queries=object_queries, output_attentions=output_attentions)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
hidden_states (`torch.FloatTensor`): input to the layer of shape `(seq_len, batch, embed_dim)`
attention_mask (`torch.FloatTensor`): attention mask of size
`(batch, source_len)` where padding elements are indicated by very large negative
values.
object_queries (`torch.FloatTensor`, *optional*):
Object queries (also called content embeddings), to be added to the hidden states.
output_attentions (`bool`, *optional*):
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
returned tensors for more detail.


def check_import_stdlib(module):
    if ((module in stdlib_list('2.7')) or (module in stdlib_list('3.4')) or (module in stdlib_list('3.5')) or (module in stdlib_list('3.6')) or (module in stdlib_list('3.7')) or (module in ['app', 'args', 'playbook_app'])):
        return True
    return False

Check if module is in Python stdlib.

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

Returns:
bool: Returns True if the module is in the stdlib or template.


def get_allocated_fragments(self, id_or_uri, count=(- 1), start=0):
    uri = (self._client.build_uri(id_or_uri) + '/allocated-fragments?start={0}&count={1}'.format(start, count))
    return self._client.get_collection(uri)

Gets all fragments that have been allocated in range.

Args:
id_or_uri:
ID or URI of range.
count:
The number of resources to return. A count of -1 requests all items. The actual number of items in
the response may differ from the requested count if the sum of start and count exceed the total number
of items.
start:
The first item to return, using 0-based indexing. If not specified, the default is 0 - start with the
first available item.

Returns:
list: A list with the allocated fragements.


def get_rots(self) -> Rotation:
    return self._rots

Getter for the rotation.

Returns:
The rotation object


def create_store(reducer, initial_state=None, enhancer=None):
    if (enhancer is not None):
        if (not hasattr(enhancer, '__call__')):
            raise TypeError('Expected the enhancer to be a function.')
        return enhancer(create_store)(reducer, initial_state)
    if (not hasattr(reducer, '__call__')):
        raise TypeError('Expected the reducer to be a function.')
    current_reducer = [reducer]
    current_state = [initial_state]
    current_listeners = [[]]
    next_listeners = [current_listeners[0]]
    is_dispatching = [False]

    def ensure_can_mutate_next_listeners():
        if (next_listeners[0] == current_listeners[0]):
            next_listeners[0] = current_listeners[0][:]

    def get_state():
        return current_state[0]

    def subscribe(listener):
        if (not hasattr(listener, '__call__')):
            raise TypeError('Expected listener to be a function.')
        is_subscribed = [True]
        ensure_can_mutate_next_listeners()
        next_listeners[0].append(listener)

        def unsubcribe():
            if (not is_subscribed[0]):
                return
            is_subscribed[0] = False
            ensure_can_mutate_next_listeners()
            index = next_listeners[0].index(listener)
            next_listeners[0].pop(index)
        return unsubcribe

    def dispatch(action):
        if (not isinstance(action, dict)):
            raise TypeError('Actions must be a dict. Use custom middleware for async actions.')
        if (action.get('type') is None):
            raise ValueError('Actions must have a non-None "type" property. Have you misspelled a constant?')
        if is_dispatching[0]:
            raise Exception('Reducers may not dispatch actions.')
        try:
            is_dispatching[0] = True
            current_state[0] = current_reducer[0](current_state[0], action)
        finally:
            is_dispatching[0] = False
        listeners = current_listeners[0] = next_listeners[0]
        for listener in listeners:
            listener()
        return action

    def replace_reducer(next_reducer):
        if (not hasattr(next_reducer, '__call__')):
            raise TypeError('Expected next_reducer to be a function')
        current_reducer[0] = next_reducer
        dispatch({'type': ActionTypes.INIT})
    dispatch({'type': ActionTypes.INIT})
    return StoreDict(dispatch=dispatch, subscribe=subscribe, get_state=get_state, replace_reducer=replace_reducer)

redux in a nutshell.

observable has been omitted.

Args:
reducer: root reducer function for the state tree
initial_state: optional initial state data
enhancer: optional enhancer function for middleware etc.

Returns:
a Pydux store


def reset(self):
    if (self._status is not TaskStatus.STOPPED):
        raise RuntimeError(('Cannot reset %s in state %s' % (self, self._status)))
    self._reset()
    self.return_values = {}
    self._status = TaskStatus.IDLE

Reset a task.

Allows a task to be started again, clears the ``return_values``.

Raises:
RuntimeError: If the task has not been stopped.