def capture_insert_from_model(cls, table_name, record_id, *, exclude_fields=()):
    exclude_cols = ()
    if exclude_fields:
        model_cls = get_connected_model_for_table_name(table_name)
        exclude_cols = cls._fieldnames_to_colnames(model_cls, exclude_fields)
    raw_query = sql.SQL('\n            SELECT {schema}.hc_capture_insert_from_row(\n              hstore({schema}.{table_name}.*),\n              %(table_name)s,\n              ARRAY[{exclude_cols}]::text[]  -- cast to type expected by stored procedure\n            ) AS id\n            FROM {schema}.{table_name}\n            WHERE id = %(record_id)s\n        ').format(schema=sql.Identifier(settings.HEROKU_CONNECT_SCHEMA), table_name=sql.Identifier(table_name), exclude_cols=sql.SQL(', ').join((sql.Identifier(col) for col in exclude_cols)))
    params = {'record_id': record_id, 'table_name': table_name}
    result_qs = TriggerLog.objects.raw(raw_query, params)
    return list(result_qs)

Create a fresh insert record from the current model state in the database.

For read-write connected models, this will lead to the attempted creation of a
corresponding object in Salesforce.

Args:
table_name (str): The name of the table backing the connected model (without schema)
record_id (int): The primary id of the connected model
exclude_fields (Iterable[str]): The names of fields that will not be included in the
write record

Returns:
A list of the created TriggerLog entries (usually one).

Raises:
LookupError: if ``table_name`` does not belong to a connected model


def __add__(self, other: Any) -> 'KeyPath':
    if other is None:
        return self
    if isinstance(other, str):
        other = KeyPath.parse(other)
    elif isinstance(other, KeyPathSet):
        other = other.copy()
        other.rebase(self)
        return other
    elif not isinstance(other, KeyPath):
        other = KeyPath(other)
    assert isinstance(other, KeyPath)
    return KeyPath(other.keys, self)

Concatenates a KeyPath equivalent object.

Args:
other: Object to add, which can be None, int (as a 1-level KeyPath),
string (parsed as a KeyPath), a KeyPath object, or any other object as
a single key.

Returns:
Newly concatenated KeyPath.

Raises:
ValueError: If other is a string that cannot be parsed into a KeyPath.


def get(self, id):
    request_url = (self._client.base_api_url + self.detail_url.format(id=id))
    response = self._client.session.get(request_url)
    self.validate_request_success(response_text=response.text, request_url=request_url, status_code=response.status_code, expected_status_code=HTTP_200_OK)
    return self.response_data_to_model_instance(response.json())

Get the model instance with a given id.

Args:
id (int or str): The primary identifier (e.g., pk or UUID)
for the task instance to get.

Returns:
:class:`saltant.models.resource.Model`:
A :class:`saltant.models.resource.Model` subclass
instance representing the resource requested.


def imatch_any(patterns, name):

    if not patterns:
        return True
    return any(imatch(pattern, name) for pattern in patterns)

Test if a name matches any of a list of patterns (case insensitive).

Will return `True` if ``patterns`` is an empty list.

Arguments:
patterns (list): A list of wildcard pattern, e.g ``["*.py",
"*.pyc"]``
name (str): A filename.

Returns:
bool: `True` if the name matches at least one of the patterns.


def decode_event(self, log_topics, log_data):

        if not len(log_topics) or log_topics[0] not in self.event_data:
            raise ValueError('Unknown log type')

        event_id_ = log_topics[0]

        event = self.event_data[event_id_]

        unindexed_types = [
            type_
            for type_, indexed in zip(event['types'], event['indexed'])
            if not indexed
        ]
        unindexed_args = decode_abi(unindexed_types, log_data)

        indexed_count = 1

        result = {}
        for name, type_, indexed in zip(
                event['names'], event['types'], event['indexed']):
            if indexed:
                topic_bytes = utils.zpad(
                    utils.encode_int(log_topics[indexed_count]),
                    32,
                )
                indexed_count += 1
                value = decode_single(process_type(type_), topic_bytes)
            else:
                value = unindexed_args.pop(0)

            result[name] = value
        result['_event_type'] = utils.to_string(event['name'])

        return result

Return a dictionary representation the log.

Note:
This function won't work with anonymous events.

Args:
log_topics (List[bin]): The log's indexed arguments.
log_data (bin): The encoded non-indexed arguments.


def _get_subcommand(name):

    _LOGGER.debug('Accessing subcommand "%s".', name)
    if name not in settings.subcommands:
        raise ValueError(
            '"{subcommand}" is not a {command} command. \'{command} help -a\' '
            'lists all available subcommands.'.format(
                command=settings.command, subcommand=name)
        )
    return settings.subcommands[name]

Return the function for the specified subcommand.

Args:
name: The name of a subcommand.

Returns:
The loadable object from the entry point represented by the subcommand.


def GetDataByPath(self, path):

    _, path_data = self._paths.get(path, (None, None))
    return path_data

Retrieves the data associated to a path.

Args:
path (str): path of the file entry.

Returns:
bytes: data or None if not available.


def dict2str(self, d: Dict, joiner: str) -> str:
    result = str()
    for key in d:
        result = ((result + str(key)) + ' : ')
        if isinstance(d[key], list):
            result = ((result + self.list2str(d[key], joiner)) + joiner)
        elif isinstance(d[key], dict):
            result = ((result + self.dict2str(d[key], joiner)) + joiner)
        elif d[key]:
            result = ((result + str(d[key])) + joiner)
    return result

Convert dict to str as input for tokenizer

Args:
d (dict): dict for converting
joiner (str): join the elements using this string to separate them.

Returns: the value of the dict as a string


def read_until(self, expected_commands, timeout):
    msg = timeouts.loop_until_timeout_or_valid(timeout, (lambda : self.read_message(timeout)), (lambda m: (m.command in expected_commands)), 0)
    if (msg.command not in expected_commands):
        raise usb_exceptions.AdbTimeoutError('Timed out establishing connection, waiting for: %s', expected_commands)
    return msg

Read AdbMessages from this transport until we get an expected command.

The ADB protocol specifies that before a successful CNXN handshake, any
other packets must be ignored, so this method provides the ability to
ignore unwanted commands.  It's primarily used during the initial
connection to the device.  See Read() for more details, including more
exceptions that may be raised.

Args:
expected_commands: Iterable of expected command responses, like
('CNXN', 'AUTH').
timeout: timeouts.PolledTimeout object to use for timeout.

Returns:
The ADB message received that matched one of expected_commands.

Raises:
AdbProtocolError: If timeout expires between reads, this can happen
if we are getting spammed with unexpected commands.


def forall(self, vars_list: List[str]) -> 'TensorFluent':

        return self._aggregation_op(tf.reduce_all, self, vars_list)

Returns the TensorFluent for the forall aggregation function.

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

Returns:
A TensorFluent wrapping the forall aggregation function.


def download_file(url, file_path, mkdir=False):

    folder, fname = os.path.split(file_path)
    return download_file_by_name(url, folder, fname, mkdir)

Write a string of data to file.

Args:
url: A string to a valid URL.
file_path: Full path to intended download location (e.g. c:/ladybug/testPts.pts)
mkdir: Set to True to create the directory if doesn't exist (Default: False)


def preprocess_input(features, target, train_config, preprocess_output_dir, model_type):
    target_name = train_config['target_column']
    key_name = train_config['key_column']
    with tf.name_scope('numerical_feature_preprocess'):
        if train_config['numerical_columns']:
            numerical_analysis_file = os.path.join(preprocess_output_dir, NUMERICAL_ANALYSIS)
            if (not file_io.file_exists(numerical_analysis_file)):
                raise ValueError(('File %s not found in %s' % (NUMERICAL_ANALYSIS, preprocess_output_dir)))
            numerical_anlysis = json.loads(python_portable_string(file_io.read_file_to_string(numerical_analysis_file)))
            for name in train_config['numerical_columns']:
                if ((name == target_name) or (name == key_name)):
                    continue
                transform_config = train_config['transforms'].get(name, {})
                transform_name = transform_config.get('transform', None)
                if (transform_name == 'scale'):
                    value = float(transform_config.get('value', 1.0))
                    features[name] = _scale_tensor(features[name], range_min=numerical_anlysis[name]['min'], range_max=numerical_anlysis[name]['max'], scale_min=(- value), scale_max=value)
                elif ((transform_name == 'identity') or (transform_name is None)):
                    pass
                else:
                    raise ValueError(('For numerical variables, only scale and identity are supported: Error for %s' % name))
    if (target is not None):
        with tf.name_scope('target_feature_preprocess'):
            if (target_name in train_config['categorical_columns']):
                labels = train_config['vocab_stats'][target_name]['labels']
                table = tf.contrib.lookup.string_to_index_table_from_tensor(labels)
                target = table.lookup(target)
    with tf.name_scope('categorical_feature_preprocess'):
        for name in train_config['categorical_columns']:
            if ((name == key_name) or (name == target_name)):
                continue
            transform_config = train_config['transforms'].get(name, {})
            transform_name = transform_config.get('transform', None)
            if is_dnn_model(model_type):
                if ((transform_name == 'embedding') or (transform_name == 'one_hot') or (transform_name is None)):
                    map_vocab = True
                else:
                    raise ValueError(('Unknown transform %s' % transform_name))
            elif is_linear_model(model_type):
                if ((transform_name == 'one_hot') or (transform_name is None)):
                    map_vocab = True
                elif (transform_name == 'embedding'):
                    map_vocab = False
                else:
                    raise ValueError(('Unknown transform %s' % transform_name))
            if map_vocab:
                labels = train_config['vocab_stats'][name]['labels']
                table = tf.contrib.lookup.string_to_index_table_from_tensor(labels)
                features[name] = table.lookup(features[name])
    return (features, target)

Perform some transformations after reading in the input tensors.

Args:
features: dict of feature_name to tensor
target: tensor
train_config: our training config object
preprocess_output_dir: folder should contain the vocab files.
model_type: the tf model type.

Raises:
ValueError: if wrong transforms are used

Returns:
New features dict and new target tensor.


def get_extension_by_name(cert_obj, extension_name):
    try:
        return cert_obj.extensions.get_extension_for_oid(getattr(cryptography.x509.oid.ExtensionOID, extension_name))
    except cryptography.x509.ExtensionNotFound:
        pass

Get a standard certificate extension by attribute name.

Args:
cert_obj: cryptography.Certificate
Certificate containing a standard extension.

extension_name : str
Extension name. E.g., 'SUBJECT_DIRECTORY_ATTRIBUTES'.

Returns:
Cryptography.Extension


def get_psd_product(self, vector, dtype=None):

    if dtype is None:
      dtype = self.nn_dtype
    vector = tf.cast(vector, self.nn_dtype)
    alpha = tf.reshape(vector[0], shape=[1, 1])
    beta = vector[1:]

    h_beta = self.get_h_product(beta)

    result = tf.concat(
        [
            alpha * self.nu + tf.reduce_sum(tf.multiply(beta, self.vector_g)),
            tf.multiply(alpha, self.vector_g) + h_beta
        ],
        axis=0)
    return tf.cast(result, dtype)

Function that provides matrix product interface with PSD matrix.

Args:
vector: the vector to be multiplied with matrix M

Returns:
result_product: Matrix product of M and vector


def create_from_settings(settings):
    return Connection(settings['url'], settings['base_url'], settings['user'], settings['password'], authorizations=settings['authorizations'], debug=settings['debug'])

Create a connection with given settings.

Args:
settings (dict): A dictionary of settings

Returns:
:class:`Connection`. The connection


def _ReadSemanticDataTypeDefinition(self, definitions_registry, definition_values, data_type_definition_class, definition_name, supported_definition_values):
    return self._ReadDataTypeDefinition(definitions_registry, definition_values, data_type_definition_class, definition_name, supported_definition_values)

Reads a semantic data type definition.

Args:
definitions_registry (DataTypeDefinitionsRegistry): data type definitions
registry.
definition_values (dict[str, object]): definition values.
data_type_definition_class (str): data type definition class.
definition_name (str): name of the definition.
supported_definition_values (set[str]): names of the supported definition
values.

Returns:
SemanticDataTypeDefinition: semantic data type definition.

Raises:
DefinitionReaderError: if the definitions values are missing or if
the format is incorrect.


def update_hparams_for_universal_transformer(hparams):

  hparams.daisy_chain_variables = False

  hparams.add_hparam("mix_with_transformer", None)

  hparams.add_hparam("num_mixedin_layers", 2)

  hparams.add_hparam("num_inrecurrence_layers", 1)

  hparams.add_hparam("recurrence_type", "basic")

  hparams.add_hparam("num_rec_steps", hparams.num_hidden_layers)

  hparams.add_hparam("add_position_timing_signal", True)
  if hparams.add_position_timing_signal:
    hparams.pos = None

  hparams.add_hparam("position_start_index", None)

  hparams.add_hparam("add_step_timing_signal", True)

  hparams.add_hparam("step_timing_signal_type", "learned")

  hparams.add_hparam("add_or_concat_timing_signal", "add")

  hparams.add_hparam("add_sru", False)

  hparams.add_hparam("transformer_ffn_type", "fc")

  hparams.add_hparam("transform_bias_init", -1.0)
  hparams.add_hparam("couple_carry_transform_gates", True)

  hparams.add_hparam("depth_embedding", True)

  hparams.add_hparam("dwa_elements", True)

  hparams.add_hparam("gate_ffn_layer", "dense")

  hparams.add_hparam("lstm_forget_bias", 1.0)

  hparams.add_hparam("use_memory_as_final_state", False)

  hparams.add_hparam("add_ffn_unit_to_the_transition_function", False)

  hparams.add_hparam("act_type", "basic")

  hparams.add_hparam("act_max_steps", 2 * hparams.num_hidden_layers)
  hparams.add_hparam("act_halting_bias_init", 1.0)
  hparams.add_hparam("act_epsilon", 0.01)
  hparams.add_hparam("act_loss_weight", 0.01)

  return hparams

Adds default hparams for all of the variants of the Universal Transformer.

Args:
hparams: default hparams (usually one of the standard hparams from
transformer model (like "transformer_base")

Returns:
hparams with default values for Universal Transformers hyper-parameters


def play_from_queue(self, index, start=True):
    if (not self.speaker_info):
        self.get_speaker_info()
    uri = 'x-rincon-queue:{0}
    self.avTransport.SetAVTransportURI([('InstanceID', 0), ('CurrentURI', uri), ('CurrentURIMetaData', '')])
    self.avTransport.Seek([('InstanceID', 0), ('Unit', 'TRACK_NR'), ('Target', (index + 1))])
    if start:
        self.play()

Play a track from the queue by index.

The index number is required as an argument, where the first index
is 0.

Args:
index (int): 0-based index of the track to play
start (bool): If the item that has been set should start playing


def figure_naming(pretitle='', posttitle='', prefile='', postfile=''):
    if pretitle:
        pretitle = ('%s -- ' % pretitle)
    if posttitle:
        posttitle = (' -- %s' % posttitle)
    if prefile:
        prefile = ('%s_' % prefile)
    if postfile:
        postfile = ('_%s' % postfile)
    return (pretitle, posttitle, prefile, postfile)

Helper function to define the strings that handle pre-post conventions
for viewing - plotting title and saving options.

Args:
pretitle(str): String to include before the general title of the figure.
posttitle(str): String to include after the general title of the figure.
prefile(str): String to include before the general filename of the figure.
postfile(str): String to include after the general filename of the figure.

Returns:
str: String to include in the figure name and title, in a suitable form.


def initialize(
        self, config_file: str = "bmi_config.txt", initialize_indicators=True
    ):

        self.t = 0.0
        if not os.path.isfile(config_file):
            self.create_bmi_config_file(config_file)

        self.s0 = [
            pd.read_csv(
                config_file, index_col=0, header=None, error_bad_lines=False
            )[1]
            for _ in range(self.res)
        ]
        self.s0_original = self.s0[0].copy(deep=True)

        self.latent_state_vector = self.construct_default_initial_state()

        for n in self.nodes(data=True):
            rv = LatentVar(n[0])
            n[1]["rv"] = rv
            n[1]["update_function"] = self.default_update_function
            rv.dataset = [1.0 for _ in range(self.res)]
            rv.partial_t = self.s0[0][f"∂({n[0]})/∂t"]
            if initialize_indicators:
                for indicator in n[1]["indicators"].values():
                    indicator.samples = np.random.normal(
                        indicator.mean * np.array(n[1]["rv"].dataset),
                        scale=0.01,
                    )

Initialize the executable AnalysisGraph with a config file.

Args:
config_file

Returns:
AnalysisGraph


def find_base_model_checkpoint(model_type: str, model_files: Optional[Dict[str, Union[Path, List[Path]]]]=None) -> str:
    if model_files is None:
        model_files = get_model_files(model_type)
    module_files = model_files['model_files']
    for fname in module_files:
        if 'modeling' not in str(fname):
            continue
        with open(fname, 'r', encoding='utf-8') as f:
            content = f.read()
            if _re_checkpoint_for_doc.search(content) is not None:
                checkpoint = _re_checkpoint_for_doc.search(content).groups()[0]
                checkpoint = checkpoint.replace('"', '')
                checkpoint = checkpoint.replace("'", '')
                return checkpoint
    return ''

Finds the model checkpoint used in the docstrings for a given model.

Args:
model_type (`str`): A valid model type (like "bert" or "gpt2")
model_files (`Dict[str, Union[Path, List[Path]]`, *optional*):
The files associated to `model_type`. Can be passed to speed up the function, otherwise will be computed.

Returns:
`str`: The checkpoint used.


def _to_tensor_list_helper(encode_fn, element_spec, element):
    nest.assert_same_structure(element_spec, element)

    def reduce_fn(state, value):
        spec, component = value
        if isinstance(spec, internal.TensorSpec):
            try:
                component = ops.convert_to_tensor(component, spec.dtype)
            except (TypeError, ValueError):
                raise ValueError(f'Value {component} is not convertible to a tensor with dtype {spec.dtype} and shape {spec.shape}.')
            if not component.shape.is_compatible_with(spec.shape):
                raise ValueError(f'Value {component} is not convertible to a tensor with dtype {spec.dtype} and shape {spec.shape}.')
        return encode_fn(state, spec, component)
    return functools.reduce(reduce_fn, zip(nest.flatten(element_spec), nest.flatten(element)), [])

Returns a tensor list representation of the element.

Args:
encode_fn: Method that constructs a tensor list representation from the
given element spec and element.
element_spec: A nested structure of `tf.TypeSpec` objects representing to
element type specification.
element: The element to convert to tensor list representation.

Returns:
A tensor list representation of `element`.

Raises:
ValueError: If `element_spec` and `element` do not have the same number of
elements or if the two structures are not nested in the same way.
TypeError: If `element_spec` and `element` differ in the type of sequence
in any of their substructures.


def to_frame(data_list, exc_cols=None, **kwargs):
    from collections import OrderedDict
    return pd.DataFrame(pd.Series(data_list).apply(OrderedDict).tolist(), **kwargs).drop(columns=([] if (exc_cols is None) else exc_cols))

Dict in Python 3.6 keeps insertion order, but cannot be relied upon
This method is to keep column names in order
In Python 3.7 this method is redundant

Args:
data_list: list of dict
exc_cols: exclude columns

Returns:
pd.DataFrame

Example:
>>> d_list = [
...     dict(sid=1, symbol='1 HK', price=89),
...     dict(sid=700, symbol='700 HK', price=350)
... ]
>>> to_frame(d_list)
sid  symbol  price
0    1    1 HK     89
1  700  700 HK    350
>>> to_frame(d_list, exc_cols=['price'])
sid  symbol
0    1    1 HK
1  700  700 HK


def emit_obj_snapshot(self, category: str, name: str, timestamp: int, pid: int, tid: int, object_id: int, snapshot: Dict[str, Any]) -> None:
    event = self._create_event('O', category, name, pid, tid, timestamp)
    event['id'] = object_id
    event['args'] = {'snapshot': snapshot}
    self._events.append(event)

Adds an object snapshot event to the trace.

Args:
category: The event category as a string.
name:  The event name as a string.
timestamp:  The timestamp of this event as a long integer.
pid:  Identifier of the process generating this event as an integer.
tid:  Identifier of the thread generating this event as an integer.
object_id: Identifier of the object as an integer.
snapshot:  A JSON-compatible representation of the object.


def create_additional_charge(self, *, subscription_id, description, plan_value, plan_tax, plan_tax_return_base,
                                 currency):

        payload = {
            "description": description,
            "additionalValues": [
                {
                    "name": "ITEM_VALUE",
                    "value": plan_value,
                    "currency": currency
                },
                {
                    "name": "ITEM_TAX",
                    "value": plan_tax,
                    "currency": currency
                },
                {
                    "name": "ITEM_TAX_RETURN_BASE",
                    "value": plan_tax_return_base,
                    "currency": currency
                }
            ]
        }
        fmt = 'subscriptions/{}/recurringBillItems'.format(subscription_id)
        return self.client._post(self.url + fmt, json=payload, headers=self.get_headers())

Adds extra charges to the respective invoice for the current period.

Args:
subscription_id: Identification of the subscription
description:
plan_value:
plan_tax:
plan_tax_return_base:
currency:

Returns:


def calc_clusters(returns, n=None, plot=False):

    corr = returns.corr()

    diss = 1 - corr

    mds = sklearn.manifold.MDS(dissimilarity='precomputed')
    xy = mds.fit_transform(diss)

    def routine(k):

        km = sklearn.cluster.KMeans(n_clusters=k)
        km_fit = km.fit(xy)
        labels = km_fit.labels_
        centers = km_fit.cluster_centers_

        mappings = dict(zip(returns.columns, labels))

        totss = 0
        withinss = 0

        avg = np.array([np.mean(xy[:, 0]), np.mean(xy[:, 1])])
        for idx, lbl in enumerate(labels):
            withinss += sum((xy[idx] - centers[lbl]) ** 2)
            totss += sum((xy[idx] - avg) ** 2)
        pvar_expl = 1.0 - withinss / totss

        return mappings, pvar_expl, labels

    if n:
        result = routine(n)
    else:
        n = len(returns.columns)
        n1 = int(np.ceil(n * 0.6666666666))
        for i in range(2, n1 + 1):
            result = routine(i)
            if result[1] > 0.9:
                break

    if plot:
        fig, ax = plt.subplots()
        ax.scatter(xy[:, 0], xy[:, 1], c=result[2], s=90)
        for i, txt in enumerate(returns.columns):
            ax.annotate(txt, (xy[i, 0], xy[i, 1]), size=14)

    tmp = result[0]

    inv_map = {}
    for k, v in iteritems(tmp):
        inv_map[v] = inv_map.get(v, [])
        inv_map[v].append(k)

    return inv_map

Calculates the clusters based on k-means
clustering.

Args:
* returns (pd.DataFrame): DataFrame of returns
* n (int): Specify # of clusters. If None, this
will be automatically determined
* plot (bool): Show plot?

Returns:
* dict with structure: {cluster# : [col names]}


def get_country_name_from_iso2(cls, iso2, use_live=True, exception=None):

        iso3 = cls.get_iso3_from_iso2(iso2, use_live=use_live, exception=exception)
        if iso3 is not None:
            return cls.get_country_name_from_iso3(iso3, exception=exception)
        return None

Get country name from ISO2 code

Args:
iso2 (str): ISO2 code for which to get country name
use_live (bool): Try to get use latest data from web rather than file in package. Defaults to True.
exception (Optional[ExceptionUpperBound]): An exception to raise if country not found. Defaults to None.

Returns:
Optional[str]: Country name


def create_report(self, uri, timeout=(- 1)):
    logger.debug('Creating Report (uri = %s)'.format(uri))
    (task, _) = self._connection.post(uri, {})
    if (not task):
        raise exceptions.HPOneViewException(RESOURCE_CLIENT_TASK_EXPECTED)
    task = self._task_monitor.get_completed_task(task, timeout)
    return task['taskOutput']

Creates a report and returns the output.

Args:
uri: URI
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:
list:


def go_in(self, vertex):

        if self.vertex_in:
            self.vertex_in.edges_in.remove(self)
        self.vertex_in = vertex
        vertex.edges_in.add(self)

Tell the edge to go into this vertex.

Args:
vertex (Vertex): vertex to go into.


def recipe_anonymize_query(config, auth_read, from_project, from_dataset, from_query, to_project, to_dataset, to_table):
    anonymize(config, {'auth': auth_read, 'bigquery': {'from': {'project': from_project, 'dataset': from_dataset, 'query': from_query}, 'to': {'project': to_project, 'dataset': to_dataset, 'table': to_table}}})

Runs a query and anynonamizes all rows.  Used to create sample table for
dashboards.

Args:
auth_read (authentication) - Credentials used.
from_project (string) - Original project to read from.
from_dataset (string) - Original dataset to read from.
from_query (string) - Query to read data.
to_project (string) - Anonymous data will be writen to.
to_dataset (string) - Anonymous data will be writen to.
to_table (string) - Anonymous data will be writen to.


def get_all_keys(tweet, parent_key=''):
    items = []
    for (k, v) in tweet.items():
        new_key = ((parent_key + ' ') + k)
        if isinstance(v, dict):
            items.extend(get_all_keys(v, parent_key=new_key))
        else:
            items.append(new_key.strip(' '))
    return items

Takes a tweet object and recursively returns a list of all keys contained
in this level and all nexstted levels of the tweet.

Args:
tweet (Tweet): the tweet dict
parent_key (str): key from which this process will start, e.g., you can
get keys only under some key that is not the top-level key.

Returns:
list of all keys in nested dicts.

Example:
>>> import tweet_parser.tweet_checking as tc
>>> tweet = {"created_at": 124125125125, "text": "just setting up my twttr",
...          "nested_field": {"nested_1": "field", "nested_2": "field2"}}
>>> tc.get_all_keys(tweet)
['created_at', 'text', 'nested_field nested_1', 'nested_field nested_2']


def _CreateAnalysisPlugins(self, options):

    if not self._analysis_plugins:
      return {}

    analysis_plugins = (
        analysis_manager.AnalysisPluginManager.GetPluginObjects(
            self._analysis_plugins))

    for analysis_plugin in analysis_plugins.values():
      helpers_manager.ArgumentHelperManager.ParseOptions(
          options, analysis_plugin)

    return analysis_plugins

Creates the analysis plugins.

Args:
options (argparse.Namespace): command line arguments.

Returns:
dict[str, AnalysisPlugin]: analysis plugins and their names.


def all(script, face=True, vert=True):

    filter_xml = ''.join([
        '  <filter name="Select All">\n',
        '    <Param name="allFaces" ',
        'value="{}" '.format(str(face).lower()),
        'description="DSelect all Faces" ',
        'type="RichBool" ',
        '/>\n',
        '    <Param name="allVerts" ',
        'value="{}" '.format(str(vert).lower()),
        'description="Select all Vertices" ',
        'type="RichBool" ',
        '/>\n',
        '  </filter>\n'])
    util.write_filter(script, filter_xml)
    return None

Select all the faces of the current mesh

Args:
script: the FilterScript object or script filename to write
the filter to.
faces (bool): If True the filter will select all the faces.
verts (bool): If True the filter will select all the vertices.

Layer stack:
No impacts

MeshLab versions:
2016.12
1.3.4BETA


def _ParseRecurseKeys(self, parser_mediator, root_key):

    for registry_key in root_key.RecurseKeys():
      if parser_mediator.abort:
        break

      self._ParseKey(parser_mediator, registry_key)

Parses the Registry keys recursively.

Args:
parser_mediator (ParserMediator): parser mediator.
root_key (dfwinreg.WinRegistryKey): root Windows Registry key.


def vol_tetra(vt1, vt2, vt3, vt4):

    vol_tetra = np.abs(np.dot((vt1 - vt4),
                              np.cross((vt2 - vt4), (vt3 - vt4)))) / 6
    return vol_tetra

Calculate the volume of a tetrahedron, given the four vertices of vt1,
vt2, vt3 and vt4.
Args:
vt1 (array-like): coordinates of vertex 1.
vt2 (array-like): coordinates of vertex 2.
vt3 (array-like): coordinates of vertex 3.
vt4 (array-like): coordinates of vertex 4.
Returns:
(float): volume of the tetrahedron.


def _check(cls, name, val, can_be_zero=False, val_type=float):

    valid_types = [val_type]
    if val_type is float:
      valid_types.append(int)

    if type(val) not in valid_types:
      raise TypeError(
          'Expect type %s for parameter %s' % (val_type.__name__, name))
    if val < 0:
      raise ValueError(
          'Value for parameter %s has to be greater than 0' % name)
    if not can_be_zero and val == 0:
      raise ValueError(
          'Value for parameter %s can not be 0' % name)
    return val

Check init arguments.

Args:
name: name of the argument. For logging purpose.
val: value. Value has to be non negative number.
can_be_zero: whether value can be zero.
val_type: Python type of the value.

Returns:
The value.

Raises:
ValueError: when invalid value is passed in.
TypeError: when invalid value type is passed in.


def while_stmt(test, body, get_state, set_state, symbol_names, opts):
    with func_graph.FuncGraph('tmp').as_default():
        init_test = test()
    if tensors.is_dense_tensor(init_test):
        _tf_while_stmt(test, body, get_state, set_state, symbol_names, opts)
        return
    if not init_test:
        return
    body()
    _py_while_stmt(test, body, get_state, set_state, opts)

Functional form of a while statement.

The loop operates on a so-called state, which includes all symbols that are
variant across loop iterations. In what follows we refer to state as either
a tuple of entities that represent an actual state, or a list of arguments
of the corresponding types.

The inputs and outputs of the callables representing the loop blocks are not
explicit - instead, these functions must use nonlocal/global for side effects.
The inputs and outputs are instead controlled by the set_state/get_state
functions.

Args:
test: Callable with boolean return type. The loop condition.
body: Callable representing the actual loop body.
get_state: Additional callable which can capture additional state (such as
the values of composite symbols). This is only useful when staging the
loop.
set_state: Additional callable which save values captured by get_state back
into the Python environment. This is only useful when staging the loop.
symbol_names: Tuple containing the names of all loop variables.
opts: Optional dict of extra loop parameters.

Returns:
Tuple containing the final state.


def thumbnail(self, image: 'torch.Tensor', size: SizeDict) -> 'torch.Tensor':
    input_height, input_width = image.shape[-2:]
    output_height, output_width = (size.height, size.width)
    height = min(input_height, output_height)
    width = min(input_width, output_width)
    if height == input_height and width == input_width:
        return image
    if input_height > input_width:
        width = int(input_width * height / input_height)
    elif input_width > input_height:
        height = int(input_height * width / input_width)
    return self.resize(image, size=SizeDict(width=width, height=height), interpolation=F.InterpolationMode.BICUBIC)

Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any
corresponding dimension of the specified size.

Args:
image (`torch.Tensor`):
The image to be resized.
size (`Dict[str, int]`):
The size `{"height": h, "width": w}` to resize the image to.
resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
The resampling filter to use.
data_format (`Optional[Union[str, ChannelDimension]]`, *optional*):
The data format of the output image. If unset, the same format as the input image is used.
input_data_format (`ChannelDimension` or `str`, *optional*):
The channel dimension format of the input image. If not provided, it will be inferred.


def __init__(self, save_steps=None, save_secs=None, output_dir='', show_dataflow=True, show_memory=False):
    self._output_file = os.path.join(output_dir, 'timeline-{}.json')
    self._file_writer = SummaryWriterCache.get(output_dir)
    self._show_dataflow = show_dataflow
    self._show_memory = show_memory
    self._timer = SecondOrStepTimer(every_secs=save_secs, every_steps=save_steps)

Initializes a hook that takes periodic profiling snapshots.

`options.run_metadata` argument of `tf.Session.Run` is used to collect
metadata about execution. This hook sets the metadata and dumps it in Chrome
Trace format.

Args:
save_steps: `int`, save profile traces every N steps. Exactly one of
`save_secs` and `save_steps` should be set.
save_secs: `int` or `float`, save profile traces every N seconds.
output_dir: `string`, the directory to save the profile traces to.
Defaults to the current directory.
show_dataflow: `bool`, if True, add flow events to the trace connecting
producers and consumers of tensors.
show_memory: `bool`, if True, add object snapshot events to the trace
showing the sizes and lifetimes of tensors.


def acquire(self):

        if os.path.exists(self.path):
            try:
                pid = None

                with open(self.path, 'r') as f:
                    line = f.readline().strip()
                    pid = int(line)

                if not psutil.pid_exists(pid):
                    os.remove(self.path)

            except ValueError as e:

                os.remove(self.path)

            except IOError as e:

                pass

        try:
            self.fd = os.open(self.path, os.O_CREAT | os.O_EXCL | os.O_RDWR)

            to_write = '%s%s' % (os.getpid(), os.linesep)
            os.write(self.fd, to_write.encode())

        except OSError as e:
            if not os.path.exists(self.path):
                raise
            return False

        self.acquired = True
        return True

Attempts to acquire a lock for the J-Link lockfile.

If the lockfile exists but does not correspond to an active process,
the lockfile is first removed, before an attempt is made to acquire it.

Args:
self (Jlock): the ``JLock`` instance

Returns:
``True`` if the lock was acquired, otherwise ``False``.

Raises:
OSError: on file errors.


def get_service_health(service_id: str) -> str:

        if DC.get_replicas(service_id) != DC.get_actual_replica(service_id):
            health_status = "Unhealthy"
        else:
            health_status = "Healthy"

        return health_status

Get the health of a service using service_id.

Args:
service_id

Returns:
str, health status


def bbox_clip(bboxes, img_shape):

    assert bboxes.shape[-1] % 4 == 0
    clipped_bboxes = np.empty_like(bboxes, dtype=bboxes.dtype)
    clipped_bboxes[..., 0::2] = np.maximum(
        np.minimum(bboxes[..., 0::2], img_shape[1] - 1), 0)
    clipped_bboxes[..., 1::2] = np.maximum(
        np.minimum(bboxes[..., 1::2], img_shape[0] - 1), 0)
    return clipped_bboxes

Clip bboxes to fit the image shape.

Args:
bboxes (ndarray): Shape (..., 4*k)
img_shape (tuple): (height, width) of the image.

Returns:
ndarray: Clipped bboxes.


def intersect(self, other):

        self.automaton = fst.intersect(self.automaton, other.automaton)
        return  self

Constructs an unminimized DFA recognizing
the intersection of the languages of two given DFAs.
Args:
other (DFA): The other DFA that will be used
for the intersect operation
Returns:
Returns:
DFA: The resulting DFA


def make_innermost_getter(getter):

    @functools.wraps(getter)
    def _new_getter(kernel_results, *args, **kwargs):
        'Wrapped getter.'
        results_stack = []
        while hasattr(kernel_results, 'inner_results'):
            results_stack.append(kernel_results)
            kernel_results = kernel_results.inner_results
        return getter(kernel_results, *args, **kwargs)
    return _new_getter

Wraps a getter so it applies to the inner-most results in `kernel_results`.

The wrapped getter unwraps `kernel_results` and returns the return value of
`getter` called with the first results without an `inner_results` attribute.

Args:
getter: A callable that takes Kernel results and returns some value.

Returns:
new_getter: A wrapped `getter`.


def _op_in_graph_mode(tensor):
    if context.executing_eagerly():
        return tensor
    return tensor.op

Returns the tensor's op in graph mode, or the tensor in eager mode.

This is useful because sometimes an op is needed in graph mode instead of a
tensor. In eager mode, there are no ops.

Args:
tensor: A tensor.

Returns:
The tensor's op in graph mode. The tensor in eager mode.


def new_from_json(cls, json_data):
    json_data_as_unicode = _helpers._from_bytes(json_data)
    data = json.loads(json_data_as_unicode)
    module_name = data['_module']
    try:
        module_obj = __import__(module_name)
    except ImportError:
        module_name = module_name.replace('.googleapiclient', '')
        module_obj = __import__(module_name)
    module_obj = __import__(module_name, fromlist=module_name.split('.')[:(- 1)])
    kls = getattr(module_obj, data['_class'])
    return kls.from_json(json_data_as_unicode)

Utility class method to instantiate a Credentials subclass from JSON.

Expects the JSON string to have been produced by to_json().

Args:
json_data: string or bytes, JSON from to_json().

Returns:
An instance of the subclass of Credentials that was serialized with
to_json().


def _find_channel_index(data_format):

  for i, c in enumerate(data_format):
    if c == "C":
      return i
  raise ValueError("data_format requires a channel dimension. Got: {}"
                   .format(data_format))

Returns the index of the channel dimension.

Args:
data_format: A string of characters corresponding to Tensor dimensionality.

Returns:
channel_index: An integer indicating the channel dimension.

Raises:
ValueError: If no channel dimension was found.


def is_distributed(partition_column, lower_bound, upper_bound):
    if ((partition_column is not None) and (lower_bound is not None) and (upper_bound is not None)):
        if (upper_bound > lower_bound):
            return True
        else:
            raise InvalidArguments('upper_bound must be greater than lower_bound.')
    elif ((partition_column is None) and (lower_bound is None) and (upper_bound is None)):
        return False
    else:
        raise InvalidArguments('Invalid combination of partition_column, lower_bound, upper_bound.All these arguments should be passed (distributed) or none of them (standard pandas).')

Check if is possible distribute a query given that args

Args:
partition_column: column used to share the data between the workers
lower_bound: the minimum value to be requested from the partition_column
upper_bound: the maximum value to be requested from the partition_column

Returns:
True for distributed or False if not


def convert_constant(params, w_name, scope_name, inputs, layers, weights, names):
    print('Converting constant ...')
    params_list = params['value'].numpy()

    def target_layer(x, value=params_list):
        return tf.constant(value.tolist(), shape=value.shape)
    lambda_layer = keras.layers.Lambda(target_layer)
    layers[(scope_name + '_np')] = params_list
    layers[scope_name] = lambda_layer(layers[list(layers.keys())[0]])

Convert constant layer.

Args:
params: dictionary with layer parameters
w_name: name prefix in state_dict
scope_name: pytorch scope name
inputs: pytorch node inputs
layers: dictionary with keras tensors
weights: pytorch state_dict
names: use short names for keras layers


def file_move(filename, settings):

        if len(settings) != 1:
            raise ValueError("Settings must only contain one item with key "
                             "'dest'.")
        for k, v in settings.items():
            if k == "dest":
                shutil.move(filename, v)

Moves a file. {'_file_move': {'dest': 'new_file_name'}}

Args:
filename (str): Filename.
settings (dict): Must be {"dest": path of new file}


def metropolis_hastings_step(current_state: State, proposed_state: State, energy_change: FloatTensor, seed=None) -> Tuple[(State, tf.Tensor, tf.Tensor)]:
    flat_current = tf.nest.flatten(current_state)
    flat_proposed = nest.flatten_up_to(current_state, proposed_state)
    flat_current = [(p if (c is None) else c) for (p, c) in zip(flat_proposed, flat_current)]
    current_state = tf.nest.pack_sequence_as(current_state, flat_current)
    current_state = tf.nest.map_structure(tf.convert_to_tensor, current_state)
    proposed_state = tf.nest.map_structure(tf.convert_to_tensor, proposed_state)
    energy_change = tf.convert_to_tensor(value=energy_change)
    log_accept_ratio = (- energy_change)
    log_uniform = tf.math.log(tf.random.uniform(shape=tf.shape(input=log_accept_ratio), dtype=log_accept_ratio.dtype.base_dtype, seed=seed))
    is_accepted = (log_uniform < log_accept_ratio)
    next_state = mcmc_util.choose(is_accepted, proposed_state, current_state, name='choose_next_state')
    return (next_state, is_accepted, log_uniform)

Metropolis-Hastings step.

This probabilistically chooses between `current_state` and `proposed_state`
based on the `energy_change` so as to preserve detailed balance.

Energy change is the negative of `log_accept_ratio`.

Args:
current_state: Current state.
proposed_state: Proposed state.
energy_change: E(proposed_state) - E(previous_state).
seed: For reproducibility.

Returns:
new_state: The chosen state.
is_accepted: Whether the proposed state was accepted.
log_uniform: The random number that was used to select between the two
states.


def expand_unique_results(y, idx):
    expanded = tf.gather(y, idx, axis=0)
    return expanded

Inverse of unique_bitstrings_with_counts.

Args:
y: Values to pick according to `idx`.
idx: The index at which to place each value of `y` in the output.

Returns:
expanded: `tf.Tensor` such that `expanded[i] == y[idx[i]]`.


def _weight_generator(self, reviewers):
    scores = [r.anomalous_score for r in reviewers]
    mu = np.average(scores)
    sigma = np.std(scores)
    if sigma:

        def w(v):
            'Compute a weight for the given reviewer.\n\n                Args:\n                  v: anomalous score of a reviewer.\n                Returns:\n                  weight of the given anomalous score.\n                '
            try:
                exp = math.exp(((self.alpha * (v - mu)) / sigma))
                return (1.0 / (1.0 + exp))
            except OverflowError:
                return 0.0
        return w
    else:
        return (lambda v: 1.0)

Compute a weight function for the given reviewers.

Args:
reviewers: a set of reviewers to compute weight function.

Returns:
a function computing a weight for a reviewer.


def _run_graph_for_calibration_eager_mode(model_dir: str, tags: Collection[str], representative_dataset_map: rd.RepresentativeDatasetMapping) -> None:
    root: autotrackable.AutoTrackable = load.load(model_dir, tags)
    for signature_key, repr_ds in representative_dataset_map.items():
        try:
            _run_function_for_calibration_eager_mode(func=root.signatures[signature_key], representative_dataset=repr_ds)
        except Exception as ex:
            raise ValueError(f'Failed to run representative dataset through the function with the signature key: {signature_key}.') from ex

Runs the graph for calibration in eager mode.

This function assumes _eager mode_ (enabled in TF2 by default) when running
the graph. This step is used in order to collect the statistics in
CustomAggregatorOp for quantization using the representative dataset for the
actual data provided for inference.

Args:
model_dir: Path to SavedModel directory.
tags: Collection of tags identifying the MetaGraphDef within the SavedModel.
representative_dataset_map: A map where signature keys are mapped to
corresponding representative datasets.

Raises:
ValueError: When running the function with the representative dataset fails.


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

Remove a person from the system.

Only an admin can remove a person.

Args:
personId(basestring): The ID of the person to be deleted.

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


def get_module_names(p):

    mods = list()
    mods = [f.split('.')[0] for f in listdir(p)
            if isfile(join(p, f)) and not f.endswith('.pyc') and not f.startswith('__')]
    print len(mods)
    return mods

Accepts a path to search for modules. The method will filter on files
that end in .pyc or files that start with __.

Arguments:
p (string): The path to search
Returns:
list of file names


def build(bucket_name, version, force, verbose):
    if verbose:
        log.setLevel('DEBUG')
    if (not version):
        version = setuptools_scm.get_version()
    release = ('dev' if ('dev' in version) else 'release')
    tarball = TARBALL_FORMAT.format(version)
    tarball_path = os.path.join(tempfile.gettempdir(), tarball)
    s3_key = os.path.join(release, tarball)
    try:
        run('npm i')
        run('./node_modules/.bin/gulp build.prod')
    except ExecutionError:
        log.exception('Failed executing command')
        return
    log.debug('Creating archive')
    tar = tarfile.open(tarball_path, 'w:gz')
    for (root, dirnames, filenames) in os.walk('dist'):
        for f in filenames:
            tar.add(os.path.join(root, f), recursive=False, filter=strip_path)
    tar.close()
    log.debug('Uploading {} to s3:
    try:
        bucket = get_bucket_resource(bucket_name)
        if (s3_file_exists(bucket, s3_key) and (not force)):
            log.error('File already exists in S3, use --force to overwrite')
            return
        bucket.upload_file(tarball_path, os.path.join(release, tarball))
    except ClientError:
        log.exception('AWS API failure')

Build and upload a new tarball

Args:
bucket_name (str): Name of the bucket to upload to
version (str): Override build version. Defaults to using SCM based versioning (git tags)
force (bool): Overwrite existing files in S3, if present
verbose (bool): Verbose output


def plot_conductivity_mu(self, temp=600, output='eig',
                             relaxation_time=1e-14, xlim=None):

        import matplotlib.pyplot as plt
        cond = self._bz.get_conductivity(relaxation_time=relaxation_time,
                                         output=output, doping_levels=False)[
            temp]
        plt.figure(figsize=(9, 7))
        plt.semilogy(self._bz.mu_steps, cond, linewidth=3.0)
        self._plot_bg_limits()
        self._plot_doping(temp)
        if output == 'eig':
            plt.legend(['$\\Sigma_1$', '$\\Sigma_2$', '$\\Sigma_3$'])
        if xlim is None:
            plt.xlim(-0.5, self._bz.gap + 0.5)
        else:
            plt.xlim(xlim)
        plt.ylim([1e13 * relaxation_time, 1e20 * relaxation_time])
        plt.ylabel("conductivity,\n $\\Sigma$ (1/($\\Omega$ m))", fontsize=30.0)
        plt.xlabel("E-E$_f$ (eV)", fontsize=30.0)
        plt.xticks(fontsize=25)
        plt.yticks(fontsize=25)
        plt.tight_layout()
        return plt

Plot the conductivity in function of Fermi level. Semi-log plot

Args:
temp: the temperature
xlim: a list of min and max fermi energy by default (0, and band
gap)
tau: A relaxation time in s. By default none and the plot is by
units of relaxation time

Returns:
a matplotlib object


def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs):
    vision_data = {}
    if image_sizes is not None:
        num_image_tokens = [self.image_seq_length] * len(image_sizes)
        num_image_patches = [1] * len(image_sizes)
        vision_data.update({'num_image_tokens': num_image_tokens, 'num_image_patches': num_image_patches})
    return MultiModalData(**vision_data)

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

Args:
image_sizes (List[List[str]], *optional*):
The input sizes formatted as (height, width) per each image.
Returns:
Dict[str, List[int]]: A dictionary mapping each modality ("image", "video", "audio")
to a list containing the number of placeholder tokens required. If the model doesn't accept
a certain modality or no input sizes are provided, the dict value is set to an empty list.


def parse_kegg_gene_metadata(infile):
    metadata = defaultdict(str)
    with open(infile) as mf:
        kegg_parsed = bs_kegg.parse(mf.read())
    if ('DBLINKS' in kegg_parsed.keys()):
        if ('UniProt' in kegg_parsed['DBLINKS']):
            unis = str(kegg_parsed['DBLINKS']['UniProt']).split(' ')
            if isinstance(unis, list):
                metadata['uniprot'] = unis[0]
            else:
                metadata['uniprot'] = unis
        if ('NCBI-ProteinID' in kegg_parsed['DBLINKS']):
            metadata['refseq'] = str(kegg_parsed['DBLINKS']['NCBI-ProteinID'])
    if ('STRUCTURE' in kegg_parsed.keys()):
        metadata['pdbs'] = str(kegg_parsed['STRUCTURE']['PDB']).split(' ')
    else:
        metadata['pdbs'] = None
    if ('ORGANISM' in kegg_parsed.keys()):
        metadata['taxonomy'] = str(kegg_parsed['ORGANISM'])
    return metadata

Parse the KEGG flatfile and return a dictionary of metadata.

Dictionary keys are:
refseq
uniprot
pdbs
taxonomy

Args:
infile: Path to KEGG flatfile

Returns:
dict: Dictionary of metadata


def cubic_jacobian_polynomial(nodes):
    jac_parts = _helpers.matrix_product(nodes, _CUBIC_JACOBIAN_HELPER)
    jac_at_nodes = np.empty((1, 15), order='F')
    jac_at_nodes[(0, 0)] = two_by_two_det(jac_parts[(:, :2)])
    jac_at_nodes[(0, 1)] = two_by_two_det(jac_parts[(:, 2:4)])
    jac_at_nodes[(0, 2)] = two_by_two_det(jac_parts[(:, 4:6)])
    jac_at_nodes[(0, 3)] = two_by_two_det(jac_parts[(:, 6:8)])
    jac_at_nodes[(0, 4)] = two_by_two_det(jac_parts[(:, 8:10)])
    jac_at_nodes[(0, 5)] = two_by_two_det(jac_parts[(:, 10:12)])
    jac_at_nodes[(0, 6)] = two_by_two_det(jac_parts[(:, 12:14)])
    jac_at_nodes[(0, 7)] = two_by_two_det(jac_parts[(:, 14:16)])
    jac_at_nodes[(0, 8)] = two_by_two_det(jac_parts[(:, 16:18)])
    jac_at_nodes[(0, 9)] = two_by_two_det(jac_parts[(:, 18:20)])
    jac_at_nodes[(0, 10)] = two_by_two_det(jac_parts[(:, 20:22)])
    jac_at_nodes[(0, 11)] = two_by_two_det(jac_parts[(:, 22:24)])
    jac_at_nodes[(0, 12)] = two_by_two_det(jac_parts[(:, 24:26)])
    jac_at_nodes[(0, 13)] = two_by_two_det(jac_parts[(:, 26:28)])
    jac_at_nodes[(0, 14)] = two_by_two_det(jac_parts[(:, 28:)])
    bernstein = _helpers.matrix_product(jac_at_nodes, _QUARTIC_TO_BERNSTEIN)
    bernstein /= _QUARTIC_BERNSTEIN_FACTOR
    return bernstein

r"""Compute the Jacobian determinant of a cubic surface.

.. note::

This is used **only** by :meth:`Surface._compute_valid` (which is
in turn used to compute / cache the :attr:`Surface.is_valid`
property).

Converts :math:`\det(J(s, t))` to a polynomial on the reference
triangle and represents it as a surface object.

.. note::

This assumes that ``nodes`` is ``2 x 10`` but doesn't verify this.
(However, the right multiplication by ``_CUBIC_JACOBIAN_HELPER``
would fail if ``nodes`` wasn't ``R x 10`` and then the ensuing
determinants would fail if there weren't 2 rows.)

Args:
nodes (numpy.ndarray): A 2 x 10 array of nodes in a surface.

Returns:
numpy.ndarray: 1 x 15 array, coefficients in Bernstein basis.


def iaf_flow(one_hot_assignments, scale_weights, scale_bias, num_codes, summary=True, name=None):
    with tf.name_scope(name, default_name='iaf'):
        padded_assignments = tf.pad(one_hot_assignments, [[0, 0], [0, 0], [1, 0], [0, 0]])[(:, :, :(- 1), :)]
        scale_bijector = tfp.distributions.bijectors.Affine(scale_tril=tfp.distributions.fill_triangular(scale_weights))
        scale = scale_bijector.forward(tf.transpose(padded_assignments, [0, 1, 3, 2]))
        scale = tf.transpose(scale, [0, 1, 3, 2])
        scale = tf.nn.softplus(scale)
        scale = (scale + tf.nn.softplus(scale_bias[(tf.newaxis, tf.newaxis, ...)]))
        scale = scale[(..., :(- 1))]
        z = one_hot_assignments[(..., :(- 1))]
        unnormalized_probs = tf.concat([(z * scale), one_hot_assignments[(..., (- 1), tf.newaxis)]], axis=(- 1))
        normalizer = tf.reduce_sum(unnormalized_probs, axis=(- 1))
        flow_output = (unnormalized_probs / normalizer[(..., tf.newaxis)])
        inverse_log_det_jacobian = ((- tf.reduce_sum(tf.log(scale), axis=(- 1))) + (num_codes * tf.log(normalizer)))
        if summary:
            tf.summary.histogram('iaf/scale', tf.reshape(scale, [(- 1)]))
            tf.summary.histogram('iaf/inverse_log_det_jacobian', tf.reshape(inverse_log_det_jacobian, [(- 1)]))
        return (flow_output, inverse_log_det_jacobian)

Performs a single IAF flow using scale and normalization transformations.

Args:
one_hot_assignments: Assignments Tensor with shape [num_samples, batch_size,
latent_size, num_codes].
scale_weights: Tensor corresponding to lower triangular matrix used to
autoregressively generate scale matrix from assignments. To ensure the
lower-triangular matrix has length of latent_size, scale_weights should
be a rank-one tensor with size latent_size * (latent_size + 1) / 2.
scale_bias: Bias tensor to be added to scale tensor, with shape
[latent_size, num_codes]. If scale weights are zero, initialize scale_bias
to be log(exp(1.) / 2. - 1) so initial transformation is identity.
num_codes: Number of codes in codebook.
summary: Whether to save summaries.
name: String used for name scope.

Returns:
flow_output: Transformed one-hot assignments.
inverse_log_det_jacobian: Inverse log deteriminant of Jacobian corresponding
to transformation.


def build_global(self, global_node):

        config_block_lines = self.__build_config_block(
            global_node.config_block)
        return config.Global(config_block=config_block_lines)

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

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

Returns:
config.Global: an object


def register_many(self, *args):

        params = []
        for name in args:
            params.append(self.register(name))

        return params

Register many configuration names.

Arguments:
*args: Config names as strings.

Returns:
list: List of registered configs.


def _send(self, email_message):

        pre_send.send(self.__class__, message=email_message)

        if not email_message.recipients():
            return False

        from_email = sanitize_address(email_message.from_email,
                                      email_message.encoding)
        recipients = [sanitize_address(addr, email_message.encoding)
                      for addr in email_message.recipients()]
        message = email_message.message().as_bytes(linesep='\r\n')

        try:
            result = self.conn.send_raw_email(
                Source=from_email,
                Destinations=recipients,
                RawMessage={
                    'Data': message
                }
            )
            message_id = result['MessageId']
            post_send.send(
                self.__class__,
                message=email_message,
                message_id=message_id
            )
        except ClientError:
            if not self.fail_silently:
                raise
            return False
        return True

Sends an individual message via the Amazon SES HTTP API.

Args:
email_message: A single Django EmailMessage object.
Returns:
True if the EmailMessage was sent successfully, otherwise False.
Raises:
ClientError: An interaction with the Amazon SES HTTP API
failed.


def map(self, key_pattern, func, all_args, timeout=None):
    results = []
    keys = [make_key(key_pattern, func, args, {}) for args in all_args]
    cached = dict(zip(keys, self.get_many(keys)))
    cache_to_add = {}
    for (key, args) in zip(keys, all_args):
        val = cached[key]
        if (val is None):
            val = func(*args)
            cache_to_add[key] = (val if (val is not None) else NONE_RESULT)
        if (val == NONE_RESULT):
            val = None
        results.append(val)
    if cache_to_add:
        self.set_many(cache_to_add, timeout)
    return results

Cache return value of multiple calls.

Args:
key_pattern (str): the key pattern to use for generating
keys for caches of the decorated function.
func (function): the function to call.
all_args (list): a list of args to be used to make calls to
the function.
timeout (int): the cache timeout

Returns:
A list of the return values of the calls.

Example::

def add(a, b):
return a + b

cache.map(key_pat, add, [(1, 2), (3, 4)]) == [3, 7]


def forward(self, inputs: torch.Tensor, loc: torch.Tensor, scale: torch.Tensor):
    mean = loc.transpose(-1, -2)
    mean = mean.unsqueeze(-2)
    mean = mean.repeat(1, 1, self.num_patches, 1)
    stdev = scale.transpose(-1, -2)
    stdev = stdev.unsqueeze(-2)
    stdev = stdev.repeat(1, 1, self.num_patches, 1)
    concat_stats = torch.cat([mean, stdev], dim=-1)
    concat_stats = self.map_scale_expansion(concat_stats)
    concat_stats = self.map_scale_compression(concat_stats)
    inputs = torch.cat([inputs, concat_stats], dim=-1)
    inputs = self.inverse_trans_expansion(inputs)
    inputs = self.inverse_trans_compression(inputs)
    return inputs

Args:
inputs (`torch.Tensor` of shape `(batch_size, num_input_channels, num_patch, d_model)`)
loc (`torch.Tensor` of shape `(batch_size, 1, num_input_channels)`)
scale (`torch.Tensor` of shape `(batch_size, 1, num_input_channels)`)
Returns:
`torch.Tensor` of shape `(batch_size, num_input_channels, num_patch, d_model)`


def _validate_ids(self, resource_ids):
    for resource_id in resource_ids:
        if (self._id_regex.fullmatch(resource_id) is None):
            LOGGER.debug('Invalid resource id requested: %s', resource_id)
            raise _ResponseFailed(self._status.INVALID_ID)

Validates a list of ids, raising a ResponseFailed error if invalid.

Args:
resource_id (list of str): The ids to validate

Raises:
ResponseFailed: The id was invalid, and a status of INVALID_ID
will be sent with the response.


def _find_children_hints(call, graph_def):
    name_to_input_name, _, _ = _extract_graph_summary(graph_def)
    input_names, output_names = call.flattened_inputs_and_outputs()
    reachable_by_input = _bfs_for_reachable_nodes(input_names, name_to_input_name)
    reachable_by_output = _bfs_for_reachable_nodes(output_names, name_to_input_name)
    output_nodes_set = set(output_names)
    children_hints = []
    out = _graph_pb2.GraphDef()
    out.library.CopyFrom(graph_def.library)
    out.versions.CopyFrom(graph_def.versions)
    function_def_nodes = set()
    for node in graph_def.node:
        out.node.extend([_copy.deepcopy(node)])
        n = _tensor_name_base(node.name)
        if n in reachable_by_output:
            if n not in reachable_by_input and n not in output_nodes_set:
                if node.op == 'While' or node.op == 'StatelessWhile':
                    body_name = node.attr['body'].func.name
                    inputs_outside_loop = node.input
                    for function_def in graph_def.library.function:
                        if function_def.signature.name == body_name:
                            function_inputs = function_def.signature.input_arg
                            assert len(inputs_outside_loop) == len(function_inputs)
                            nodes_mapping = {}
                            for i, function_input in enumerate(function_inputs):
                                nodes_mapping[function_input.name] = inputs_outside_loop[i]
                            children_hints_in_loop, new_nodes = _find_children_hints_in_while_loop(function_def, nodes_mapping)
                            function_def_nodes.update([x.name for x in new_nodes])
                            children_hints.extend(children_hints_in_loop)
                            out.node.extend(new_nodes)
    return (children_hints, out, function_def_nodes)

Find all children hints.

For a given OpHint, we find all children hints inside it, we also copy all the
nodes inside function defs (if applicable) to the original graph_def, they are
returned in a list as well.

Args:
call: Parent OpHint that contains children ophints.
graph_def: Original graph def.

Returns:
Ordered children hints inside the parent ophint; new graph def that contains
nodes inside function defs (if applicable); nodes inside function defs.


def Lock(fd, path, blocking):

  operation = fcntl.LOCK_EX if blocking else fcntl.LOCK_EX | fcntl.LOCK_NB
  try:
    fcntl.flock(fd, operation)
  except IOError as e:
    if e.errno == errno.EWOULDBLOCK:
      raise IOError('Exception locking %s. File already locked.' % path)
    else:
      raise IOError('Exception locking %s. %s.' % (path, str(e)))

Lock the provided file descriptor.

Args:
fd: int, the file descriptor of the file to lock.
path: string, the name of the file to lock.
blocking: bool, whether the function should return immediately.

Raises:
IOError, raised from flock while attempting to lock a file.


def cycle_iters(iters: t.List[t.Iterator], take: int=1) -> t.Iterator:
    while iters:
        for i, it in enumerate(iters):
            try:
                for j in range(take):
                    logger.debug(f'yielding item {j!r} from iterable {i!r}.')
                    yield next(it)
            except StopIteration:
                iters.remove(it)

Evenly cycle through a list of iterators.

Args:
iters: A list of iterators to evely cycle through.
take: Yield N items at a time. When not set to 1, this will yield
multiple items from the same collection.

Returns:
An iteration across several iterators in a round-robin order.


def _index(array, item, key=None):
    for (i, el) in enumerate(array):
        resolved_el = (key(el) if key else el)
        if (resolved_el == item):
            return i
    return (- 1)

Array search function.

Written, because ``.index()`` method for array doesn't have `key` parameter
and raises `ValueError`, if the item is not found.

Args:
array (list): List of items, which will be searched.
item (whatever): Item, which will be matched to elements in `array`.
key (function, default None): Function, which will be used for lookup
into each element in `array`.

Return:
Index of `item` in `array`, if the `item` is in `array`, else `-1`.


def aggregate_and_return_name_for_output(self, fused_op_name, output_index, out_graphdef):
    del fused_op_name, output_index, out_graphdef
    raise RuntimeError('Unimplemented abstract method.')

Add node(s) to graph representing output operands and returns type.

Args:
fused_op_name: name of the fused op stub name.
output_index: Output index that we are currently processing from stub.
out_graphdef: The destination graphdef we are currently building up.

Returns:
The datatype of this identity.

Raises:
RuntimeError: if the method is not implemented.


def fixed_gaussian_prior_builder(getter, name, dtype=None, *args, **kwargs):
    del getter
    del args
    del kwargs
    loc = tf.constant(0.0, shape=(), dtype=dtype)
    scale = tf.constant(0.01, shape=(), dtype=dtype)
    return tfp.distributions.Normal(loc=loc, scale=scale, name='{}_prior_dist'.format(name))

A pre-canned builder for fixed gaussian prior distributions.

Given a true `getter` function and arguments forwarded from `tf.get_variable`,
return a distribution object for a scalar-valued fixed gaussian prior which
will be broadcast over a variable of the requisite shape.

Args:
getter: The `getter` passed to a `custom_getter`. Please see the
documentation for `tf.get_variable`.
name: The `name` argument passed to `tf.get_variable`.
dtype: The `dtype` argument passed to `tf.get_variable`.
*args: See positional arguments passed to `tf.get_variable`.
**kwargs: See keyword arguments passed to `tf.get_variable`.

Returns:
An instance of `tfp.distributions.Normal` representing the prior
distribution over the variable in question.


def fetch_raw(self, method, url, params=None, headers=None, data=None):
    if (not urllib.parse.urlparse(url).hostname.endswith('.google.com')):
        raise Exception('expected google.com domain')
    headers = (headers or {})
    headers.update(self._authorization_headers)
    return self._session.request(method, url, params=params, headers=headers, data=data, proxy=self._proxy)

Make an HTTP request using aiohttp directly.

Automatically uses configured HTTP proxy, and adds Google authorization
header and cookies.

Args:
method (str): Request method.
url (str): Request URL.
params (dict): (optional) Request query string parameters.
headers (dict): (optional) Request headers.
data: (str): (optional) Request body data.

Returns:
aiohttp._RequestContextManager: ContextManager for a HTTP response.

Raises:
See ``aiohttp.ClientSession.request``.


def set_charge_and_spin(self, charge, spin_multiplicity=None):
    self._charge = charge
    nelectrons = 0
    for site in self._sites:
        for (sp, amt) in site.species.items():
            if (not isinstance(sp, DummySpecie)):
                nelectrons += (sp.Z * amt)
    nelectrons -= charge
    self._nelectrons = nelectrons
    if spin_multiplicity:
        if (((nelectrons + spin_multiplicity) % 2) != 1):
            raise ValueError('Charge of {} and spin multiplicity of {} is not possible for this molecule'.format(self._charge, spin_multiplicity))
        self._spin_multiplicity = spin_multiplicity
    else:
        self._spin_multiplicity = (1 if ((nelectrons % 2) == 0) else 2)

Set the charge and spin multiplicity.

Args:
charge (int): Charge for the molecule. Defaults to 0.
spin_multiplicity (int): Spin multiplicity for molecule.
Defaults to None, which means that the spin multiplicity is
set to 1 if the molecule has no unpaired electrons and to 2
if there are unpaired electrons.


def apply(self, s, active=None):

        if active is None:
            active = self.active
        return self.group.apply(s, active=active)

Apply the REPP's rewrite rules to the input string *s*.

Args:
s (str): the input string to process
active (optional): a collection of external module names
that may be applied if called
Returns:
a :class:`REPPResult` object containing the processed
string and characterization maps


def state_province_region(self, value=None):

        if value is not None:
            try:
                value = str(value)
            except ValueError:
                raise ValueError(
                    'value {} need to be of type str '
                    'for field `state_province_region`'.format(value))
            if ',' in value:
                raise ValueError('value should not contain a comma '
                                 'for field `state_province_region`')

        self._state_province_region = value

Corresponds to IDD Field `state_province_region`

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

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


def get_artifact_url(context, task_id, path):
    if path.startswith('public/'):
        url = context.queue.buildUrl('getLatestArtifact', task_id, path)
    else:
        url = context.queue.buildSignedUrl('getLatestArtifact', task_id, path)
    return url

Get a TaskCluster artifact url.

Args:
context (scriptworker.context.Context): the scriptworker context
task_id (str): the task id of the task that published the artifact
path (str): the relative path of the artifact

Returns:
str: the artifact url

Raises:
TaskClusterFailure: on failure.


def alignment(layer, decay_ratio=2):

    def inner(T):
        batch_n = T(layer).get_shape().as_list()[0]
        arr = T(layer)
        accum = 0
        for d in [1, 2, 3, 4]:
            for i in range((batch_n - d)):
                (a, b) = (i, (i + d))
                (arr1, arr2) = (arr[a], arr[b])
                accum += (tf.reduce_mean(((arr1 - arr2) ** 2)) / (decay_ratio ** float(d)))
        return (- accum)
    return inner

Encourage neighboring images to be similar.

When visualizing the interpolation between two objectives, it's often
desireable to encourage analagous boejcts to be drawn in the same position,
to make them more comparable.

This term penalizes L2 distance between neighboring images, as evaluated at
layer.

In general, we find this most effective if used with a paramaterization that
shares across the batch. (In fact, that works quite well by iteself, so this
function may just be obselete.)

Args:
layer: layer to penalize at.
decay_ratio: how much to decay penalty as images move apart in batch.

Returns:
Objective.


def as_text(bytes_or_text, encoding='utf-8'):

    if isinstance(bytes_or_text, _six.text_type):
        return bytes_or_text
    elif isinstance(bytes_or_text, bytes):
        return bytes_or_text.decode(encoding)
    else:
        raise TypeError('Expected binary or unicode string, got %r' % bytes_or_text)

Returns the given argument as a unicode string.
Args:
bytes_or_text: A `bytes`, `str, or `unicode` object.
encoding: A string indicating the charset for decoding unicode.
Returns:
A `unicode` (Python 2) or `str` (Python 3) object.
Raises:
TypeError: If `bytes_or_text` is not a binary or unicode string.


def _read_range(self, start, end=0):

        response = self._client.request(
            'GET', self.name, headers=dict(Range=self._http_range(start, end)),
            timeout=self._TIMEOUT)

        if response.status_code == 416:

            return b''

        return _handle_http_errors(response).content

Read a range of bytes in stream.

Args:
start (int): Start stream position.
end (int): End stream position.
0 To not specify end.

Returns:
bytes: number of bytes read


def _cast_to_type_if_compatible(name, param_type, value):
    fail_msg = ("Could not cast hparam '%s' of type '%s' from value %r" % (name, param_type, value))
    if issubclass(param_type, type(None)):
        return value
    if (issubclass(param_type, (six.string_types, six.binary_type)) and (not isinstance(value, (six.string_types, six.binary_type)))):
        raise ValueError(fail_msg)
    if (issubclass(param_type, bool) != isinstance(value, bool)):
        raise ValueError(fail_msg)
    if (issubclass(param_type, numbers.Integral) and (not isinstance(value, numbers.Integral))):
        raise ValueError(fail_msg)
    if (issubclass(param_type, numbers.Number) and (not isinstance(value, numbers.Number))):
        raise ValueError(fail_msg)
    return param_type(value)

Cast hparam to the provided type, if compatible.

Args:
name: Name of the hparam to be cast.
param_type: The type of the hparam.
value: The value to be cast, if compatible.

Returns:
The result of casting `value` to `param_type`.

Raises:
ValueError: If the type of `value` is not compatible with param_type.
* If `param_type` is a string type, but `value` is not.
* If `param_type` is a boolean, but `value` is not, or vice versa.
* If `param_type` is an integer type, but `value` is not.
* If `param_type` is a float type, but `value` is not a numeric type.


def prepare(self, variables):
    initializedsteps = []
    if (variables is None):
        variables = dict()
    for (step, params, _resources, _files) in self.steps:
        new_params = _complete_parameters(params, variables)
        initializedsteps.append(step(new_params))
    return initializedsteps

Initialize all steps in this recipe using their parameters.

Args:
variables (dict): A dictionary of global variable definitions
that may be used to replace or augment the parameters given
to each step.

Returns:
list of RecipeActionObject like instances: The list of instantiated
steps that can be used to execute this recipe.


def parse(raw_config):
    config_dict = yaml_to_ordered_dict(raw_config)
    if config_dict:
        for top_level_key in ['stacks', 'pre_build', 'post_build', 'pre_destroy', 'post_destroy']:
            top_level_value = config_dict.get(top_level_key)
            if isinstance(top_level_value, dict):
                tmp_list = []
                for (key, value) in top_level_value.items():
                    tmp_dict = copy.deepcopy(value)
                    if (top_level_key == 'stacks'):
                        tmp_dict['name'] = key
                    tmp_list.append(tmp_dict)
                config_dict[top_level_key] = tmp_list
    try:
        return Config(config_dict, strict=True)
    except SchematicsError as e:
        raise exceptions.InvalidConfig(e.errors)

Parse a raw yaml formatted stacker config.

Args:
raw_config (str): the raw stacker configuration string in yaml format.

Returns:
:class:`Config`: the parsed stacker config.


def iterable(obj, strok=False):
    try:
        iter(obj)
    except Exception:
        return False
    else:
        return (strok or (not isinstance(obj, six.string_types)))

Checks if the input implements the iterator interface. An exception is made
for strings, which return False unless `strok` is True

Args:
obj (object): a scalar or iterable input

strok (bool): if True allow strings to be interpreted as iterable

Returns:
bool: True if the input is iterable

Example:
>>> obj_list = [3, [3], '3', (3,), [3, 4, 5], {}]
>>> result = [iterable(obj) for obj in obj_list]
>>> assert result == [False, True, False, True, True, True]
>>> result = [iterable(obj, strok=True) for obj in obj_list]
>>> assert result == [False, True, True, True, True, True]


def __get_first_available_id(self):
    traps = self.get_all()
    if traps:
        used_ids = [0]
        for trap in traps:
            used_uris = trap.get('uri')
            used_ids.append(int(used_uris.split('/')[(- 1)]))
        used_ids.sort()
        return self.__findFirstMissing(used_ids, 0, (len(used_ids) - 1))
    else:
        return 1

Private method to get the first available id.
The id can only be an integer greater than 0.

Returns:
int: The first available id


def get_thread(self, thread_id, update_if_cached=True, raise_404=False):

        cached_thread = self._thread_cache.get(thread_id)
        if cached_thread:
            if update_if_cached:
                cached_thread.update()
            return cached_thread

        res = self._requests_session.get(
            self._url.thread_api_url(
                thread_id = thread_id
                )
        )

        if raise_404:
            res.raise_for_status()
        elif not res.ok:
            return None

        thread = Thread._from_request(self, res, thread_id)
        self._thread_cache[thread_id] = thread

        return thread

Get a thread from 4chan via 4chan API.

Args:
thread_id (int): Thread ID
update_if_cached (bool): Whether the thread should be updated if it's already in our cache
raise_404 (bool): Raise an Exception if thread has 404'd

Returns:
:class:`basc_py4chan.Thread`: Thread object


def configure_interface(self, name, commands):

        commands = make_iterable(commands)
        commands.insert(0, 'interface %s' % name)
        return self.configure(commands)

Configures the specified interface with the commands

Args:
name (str): The interface name to configure
commands: The commands to configure in the interface

Returns:
True if the commands completed successfully


def convert_timedelta_type(obj):
    if isinstance(obj, dt.timedelta):
        return (obj.total_seconds() * 1000.0)
    elif isinstance(obj, np.timedelta64):
        return (obj / NP_MS_DELTA)

Convert any recognized timedelta value to floating point absolute
milliseconds.

Arg:
obj (object) : the object to convert

Returns:
float : milliseconds


def get_path_spec(self, path, action=None):
    path_spec = None
    path_name = None
    for base_path in self.paths.keys():
        if (path == base_path):
            path_spec = self.paths[base_path]
            path_name = base_path
    if (path_spec is None):
        for base_path in self.paths.keys():
            regex_from_path = re.compile((re.sub('{[^/]*}', '([^/]*)', base_path) + '$'))
            if re.match(regex_from_path, path):
                path_spec = self.paths[base_path]
                path_name = base_path
    if ((path_spec is not None) and (action is not None)):
        if (action not in path_spec.keys()):
            return (None, None)
        else:
            path_spec = path_spec[action]
    return (path_name, path_spec)

Get the specification matching with the given path.

Args:
path: path we want the specification.
action: get the specification for the given action.

Returns:
A tuple with the base name of the path and the specification.
Or (None, None) if no specification is found.


def ParseFileLNKFile(
      self, parser_mediator, file_object, display_name):

    lnk_file = pylnk.file()
    lnk_file.set_ascii_codepage(parser_mediator.codepage)

    try:
      lnk_file.open_file_object(file_object)
    except IOError as exception:
      parser_mediator.ProduceExtractionWarning(
          'unable to open file with error: {0!s}'.format(exception))
      return

    link_target = None
    if lnk_file.link_target_identifier_data:

      display_name = parser_mediator.GetFilename()
      shell_items_parser = shell_items.ShellItemsParser(display_name)
      shell_items_parser.ParseByteStream(
          parser_mediator, lnk_file.link_target_identifier_data,
          codepage=parser_mediator.codepage)

      link_target = shell_items_parser.CopyToPath()

    event_data = WinLnkLinkEventData()
    event_data.birth_droid_file_identifier = (
        lnk_file.birth_droid_file_identifier)
    event_data.birth_droid_volume_identifier = (
        lnk_file.birth_droid_volume_identifier)
    event_data.command_line_arguments = lnk_file.command_line_arguments
    event_data.description = lnk_file.description
    event_data.drive_serial_number = lnk_file.drive_serial_number
    event_data.drive_type = lnk_file.drive_type
    event_data.droid_file_identifier = lnk_file.droid_file_identifier
    event_data.droid_volume_identifier = lnk_file.droid_volume_identifier
    event_data.env_var_location = lnk_file.environment_variables_location
    event_data.file_attribute_flags = lnk_file.file_attribute_flags
    event_data.file_size = lnk_file.file_size
    event_data.icon_location = lnk_file.icon_location
    event_data.link_target = link_target
    event_data.local_path = lnk_file.local_path
    event_data.network_path = lnk_file.network_path
    event_data.relative_path = lnk_file.relative_path
    event_data.volume_label = lnk_file.volume_label
    event_data.working_directory = lnk_file.working_directory

    access_time = lnk_file.get_file_access_time_as_integer()
    if access_time != 0:
      date_time = dfdatetime_filetime.Filetime(timestamp=access_time)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_LAST_ACCESS)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    creation_time = lnk_file.get_file_creation_time_as_integer()
    if creation_time != 0:
      date_time = dfdatetime_filetime.Filetime(timestamp=creation_time)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_CREATION)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    modification_time = lnk_file.get_file_modification_time_as_integer()
    if modification_time != 0:
      date_time = dfdatetime_filetime.Filetime(timestamp=modification_time)
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_MODIFICATION)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    if access_time == 0 and creation_time == 0 and modification_time == 0:
      date_time = dfdatetime_semantic_time.SemanticTime('Not set')
      event = time_events.DateTimeValuesEvent(
          date_time, definitions.TIME_DESCRIPTION_NOT_A_TIME)
      parser_mediator.ProduceEventWithEventData(event, event_data)

    if lnk_file.droid_file_identifier:
      try:
        self._ParseDistributedTrackingIdentifier(
            parser_mediator, lnk_file.droid_file_identifier, display_name)
      except (TypeError, ValueError) as exception:
        parser_mediator.ProduceExtractionWarning(
            'unable to read droid file identifier with error: {0!s}.'.format(
                exception))

    if lnk_file.birth_droid_file_identifier:
      try:
        self._ParseDistributedTrackingIdentifier(
            parser_mediator, lnk_file.birth_droid_file_identifier, display_name)
      except (TypeError, ValueError) as exception:
        parser_mediator.ProduceExtractionWarning((
            'unable to read birth droid file identifier with error: '
            '{0!s}.').format(exception))

    lnk_file.close()

Parses a Windows Shortcut (LNK) file-like object.

Args:
parser_mediator (ParserMediator): mediates interactions between parsers
and other components, such as storage and dfvfs.
file_object (dfvfs.FileIO): file-like object.
display_name (str): display name.


def merge(prior, latest):
    if (not _buckets_nearly_equal(prior, latest)):
        _logger.error(u'Bucket options do not match. From %s To: %s', prior, latest)
        raise ValueError(u'Bucket options do not match')
    if (len(prior.bucketCounts) != len(latest.bucketCounts)):
        _logger.error(u'Bucket count sizes do not match. From %s To: %s', prior, latest)
        raise ValueError(u'Bucket count sizes do not match')
    if (prior.count <= 0):
        return
    old_count = latest.count
    old_mean = latest.mean
    old_summed_variance = latest.sumOfSquaredDeviation
    bucket_counts = latest.bucketCounts
    latest.count += prior.count
    latest.maximum = max(prior.maximum, latest.maximum)
    latest.minimum = min(prior.minimum, latest.minimum)
    latest.mean = (((old_count * old_mean) + (prior.count * prior.mean)) / latest.count)
    latest.sumOfSquaredDeviation = (((old_summed_variance + prior.sumOfSquaredDeviation) + (old_count * ((latest.mean - old_mean) ** 2))) + (prior.count * ((latest.mean - prior.mean) ** 2)))
    for (i, (x, y)) in enumerate(zip(prior.bucketCounts, bucket_counts)):
        bucket_counts[i] = (x + y)

Merge `prior` into `latest`.

N.B, this mutates latest. It ensures that the statistics and histogram are
updated to correctly include the original values from both instances.

Args:
prior (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`):
an instance
latest (:class:`endpoints_management.gen.servicecontrol_v1_messages.Distribution`):
an instance to be updated

Raises:
ValueError: if the bucket options of `prior` and `latest` do not match
ValueError: if the bucket counts of `prior` and `latest` do not match


def GrabFileSystem(self, path_spec):
    identifier = self._GetFileSystemCacheIdentifier(path_spec)
    self._file_system_cache.GrabObject(identifier)

Grabs a cached file system object defined by path specification.

Args:
path_spec (PathSpec): path specification.


def credits(self, **kwargs):

        path = self._get_id_path('credits')

        response = self._GET(path, kwargs)
        self._set_attrs_to_values(response)
        return response

Get the cast and crew information for a specific movie id.

Args:
append_to_response: (optional) Comma separated, any movie method.

Returns:
A dict representation of the JSON returned from the API.


def ias60(msg):

    d = hex2bin(data(msg))

    if d[12] == '0':
        return None

    ias = bin2int(d[13:23])
    return ias

Indicated airspeed

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

Returns:
int: indicated airspeed in knots


def sparse_retain(sp_input, to_retain):
    sp_input = _convert_to_sparse_tensor(sp_input)
    to_retain = ops.convert_to_tensor(to_retain)
    retain_shape = to_retain.get_shape()
    retain_shape.assert_has_rank(1)
    if sp_input.values.get_shape().dims is not None:
        sp_input.values.get_shape().dims[0].assert_is_compatible_with(tensor_shape.dimension_at_index(retain_shape, 0))
    where_true = array_ops.reshape(array_ops.where_v2(to_retain), [-1])
    new_indices = array_ops.gather(sp_input.indices, where_true)
    new_values = array_ops.gather(sp_input.values, where_true)
    return sparse_tensor.SparseTensor(new_indices, new_values, array_ops.identity(sp_input.dense_shape))

Retains specified non-empty values within a `SparseTensor`.

For example, if `sp_input` has shape `[4, 5]` and 4 non-empty string values:

[0, 1]: a
[0, 3]: b
[2, 0]: c
[3, 1]: d

and `to_retain = [True, False, False, True]`, then the output will
be a `SparseTensor` of shape `[4, 5]` with 2 non-empty values:

[0, 1]: a
[3, 1]: d

Args:
sp_input: The input `SparseTensor` with `N` non-empty elements.
to_retain: A bool vector of length `N` with `M` true values.

Returns:
A `SparseTensor` with the same shape as the input and `M` non-empty
elements corresponding to the true positions in `to_retain`.

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


def get_submission_ids(self, tournament=1):

        query =
        arguments = {'tournament': tournament}
        data = self.raw_query(query, arguments)['data']['rounds'][0]
        if data is None:
            return None
        mapping = {item['username']: item['submissionId']
                   for item in data['leaderboard']}
        return mapping

Get dict with username->submission_id mapping.

Args:
tournament (int): ID of the tournament (optional, defaults to 1)

Returns:
dict: username->submission_id mapping, string->string

Example:
>>> NumerAPI().get_submission_ids()
{'1337ai': '93c46857-fed9-4594-981e-82db2b358daf',
'1x0r': '108c7601-822c-4910-835d-241da93e2e24',
...
}


def _delete_example(self, request):

    index = int(request.args.get('index'))
    if index >= len(self.examples):
      return http_util.Respond(request, {'error': 'invalid index provided'},
                               'application/json', code=400)
    del self.examples[index]
    self.updated_example_indices = set([
        i if i < index else i - 1 for i in self.updated_example_indices])
    self.generate_sprite([ex.SerializeToString() for ex in self.examples])
    return http_util.Respond(request, {}, 'application/json')

Deletes the specified example.

Args:
request: A request that should contain 'index'.

Returns:
An empty response.


Reconnects to the app after device USB was disconnected.

Instead of creating new instance of the client:
- Uses the given port (or finds a new available host_port if none is
given).
- Tries to connect to remote server with selected port.

Must be implemented by subclasses.

Args:
port: If given, this is the host port from which to connect to remote
device port. If not provided, find a new available port as host
port.

Raises:
AppRestoreConnectionError: When the app was not able to be
reconnected.


def nac_p(msg):

    tc = typecode(msg)

    if tc not in [29, 31]:
        raise RuntimeError("%s: Not a target state and status message, \
                           or operation status message, expecting TC = 29 or 31" % msg)

    msgbin = common.hex2bin(msg)

    if tc == 29:
        NACp = common.bin2int(msgbin[71:75])
    elif tc == 31:
        NACp = common.bin2int(msgbin[76:80])

    try:
        EPU = uncertainty.NACp[NACp]['EPU']
        VEPU = uncertainty.NACp[NACp]['VEPU']
    except KeyError:
        EPU, VEPU = uncertainty.NA, uncertainty.NA

    return EPU, VEPU

Calculate NACp, Navigation Accuracy Category - Position

Args:
msg (string): 28 bytes hexadecimal message string, TC = 29 or 31

Returns:
int or string: 95% horizontal accuracy bounds, Estimated Position Uncertainty
int or string: 95% vertical accuracy bounds, Vertical Estimated Position Uncertainty


def register_module_for_export(module, export_name):
    for (used_name, _) in tf_v1.get_collection(_EXPORT_MODULES_COLLECTION):
        if (used_name == export_name):
            raise ValueError(('There is already a module registered to be exported as %r' % export_name))
    tf_v1.add_to_collection(_EXPORT_MODULES_COLLECTION, (export_name, module))

Register a Module to be exported under `export_name`.

This function registers `module` to be exported by `LatestModuleExporter`
under a subdirectory named `export_name`.

Note that `export_name` must be unique for each module exported from the
current graph. It only controls the export subdirectory name and it has
no scope effects such as the `name` parameter during Module instantiation.

Args:
module: Module instance to be exported.
export_name: subdirectory name to use when performing the export.

Raises:
ValueError: if `export_name` is already taken in the current graph.


def verify_signature(public_key, signature, hash, hash_algo):

    hash_algo = _hash_algorithms[hash_algo]
    try:
        return get_publickey(public_key).verify(
            signature,
            hash,
            padding.PKCS1v15(),
            utils.Prehashed(hash_algo),
        ) is None
    except InvalidSignature:
        return False

Verify the given signature is correct for the given hash and public key.

Args:
public_key (str): PEM encoded public key
signature (bytes): signature to verify
hash (bytes): hash of data
hash_algo (str): hash algorithm used

Returns:
True if the signature is valid, False otherwise


def get_video_features(self, pixel_values: torch.FloatTensor, vision_feature_layer: Union[int, List[int]], vision_feature_select_strategy: str):
    batch_size, frames, channels, height, width = pixel_values.shape
    pixel_values = pixel_values.view(batch_size * frames, channels, height, width)
    video_features = self.vision_tower(pixel_values, output_hidden_states=True)
    if isinstance(vision_feature_layer, int):
        selected_video_feature = video_features.hidden_states[vision_feature_layer]
    else:
        hs_pool = [video_features.hidden_states[layer_idx] for layer_idx in vision_feature_layer]
        selected_video_feature = torch.cat(hs_pool, dim=-1)
    if vision_feature_select_strategy == 'default':
        selected_video_feature = selected_video_feature[:, 1:]
    elif vision_feature_select_strategy == 'full':
        selected_video_feature = selected_video_feature
    video_features = self.multi_modal_projector(selected_video_feature)
    video_features = self.apply_pooling(video_features)
    video_features = video_features.reshape(batch_size, frames * video_features.shape[1], -1)
    return video_features

Obtains video last hidden states from the vision tower, apply multimodal projection and pooling.

Args:
pixel_values (`torch.FloatTensor]` of shape `(batch_size, num_frames, channels, height, width)`)
The tensors corresponding to the input video.
vision_feature_layer (`Union[int, List[int]], *optional*, defaults to -2`):
The index of the layer to select the vision feature. If multiple indices are provided,
the vision feature of the corresponding indices will be concatenated to form the
vision features.
vision_feature_select_strategy (`str`):
The feature selection strategy used to select the vision feature from the vision backbone.
Can be one of `"default"` or `"full"`
Returns:
video_features (List[`torch.Tensor`]): List of video feature tensor, each contains all the visual feature of all patches
and are of shape `(num_videos, video_length, embed_dim)`).


def extract_paths(disk_path, disk_root, paths, ignore_nopath):
    with guestfs_conn_mount_ro(disk_path, disk_root) as conn:
        for (guest_path, host_path) in paths:
            msg = 'Extracting guestfs:
            LOGGER.debug(msg)
            try:
                _copy_path(conn, guest_path, host_path)
            except ExtractPathNoPathError as err:
                if ignore_nopath:
                    LOGGER.debug('%s - ignoring', err)
                else:
                    raise

Extract paths from a disk using guestfs

Args:
disk_path(str): path to the disk
disk_root(str): root partition
paths(list of tuples): files to extract in
`[(src1, dst1), (src2, dst2)...]` format, if ``srcN`` is a
directory in the guest, and ``dstN`` does not exist on the host,
it will be created. If ``srcN`` is a file on the guest, it will be
copied exactly to ``dstN``
ignore_nopath(bool): If set to True, ignore paths in the guest that
do not exit

Returns:
None

Raises:
:exc:`~lago.plugins.vm.ExtractPathNoPathError`: if a none existing
path was found on the guest, and `ignore_nopath` is False.
:exc:`~lago.plugins.vm.ExtractPathError`: on all other failures.


def delete_item(self, key: InstanceKey) -> "InstanceNode":

        if not isinstance(self.value, StructuredValue):
            raise InstanceValueError(self.json_pointer(), "scalar value")
        newval = self.value.copy()
        try:
            del newval[key]
        except (KeyError, IndexError, TypeError):
            raise NonexistentInstance(self.json_pointer(),
                                      f"item '{key}'") from None
        return self._copy(newval)

Delete an item (member or entry) from receiver's value.

Args:
key: Key of the item (instance name or index).

Raises:
NonexistentInstance: If receiver's value doesn't contain the item.
InstanceValueError: If the receiver's value is a scalar.


def broadcast_to(rt_input, shape, broadcast_inner_dimensions=True):
    if not isinstance(shape, RaggedTensorDynamicShape):
        raise TypeError('shape must be a RaggedTensorDynamicShape')
    rt_input = ragged_tensor.convert_to_tensor_or_ragged_tensor(rt_input)
    if shape.num_partitioned_dimensions == 0:
        return _broadcast_to_uniform_shape(rt_input, shape, broadcast_inner_dimensions)
    else:
        return _broadcast_to_ragged_shape(rt_input, shape, broadcast_inner_dimensions)

Broadcasts a potentially ragged tensor to a ragged shape.

Tiles `rt_input` as necessary to match the given shape.

Behavior is undefined if `rt_input` is not broadcast-compatible with `shape`.

Args:
rt_input: The potentially ragged tensor to broadcast.
shape: A `RaggedTensorDynamicShape`
broadcast_inner_dimensions: If false, then inner dimensions will not be
tiled.

Returns:
A potentially ragged tensor whose values are taken from
`rt_input`, and whose shape matches `shape`.


def _make_cloud_datastore_context(app_id, external_app_ids=()):
    from . import model
    if (not datastore_pbs._CLOUD_DATASTORE_ENABLED):
        raise datastore_errors.BadArgumentError(datastore_pbs.MISSING_CLOUD_DATASTORE_MESSAGE)
    import googledatastore
    try:
        from google.appengine.datastore import cloud_datastore_v1_remote_stub
    except ImportError:
        from google3.apphosting.datastore import cloud_datastore_v1_remote_stub
    current_app_id = os.environ.get('APPLICATION_ID', None)
    if (current_app_id and (current_app_id != app_id)):
        raise ValueError(('Cannot create a Cloud Datastore context that connects to an application (%s) that differs from the application already connected to (%s).' % (app_id, current_app_id)))
    os.environ['APPLICATION_ID'] = app_id
    id_resolver = datastore_pbs.IdResolver(((app_id,) + tuple(external_app_ids)))
    project_id = id_resolver.resolve_project_id(app_id)
    endpoint = googledatastore.helper.get_project_endpoint_from_env(project_id)
    datastore = googledatastore.Datastore(project_endpoint=endpoint, credentials=googledatastore.helper.get_credentials_from_env())
    conn = model.make_connection(_api_version=datastore_rpc._CLOUD_DATASTORE_V1, _id_resolver=id_resolver)
    try:
        stub = cloud_datastore_v1_remote_stub.CloudDatastoreV1RemoteStub(datastore)
        apiproxy_stub_map.apiproxy.RegisterStub(datastore_rpc._CLOUD_DATASTORE_V1, stub)
    except:
        pass
    try:
        apiproxy_stub_map.apiproxy.RegisterStub('memcache', _ThrowingStub())
    except:
        pass
    try:
        apiproxy_stub_map.apiproxy.RegisterStub('taskqueue', _ThrowingStub())
    except:
        pass
    return make_context(conn=conn)

Creates a new context to connect to a remote Cloud Datastore instance.

This should only be used outside of Google App Engine.

Args:
app_id: The application id to connect to. This differs from the project
id as it may have an additional prefix, e.g. "s~" or "e~".
external_app_ids: A list of apps that may be referenced by data in your
application. For example, if you are connected to s~my-app and store keys
for s~my-other-app, you should include s~my-other-app in the external_apps
list.
Returns:
An ndb.Context that can connect to a Remote Cloud Datastore. You can use
this context by passing it to ndb.set_context.


def _get_mu_tensor(self):
    root = self._get_cubic_root()
    dr = (self._h_max / self._h_min)
    mu = tf.maximum((root ** 2), (((tf.sqrt(dr) - 1) / (tf.sqrt(dr) + 1)) ** 2))
    return mu

Get the min mu which minimize the surrogate.

Returns:
The mu_t.


def exec_inspect(self, exec_id):
    if isinstance(exec_id, dict):
        exec_id = exec_id.get('Id')
    res = self._get(self._url('/exec/{0}/json', exec_id))
    return self._result(res, True)

Return low-level information about an exec command.

Args:
exec_id (str): ID of the exec instance

Returns:
(dict): Dictionary of values returned by the endpoint.

Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.